From 1b6e1f99f8b89e085a127e0ab3fff42641c42316 Mon Sep 17 00:00:00 2001 From: JKorf Date: Thu, 29 Nov 2018 11:28:46 +0100 Subject: [PATCH] Added name to message handlers, added socket type to subscription for background sockets --- CryptoExchange.Net/Objects/Enums.cs | 7 +++++++ CryptoExchange.Net/SocketClient.cs | 16 +++++++++++++++- CryptoExchange.Net/Sockets/SocketSubscription.cs | 6 ++++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/CryptoExchange.Net/Objects/Enums.cs b/CryptoExchange.Net/Objects/Enums.cs index 7e821d8..825ca1d 100644 --- a/CryptoExchange.Net/Objects/Enums.cs +++ b/CryptoExchange.Net/Objects/Enums.cs @@ -17,4 +17,11 @@ FormData, Json } + + public enum SocketType + { + Normal, + Background, + BackgroundAuthenticated + } } diff --git a/CryptoExchange.Net/SocketClient.cs b/CryptoExchange.Net/SocketClient.cs index 8fa26a8..b5d1248 100644 --- a/CryptoExchange.Net/SocketClient.cs +++ b/CryptoExchange.Net/SocketClient.cs @@ -26,6 +26,15 @@ namespace CryptoExchange.Net protected TimeSpan reconnectInterval; protected Func dataInterpreter; + + protected const string DataHandlerName = "DataHandler"; + protected const string AuthenticationHandlerName = "AuthenticationHandler"; + protected const string SubscriptionHandlerName = "SubscriptionHandler"; + protected const string PingHandlerName = "SubscriptionHandler"; + + protected const string DataEvent = "Data"; + protected const string SubscriptionEvent = "Subscription"; + protected const string AuthenticationEvent = "Authentication"; #endregion protected SocketClient(SocketClientOptions exchangeOptions, AuthenticationProvider authenticationProvider): base(exchangeOptions, authenticationProvider) @@ -84,6 +93,11 @@ namespace CryptoExchange.Net return socket; } + protected virtual SocketSubscription GetBackgroundSocket(bool authenticated = false) + { + return sockets.SingleOrDefault(s => s.Type == (authenticated ? SocketType.BackgroundAuthenticated : SocketType.Background)); + } + protected virtual void SocketOpened(IWebsocket socket) { } protected virtual void SocketClosed(IWebsocket socket) { } protected virtual void SocketError(IWebsocket socket, Exception ex) { } @@ -125,7 +139,7 @@ namespace CryptoExchange.Net { log.Write(LogVerbosity.Debug, $"Socket {subscription.Socket.Id} received data: " + data); foreach (var handler in subscription.MessageHandlers) - if (handler(subscription, JToken.Parse(data))) + if (handler.Value(subscription, JToken.Parse(data))) return; } diff --git a/CryptoExchange.Net/Sockets/SocketSubscription.cs b/CryptoExchange.Net/Sockets/SocketSubscription.cs index e19a0ba..fac979e 100644 --- a/CryptoExchange.Net/Sockets/SocketSubscription.cs +++ b/CryptoExchange.Net/Sockets/SocketSubscription.cs @@ -16,12 +16,14 @@ namespace CryptoExchange.Net.Sockets /// /// Message handlers for this subscription. Should return true if the message is handled and should not be distributed to the other handlers /// - public List> MessageHandlers { get; set; } + public Dictionary> MessageHandlers { get; set; } public List Events { get; set; } public IWebsocket Socket { get; set; } public SocketRequest Request { get; set; } + public SocketType Type { get; set; } + private bool lostTriggered; private List waitingForEvents; @@ -32,7 +34,7 @@ namespace CryptoExchange.Net.Sockets Events = new List(); waitingForEvents = new List(); - MessageHandlers = new List>(); + MessageHandlers = new Dictionary>(); Socket.OnClose += () => {