From 17f15603109200bdacf1809510ae4de4efa42999 Mon Sep 17 00:00:00 2001 From: Jkorf Date: Fri, 1 Nov 2024 09:38:01 +0100 Subject: [PATCH] Fixed socket connections trying to authenticated connection when it's marked as dedicated request connection even when no authentication is needed --- CryptoExchange.Net/Clients/SocketApiClient.cs | 18 ++++++++++++++---- .../Sockets/DedicatedConnectionConfig.cs | 15 +++++++++++++++ CryptoExchange.Net/Sockets/SocketConnection.cs | 10 +++++----- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/CryptoExchange.Net/Clients/SocketApiClient.cs b/CryptoExchange.Net/Clients/SocketApiClient.cs index 74c3f22..5f6b25b 100644 --- a/CryptoExchange.Net/Clients/SocketApiClient.cs +++ b/CryptoExchange.Net/Clients/SocketApiClient.cs @@ -490,11 +490,14 @@ namespace CryptoExchange.Net.Clients SocketConnection connection; if (!dedicatedRequestConnection) { - connection = socketQuery.Where(s => !s.Value.DedicatedRequestConnection).OrderBy(s => s.Value.UserSubscriptionCount).FirstOrDefault().Value; + connection = socketQuery.Where(s => !s.Value.DedicatedRequestConnection.IsDedicatedRequestConnection).OrderBy(s => s.Value.UserSubscriptionCount).FirstOrDefault().Value; } else { - connection = socketQuery.Where(s => s.Value.DedicatedRequestConnection).FirstOrDefault().Value; + connection = socketQuery.Where(s => s.Value.DedicatedRequestConnection.IsDedicatedRequestConnection).FirstOrDefault().Value; + if (connection != null && !connection.DedicatedRequestConnection.Authenticated) + // Mark dedicated request connection as authenticated if the request is authenticated + connection.DedicatedRequestConnection.Authenticated = authenticated; } if (connection != null) @@ -519,7 +522,14 @@ namespace CryptoExchange.Net.Clients var socketConnection = new SocketConnection(_logger, this, socket, address); socketConnection.UnhandledMessage += HandleUnhandledMessage; socketConnection.ConnectRateLimitedAsync += HandleConnectRateLimitedAsync; - socketConnection.DedicatedRequestConnection = dedicatedRequestConnection; + if (dedicatedRequestConnection) + { + socketConnection.DedicatedRequestConnection = new DedicatedConnectionState + { + IsDedicatedRequestConnection = dedicatedRequestConnection, + Authenticated = authenticated + }; + } foreach (var ptg in PeriodicTaskRegistrations) socketConnection.QueryPeriodic(ptg.Identifier, ptg.Interval, ptg.QueryDelegate, ptg.Callback); @@ -652,7 +662,7 @@ namespace CryptoExchange.Net.Clients var tasks = new List(); { var socketList = socketConnections.Values; - foreach (var connection in socketList.Where(s => !s.DedicatedRequestConnection)) + foreach (var connection in socketList.Where(s => !s.DedicatedRequestConnection.IsDedicatedRequestConnection)) tasks.Add(connection.CloseAsync()); } diff --git a/CryptoExchange.Net/Sockets/DedicatedConnectionConfig.cs b/CryptoExchange.Net/Sockets/DedicatedConnectionConfig.cs index 96d0393..af21b0e 100644 --- a/CryptoExchange.Net/Sockets/DedicatedConnectionConfig.cs +++ b/CryptoExchange.Net/Sockets/DedicatedConnectionConfig.cs @@ -14,4 +14,19 @@ /// public bool Authenticated { get; set; } } + + /// + /// Dedicated connection state + /// + public class DedicatedConnectionState + { + /// + /// Whether the connection is a dedicated request connection + /// + public bool IsDedicatedRequestConnection { get; set; } + /// + /// Whether the dedication request connection should be authenticated + /// + public bool Authenticated { get; set; } + } } diff --git a/CryptoExchange.Net/Sockets/SocketConnection.cs b/CryptoExchange.Net/Sockets/SocketConnection.cs index ab28d2d..755df4b 100644 --- a/CryptoExchange.Net/Sockets/SocketConnection.cs +++ b/CryptoExchange.Net/Sockets/SocketConnection.cs @@ -186,9 +186,9 @@ namespace CryptoExchange.Net.Sockets } /// - /// Whether this connection should be kept alive even when there is no subscription + /// Info on whether this connection is a dedicated request connection /// - public bool DedicatedRequestConnection { get; internal set; } + public DedicatedConnectionState DedicatedRequestConnection { get; internal set; } = new DedicatedConnectionState(); private bool _pausedActivity; private readonly object _listenersLock; @@ -618,7 +618,7 @@ namespace CryptoExchange.Net.Sockets bool shouldCloseConnection; lock (_listenersLock) - shouldCloseConnection = _listeners.OfType().All(r => !r.UserSubscription || r.Closed) && !DedicatedRequestConnection; + shouldCloseConnection = _listeners.OfType().All(r => !r.UserSubscription || r.Closed) && !DedicatedRequestConnection.IsDedicatedRequestConnection; if (!anyDuplicateSubscription) { @@ -841,7 +841,7 @@ namespace CryptoExchange.Net.Sockets if (!_socket.IsOpen) return new CallResult(new WebError("Socket not connected")); - if (!DedicatedRequestConnection) + if (!DedicatedRequestConnection.IsDedicatedRequestConnection) { bool anySubscriptions; lock (_listenersLock) @@ -859,7 +859,7 @@ namespace CryptoExchange.Net.Sockets lock (_listenersLock) { anyAuthenticated = _listeners.OfType().Any(s => s.Authenticated) - || (DedicatedRequestConnection && ApiClient.AuthenticationProvider != null); + || (DedicatedRequestConnection.IsDedicatedRequestConnection && DedicatedRequestConnection.Authenticated); } if (anyAuthenticated)