fix: properly dispose ChannelSession

This commit is contained in:
Timothy Schenk 2024-03-02 13:15:39 +01:00
parent 2ec47ae884
commit f1f22ce1f2
Signed by: rainote
SSH key fingerprint: SHA256:pnkNSDwpAnaip00xaZlVFHKKsS7T8UtOomMzvs0yITE

View file

@ -23,12 +23,13 @@ public class ChannelSession : TcpSession
private readonly ICryptoTransform _encryptor;
private readonly ILogger<ChannelSession> _logger;
private readonly IMediator _mediator;
private bool _disposed;
public ChannelSession(TcpServer server, IMediator mediator, ILogger<ChannelSession> logger) : base(server)
{
_mediator = mediator;
_logger = logger;
var aes = Aes.Create();
using var aes = Aes.Create();
aes.Key = _key;
aes.IV = _iv;
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)
{
for (var i = 0; i < buffer.Length; ++i)