fix: properly dispose ChannelSession
This commit is contained in:
parent
2ec47ae884
commit
f1f22ce1f2
1 changed files with 24 additions and 1 deletions
|
@ -23,12 +23,13 @@ public class ChannelSession : TcpSession
|
||||||
private readonly ICryptoTransform _encryptor;
|
private readonly ICryptoTransform _encryptor;
|
||||||
private readonly ILogger<ChannelSession> _logger;
|
private readonly ILogger<ChannelSession> _logger;
|
||||||
private readonly IMediator _mediator;
|
private readonly IMediator _mediator;
|
||||||
|
private bool _disposed;
|
||||||
|
|
||||||
public ChannelSession(TcpServer server, IMediator mediator, ILogger<ChannelSession> logger) : base(server)
|
public ChannelSession(TcpServer server, IMediator mediator, ILogger<ChannelSession> logger) : base(server)
|
||||||
{
|
{
|
||||||
_mediator = mediator;
|
_mediator = mediator;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
var aes = Aes.Create();
|
using var aes = Aes.Create();
|
||||||
aes.Key = _key;
|
aes.Key = _key;
|
||||||
aes.IV = _iv;
|
aes.IV = _iv;
|
||||||
aes.Padding = PaddingMode.None;
|
aes.Padding = PaddingMode.None;
|
||||||
|
@ -63,6 +64,28 @@ public class ChannelSession : TcpSession
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void Dispose(bool disposingManagedResources)
|
||||||
|
{
|
||||||
|
if (_disposed)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_disposed = true;
|
||||||
|
if (disposingManagedResources)
|
||||||
|
{
|
||||||
|
_decryptor.Dispose();
|
||||||
|
_encryptor.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
base.Dispose(disposingManagedResources);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void ThrowIfDisposed()
|
||||||
|
{
|
||||||
|
ObjectDisposedException.ThrowIf(_disposed, GetType());
|
||||||
|
}
|
||||||
|
|
||||||
private static byte[] Decrypt(byte[] buffer)
|
private static byte[] Decrypt(byte[] buffer)
|
||||||
{
|
{
|
||||||
for (var i = 0; i < buffer.Length; ++i)
|
for (var i = 0; i < buffer.Length; ++i)
|
||||||
|
|
Loading…
Reference in a new issue