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

Updated socket with locks, added null check to timestamp converter

This commit is contained in:
JKorf 2018-04-05 14:56:34 +02:00
parent 9dd1b7b318
commit 745d834458
3 changed files with 60 additions and 23 deletions

View File

@ -11,9 +11,25 @@ namespace CryptoExchange.Net.Authentication
Credentials = credentials; Credentials = credentials;
} }
public abstract string AddAuthenticationToUriString(string uri, bool signed); public virtual string AddAuthenticationToUriString(string uri, bool signed)
public abstract IRequest AddAuthenticationToRequest(IRequest request, bool signed); {
public abstract string Sign(string toSign); 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) protected string ByteToString(byte[] buff)
{ {

View File

@ -12,6 +12,9 @@ namespace CryptoExchange.Net.Converters
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 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()); var t = long.Parse(reader.Value.ToString());
return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(t); return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(t);
} }

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Security.Authentication; using System.Security.Authentication;
@ -14,6 +15,7 @@ namespace CryptoExchange.Net.Implementation
public class BaseSocket: IWebsocket public class BaseSocket: IWebsocket
{ {
protected WebSocket socket; protected WebSocket socket;
protected object socketLock = new object();
protected readonly List<Action<Exception>> errorhandlers = new List<Action<Exception>>(); protected readonly List<Action<Exception>> errorhandlers = new List<Action<Exception>>();
protected readonly List<Action> openhandlers = new List<Action>(); protected readonly List<Action> openhandlers = new List<Action>();
@ -89,12 +91,21 @@ namespace CryptoExchange.Net.Implementation
{ {
await Task.Run(() => await Task.Run(() =>
{ {
ManualResetEvent evnt = new ManualResetEvent(false); lock (socketLock)
var handler = new EventHandler((o, a) => evnt.Set()); {
socket.Closed += handler; if (socket == null || IsClosed)
socket.Close(); return;
evnt.WaitOne();
socket.Closed -= handler; 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); }).ConfigureAwait(false);
} }
@ -107,15 +118,18 @@ namespace CryptoExchange.Net.Implementation
{ {
return await Task.Run(() => return await Task.Run(() =>
{ {
ManualResetEvent evnt = new ManualResetEvent(false); lock (socketLock)
var handler = new EventHandler((o, a) => evnt.Set()); {
socket.Opened += handler; ManualResetEvent evnt = new ManualResetEvent(false);
socket.Closed += handler; var handler = new EventHandler((o, a) => evnt.Set());
socket.Open(); socket.Opened += handler;
evnt.WaitOne(); socket.Closed += handler;
socket.Opened -= handler; socket.Open();
socket.Closed -= handler; evnt.WaitOne();
return socket.State == WebSocketState.Open; socket.Opened -= handler;
socket.Closed -= handler;
return socket.State == WebSocketState.Open;
}
}).ConfigureAwait(false); }).ConfigureAwait(false);
} }
@ -134,12 +148,16 @@ namespace CryptoExchange.Net.Implementation
public void Dispose() public void Dispose()
{ {
socket?.Dispose(); lock (socketLock)
{
socket?.Dispose();
socket = null;
errorhandlers.Clear(); errorhandlers.Clear();
openhandlers.Clear(); openhandlers.Clear();
closehandlers.Clear(); closehandlers.Clear();
messagehandlers.Clear(); messagehandlers.Clear();
}
} }
} }
} }