1
0
mirror of https://github.com/JKorf/CryptoExchange.Net.git synced 2026-08-15 02:13:01 +00:00

Socket connection improvements on reconnect

This commit is contained in:
JKorf
2024-03-28 20:35:49 +01:00
parent e62786a70f
commit b90a0a71e9
2 changed files with 17 additions and 3 deletions
@@ -194,11 +194,16 @@ namespace CryptoExchange.Net.Sockets
try
{
using CancellationTokenSource tcs = new(TimeSpan.FromSeconds(10));
await _socket.ConnectAsync(Uri, tcs.Token).ConfigureAwait(false);
using var linked = CancellationTokenSource.CreateLinkedTokenSource(tcs.Token, _ctsSource.Token);
await _socket.ConnectAsync(Uri, linked.Token).ConfigureAwait(false);
}
catch (Exception e)
{
_logger.SocketConnectionFailed(Id, e.Message, e);
if (!_ctsSource.IsCancellationRequested)
{
// if _ctsSource was canceled this was already logged
_logger.SocketConnectionFailed(Id, e.Message, e);
}
return false;
}
@@ -386,10 +391,13 @@ namespace CryptoExchange.Net.Sockets
if (_disposed)
return;
if (_ctsSource?.IsCancellationRequested == false)
_ctsSource.Cancel();
_logger.SocketDisposing(Id);
_disposed = true;
_socket.Dispose();
_ctsSource.Dispose();
_ctsSource?.Dispose();
_logger.SocketDisposed(Id);
}