mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-06-08 16:36:15 +00:00
Merge branch 'master' of https://github.com/JKorf/CryptoExchange.Net
This commit is contained in:
commit
2b1c77577c
@ -32,6 +32,7 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations
|
||||
public SslProtocols SSLProtocols { get; set; }
|
||||
|
||||
public int ConnectCalls { get; private set; }
|
||||
public bool Reconnecting { get; set; }
|
||||
|
||||
public static int lastId = 0;
|
||||
public static object lastIdLock = new object();
|
||||
|
@ -7,7 +7,7 @@
|
||||
<PropertyGroup>
|
||||
<PackageId>CryptoExchange.Net</PackageId>
|
||||
<Authors>JKorf</Authors>
|
||||
<PackageVersion>2.0.3</PackageVersion>
|
||||
<PackageVersion>2.0.6</PackageVersion>
|
||||
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
|
||||
<PackageProjectUrl>https://github.com/JKorf/CryptoExchange.Net</PackageProjectUrl>
|
||||
<PackageLicenseUrl>https://github.com/JKorf/CryptoExchange.Net/blob/master/LICENSE</PackageLicenseUrl>
|
||||
|
@ -14,6 +14,7 @@ namespace CryptoExchange.Net.Interfaces
|
||||
|
||||
int Id { get; }
|
||||
bool ShouldReconnect { get; set; }
|
||||
bool Reconnecting { get; set; }
|
||||
Func<byte[], string> DataInterpreter { get; set; }
|
||||
DateTime? DisconnectTime { get; set; }
|
||||
string Url { get; }
|
||||
|
@ -169,6 +169,11 @@ namespace CryptoExchange.Net
|
||||
{
|
||||
if (socket.ShouldReconnect)
|
||||
{
|
||||
if (socket.Reconnecting)
|
||||
return; // Already reconnecting
|
||||
|
||||
socket.Reconnecting = true;
|
||||
|
||||
log.Write(LogVerbosity.Info, $"Socket {socket.Id} Connection lost, will try to reconnect");
|
||||
Task.Run(() =>
|
||||
{
|
||||
@ -191,6 +196,7 @@ namespace CryptoExchange.Net
|
||||
lock (sockets)
|
||||
subscription = sockets.Single(s => s.Socket == socket);
|
||||
|
||||
socket.Reconnecting = false;
|
||||
if (!SocketReconnect(subscription, DateTime.UtcNow - time.Value))
|
||||
socket.Close().Wait(); // Close so we end up reconnecting again
|
||||
else
|
||||
|
@ -32,7 +32,10 @@ namespace CryptoExchange.Net.Sockets
|
||||
|
||||
public int Id { get; }
|
||||
public DateTime? DisconnectTime { get; set; }
|
||||
|
||||
public bool ShouldReconnect { get; set; }
|
||||
public bool Reconnecting { get; set; }
|
||||
|
||||
public string Url { get; }
|
||||
public bool IsClosed => socket.State == WebSocketState.Closed;
|
||||
public bool IsOpen => socket.State == WebSocketState.Open;
|
||||
@ -115,14 +118,17 @@ namespace CryptoExchange.Net.Sockets
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (socket == null || socket.State != WebSocketState.Open)
|
||||
return;
|
||||
|
||||
if (DateTime.UtcNow - LastActionTime > Timeout)
|
||||
lock (socketLock)
|
||||
{
|
||||
log.Write(LogVerbosity.Warning, $"No data received for {Timeout}, reconnecting socket");
|
||||
Close().Wait();
|
||||
return;
|
||||
if (socket == null || socket.State != WebSocketState.Open)
|
||||
return;
|
||||
|
||||
if (DateTime.UtcNow - LastActionTime > Timeout)
|
||||
{
|
||||
log.Write(LogVerbosity.Warning, $"No data received for {Timeout}, reconnecting socket");
|
||||
Close().ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Thread.Sleep(500);
|
||||
@ -165,8 +171,11 @@ namespace CryptoExchange.Net.Sockets
|
||||
|
||||
public virtual void Reset()
|
||||
{
|
||||
socket.Dispose();
|
||||
socket = null;
|
||||
lock (socketLock)
|
||||
{
|
||||
socket?.Dispose();
|
||||
socket = null;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Send(string data)
|
||||
|
@ -26,6 +26,7 @@ namespace CryptoExchange.Net.Sockets
|
||||
|
||||
private bool lostTriggered;
|
||||
private readonly List<SocketEvent> waitingForEvents;
|
||||
private object eventLock = new object();
|
||||
|
||||
|
||||
public SocketSubscription(IWebsocket socket)
|
||||
@ -44,8 +45,11 @@ namespace CryptoExchange.Net.Sockets
|
||||
Socket.DisconnectTime = DateTime.UtcNow;
|
||||
lostTriggered = true;
|
||||
|
||||
foreach (var events in Events)
|
||||
events.Reset();
|
||||
lock (eventLock)
|
||||
{
|
||||
foreach (var events in Events)
|
||||
events.Reset();
|
||||
}
|
||||
|
||||
if (Socket.ShouldReconnect)
|
||||
ConnectionLost?.Invoke();
|
||||
@ -62,72 +66,89 @@ namespace CryptoExchange.Net.Sockets
|
||||
|
||||
public void AddEvent(string name)
|
||||
{
|
||||
Events.Add(new SocketEvent(name));
|
||||
lock (eventLock)
|
||||
Events.Add(new SocketEvent(name));
|
||||
}
|
||||
|
||||
public void SetEventByName(string name, bool success, Error error)
|
||||
{
|
||||
var waitingEvent = waitingForEvents.SingleOrDefault(e => e.Name == name);
|
||||
if (waitingEvent != null)
|
||||
lock (eventLock)
|
||||
{
|
||||
waitingEvent.Set(success, error);
|
||||
waitingForEvents.Remove(waitingEvent);
|
||||
var waitingEvent = waitingForEvents.SingleOrDefault(e => e.Name == name);
|
||||
if (waitingEvent != null)
|
||||
{
|
||||
waitingEvent.Set(success, error);
|
||||
waitingForEvents.Remove(waitingEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SetEventById(string id, bool success, Error error)
|
||||
{
|
||||
var waitingEvent = waitingForEvents.SingleOrDefault(e => e.WaitingId == id);
|
||||
if (waitingEvent != null)
|
||||
lock (eventLock)
|
||||
{
|
||||
waitingEvent.Set(success, error);
|
||||
waitingForEvents.Remove(waitingEvent);
|
||||
var waitingEvent = waitingForEvents.SingleOrDefault(e => e.WaitingId == id);
|
||||
if (waitingEvent != null)
|
||||
{
|
||||
waitingEvent.Set(success, error);
|
||||
waitingForEvents.Remove(waitingEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public SocketEvent GetWaitingEvent(string name)
|
||||
{
|
||||
return waitingForEvents.SingleOrDefault(w => w.Name == name);
|
||||
lock (eventLock)
|
||||
return waitingForEvents.SingleOrDefault(w => w.Name == name);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Task<CallResult<bool>> WaitForEvent(string name, TimeSpan timeout)
|
||||
{
|
||||
return WaitForEvent(name, (int)Math.Round(timeout.TotalMilliseconds, 0));
|
||||
lock (eventLock)
|
||||
return WaitForEvent(name, (int)Math.Round(timeout.TotalMilliseconds, 0));
|
||||
}
|
||||
|
||||
public Task<CallResult<bool>> WaitForEvent(string name, int timeout)
|
||||
{
|
||||
var evnt = Events.Single(e => e.Name == name);
|
||||
waitingForEvents.Add(evnt);
|
||||
return Task.Run(() => evnt.Wait(timeout));
|
||||
lock (eventLock)
|
||||
{
|
||||
var evnt = Events.Single(e => e.Name == name);
|
||||
waitingForEvents.Add(evnt);
|
||||
return Task.Run(() => evnt.Wait(timeout));
|
||||
}
|
||||
}
|
||||
|
||||
public Task<CallResult<bool>> WaitForEvent(string name, string id, TimeSpan timeout)
|
||||
{
|
||||
return WaitForEvent(name, id, (int)Math.Round(timeout.TotalMilliseconds, 0));
|
||||
lock (eventLock)
|
||||
return WaitForEvent(name, id, (int)Math.Round(timeout.TotalMilliseconds, 0));
|
||||
}
|
||||
|
||||
public Task<CallResult<bool>> WaitForEvent(string name, string id, int timeout)
|
||||
{
|
||||
var evnt = Events.Single(e => e.Name == name);
|
||||
evnt.WaitingId = id;
|
||||
waitingForEvents.Add(evnt);
|
||||
return Task.Run(() => evnt.Wait(timeout));
|
||||
lock (eventLock)
|
||||
{
|
||||
var evnt = Events.Single(e => e.Name == name);
|
||||
evnt.WaitingId = id;
|
||||
waitingForEvents.Add(evnt);
|
||||
return Task.Run(() => evnt.Wait(timeout));
|
||||
}
|
||||
}
|
||||
|
||||
public void ResetEvents()
|
||||
{
|
||||
foreach (var waiting in new List<SocketEvent>(waitingForEvents))
|
||||
waiting.Set(false, new UnknownError("Connection reset"));
|
||||
waitingForEvents.Clear();
|
||||
lock (eventLock)
|
||||
{
|
||||
foreach (var waiting in waitingForEvents)
|
||||
waiting.Set(false, new UnknownError("Connection reset"));
|
||||
waitingForEvents.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task Close()
|
||||
{
|
||||
Socket.ShouldReconnect = false;
|
||||
await Socket.Close();
|
||||
await Socket.Close().ConfigureAwait(false);
|
||||
Socket.Dispose();
|
||||
}
|
||||
}
|
||||
|
@ -41,7 +41,7 @@ namespace CryptoExchange.Net.Sockets
|
||||
/// <returns></returns>
|
||||
public async Task Close()
|
||||
{
|
||||
await subscription.Close();
|
||||
await subscription.Close().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user