1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-07 16:06:15 +00:00

Fixed socket connections trying to authenticated connection when it's marked as dedicated request connection even when no authentication is needed

This commit is contained in:
Jkorf 2024-11-01 09:38:01 +01:00
parent 41de0a3150
commit 17f1560310
3 changed files with 34 additions and 9 deletions

View File

@ -490,11 +490,14 @@ namespace CryptoExchange.Net.Clients
SocketConnection connection; SocketConnection connection;
if (!dedicatedRequestConnection) 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 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) if (connection != null)
@ -519,7 +522,14 @@ namespace CryptoExchange.Net.Clients
var socketConnection = new SocketConnection(_logger, this, socket, address); var socketConnection = new SocketConnection(_logger, this, socket, address);
socketConnection.UnhandledMessage += HandleUnhandledMessage; socketConnection.UnhandledMessage += HandleUnhandledMessage;
socketConnection.ConnectRateLimitedAsync += HandleConnectRateLimitedAsync; socketConnection.ConnectRateLimitedAsync += HandleConnectRateLimitedAsync;
socketConnection.DedicatedRequestConnection = dedicatedRequestConnection; if (dedicatedRequestConnection)
{
socketConnection.DedicatedRequestConnection = new DedicatedConnectionState
{
IsDedicatedRequestConnection = dedicatedRequestConnection,
Authenticated = authenticated
};
}
foreach (var ptg in PeriodicTaskRegistrations) foreach (var ptg in PeriodicTaskRegistrations)
socketConnection.QueryPeriodic(ptg.Identifier, ptg.Interval, ptg.QueryDelegate, ptg.Callback); socketConnection.QueryPeriodic(ptg.Identifier, ptg.Interval, ptg.QueryDelegate, ptg.Callback);
@ -652,7 +662,7 @@ namespace CryptoExchange.Net.Clients
var tasks = new List<Task>(); var tasks = new List<Task>();
{ {
var socketList = socketConnections.Values; 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()); tasks.Add(connection.CloseAsync());
} }

View File

@ -14,4 +14,19 @@
/// </summary> /// </summary>
public bool Authenticated { get; set; } public bool Authenticated { get; set; }
} }
/// <summary>
/// Dedicated connection state
/// </summary>
public class DedicatedConnectionState
{
/// <summary>
/// Whether the connection is a dedicated request connection
/// </summary>
public bool IsDedicatedRequestConnection { get; set; }
/// <summary>
/// Whether the dedication request connection should be authenticated
/// </summary>
public bool Authenticated { get; set; }
}
} }

View File

@ -186,9 +186,9 @@ namespace CryptoExchange.Net.Sockets
} }
/// <summary> /// <summary>
/// Whether this connection should be kept alive even when there is no subscription /// Info on whether this connection is a dedicated request connection
/// </summary> /// </summary>
public bool DedicatedRequestConnection { get; internal set; } public DedicatedConnectionState DedicatedRequestConnection { get; internal set; } = new DedicatedConnectionState();
private bool _pausedActivity; private bool _pausedActivity;
private readonly object _listenersLock; private readonly object _listenersLock;
@ -618,7 +618,7 @@ namespace CryptoExchange.Net.Sockets
bool shouldCloseConnection; bool shouldCloseConnection;
lock (_listenersLock) lock (_listenersLock)
shouldCloseConnection = _listeners.OfType<Subscription>().All(r => !r.UserSubscription || r.Closed) && !DedicatedRequestConnection; shouldCloseConnection = _listeners.OfType<Subscription>().All(r => !r.UserSubscription || r.Closed) && !DedicatedRequestConnection.IsDedicatedRequestConnection;
if (!anyDuplicateSubscription) if (!anyDuplicateSubscription)
{ {
@ -841,7 +841,7 @@ namespace CryptoExchange.Net.Sockets
if (!_socket.IsOpen) if (!_socket.IsOpen)
return new CallResult(new WebError("Socket not connected")); return new CallResult(new WebError("Socket not connected"));
if (!DedicatedRequestConnection) if (!DedicatedRequestConnection.IsDedicatedRequestConnection)
{ {
bool anySubscriptions; bool anySubscriptions;
lock (_listenersLock) lock (_listenersLock)
@ -859,7 +859,7 @@ namespace CryptoExchange.Net.Sockets
lock (_listenersLock) lock (_listenersLock)
{ {
anyAuthenticated = _listeners.OfType<Subscription>().Any(s => s.Authenticated) anyAuthenticated = _listeners.OfType<Subscription>().Any(s => s.Authenticated)
|| (DedicatedRequestConnection && ApiClient.AuthenticationProvider != null); || (DedicatedRequestConnection.IsDedicatedRequestConnection && DedicatedRequestConnection.Authenticated);
} }
if (anyAuthenticated) if (anyAuthenticated)