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:
parent
9dd1b7b318
commit
745d834458
@ -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)
|
||||
{
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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<Action<Exception>> errorhandlers = new List<Action<Exception>>();
|
||||
protected readonly List<Action> openhandlers = new List<Action>();
|
||||
@ -89,12 +91,21 @@ namespace CryptoExchange.Net.Implementation
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
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();
|
||||
evnt.WaitOne();
|
||||
bool triggered = evnt.WaitOne(3000);
|
||||
socket.Closed -= handler;
|
||||
|
||||
if (!triggered)
|
||||
Debug.WriteLine($"Not triggered, {socket.State}, Open: {IsOpen}, Closed: {IsClosed}");
|
||||
}
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
@ -106,6 +117,8 @@ namespace CryptoExchange.Net.Implementation
|
||||
public async Task<bool> Connect()
|
||||
{
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
lock (socketLock)
|
||||
{
|
||||
ManualResetEvent evnt = new ManualResetEvent(false);
|
||||
var handler = new EventHandler((o, a) => evnt.Set());
|
||||
@ -116,6 +129,7 @@ namespace CryptoExchange.Net.Implementation
|
||||
socket.Opened -= handler;
|
||||
socket.Closed -= handler;
|
||||
return socket.State == WebSocketState.Open;
|
||||
}
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
@ -133,8 +147,11 @@ namespace CryptoExchange.Net.Implementation
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
lock (socketLock)
|
||||
{
|
||||
socket?.Dispose();
|
||||
socket = null;
|
||||
|
||||
errorhandlers.Clear();
|
||||
openhandlers.Clear();
|
||||
@ -142,4 +159,5 @@ namespace CryptoExchange.Net.Implementation
|
||||
messagehandlers.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user