From 745d83445886c7d7a71e7989088b07ba43b7659a Mon Sep 17 00:00:00 2001 From: JKorf Date: Thu, 5 Apr 2018 14:56:34 +0200 Subject: [PATCH] Updated socket with locks, added null check to timestamp converter --- .../Authentication/AuthenticationProvider.cs | 22 ++++++- .../Converters/TimestampConverter.cs | 3 + .../Implementation/BaseSocket.cs | 58 ++++++++++++------- 3 files changed, 60 insertions(+), 23 deletions(-) diff --git a/CryptoExchange.Net/Authentication/AuthenticationProvider.cs b/CryptoExchange.Net/Authentication/AuthenticationProvider.cs index d562c98..4dd1431 100644 --- a/CryptoExchange.Net/Authentication/AuthenticationProvider.cs +++ b/CryptoExchange.Net/Authentication/AuthenticationProvider.cs @@ -11,9 +11,25 @@ namespace CryptoExchange.Net.Authentication Credentials = credentials; } - public abstract string AddAuthenticationToUriString(string uri, bool signed); - public abstract IRequest AddAuthenticationToRequest(IRequest request, bool signed); - public abstract string Sign(string toSign); + public virtual string AddAuthenticationToUriString(string uri, bool signed) + { + return uri; + } + + public virtual IRequest AddAuthenticationToRequest(IRequest request, bool signed) + { + return request; + } + + public virtual string Sign(string toSign) + { + return toSign; + } + + public virtual byte[] Sign(byte[] toSign) + { + return toSign; + } protected string ByteToString(byte[] buff) { diff --git a/CryptoExchange.Net/Converters/TimestampConverter.cs b/CryptoExchange.Net/Converters/TimestampConverter.cs index b3358eb..17e7c5f 100644 --- a/CryptoExchange.Net/Converters/TimestampConverter.cs +++ b/CryptoExchange.Net/Converters/TimestampConverter.cs @@ -12,6 +12,9 @@ namespace CryptoExchange.Net.Converters public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { + if (reader.Value == null) + return null; + var t = long.Parse(reader.Value.ToString()); return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(t); } diff --git a/CryptoExchange.Net/Implementation/BaseSocket.cs b/CryptoExchange.Net/Implementation/BaseSocket.cs index 50f9df1..7ddc940 100644 --- a/CryptoExchange.Net/Implementation/BaseSocket.cs +++ b/CryptoExchange.Net/Implementation/BaseSocket.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Net; using System.Security.Authentication; @@ -14,6 +15,7 @@ namespace CryptoExchange.Net.Implementation public class BaseSocket: IWebsocket { protected WebSocket socket; + protected object socketLock = new object(); protected readonly List> errorhandlers = new List>(); protected readonly List openhandlers = new List(); @@ -89,12 +91,21 @@ namespace CryptoExchange.Net.Implementation { await Task.Run(() => { - ManualResetEvent evnt = new ManualResetEvent(false); - var handler = new EventHandler((o, a) => evnt.Set()); - socket.Closed += handler; - socket.Close(); - evnt.WaitOne(); - socket.Closed -= handler; + lock (socketLock) + { + if (socket == null || IsClosed) + return; + + ManualResetEvent evnt = new ManualResetEvent(false); + var handler = new EventHandler((o, a) => evnt.Set()); + socket.Closed += handler; + socket.Close(); + bool triggered = evnt.WaitOne(3000); + socket.Closed -= handler; + + if (!triggered) + Debug.WriteLine($"Not triggered, {socket.State}, Open: {IsOpen}, Closed: {IsClosed}"); + } }).ConfigureAwait(false); } @@ -107,15 +118,18 @@ namespace CryptoExchange.Net.Implementation { return await Task.Run(() => { - ManualResetEvent evnt = new ManualResetEvent(false); - var handler = new EventHandler((o, a) => evnt.Set()); - socket.Opened += handler; - socket.Closed += handler; - socket.Open(); - evnt.WaitOne(); - socket.Opened -= handler; - socket.Closed -= handler; - return socket.State == WebSocketState.Open; + lock (socketLock) + { + ManualResetEvent evnt = new ManualResetEvent(false); + var handler = new EventHandler((o, a) => evnt.Set()); + socket.Opened += handler; + socket.Closed += handler; + socket.Open(); + evnt.WaitOne(); + socket.Opened -= handler; + socket.Closed -= handler; + return socket.State == WebSocketState.Open; + } }).ConfigureAwait(false); } @@ -134,12 +148,16 @@ namespace CryptoExchange.Net.Implementation public void Dispose() { - socket?.Dispose(); + lock (socketLock) + { + socket?.Dispose(); + socket = null; - errorhandlers.Clear(); - openhandlers.Clear(); - closehandlers.Clear(); - messagehandlers.Clear(); + errorhandlers.Clear(); + openhandlers.Clear(); + closehandlers.Clear(); + messagehandlers.Clear(); + } } } }