From cb68b4d6e38b2bc552c44f9eff2b3224e3a2c69b Mon Sep 17 00:00:00 2001 From: Jan Korf Date: Wed, 7 Aug 2019 14:57:58 +0200 Subject: [PATCH] Resubscribing after reconnecting now in parallel, fixed bug with ghost socket --- CryptoExchange.Net/CryptoExchange.Net.xml | 2 +- .../Sockets/SocketConnection.cs | 31 ++++++++++++++----- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/CryptoExchange.Net/CryptoExchange.Net.xml b/CryptoExchange.Net/CryptoExchange.Net.xml index 0206b87..fd9367b 100644 --- a/CryptoExchange.Net/CryptoExchange.Net.xml +++ b/CryptoExchange.Net/CryptoExchange.Net.xml @@ -2371,7 +2371,7 @@ - Close the subscriptions + Close the subscription Subscription to close diff --git a/CryptoExchange.Net/Sockets/SocketConnection.cs b/CryptoExchange.Net/Sockets/SocketConnection.cs index d827517..2bb0df3 100644 --- a/CryptoExchange.Net/Sockets/SocketConnection.cs +++ b/CryptoExchange.Net/Sockets/SocketConnection.cs @@ -148,6 +148,7 @@ namespace CryptoExchange.Net.Sockets if (tokenData == null) return; + var handledResponse = false; foreach (var pendingRequest in pendingRequests.ToList()) { if (pendingRequest.Check(tokenData)) @@ -155,11 +156,12 @@ namespace CryptoExchange.Net.Sockets pendingRequests.Remove(pendingRequest); if (!socketClient.ContinueOnQueryResponse) return; + handledResponse = true; break; } } - - if (!HandleData(tokenData)) + + if (!HandleData(tokenData) && !handledResponse) { log.Write(LogVerbosity.Debug, "Message not handled: " + tokenData); } @@ -306,6 +308,9 @@ namespace CryptoExchange.Net.Sockets else { log.Write(LogVerbosity.Info, $"Socket {Socket.Id} closed"); + if (socketClient.sockets.ContainsKey(Socket.Id)) + socketClient.sockets.TryRemove(Socket.Id, out _); + Socket.Dispose(); Closed?.Invoke(); } @@ -333,14 +338,24 @@ namespace CryptoExchange.Net.Sockets List handlerList; lock (handlersLock) handlerList = handlers.Where(h => h.Request != null).ToList(); + + var success = true; + var taskList = new List(); foreach (var handler in handlerList) { - var resubResult = await socketClient.SubscribeAndWait(this, handler.Request, handler).ConfigureAwait(false); - if (!resubResult.Success) + var task = socketClient.SubscribeAndWait(this, handler.Request, handler).ContinueWith(t => { - log.Write(LogVerbosity.Debug, "Resubscribing all subscriptions failed on reconnected socket. Disconnecting and reconnecting."); - return false; - } + if (!t.Result.Success) + success = false; + }); + taskList.Add(task); + } + + Task.WaitAll(taskList.ToArray()); + if (!success) + { + log.Write(LogVerbosity.Debug, "Resubscribing all subscriptions failed on reconnected socket. Disconnecting and reconnecting."); + return false; } log.Write(LogVerbosity.Debug, "All subscription successfully resubscribed on reconnected socket."); @@ -363,7 +378,7 @@ namespace CryptoExchange.Net.Sockets } /// - /// Close the subscriptions + /// Close the subscription /// /// Subscription to close ///