mirror of
https://github.com/JKorf/CryptoExchange.Net.git
synced 2026-08-23 06:13:09 +00:00
Various socket client changes
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
using CryptoExchange.Net.Objects;
|
||||
using System.Threading;
|
||||
|
||||
namespace CryptoExchange.Net.Sockets
|
||||
{
|
||||
public class SocketEvent
|
||||
{
|
||||
public string Name { get; set; }
|
||||
|
||||
private CallResult<bool> result;
|
||||
private ManualResetEvent setEvnt;
|
||||
|
||||
public SocketEvent(string name)
|
||||
{
|
||||
Name = name;
|
||||
setEvnt = new ManualResetEvent(false);
|
||||
result = new CallResult<bool>(false, new UnknownError("No response received"));
|
||||
}
|
||||
|
||||
public void Set(bool result, Error error)
|
||||
{
|
||||
this.result = new CallResult<bool>(result, error);
|
||||
setEvnt.Set();
|
||||
}
|
||||
|
||||
public CallResult<bool> Wait(int timeout = 5000)
|
||||
{
|
||||
setEvnt.WaitOne(timeout);
|
||||
return result;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
setEvnt.Reset();
|
||||
result = new CallResult<bool>(false, new UnknownError("No response received"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,20 @@
|
||||
using CryptoExchange.Net.Interfaces;
|
||||
using CryptoExchange.Net.Objects;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CryptoExchange.Net.Sockets
|
||||
{
|
||||
public class SocketSubscription
|
||||
{
|
||||
public event Action ConnectionLost;
|
||||
public event Action ConnectionRestored;
|
||||
public event Action<TimeSpan> ConnectionRestored;
|
||||
|
||||
public List<Action<SocketSubscription, JToken>> DataHandlers { get; set; }
|
||||
public List<SocketEvent> Events { get; set; }
|
||||
|
||||
public IWebsocket Socket { get; set; }
|
||||
public object Request { get; set; }
|
||||
@@ -18,6 +24,9 @@ namespace CryptoExchange.Net.Sockets
|
||||
public SocketSubscription(IWebsocket socket)
|
||||
{
|
||||
Socket = socket;
|
||||
Events = new List<SocketEvent>();
|
||||
|
||||
DataHandlers = new List<Action<SocketSubscription, JToken>>();
|
||||
|
||||
Socket.OnClose += () =>
|
||||
{
|
||||
@@ -25,6 +34,10 @@ namespace CryptoExchange.Net.Sockets
|
||||
return;
|
||||
|
||||
lostTriggered = true;
|
||||
|
||||
foreach (var events in Events)
|
||||
events.Reset();
|
||||
|
||||
if (Socket.ShouldReconnect)
|
||||
ConnectionLost?.Invoke();
|
||||
};
|
||||
@@ -32,8 +45,30 @@ namespace CryptoExchange.Net.Sockets
|
||||
{
|
||||
lostTriggered = false;
|
||||
if (Socket.DisconnectTime != null)
|
||||
ConnectionRestored?.Invoke();
|
||||
ConnectionRestored?.Invoke(DateTime.UtcNow - Socket.DisconnectTime.Value);
|
||||
};
|
||||
}
|
||||
|
||||
public void AddEvent(string name)
|
||||
{
|
||||
Events.Add(new SocketEvent(name));
|
||||
}
|
||||
|
||||
public void SetEvent(string name, bool success, Error error)
|
||||
{
|
||||
Events.SingleOrDefault(e => e.Name == name)?.Set(success, error);
|
||||
}
|
||||
|
||||
public CallResult<bool> WaitForEvent(string name)
|
||||
{
|
||||
return Events.Single(e => e.Name == name).Wait();
|
||||
}
|
||||
|
||||
public async Task Close()
|
||||
{
|
||||
Socket.ShouldReconnect = false;
|
||||
await Socket.Close();
|
||||
Socket.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CryptoExchange.Net.Sockets
|
||||
{
|
||||
public class UpdateSubscription
|
||||
{
|
||||
private SocketSubscription subscription;
|
||||
|
||||
/// <summary>
|
||||
/// Event when the connection is lost
|
||||
/// </summary>
|
||||
public event Action ConnectionLost
|
||||
{
|
||||
add => subscription.ConnectionLost += value;
|
||||
remove => subscription.ConnectionLost -= value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event when the connection is restored. Timespan parameter indicates the time the socket has been offline for before reconnecting
|
||||
/// </summary>
|
||||
public event Action<TimeSpan> ConnectionRestored
|
||||
{
|
||||
add => subscription.ConnectionRestored += value;
|
||||
remove => subscription.ConnectionRestored -= value;
|
||||
}
|
||||
|
||||
public UpdateSubscription(SocketSubscription sub)
|
||||
{
|
||||
subscription = sub;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Close the subscription
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task Close()
|
||||
{
|
||||
await subscription.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user