From 66c0b9001611cbf165b603020f5c906407f641c6 Mon Sep 17 00:00:00 2001 From: JKorf Date: Wed, 5 Feb 2020 11:26:36 +0100 Subject: [PATCH] Added paused activity socket events --- CryptoExchange.Net/CryptoExchange.Net.xml | 20 ++++++++++++++ CryptoExchange.Net/SocketClient.cs | 5 ++++ .../Sockets/SocketConnection.cs | 27 +++++++++++++++++-- .../Sockets/UpdateSubscription.cs | 18 +++++++++++++ 4 files changed, 68 insertions(+), 2 deletions(-) diff --git a/CryptoExchange.Net/CryptoExchange.Net.xml b/CryptoExchange.Net/CryptoExchange.Net.xml index 62e5487..97639e7 100644 --- a/CryptoExchange.Net/CryptoExchange.Net.xml +++ b/CryptoExchange.Net/CryptoExchange.Net.xml @@ -2689,6 +2689,16 @@ Connecting restored event + + + The connection is paused event + + + + + The connection is unpaused event + + Connecting closed event @@ -2858,6 +2868,16 @@ Event when the connection is restored. Timespan parameter indicates the time the socket has been offline for before reconnecting + + + Event when the connection to the server is paused. No operations can be performed while paused + + + + + Event when the connection to the server is unpaused + + Event when an exception happened diff --git a/CryptoExchange.Net/SocketClient.cs b/CryptoExchange.Net/SocketClient.cs index c1848ef..77ea973 100644 --- a/CryptoExchange.Net/SocketClient.cs +++ b/CryptoExchange.Net/SocketClient.cs @@ -161,6 +161,11 @@ namespace CryptoExchange.Net semaphoreSlim.Release(); } + if (socket.PausedActivity) + { + log.Write(LogVerbosity.Info, "Socket has been paused, can't subscribe at this moment"); + return new CallResult(default, new ServerError("Socket is paused")); + } if (request != null) { diff --git a/CryptoExchange.Net/Sockets/SocketConnection.cs b/CryptoExchange.Net/Sockets/SocketConnection.cs index e1704e0..8b6e2c8 100644 --- a/CryptoExchange.Net/Sockets/SocketConnection.cs +++ b/CryptoExchange.Net/Sockets/SocketConnection.cs @@ -25,6 +25,14 @@ namespace CryptoExchange.Net.Sockets /// public event Action? ConnectionRestored; /// + /// The connection is paused event + /// + public event Action? ActivityPaused; + /// + /// The connection is unpaused event + /// + public event Action? ActivityUnpaused; + /// /// Connecting closed event /// public event Action? Closed; @@ -60,11 +68,26 @@ namespace CryptoExchange.Net.Sockets /// Time of disconnecting /// public DateTime? DisconnectTime { get; set; } + /// /// If activity is paused /// - public bool PausedActivity { get; set; } + public bool PausedActivity + { + get => pausedActivity; + set + { + if (pausedActivity != value) + { + pausedActivity = value; + log.Write(LogVerbosity.Debug, "Paused activity: " + value); + if(pausedActivity) ActivityPaused?.Invoke(); + else ActivityUnpaused?.Invoke(); + } + } + } + private bool pausedActivity; private readonly List handlers; private readonly object handlersLock = new object(); @@ -155,7 +178,7 @@ namespace CryptoExchange.Net.Sockets var sw = Stopwatch.StartNew(); lock (handlersLock) { - foreach (var handler in handlers) + foreach (var handler in handlers.ToList()) { currentSubscription = handler; if (handler.Request == null) diff --git a/CryptoExchange.Net/Sockets/UpdateSubscription.cs b/CryptoExchange.Net/Sockets/UpdateSubscription.cs index 681b0c5..98f37b4 100644 --- a/CryptoExchange.Net/Sockets/UpdateSubscription.cs +++ b/CryptoExchange.Net/Sockets/UpdateSubscription.cs @@ -29,6 +29,24 @@ namespace CryptoExchange.Net.Sockets remove => connection.ConnectionRestored -= value; } + /// + /// Event when the connection to the server is paused. No operations can be performed while paused + /// + public event Action ActivityPaused + { + add => connection.ActivityPaused += value; + remove => connection.ActivityPaused -= value; + } + + /// + /// Event when the connection to the server is unpaused + /// + public event Action ActivityUnpaused + { + add => connection.ActivityUnpaused += value; + remove => connection.ActivityUnpaused -= value; + } + /// /// Event when an exception happened ///