1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-12 10:26:27 +00:00

Added ConnectionClosed event for when no reconnecting is happening

This commit is contained in:
Jkorf 2021-08-24 13:25:11 +02:00
parent d775d228da
commit 307aab130b
5 changed files with 46 additions and 14 deletions

View File

@ -3036,7 +3036,7 @@
<inheritdoc cref="P:CryptoExchange.Net.Objects.SocketClientOptions.MaxResubscribeTries"/> <inheritdoc cref="P:CryptoExchange.Net.Objects.SocketClientOptions.MaxResubscribeTries"/>
</member> </member>
<member name="P:CryptoExchange.Net.SocketClient.MaxConcurrentResubscriptionsPerSocket"> <member name="P:CryptoExchange.Net.SocketClient.MaxConcurrentResubscriptionsPerSocket">
<inheritdoc cref="!:SocketClientOptions.MaxConcurrentResubscriptions"/> <inheritdoc cref="P:CryptoExchange.Net.Objects.SocketClientOptions.MaxConcurrentResubscriptionsPerSocket"/>
</member> </member>
<member name="F:CryptoExchange.Net.SocketClient.dataInterpreterBytes"> <member name="F:CryptoExchange.Net.SocketClient.dataInterpreterBytes">
<summary> <summary>
@ -3609,6 +3609,11 @@
Connection lost event Connection lost event
</summary> </summary>
</member> </member>
<member name="E:CryptoExchange.Net.Sockets.SocketConnection.ConnectionClosed">
<summary>
Connection closed and no reconnect is happening
</summary>
</member>
<member name="E:CryptoExchange.Net.Sockets.SocketConnection.ConnectionRestored"> <member name="E:CryptoExchange.Net.Sockets.SocketConnection.ConnectionRestored">
<summary> <summary>
Connecting restored event Connecting restored event
@ -3809,6 +3814,12 @@
Event when the connection is lost. The socket will automatically reconnect when possible. Event when the connection is lost. The socket will automatically reconnect when possible.
</summary> </summary>
</member> </member>
<member name="E:CryptoExchange.Net.Sockets.UpdateSubscription.ConnectionClosed">
<summary>
Event when the connection is closed. This event happens when reconnecting/resubscribing has failed too often based on the <see cref="P:CryptoExchange.Net.Objects.SocketClientOptions.MaxReconnectTries"/> and <see cref="P:CryptoExchange.Net.Objects.SocketClientOptions.MaxResubscribeTries"/> options,
or <see cref="P:CryptoExchange.Net.Objects.SocketClientOptions.AutoReconnect"/> is false
</summary>
</member>
<member name="E:CryptoExchange.Net.Sockets.UpdateSubscription.ConnectionRestored"> <member name="E:CryptoExchange.Net.Sockets.UpdateSubscription.ConnectionRestored">
<summary> <summary>
Event when the connection is restored. Timespan parameter indicates the time the socket has been offline for before reconnecting. Event when the connection is restored. Timespan parameter indicates the time the socket has been offline for before reconnecting.

View File

@ -247,11 +247,16 @@ namespace CryptoExchange.Net.OrderBook
subscription = startResult.Data; subscription = startResult.Data;
subscription.ConnectionLost += () => subscription.ConnectionLost += () =>
{ {
log.Write(LogLevel.Warning, $"{Id} order book {Symbol} connection lost"); log.Write(LogLevel.Warning, $"{Id} order book {Symbol} connection lost");
Status = OrderBookStatus.Reconnecting; Status = OrderBookStatus.Reconnecting;
Reset(); Reset();
}; };
subscription.ConnectionClosed += () =>
{
log.Write(LogLevel.Warning, $"{Id} order book {Symbol} disconnected");
Status = OrderBookStatus.Disconnected;
StopAsync();
};
subscription.ConnectionRestored += async time => await ResyncAsync().ConfigureAwait(false); subscription.ConnectionRestored += async time => await ResyncAsync().ConfigureAwait(false);
Status = OrderBookStatus.Synced; Status = OrderBookStatus.Synced;
@ -336,6 +341,7 @@ namespace CryptoExchange.Net.OrderBook
if(subscription != null) if(subscription != null)
await subscription.CloseAsync().ConfigureAwait(false); await subscription.CloseAsync().ConfigureAwait(false);
log.Write(LogLevel.Debug, $"{Id} order book {Symbol} stopped");
} }
/// <summary> /// <summary>

View File

@ -53,7 +53,7 @@ namespace CryptoExchange.Net
public int? MaxReconnectTries { get; protected set; } public int? MaxReconnectTries { get; protected set; }
/// <inheritdoc cref="SocketClientOptions.MaxResubscribeTries"/> /// <inheritdoc cref="SocketClientOptions.MaxResubscribeTries"/>
public int? MaxResubscribeTries { get; protected set; } public int? MaxResubscribeTries { get; protected set; }
/// <inheritdoc cref="SocketClientOptions.MaxConcurrentResubscriptions"/> /// <inheritdoc cref="SocketClientOptions.MaxConcurrentResubscriptionsPerSocket"/>
public int MaxConcurrentResubscriptionsPerSocket { get; protected set; } public int MaxConcurrentResubscriptionsPerSocket { get; protected set; }
/// <summary> /// <summary>
/// Delegate used for processing byte data received from socket connections before it is processed by handlers /// Delegate used for processing byte data received from socket connections before it is processed by handlers

