From a969e99e056c4c43a3faac7edb3a6d28b4212893 Mon Sep 17 00:00:00 2001 From: Jkorf Date: Fri, 8 Oct 2021 13:16:20 +0200 Subject: [PATCH] Added additional checks to not send/expect data when socket is not connected --- CryptoExchange.Net/CryptoExchange.Net.xml | 143 ++++++++++++++++++ CryptoExchange.Net/SocketClient.cs | 3 + .../Sockets/CryptoExchangeWebSocketClient.cs | 2 +- .../Sockets/SocketConnection.cs | 11 +- 4 files changed, 157 insertions(+), 2 deletions(-) diff --git a/CryptoExchange.Net/CryptoExchange.Net.xml b/CryptoExchange.Net/CryptoExchange.Net.xml index ba89938..d49f94d 100644 --- a/CryptoExchange.Net/CryptoExchange.Net.xml +++ b/CryptoExchange.Net/CryptoExchange.Net.xml @@ -4044,5 +4044,148 @@ + + + Specifies that is allowed as an input even if the + corresponding type disallows it. + + + + + Initializes a new instance of the class. + + + + + Specifies that is disallowed as an input even if the + corresponding type allows it. + + + + + Initializes a new instance of the class. + + + + + Specifies that a method that will never return under any circumstance. + + + + + Initializes a new instance of the class. + + + + + Specifies that the method will not return if the associated + parameter is passed the specified value. + + + + + Gets the condition parameter value. + Code after the method is considered unreachable by diagnostics if the argument + to the associated parameter matches this value. + + + + + Initializes a new instance of the + class with the specified parameter value. + + + The condition parameter value. + Code after the method is considered unreachable by diagnostics if the argument + to the associated parameter matches this value. + + + + + Specifies that an output may be even if the + corresponding type disallows it. + + + + + Initializes a new instance of the class. + + + + + Specifies that when a method returns , + the parameter may be even if the corresponding type disallows it. + + + + + Gets the return value condition. + If the method returns this value, the associated parameter may be . + + + + + Initializes the attribute with the specified return value condition. + + + The return value condition. + If the method returns this value, the associated parameter may be . + + + + + Specifies that an output is not even if the + corresponding type allows it. + + + + + Initializes a new instance of the class. + + + + + Specifies that the output will be non- if the + named parameter is non-. + + + + + Gets the associated parameter name. + The output will be non- if the argument to the + parameter specified is non-. + + + + + Initializes the attribute with the associated parameter name. + + + The associated parameter name. + The output will be non- if the argument to the + parameter specified is non-. + + + + + Specifies that when a method returns , + the parameter will not be even if the corresponding type allows it. + + + + + Gets the return value condition. + If the method returns this value, the associated parameter will not be . + + + + + Initializes the attribute with the specified return value condition. + + + The return value condition. + If the method returns this value, the associated parameter will not be . + + diff --git a/CryptoExchange.Net/SocketClient.cs b/CryptoExchange.Net/SocketClient.cs index 1d26a8d..d5cc3aa 100644 --- a/CryptoExchange.Net/SocketClient.cs +++ b/CryptoExchange.Net/SocketClient.cs @@ -585,6 +585,9 @@ namespace CryptoExchange.Net if (disposing) break; + if (!socket.Socket.IsOpen) + continue; + var obj = objGetter(socket); if (obj == null) continue; diff --git a/CryptoExchange.Net/Sockets/CryptoExchangeWebSocketClient.cs b/CryptoExchange.Net/Sockets/CryptoExchangeWebSocketClient.cs index e1468e2..5564dcf 100644 --- a/CryptoExchange.Net/Sockets/CryptoExchangeWebSocketClient.cs +++ b/CryptoExchange.Net/Sockets/CryptoExchangeWebSocketClient.cs @@ -108,7 +108,7 @@ namespace CryptoExchange.Net.Sockets /// /// If the connection is open /// - public bool IsOpen => _socket.State == WebSocketState.Open; + public bool IsOpen => _socket.State == WebSocketState.Open && !_closing; /// /// Ssl protocols supported. NOT USED BY THIS IMPLEMENTATION diff --git a/CryptoExchange.Net/Sockets/SocketConnection.cs b/CryptoExchange.Net/Sockets/SocketConnection.cs index 3def904..169a53e 100644 --- a/CryptoExchange.Net/Sockets/SocketConnection.cs +++ b/CryptoExchange.Net/Sockets/SocketConnection.cs @@ -452,6 +452,9 @@ namespace CryptoExchange.Net.Sockets { if (Authenticated) { + if (!Socket.IsOpen) + return false; + // If we reconnected a authenticated connection we need to re-authenticate var authResult = await socketClient.AuthenticateSocketAsync(this).ConfigureAwait(false); if (!authResult) @@ -475,6 +478,9 @@ namespace CryptoExchange.Net.Sockets var taskList = new List(); foreach (var subscription in subscriptionList.Skip(i).Take(socketClient.MaxConcurrentResubscriptionsPerSocket)) { + if (!Socket.IsOpen) + continue; + var task = socketClient.SubscribeAndWaitAsync(this, subscription.Request!, subscription).ContinueWith(t => { if (!t.Result) @@ -484,7 +490,7 @@ namespace CryptoExchange.Net.Sockets } await Task.WhenAll(taskList).ConfigureAwait(false); - if (!success) + if (!success || !Socket.IsOpen) return false; } @@ -499,6 +505,9 @@ namespace CryptoExchange.Net.Sockets internal async Task> ResubscribeAsync(SocketSubscription socketSubscription) { + if (!Socket.IsOpen) + return new CallResult(false, new UnknownError("Socket is not connected")); + return await socketClient.SubscribeAndWaitAsync(this, socketSubscription.Request!, socketSubscription).ConfigureAwait(false); }