1
0
mirror of https://github.com/JKorf/CryptoExchange.Net.git synced 2026-08-19 20:33:03 +00:00
This commit is contained in:
Jan Korf
2019-10-11 14:48:30 +02:00
parent 715fe378d5
commit 8ec902951d
33 changed files with 725 additions and 655 deletions
+11 -23
View File
@@ -21,7 +21,7 @@ namespace CryptoExchange.Net.Sockets
internal static int lastStreamId;
private static readonly object streamIdLock = new object();
protected WebSocket socket;
protected WebSocket? socket;
protected Log log;
protected object socketLock = new object();
@@ -32,34 +32,22 @@ namespace CryptoExchange.Net.Sockets
protected IDictionary<string, string> cookies;
protected IDictionary<string, string> headers;
protected HttpConnectProxy proxy;
protected HttpConnectProxy? proxy;
public int Id { get; }
public bool Reconnecting { get; set; }
public string Origin { get; set; }
public string? Origin { get; set; }
public string Url { get; }
public bool IsClosed => socket.State == WebSocketState.Closed;
public bool IsOpen => socket.State == WebSocketState.Open;
public bool IsClosed => socket?.State == null ? true: socket.State == WebSocketState.Closed;
public bool IsOpen => socket?.State == WebSocketState.Open;
public SslProtocols SSLProtocols { get; set; } = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls;
public Func<byte[], string> DataInterpreterBytes { get; set; }
public Func<string, string> DataInterpreterString { get; set; }
public Func<byte[], string>? DataInterpreterBytes { get; set; }
public Func<string, string>? DataInterpreterString { get; set; }
public DateTime LastActionTime { get; private set; }
public TimeSpan Timeout { get; set; }
private Task timeoutTask;
public bool PingConnection
{
get => socket.EnableAutoSendPing;
set => socket.EnableAutoSendPing = value;
}
public TimeSpan PingInterval
{
get => TimeSpan.FromSeconds(socket.AutoSendPingInterval);
set => socket.AutoSendPingInterval = (int) Math.Round(value.TotalSeconds);
}
private Task? timeoutTask;
public WebSocketState SocketState => socket?.State ?? WebSocketState.None;
@@ -176,7 +164,7 @@ namespace CryptoExchange.Net.Sockets
var waitLock = new object();
log?.Write(LogVerbosity.Debug, $"Socket {Id} closing");
var evnt = new ManualResetEvent(false);
ManualResetEvent? evnt = new ManualResetEvent(false);
var handler = new EventHandler((o, a) =>
{
lock(waitLock)
@@ -208,7 +196,7 @@ namespace CryptoExchange.Net.Sockets
public virtual void Send(string data)
{
socket.Send(data);
socket?.Send(data);
}
public virtual Task<bool> Connect()
@@ -239,7 +227,7 @@ namespace CryptoExchange.Net.Sockets
{
log?.Write(LogVerbosity.Debug, $"Socket {Id} connecting");
var waitLock = new object();
var evnt = new ManualResetEvent(false);
ManualResetEvent? evnt = new ManualResetEvent(false);
var handler = new EventHandler((o, a) =>
{
lock (waitLock)
+11 -11
View File
@@ -19,15 +19,15 @@ namespace CryptoExchange.Net.Sockets
/// <summary>
/// Connection lost event
/// </summary>
public event Action ConnectionLost;
public event Action? ConnectionLost;
/// <summary>
/// Connecting restored event
/// </summary>
public event Action<TimeSpan> ConnectionRestored;
public event Action<TimeSpan>? ConnectionRestored;
/// <summary>
/// Connecting closed event
/// </summary>
public event Action Closed;
public event Action? Closed;
/// <summary>
/// The amount of handlers
@@ -119,7 +119,7 @@ namespace CryptoExchange.Net.Sockets
/// <returns></returns>
public SocketSubscription AddHandler(object request, bool userSubscription, Action<SocketConnection, JToken> dataHandler)
{
var handler = new SocketSubscription(null, request, userSubscription, dataHandler);
var handler = new SocketSubscription(request, userSubscription, dataHandler);
lock (handlersLock)
handlers.Add(handler);
return handler;
@@ -135,7 +135,7 @@ namespace CryptoExchange.Net.Sockets
/// <returns></returns>
public SocketSubscription AddHandler(string identifier, bool userSubscription, Action<SocketConnection, JToken> dataHandler)
{
var handler = new SocketSubscription(identifier, null, userSubscription, dataHandler);
var handler = new SocketSubscription(identifier, userSubscription, dataHandler);
lock (handlersLock)
handlers.Add(handler);
return handler;
@@ -169,7 +169,7 @@ namespace CryptoExchange.Net.Sockets
private bool HandleData(JToken tokenData)
{
SocketSubscription currentSubscription = null;
SocketSubscription? currentSubscription = null;
try
{
var handled = false;
@@ -181,7 +181,7 @@ namespace CryptoExchange.Net.Sockets
currentSubscription = handler;
if (handler.Request == null)
{
if (socketClient.MessageMatchesHandler(tokenData, handler.Identifier))
if (socketClient.MessageMatchesHandler(tokenData, handler.Identifier!))
{
handled = true;
handler.MessageHandler(this, tokenData);
@@ -326,7 +326,7 @@ namespace CryptoExchange.Net.Sockets
if (Authenticated)
{
var authResult = await socketClient.AuthenticateSocket(this).ConfigureAwait(false);
if (!authResult.Success)
if (!authResult)
{
log.Write(LogVerbosity.Info, "Authentication failed on reconnected socket. Disconnecting and reconnecting.");
return false;
@@ -343,9 +343,9 @@ namespace CryptoExchange.Net.Sockets
var taskList = new List<Task>();
foreach (var handler in handlerList)
{
var task = socketClient.SubscribeAndWait(this, handler.Request, handler).ContinueWith(t =>
var task = socketClient.SubscribeAndWait(this, handler.Request!, handler).ContinueWith(t =>
{
if (!t.Result.Success)
if (!t.Result)
success = false;
});
taskList.Add(task);
@@ -403,7 +403,7 @@ namespace CryptoExchange.Net.Sockets
internal class PendingRequest
{
public Func<JToken, bool> Handler { get; }
public JToken Result { get; private set; }
public JToken? Result { get; private set; }
public ManualResetEvent Event { get; }
public TimeSpan Timeout { get; }
@@ -11,7 +11,7 @@ namespace CryptoExchange.Net.Sockets
/// <summary>
/// Exception event
/// </summary>
public event Action<Exception> Exception;
public event Action<Exception>? Exception;
/// <summary>
/// Message handlers for this subscription. Should return true if the message is handled and should not be distributed to the other handlers
@@ -21,11 +21,11 @@ namespace CryptoExchange.Net.Sockets
/// <summary>
/// Request object
/// </summary>
public object Request { get; set; }
public object? Request { get; set; }
/// <summary>
/// Subscription identifier
/// </summary>
public string Identifier { get; set; }
public string? Identifier { get; set; }
/// <summary>
/// Is user subscription or generic
/// </summary>
@@ -36,22 +36,32 @@ namespace CryptoExchange.Net.Sockets
/// </summary>
public bool Confirmed { get; set; }
/// <summary>
/// ctor
/// </summary>
/// <param name="request"></param>
/// <param name="userSubscription"></param>
/// <param name="dataHandler"></param>
public SocketSubscription(object request, bool userSubscription, Action<SocketConnection, JToken> dataHandler)
{
UserSubscription = userSubscription;
MessageHandler = dataHandler;
Request = request;
}
/// <summary>
/// ctor
/// </summary>
/// <param name="identifier"></param>
/// <param name="request"></param>
/// <param name="userSubscription"></param>
/// <param name="dataHandler"></param>
public SocketSubscription(string identifier, object request, bool userSubscription, Action<SocketConnection, JToken> dataHandler)
public SocketSubscription(string identifier, bool userSubscription, Action<SocketConnection, JToken> dataHandler)
{
UserSubscription = userSubscription;
MessageHandler = dataHandler;
Identifier = identifier;
Request = request;
}
/// <summary>
/// Invoke the exception event
/// </summary>