From f917bf0e3fb7148abc0f38ccdc3ec50060855398 Mon Sep 17 00:00:00 2001 From: JKorf Date: Wed, 21 Feb 2024 19:39:31 +0100 Subject: [PATCH] Updated revitalize request signature, added exception handler reconnect logic --- CryptoExchange.Net/Clients/SocketApiClient.cs | 8 ++--- .../Sockets/SocketConnection.cs | 34 ++++++++++++------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/CryptoExchange.Net/Clients/SocketApiClient.cs b/CryptoExchange.Net/Clients/SocketApiClient.cs index c9c843d..982fcdf 100644 --- a/CryptoExchange.Net/Clients/SocketApiClient.cs +++ b/CryptoExchange.Net/Clients/SocketApiClient.cs @@ -424,13 +424,13 @@ namespace CryptoExchange.Net } /// - /// Update the original request to send when the connection is restored after disconnecting. Can be used to update an authentication token for example. + /// Update the subscription when the connection is restored after disconnecting. Can be used to update an authentication token for example. /// - /// The original request + /// The subscription /// - protected internal virtual Task> RevitalizeRequestAsync(object request) + protected internal virtual Task RevitalizeRequestAsync(Subscription subscription) { - return Task.FromResult(new CallResult(request)); + return Task.FromResult(new CallResult(null)); } /// diff --git a/CryptoExchange.Net/Sockets/SocketConnection.cs b/CryptoExchange.Net/Sockets/SocketConnection.cs index 30b81b6..0295d89 100644 --- a/CryptoExchange.Net/Sockets/SocketConnection.cs +++ b/CryptoExchange.Net/Sockets/SocketConnection.cs @@ -296,20 +296,28 @@ namespace CryptoExchange.Net.Sockets // Can't wait for this as it would cause a deadlock _ = Task.Run(async () => { - var reconnectSuccessful = await ProcessReconnectAsync().ConfigureAwait(false); - if (!reconnectSuccessful) + try { - _logger.Log(LogLevel.Warning, $"[Sckt {SocketId}] failed reconnect processing: {reconnectSuccessful.Error}, reconnecting again"); - _ = _socket.ReconnectAsync().ConfigureAwait(false); - } - else - { - Status = SocketStatus.Connected; - _ = Task.Run(() => + var reconnectSuccessful = await ProcessReconnectAsync().ConfigureAwait(false); + if (!reconnectSuccessful) { - ConnectionRestored?.Invoke(DateTime.UtcNow - DisconnectTime!.Value); - DisconnectTime = null; - }); + _logger.Log(LogLevel.Warning, $"[Sckt {SocketId}] failed reconnect processing: {reconnectSuccessful.Error}, reconnecting again"); + _ = _socket.ReconnectAsync().ConfigureAwait(false); + } + else + { + Status = SocketStatus.Connected; + _ = Task.Run(() => + { + ConnectionRestored?.Invoke(DateTime.UtcNow - DisconnectTime!.Value); + DisconnectTime = null; + }); + } + } + catch(Exception ex) + { + _logger.Log(LogLevel.Warning, ex, $"[Sckt {SocketId}] Unknown exception while processing reconnection, reconnecting again"); + _ = _socket.ReconnectAsync().ConfigureAwait(false); } }); @@ -755,7 +763,7 @@ namespace CryptoExchange.Net.Sockets if (!result) { _logger.Log(LogLevel.Warning, $"[Sckt {SocketId}] failed request revitalization: " + result.Error); - return result.As(false); + return result; } }