View File

@ -23,6 +23,10 @@ namespace CryptoExchange.Net.Sockets
/// </summary> /// </summary>
public event Action? ConnectionLost; public event Action? ConnectionLost;
/// <summary> /// <summary>
/// Connection closed and no reconnect is happening
/// </summary>
public event Action? ConnectionClosed;
/// <summary>
/// Connecting restored event /// Connecting restored event
/// </summary> /// </summary>
public event Action<TimeSpan>? ConnectionRestored; public event Action<TimeSpan>? ConnectionRestored;
@ -127,17 +131,6 @@ namespace CryptoExchange.Net.Sockets
Socket.Timeout = client.SocketNoDataTimeout; Socket.Timeout = client.SocketNoDataTimeout;
Socket.OnMessage += ProcessMessage; Socket.OnMessage += ProcessMessage;
Socket.OnClose += () =>
{
if (lostTriggered)
return;
DisconnectTime = DateTime.UtcNow;
lostTriggered = true;
if (ShouldReconnect)
ConnectionLost?.Invoke();
};
Socket.OnClose += SocketOnClose; Socket.OnClose += SocketOnClose;
Socket.OnOpen += () => Socket.OnOpen += () =>
{ {
@ -315,6 +308,13 @@ namespace CryptoExchange.Net.Sockets
if (Socket.Reconnecting) if (Socket.Reconnecting)
return; // Already reconnecting return; // Already reconnecting
DisconnectTime = DateTime.UtcNow;
if (!lostTriggered)
{
lostTriggered = true;
ConnectionLost?.Invoke();
}
Socket.Reconnecting = true; Socket.Reconnecting = true;
log.Write(LogLevel.Information, $"Socket {Socket.Id} Connection lost, will try to reconnect after {socketClient.ReconnectInterval}"); log.Write(LogLevel.Information, $"Socket {Socket.Id} Connection lost, will try to reconnect after {socketClient.ReconnectInterval}");
@ -345,6 +345,7 @@ namespace CryptoExchange.Net.Sockets
socketClient.sockets.TryRemove(Socket.Id, out _); socketClient.sockets.TryRemove(Socket.Id, out _);
Closed?.Invoke(); Closed?.Invoke();
_ = Task.Run(() => ConnectionClosed?.Invoke());
break; break;
} }
@ -373,6 +374,7 @@ namespace CryptoExchange.Net.Sockets
socketClient.sockets.TryRemove(Socket.Id, out _); socketClient.sockets.TryRemove(Socket.Id, out _);
Closed?.Invoke(); Closed?.Invoke();
_ = Task.Run(() => ConnectionClosed?.Invoke());
} }
else else
log.Write(LogLevel.Debug, $"Socket {Socket.Id} resubscribing all subscriptions failed on reconnected socket{(socketClient.MaxResubscribeTries != null ? $", try {ResubscribeTry}/{socketClient.MaxResubscribeTries}" : "")}. Disconnecting and reconnecting."); log.Write(LogLevel.Debug, $"Socket {Socket.Id} resubscribing all subscriptions failed on reconnected socket{(socketClient.MaxResubscribeTries != null ? $", try {ResubscribeTry}/{socketClient.MaxResubscribeTries}" : "")}. Disconnecting and reconnecting.");
@ -397,6 +399,9 @@ namespace CryptoExchange.Net.Sockets
} }
else else
{ {
if (!socketClient.AutoReconnect && ShouldReconnect)
_ = Task.Run(() => ConnectionClosed?.Invoke());
// No reconnecting needed // No reconnecting needed
log.Write(LogLevel.Information, $"Socket {Socket.Id} closed"); log.Write(LogLevel.Information, $"Socket {Socket.Id} closed");
if (socketClient.sockets.ContainsKey(Socket.Id)) if (socketClient.sockets.ContainsKey(Socket.Id))

View File

@ -21,6 +21,16 @@ namespace CryptoExchange.Net.Sockets
remove => connection.ConnectionLost -= value; remove => connection.ConnectionLost -= value;
} }
/// <summary>
/// Event when the connection is closed. This event happens when reconnecting/resubscribing has failed too often based on the <see cref="SocketClientOptions.MaxReconnectTries"/> and <see cref="SocketClientOptions.MaxResubscribeTries"/> options,
/// or <see cref="SocketClientOptions.AutoReconnect"/> is false
/// </summary>
public event Action ConnectionClosed
{
add => connection.ConnectionClosed += value;
remove => connection.ConnectionClosed -= value;
}
/// <summary> /// <summary>
/// Event when the connection is restored. Timespan parameter indicates the time the socket has been offline for before reconnecting. /// Event when the connection is restored. Timespan parameter indicates the time the socket has been offline for before reconnecting.
/// Note that when the executing code is suspended and resumed at a later period (for example laptop going to sleep) the disconnect time will be incorrect as the diconnect /// Note that when the executing code is suspended and resumed at a later period (for example laptop going to sleep) the disconnect time will be incorrect as the diconnect