1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-11 09:56:18 +00:00
This commit is contained in:
JKorf 2024-01-20 19:16:39 +01:00
parent b057974cd0
commit e3207033c3
24 changed files with 914 additions and 803 deletions

View File

@ -10,6 +10,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net.WebSockets;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -103,15 +104,12 @@ namespace CryptoExchange.Net
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public new SocketExchangeOptions ClientOptions => (SocketExchangeOptions)base.ClientOptions; public new SocketExchangeOptions ClientOptions => (SocketExchangeOptions)base.ClientOptions;
/// <inheritdoc /> /// <inheritdoc />
public new SocketApiOptions ApiOptions => (SocketApiOptions)base.ApiOptions; public new SocketApiOptions ApiOptions => (SocketApiOptions)base.ApiOptions;
/// <inheritdoc />
public abstract MessageInterpreterPipeline Pipeline { get; }
#endregion #endregion
/// <summary> /// <summary>
@ -135,19 +133,9 @@ namespace CryptoExchange.Net
RateLimiters = rateLimiters; RateLimiters = rateLimiters;
} }
/// <summary>
/// Set a delegate which can manipulate the message stream before it is processed by listeners
/// </summary>
/// <param name="interceptor">Interceptor</param>
protected void SetInterceptor(Func<Stream, Stream> interceptor)
{
this.interceptor = interceptor;
}
/// <summary> /// <summary>
/// Connect to an url and listen for data on the BaseAddress /// Connect to an url and listen for data on the BaseAddress
/// </summary> /// </summary>
/// <typeparam name="T">The type of the expected data</typeparam>
/// <param name="subscription">The subscription</param> /// <param name="subscription">The subscription</param>
/// <param name="ct">Cancellation token for closing this subscription</param> /// <param name="ct">Cancellation token for closing this subscription</param>
/// <returns></returns> /// <returns></returns>
@ -159,7 +147,6 @@ namespace CryptoExchange.Net
/// <summary> /// <summary>
/// Connect to an url and listen for data /// Connect to an url and listen for data
/// </summary> /// </summary>
/// <typeparam name="T">The type of the expected data</typeparam>
/// <param name="url">The URL to connect to</param> /// <param name="url">The URL to connect to</param>
/// <param name="subscription">The subscription</param> /// <param name="subscription">The subscription</param>
/// <param name="ct">Cancellation token for closing this subscription</param> /// <param name="ct">Cancellation token for closing this subscription</param>
@ -247,7 +234,7 @@ namespace CryptoExchange.Net
return new CallResult<UpdateSubscription>(subResult.Error!); return new CallResult<UpdateSubscription>(subResult.Error!);
} }
subscription.HandleSubQueryResponse(subQuery.Response); subscription.HandleSubQueryResponse(subQuery.Response!);
} }
subscription.Confirmed = true; subscription.Confirmed = true;
@ -382,7 +369,7 @@ namespace CryptoExchange.Net
/// Should return the request which can be used to authenticate a socket connection /// Should return the request which can be used to authenticate a socket connection
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
protected internal virtual BaseQuery GetAuthenticationRequest() => throw new NotImplementedException(); protected internal virtual Query GetAuthenticationRequest() => throw new NotImplementedException();
/// <summary> /// <summary>
/// Adds a system subscription. Used for example to reply to ping requests /// Adds a system subscription. Used for example to reply to ping requests
@ -474,7 +461,7 @@ namespace CryptoExchange.Net
/// Process an unhandled message /// Process an unhandled message
/// </summary> /// </summary>
/// <param name="message">The message that wasn't processed</param> /// <param name="message">The message that wasn't processed</param>
protected virtual void HandleUnhandledMessage(BaseParsedMessage message) protected virtual void HandleUnhandledMessage(SocketMessage message)
{ {
} }
@ -538,7 +525,7 @@ namespace CryptoExchange.Net
/// <param name="interval">How often</param> /// <param name="interval">How often</param>
/// <param name="queryDelegate">Method returning the query to send</param> /// <param name="queryDelegate">Method returning the query to send</param>
/// <param name="callback">The callback for processing the response</param> /// <param name="callback">The callback for processing the response</param>
protected virtual void QueryPeriodic(string identifier, TimeSpan interval, Func<SocketConnection, BaseQuery> queryDelegate, Action<CallResult>? callback) protected virtual void QueryPeriodic(string identifier, TimeSpan interval, Func<SocketConnection, Query> queryDelegate, Action<CallResult>? callback)
{ {
if (queryDelegate == null) if (queryDelegate == null)
throw new ArgumentNullException(nameof(queryDelegate)); throw new ArgumentNullException(nameof(queryDelegate));
@ -686,7 +673,7 @@ namespace CryptoExchange.Net
sb.AppendLine($" Id: {subscription.Id}"); sb.AppendLine($" Id: {subscription.Id}");
sb.AppendLine($" Confirmed: {subscription.Confirmed}"); sb.AppendLine($" Confirmed: {subscription.Confirmed}");
sb.AppendLine($" Invocations: {subscription.TotalInvocations}"); sb.AppendLine($" Invocations: {subscription.TotalInvocations}");
sb.AppendLine($" Identifiers: [{string.Join(", ", subscription.StreamIdentifiers)}]"); sb.AppendLine($" Identifiers: [{string.Join(", ", subscription.ListenerIdentifiers)}]");
} }
} }
return sb.ToString(); return sb.ToString();
@ -708,5 +695,20 @@ namespace CryptoExchange.Net
semaphoreSlim?.Dispose(); semaphoreSlim?.Dispose();
base.Dispose(); base.Dispose();
} }
/// <summary>
/// Get the listener identifier for the message
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public abstract string GetListenerIdentifier(SocketMessage message);
/// <summary>
/// Preprocess a stream message
/// </summary>
/// <param name="type"></param>
/// <param name="stream"></param>
/// <returns></returns>
public virtual Stream PreprocessStreamMessage(WebSocketMessageType type, Stream stream) => stream;
} }
} }

View File

@ -1,158 +1,158 @@
using CryptoExchange.Net.Interfaces; //using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.Objects.Sockets; //using CryptoExchange.Net.Objects.Sockets;
using Newtonsoft.Json; //using Newtonsoft.Json;
using Newtonsoft.Json.Linq; //using Newtonsoft.Json.Linq;
using System; //using System;
using System.Collections.Generic; //using System.Collections.Generic;
using System.IO; //using System.IO;
using System.Linq; //using System.Linq;
using System.Runtime.InteropServices.ComTypes; //using System.Runtime.InteropServices.ComTypes;
using System.Text; //using System.Text;
namespace CryptoExchange.Net.Converters //namespace CryptoExchange.Net.Converters
{ //{
internal class JTokenAccessor : IMessageAccessor // internal class JTokenAccessor : IMessageAccessor
{ // {
private readonly JToken _token; // private readonly JToken _token;
private readonly Stream _stream; // private readonly Stream _stream;
private readonly StreamReader _reader; // private readonly StreamReader _reader;
private Dictionary<string, JToken?> _cache = new Dictionary<string, JToken?>(); // private Dictionary<string, JToken?> _cache = new Dictionary<string, JToken?>();
private static JsonSerializer _serializer = JsonSerializer.Create(SerializerOptions.WithConverters); // private static JsonSerializer _serializer = JsonSerializer.Create(SerializerOptions.WithConverters);
public JTokenAccessor(Stream stream) // public JTokenAccessor(Stream stream)
{ // {
_stream = stream; // _stream = stream;
_reader = new StreamReader(stream, Encoding.UTF8, false, (int)stream.Length, true); // _reader = new StreamReader(stream, Encoding.UTF8, false, (int)stream.Length, true);
using var jsonTextReader = new JsonTextReader(_reader); // using var jsonTextReader = new JsonTextReader(_reader);
JToken token; // JToken token;
try // try
{ // {
_token = JToken.Load(jsonTextReader); // _token = JToken.Load(jsonTextReader);
} // }
catch (Exception ex) // catch (Exception ex)
{ // {
// Not a json message // // Not a json message
throw; // throw;
} // }
} // }
public BaseParsedMessage Instantiate(Type type) // public BaseParsedMessage Instantiate(Type type)
{ // {
var resultMessageType = typeof(ParsedMessage<>).MakeGenericType(type); // var resultMessageType = typeof(ParsedMessage<>).MakeGenericType(type);
var instance = (BaseParsedMessage)Activator.CreateInstance(resultMessageType, type == null ? null : _token.ToObject(type, _serializer)); // var instance = (BaseParsedMessage)Activator.CreateInstance(resultMessageType, type == null ? null : _token.ToObject(type, _serializer));
return instance; // return instance;
} // }
public string GetOriginalString() // public string GetOriginalString()
{ // {
_stream.Position = 0; // _stream.Position = 0;
return _reader.ReadToEnd(); // return _reader.ReadToEnd();
} // }
public int? GetArrayIntValue(string? key, int index) // public int? GetArrayIntValue(string? key, int index)
{ // {
var accessToken = key == null ? _token : GetToken(key); // var accessToken = key == null ? _token : GetToken(key);
if (accessToken == null || accessToken is not JArray arr) // if (accessToken == null || accessToken is not JArray arr)
return null; // return null;
return arr[index].Value<int>(); // return arr[index].Value<int>();
} // }
public string? GetArrayStringValue(string? key, int index) // public string? GetArrayStringValue(string? key, int index)
{ // {
var accessToken = key == null ? _token : GetToken(key); // var accessToken = key == null ? _token : GetToken(key);
if (accessToken == null || accessToken is not JArray arr) // if (accessToken == null || accessToken is not JArray arr)
return null; // return null;
if (arr.Count <= index) // if (arr.Count <= index)
return null; // return null;
if (arr[index].Type != JTokenType.String) // if (arr[index].Type != JTokenType.String)
return null; // return null;
return arr[index].Value<string>(); // return arr[index].Value<string>();
} // }
public int? GetCount(string key) // public int? GetCount(string key)
{ // {
var accessToken = GetToken(key); // var accessToken = GetToken(key);
return accessToken.Count(); // return accessToken.Count();
} // }
public int? GetIntValue(string key) // public int? GetIntValue(string key)
{ // {
var accessToken = GetToken(key); // var accessToken = GetToken(key);
return accessToken?.Value<int>(); // return accessToken?.Value<int>();
} // }
public string? GetStringValue(string key) // public string? GetStringValue(string key)
{ // {
var accessToken = GetToken(key); // var accessToken = GetToken(key);
if (accessToken?.Type == JTokenType.Object) // if (accessToken?.Type == JTokenType.Object)
return ((JObject)accessToken).Properties().First().Name; // return ((JObject)accessToken).Properties().First().Name;
return accessToken?.ToString(); // return accessToken?.ToString();
} // }
public bool IsObject(string? key) => _token.Type == JTokenType.Object; // public bool IsObject(string? key) => _token.Type == JTokenType.Object;
public bool IsArray(IEnumerable<int> indexes) // public bool IsArray(IEnumerable<int> indexes)
{ // {
var item = _token; // var item = _token;
foreach(var index in indexes) // foreach(var index in indexes)
{ // {
if (item.Type != JTokenType.Array) // if (item.Type != JTokenType.Array)
return false; // return false;
var arr = ((JArray)item); // var arr = ((JArray)item);
if (arr.Count <= index) // if (arr.Count <= index)
return false; // return false;
item = arr[index]; // item = arr[index];
} // }
return item.Type == JTokenType.Array; // return item.Type == JTokenType.Array;
} // }
public bool IsEmptyArray(IEnumerable<int> indexes) // public bool IsEmptyArray(IEnumerable<int> indexes)
{ // {
var item = _token; // var item = _token;
foreach (var index in indexes) // foreach (var index in indexes)
{ // {
if (item.Type != JTokenType.Array) // if (item.Type != JTokenType.Array)
return false; // return false;
var arr = ((JArray)item); // var arr = ((JArray)item);
if (arr.Count <= index) // if (arr.Count <= index)
return false; // return false;
item = arr[index]; // item = arr[index];
} // }
return item.Type == JTokenType.Array && !item.HasValues; // return item.Type == JTokenType.Array && !item.HasValues;
} // }
private JToken? GetToken(string key) // private JToken? GetToken(string key)
{ // {
if (key == null) // if (key == null)
return _token; // return _token;
if (_cache.TryGetValue(key, out var token)) // if (_cache.TryGetValue(key, out var token))
return token; // return token;
var splitTokens = key.Split(new char[] { ':' }); // var splitTokens = key.Split(new char[] { ':' });
var accessToken = _token; // var accessToken = _token;
foreach (var splitToken in splitTokens) // foreach (var splitToken in splitTokens)
{ // {
if (accessToken.Type == JTokenType.Array) // if (accessToken.Type == JTokenType.Array)
return null; // return null;
accessToken = accessToken[splitToken]; // accessToken = accessToken[splitToken];
if (accessToken == null) // if (accessToken == null)
break; // break;
} // }
_cache.Add(key, accessToken); // _cache.Add(key, accessToken);
return accessToken; // return accessToken;
} // }
} // }
} //}

View File

@ -1,21 +0,0 @@
using CryptoExchange.Net.Objects.Sockets;
using System;
using System.Collections.Generic;
using System.Text;
namespace CryptoExchange.Net.Interfaces
{
public interface IMessageAccessor
{
bool IsObject(string? key);
bool IsArray(IEnumerable<int> indexes);
bool IsEmptyArray(IEnumerable<int> indexes);
string? GetStringValue(string key);
int? GetIntValue(string key);
public int? GetCount(string key);
public int? GetArrayIntValue(string? key, int index);
public string? GetArrayStringValue(string? key, int index);
public BaseParsedMessage Instantiate(Type type);
}
}

View File

@ -8,11 +8,31 @@ using System.Threading.Tasks;
namespace CryptoExchange.Net.Interfaces namespace CryptoExchange.Net.Interfaces
{ {
/// <summary>
/// Message processor
/// </summary>
public interface IMessageProcessor public interface IMessageProcessor
{ {
/// <summary>
/// Id of the processor
/// </summary>
public int Id { get; } public int Id { get; }
public List<string> StreamIdentifiers { get; } /// <summary>
Task<CallResult> HandleMessageAsync(SocketConnection connection, DataEvent<BaseParsedMessage> message); /// The identifiers for this processor
Dictionary<string, Type> TypeMapping { get; } /// </summary>
public HashSet<string> ListenerIdentifiers { get; }
/// <summary>
/// Handle a message
/// </summary>
/// <param name="connection"></param>
/// <param name="message"></param>
/// <returns></returns>
Task<CallResult> HandleAsync(SocketConnection connection, DataEvent<object> message);
/// <summary>
/// Get the type the message should be deserialized to
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
Type? GetMessageType(SocketMessage message);
} }
} }

View File

@ -1,13 +0,0 @@
//using CryptoExchange.Net.Converters;
//using CryptoExchange.Net.Objects.Sockets;
//using System.Threading.Tasks;
//namespace CryptoExchange.Net.Interfaces
//{
// internal interface IStreamMessageListener
// {
// int Priority { get; }
// bool MessageMatches(ParsedMessage message);
// Task ProcessAsync(ParsedMessage message);
// }
//}

View File

@ -1,81 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Collections.Specialized.BitVector32;
namespace CryptoExchange.Net.Objects
{
public class ConcurrentList<T> : IEnumerable<T>
{
private readonly object _lock = new object();
private readonly List<T> _collection = new List<T>();
public void Add(T item)
{
lock (_lock)
_collection.Add(item);
}
public void Remove(T item)
{
lock (_lock)
_collection.Remove(item);
}
public T? SingleOrDefault(Func<T, bool> action)
{
lock (_lock)
return _collection.SingleOrDefault(action);
}
public bool All(Func<T, bool> action)
{
lock (_lock)
return _collection.All(action);
}
public bool Any(Func<T, bool> action)
{
lock (_lock)
return _collection.Any(action);
}
public int Count(Func<T, bool> action)
{
lock (_lock)
return _collection.Count(action);
}
public bool Contains(T item)
{
lock (_lock)
return _collection.Contains(item);
}
public T[] ToArray(Func<T, bool> predicate)
{
lock (_lock)
return _collection.Where(predicate).ToArray();
}
public List<T> ToList()
{
lock (_lock)
return _collection.ToList();
}
public IEnumerator<T> GetEnumerator()
{
lock (_lock)
{
foreach (var item in _collection)
yield return item;
}
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}

View File

@ -1,21 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace CryptoExchange.Net.Objects.Sockets
{
public interface IMatchingStrategy
{
}
internal class IdMatchingStrategy : IMatchingStrategy
{
}
internal class FieldsMatchingStrategy : IMatchingStrategy
{
}
}

View File

@ -1,54 +1,54 @@
namespace CryptoExchange.Net.Objects.Sockets //namespace CryptoExchange.Net.Objects.Sockets
{ //{
/// <summary> // /// <summary>
/// Parsed message object // /// Parsed message object
/// </summary> // /// </summary>
public abstract class BaseParsedMessage // public abstract class BaseParsedMessage
{ // {
/// <summary> // /// <summary>
/// Stream identifier string // /// Stream identifier string
/// </summary> // /// </summary>
public string StreamIdentifier { get; set; } = null!; // public string StreamIdentifier { get; set; } = null!;
/// <summary> // /// <summary>
/// Type identifier string // /// Type identifier string
/// </summary> // /// </summary>
public string TypeIdentifier { get; set; } = null!; // public string TypeIdentifier { get; set; } = null!;
/// <summary> // /// <summary>
/// Original data if the option is enabled // /// Original data if the option is enabled
/// </summary> // /// </summary>
public string? OriginalData { get; set; } // public string? OriginalData { get; set; }
/// <summary> // /// <summary>
/// If parsed // /// If parsed
/// </summary> // /// </summary>
public bool Parsed { get; set; } // public bool Parsed { get; set; }
/// <summary> // /// <summary>
/// Get the data object // /// Get the data object
/// </summary> // /// </summary>
/// <returns></returns> // /// <returns></returns>
public abstract object Data { get; } // public abstract object Data { get; }
} // }
/// <summary> // /// <summary>
/// Parsed message object // /// Parsed message object
/// </summary> // /// </summary>
/// <typeparam name="T">Data type</typeparam> // /// <typeparam name="T">Data type</typeparam>
public class ParsedMessage<T> : BaseParsedMessage // public class ParsedMessage<T> : BaseParsedMessage
{ // {
/// <summary> // /// <summary>
/// Parsed data object // /// Parsed data object
/// </summary> // /// </summary>
public override object? Data { get; } // public override object? Data { get; }
public T? TypedData => (T)Data; // public T? TypedData => (T)Data;
/// <summary> // /// <summary>
/// ctor // /// ctor
/// </summary> // /// </summary>
/// <param name="data"></param> // /// <param name="data"></param>
public ParsedMessage(T? data) // public ParsedMessage(T? data)
{ // {
Data = data; // Data = data;
} // }
} // }
} //}

View File

@ -1,19 +1,19 @@
using CryptoExchange.Net.Converters; //using CryptoExchange.Net.Converters;
using CryptoExchange.Net.Interfaces; //using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.Sockets; //using CryptoExchange.Net.Sockets;
using Newtonsoft.Json.Linq; //using Newtonsoft.Json.Linq;
using System; //using System;
using System.Collections.Generic; //using System.Collections.Generic;
using System.IO; //using System.IO;
using System.Net.WebSockets; //using System.Net.WebSockets;
using System.Text; //using System.Text;
namespace CryptoExchange.Net.Objects.Sockets //namespace CryptoExchange.Net.Objects.Sockets
{ //{
public class MessageInterpreterPipeline // public class MessageInterpreterPipeline
{ // {
public Func<WebSocketMessageType, Stream, Stream>? PreProcessCallback { get; set; } // public Func<WebSocketMessageType, Stream, Stream>? PreProcessCallback { get; set; }
public Func<IMessageAccessor, string?> GetStreamIdentifier { get; set; } // public Func<IMessageAccessor, string?> GetStreamIdentifier { get; set; }
public Func<IMessageAccessor, string, string?> GetTypeIdentifier { get; set; } = (accessor, streamId) => streamId; // public Func<IMessageAccessor, string, string?> GetTypeIdentifier { get; set; } = (accessor, streamId) => streamId;
} // }
} //}

View File

@ -1,39 +0,0 @@
using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.Objects.Sockets;
using CryptoExchange.Net.Sockets;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace CryptoExchange.Net.Objects.Testing
{
public class TestWebsocket : CryptoExchangeWebSocketClient
{
public TestWebsocket(ILogger logger, WebSocketParameters websocketParameters) : base(logger, websocketParameters)
{
}
public override bool IsClosed => false;
public override bool IsOpen => true;
public override Task<bool> ConnectAsync() => Task.FromResult(true);
public override Task CloseAsync() => Task.CompletedTask;
public override Task ReconnectAsync() => Task.CompletedTask;
public override void Send(int id, string data, int weight) { }
public void Receive(string data)
{
var bytes = Encoding.UTF8.GetBytes(data);
var stream = new MemoryStream(bytes);
stream.Position = 0;
_ = ProcessData(System.Net.WebSockets.WebSocketMessageType.Text, stream);
}
}
}

View File

@ -1,24 +0,0 @@
using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.Objects.Sockets;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;
namespace CryptoExchange.Net.Objects.Testing
{
public class TestWebsocketFactory : IWebsocketFactory
{
private readonly Func<ILogger, WebSocketParameters, IWebsocket> _websocketFactory;
public TestWebsocketFactory(Func<ILogger, WebSocketParameters, IWebsocket> websocketFactory)
{
_websocketFactory = websocketFactory;
}
public IWebsocket CreateWebsocket(ILogger logger, WebSocketParameters parameters)
{
return _websocketFactory(logger, parameters);
}
}
}

View File

@ -278,7 +278,7 @@ namespace CryptoExchange.Net.Sockets
return; return;
var bytes = Parameters.Encoding.GetBytes(data); var bytes = Parameters.Encoding.GetBytes(data);
_logger.Log(LogLevel.Trace, $"Socket {Id} - msg {id} - Adding {bytes.Length} bytes to send buffer"); _logger.Log(LogLevel.Trace, $"Socket {Id} msg {id} - Adding {bytes.Length} bytes to send buffer");
_sendBuffer.Enqueue(new SendItem { Id = id, Weight = weight, Bytes = bytes }); _sendBuffer.Enqueue(new SendItem { Id = id, Weight = weight, Bytes = bytes });
_sendEvent.Set(); _sendEvent.Set();
} }
@ -415,7 +415,7 @@ namespace CryptoExchange.Net.Sockets
if (limitResult.Success) if (limitResult.Success)
{ {
if (limitResult.Data > 0) if (limitResult.Data > 0)
_logger.Log(LogLevel.Debug, $"Socket {Id} - msg {data.Id} - send delayed {limitResult.Data}ms because of rate limit"); _logger.Log(LogLevel.Debug, $"Socket {Id} msg {data.Id} - send delayed {limitResult.Data}ms because of rate limit");
} }
} }
} }
@ -424,7 +424,7 @@ namespace CryptoExchange.Net.Sockets
{ {
await _socket.SendAsync(new ArraySegment<byte>(data.Bytes, 0, data.Bytes.Length), WebSocketMessageType.Text, true, _ctsSource.Token).ConfigureAwait(false); await _socket.SendAsync(new ArraySegment<byte>(data.Bytes, 0, data.Bytes.Length), WebSocketMessageType.Text, true, _ctsSource.Token).ConfigureAwait(false);
await (OnRequestSent?.Invoke(data.Id) ?? Task.CompletedTask).ConfigureAwait(false); await (OnRequestSent?.Invoke(data.Id) ?? Task.CompletedTask).ConfigureAwait(false);
_logger.Log(LogLevel.Trace, $"Socket {Id} - msg {data.Id} - sent {data.Bytes.Length} bytes"); _logger.Log(LogLevel.Trace, $"Socket {Id} msg {data.Id} - sent {data.Bytes.Length} bytes");
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
@ -447,13 +447,13 @@ namespace CryptoExchange.Net.Sockets
// Because this is running in a separate task and not awaited until the socket gets closed // Because this is running in a separate task and not awaited until the socket gets closed
// any exception here will crash the send processing, but do so silently unless the socket get's stopped. // any exception here will crash the send processing, but do so silently unless the socket get's stopped.
// Make sure we at least let the owner know there was an error // Make sure we at least let the owner know there was an error
_logger.Log(LogLevel.Warning, $"Socket {Id} Send loop stopped with exception"); _logger.Log(LogLevel.Warning, $"Socket {Id} send loop stopped with exception");
await (OnError?.Invoke(e) ?? Task.CompletedTask).ConfigureAwait(false); await (OnError?.Invoke(e) ?? Task.CompletedTask).ConfigureAwait(false);
throw; throw;
} }
finally finally
{ {
_logger.Log(LogLevel.Debug, $"Socket {Id} Send loop finished"); _logger.Log(LogLevel.Debug, $"Socket {Id} send loop finished");
} }
} }
@ -570,22 +570,30 @@ namespace CryptoExchange.Net.Sockets
// Because this is running in a separate task and not awaited until the socket gets closed // Because this is running in a separate task and not awaited until the socket gets closed
// any exception here will crash the receive processing, but do so silently unless the socket gets stopped. // any exception here will crash the receive processing, but do so silently unless the socket gets stopped.
// Make sure we at least let the owner know there was an error // Make sure we at least let the owner know there was an error
_logger.Log(LogLevel.Warning, $"Socket {Id} Receive loop stopped with exception"); _logger.Log(LogLevel.Warning, $"Socket {Id} receive loop stopped with exception");
await (OnError?.Invoke(e) ?? Task.CompletedTask).ConfigureAwait(false); await (OnError?.Invoke(e) ?? Task.CompletedTask).ConfigureAwait(false);
throw; throw;
} }
finally finally
{ {
_logger.Log(LogLevel.Debug, $"Socket {Id} Receive loop finished"); _logger.Log(LogLevel.Debug, $"Socket {Id} receive loop finished");
} }
} }
/// <summary>
/// Proccess a stream message
/// </summary>
/// <param name="type"></param>
/// <param name="stream"></param>
/// <returns></returns>
protected async Task ProcessData(WebSocketMessageType type, Stream stream) protected async Task ProcessData(WebSocketMessageType type, Stream stream)
{ {
LastActionTime = DateTime.UtcNow; LastActionTime = DateTime.UtcNow;
stream.Position = 0; stream.Position = 0;
if (Parameters.Interceptor != null) if (Parameters.Interceptor != null)
stream = Parameters.Interceptor.Invoke(stream); stream = Parameters.Interceptor.Invoke(stream);
if (OnStreamMessage != null) if (OnStreamMessage != null)
await OnStreamMessage.Invoke(type, stream).ConfigureAwait(false); await OnStreamMessage.Invoke(type, stream).ConfigureAwait(false);
} }
@ -596,7 +604,7 @@ namespace CryptoExchange.Net.Sockets
/// <returns></returns> /// <returns></returns>
protected async Task CheckTimeoutAsync() protected async Task CheckTimeoutAsync()
{ {
_logger.Log(LogLevel.Debug, $"Socket {Id} Starting task checking for no data received for {Parameters.Timeout}"); _logger.Log(LogLevel.Debug, $"Socket {Id} starting task checking for no data received for {Parameters.Timeout}");
LastActionTime = DateTime.UtcNow; LastActionTime = DateTime.UtcNow;
try try
{ {
@ -607,7 +615,7 @@ namespace CryptoExchange.Net.Sockets
if (DateTime.UtcNow - LastActionTime > Parameters.Timeout) if (DateTime.UtcNow - LastActionTime > Parameters.Timeout)
{ {
_logger.Log(LogLevel.Warning, $"Socket {Id} No data received for {Parameters.Timeout}, reconnecting socket"); _logger.Log(LogLevel.Warning, $"Socket {Id} no data received for {Parameters.Timeout}, reconnecting socket");
_ = ReconnectAsync().ConfigureAwait(false); _ = ReconnectAsync().ConfigureAwait(false);
return; return;
} }

View File

@ -0,0 +1,39 @@
using System;
namespace CryptoExchange.Net.Sockets.MessageParsing.Interfaces
{
/// <summary>
/// Message accessor
/// </summary>
public interface IMessageAccessor
{
/// <summary>
/// Is this a json message
/// </summary>
bool IsJson { get; }
/// <summary>
/// Get the type of node
/// </summary>
/// <returns></returns>
NodeType? GetNodeType();
/// <summary>
/// Get the type of node
/// </summary>
/// <param name="path">Access path</param>
/// <returns></returns>
NodeType? GetNodeType(MessagePath path);
/// <summary>
/// Get the value of a path
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="path"></param>
/// <returns></returns>
T? GetValue<T>(MessagePath path);
/// <summary>
/// Deserialize the message into this type
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
object Deserialize(Type type);
}
}

View File

@ -0,0 +1,132 @@
using CryptoExchange.Net.Converters;
using CryptoExchange.Net.Sockets.MessageParsing.Interfaces;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace CryptoExchange.Net.Sockets.MessageParsing
{
/// <summary>
/// Json.Net message accessor
/// </summary>
public class JsonNetMessageData : IMessageAccessor
{
private readonly JToken? _token;
private readonly Stream _stream;
private static JsonSerializer _serializer = JsonSerializer.Create(SerializerOptions.WithConverters);
/// <inheritdoc />
public bool IsJson { get; private set; }
/// <summary>
/// ctor
/// </summary>
/// <param name="stream"></param>
public JsonNetMessageData(Stream stream)
{
_stream = stream;
using var reader = new StreamReader(stream, Encoding.UTF8, false, (int)stream.Length, true);
using var jsonTextReader = new JsonTextReader(reader);
try
{
_token = JToken.Load(jsonTextReader);
IsJson = true;
}
catch (Exception)
{
// Not a json message
IsJson = false;
}
}
/// <inheritdoc />
public object Deserialize(Type type)
{
if (!IsJson)
{
var sr = new StreamReader(_stream);
return sr.ReadToEnd();
}
return _token!.ToObject(type, _serializer)!;
}
/// <inheritdoc />
public NodeType? GetNodeType()
{
if (_token == null)
return null;
if (_token.Type == JTokenType.Object)
return NodeType.Object;
if (_token.Type == JTokenType.Array)
return NodeType.Array;
return NodeType.Value;
}
/// <inheritdoc />
public NodeType? GetNodeType(MessagePath path)
{
var node = GetPathNode(path);
if (node == null)
return null;
if (node.Type == JTokenType.Object)
return NodeType.Object;
if (node.Type == JTokenType.Array)
return NodeType.Array;
return NodeType.Value;
}
/// <inheritdoc />
public T? GetValue<T>(MessagePath path)
{
var value = GetPathNode(path);
if (value == null)
return default;
if (value.Type == JTokenType.Object || value.Type == JTokenType.Array)
return default;
return value!.Value<T>();
}
private JToken? GetPathNode(MessagePath path)
{
var currentToken = _token;
foreach (var node in path)
{
if (node.Type)
{
// Int value
var val = (int)node.Value;
if (currentToken!.Type != JTokenType.Array || ((JArray)currentToken).Count <= val)
return null;
currentToken = currentToken[val];
}
else
{
// String value
if (currentToken!.Type != JTokenType.Object)
return null;
currentToken = currentToken[(string)node.Value];
}
if (currentToken == null)
return null;
}
return currentToken;
}
}
}

View File

@ -0,0 +1,38 @@
namespace CryptoExchange.Net.Sockets.MessageParsing
{
/// <summary>
/// Node accessor
/// </summary>
public struct NodeAccessor
{
/// <summary>
/// Value
/// </summary>
public object Value { get; }
/// <summary>
/// Type (true = int, false = string)
/// </summary>
public bool Type { get; }
private NodeAccessor(object value, bool type)
{
Value = value;
Type = type;
}
/// <summary>
/// Create an int node accessor
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static NodeAccessor Int(int value) { return new NodeAccessor(value, true); }
/// <summary>
/// Create a string node accessor
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static NodeAccessor String(string value) { return new NodeAccessor(value, false); }
}
}

View File

@ -0,0 +1,50 @@
using System.Collections;
using System.Collections.Generic;
namespace CryptoExchange.Net.Sockets.MessageParsing
{
/// <summary>
/// Message access definition
/// </summary>
public struct MessagePath : IEnumerable<NodeAccessor>
{
private List<NodeAccessor> _path;
internal void Add(NodeAccessor node)
{
_path.Add(node);
}
/// <summary>
/// ctor
/// </summary>
public MessagePath()
{
_path = new List<NodeAccessor>();
}
/// <summary>
/// Create a new message path
/// </summary>
/// <returns></returns>
public static MessagePath Get()
{
return new MessagePath();
}
/// <summary>
/// IEnumerable implementation
/// </summary>
/// <returns></returns>
public IEnumerator<NodeAccessor> GetEnumerator()
{
for (var i = 0; i < _path.Count; i++)
yield return _path[i];
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}

View File

@ -0,0 +1,32 @@
namespace CryptoExchange.Net.Sockets.MessageParsing
{
/// <summary>
/// Message path extension methods
/// </summary>
public static class MessagePathExtension
{
/// <summary>
/// Add a string node accessor
/// </summary>
/// <param name="path"></param>
/// <param name="propName"></param>
/// <returns></returns>
public static MessagePath Property(this MessagePath path, string propName)
{
path.Add(NodeAccessor.String(propName));
return path;
}
/// <summary>
/// Add a int node accessor
/// </summary>
/// <param name="path"></param>
/// <param name="index"></param>
/// <returns></returns>
public static MessagePath Index(this MessagePath path, int index)
{
path.Add(NodeAccessor.Int(index));
return path;
}
}
}

View File

@ -0,0 +1,21 @@
namespace CryptoExchange.Net.Sockets.MessageParsing
{
/// <summary>
/// Message node type
/// </summary>
public enum NodeType
{
/// <summary>
/// Array node
/// </summary>
Array,
/// <summary>
/// Object node
/// </summary>
Object,
/// <summary>
/// Value node
/// </summary>
Value
}
}

View File

@ -11,26 +11,42 @@ namespace CryptoExchange.Net.Sockets
/// <summary> /// <summary>
/// Query /// Query
/// </summary> /// </summary>
public abstract class BaseQuery : IMessageProcessor public abstract class Query : IMessageProcessor
{ {
/// <summary> /// <summary>
/// Unique identifier /// Unique identifier
/// </summary> /// </summary>
public int Id { get; } = ExchangeHelpers.NextId(); public int Id { get; } = ExchangeHelpers.NextId();
/// <summary>
/// Has this query been completed
/// </summary>
public bool Completed { get; set; } public bool Completed { get; set; }
public DateTime RequestTimestamp { get; set; }
public CallResult? Result { get; set; }
public BaseParsedMessage Response { get; set; }
public Action OnFinished { get; set; }
protected AsyncResetEvent _event;
protected CancellationTokenSource? _cts;
/// <summary> /// <summary>
/// Strings to identify this subscription with /// Timestamp of when the request was send
/// </summary> /// </summary>
public abstract List<string> StreamIdentifiers { get; set; } public DateTime RequestTimestamp { get; set; }
/// <summary>
/// Result
/// </summary>
public CallResult? Result { get; set; }
/// <summary>
/// Response
/// </summary>
public object? Response { get; set; }
/// <summary>
/// Action to execute when query is finished
/// </summary>
public Action? OnFinished { get; set; }
/// <summary>
/// Strings to match this query to a received message
/// </summary>
public abstract HashSet<string> ListenerIdentifiers { get; set; }
/// <summary> /// <summary>
/// The query request object /// The query request object
@ -47,7 +63,22 @@ namespace CryptoExchange.Net.Sockets
/// </summary> /// </summary>
public int Weight { get; } public int Weight { get; }
public abstract Dictionary<string, Type> TypeMapping { get; set; } /// <summary>
/// Get the type the message should be deserialized to
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public abstract Type GetMessageType(SocketMessage message);
/// <summary>
/// Wait event for response
/// </summary>
protected AsyncResetEvent _event;
/// <summary>
/// Cancellation token
/// </summary>
protected CancellationTokenSource? _cts;
/// <summary> /// <summary>
/// ctor /// ctor
@ -55,7 +86,7 @@ namespace CryptoExchange.Net.Sockets
/// <param name="request"></param> /// <param name="request"></param>
/// <param name="authenticated"></param> /// <param name="authenticated"></param>
/// <param name="weight"></param> /// <param name="weight"></param>
public BaseQuery(object request, bool authenticated, int weight = 1) public Query(object request, bool authenticated, int weight = 1)
{ {
_event = new AsyncResetEvent(false, false); _event = new AsyncResetEvent(false, false);
@ -97,8 +128,9 @@ namespace CryptoExchange.Net.Sockets
/// Handle a response message /// Handle a response message
/// </summary> /// </summary>
/// <param name="message"></param> /// <param name="message"></param>
/// <param name="connection"></param>
/// <returns></returns> /// <returns></returns>
public abstract Task<CallResult> HandleMessageAsync(SocketConnection connection, DataEvent<BaseParsedMessage> message); public abstract Task<CallResult> HandleAsync(SocketConnection connection, DataEvent<object> message);
} }
@ -106,20 +138,10 @@ namespace CryptoExchange.Net.Sockets
/// Query /// Query
/// </summary> /// </summary>
/// <typeparam name="TResponse">Response object type</typeparam> /// <typeparam name="TResponse">Response object type</typeparam>
public abstract class Query<TResponse> : BaseQuery public abstract class Query<TResponse> : Query
{ {
private Dictionary<string, Type> _typeMapping = new Dictionary<string, Type> /// <inheritdoc />
{ public override Type GetMessageType(SocketMessage message) => typeof(TResponse);
{ "", typeof(TResponse) }
};
public override Dictionary<string, Type> TypeMapping
{
get => _typeMapping;
set
{
_typeMapping = value;
}
}
/// <summary> /// <summary>
/// The typed call result /// The typed call result
@ -137,13 +159,11 @@ namespace CryptoExchange.Net.Sockets
} }
/// <inheritdoc /> /// <inheritdoc />
public override async Task<CallResult> HandleMessageAsync(SocketConnection connection, DataEvent<BaseParsedMessage> message) public override async Task<CallResult> HandleAsync(SocketConnection connection, DataEvent<object> message)
{ {
Completed = true; Completed = true;
Response = message.Data; Response = message.Data;
Result = await HandleMessageAsync(connection, message.As((ParsedMessage<TResponse>)message.Data)).ConfigureAwait(false); Result = await HandleMessageAsync(connection, message.As((TResponse)message.Data)).ConfigureAwait(false);
// Set() gives calling/waiting request the signal to continue and allows the message processing thread to continue with next message.
// However, the processing of the message isn't fully finished yet?
OnFinished?.Invoke(); OnFinished?.Invoke();
_event.Set(); _event.Set();
return Result; return Result;
@ -152,9 +172,10 @@ namespace CryptoExchange.Net.Sockets
/// <summary> /// <summary>
/// Handle the query response /// Handle the query response
/// </summary> /// </summary>
/// <param name="connection"></param>
/// <param name="message"></param> /// <param name="message"></param>
/// <returns></returns> /// <returns></returns>
public virtual Task<CallResult<TResponse>> HandleMessageAsync(SocketConnection connection, DataEvent<ParsedMessage<TResponse>> message) => Task.FromResult(new CallResult<TResponse>(message.Data.TypedData!)); public virtual Task<CallResult<TResponse>> HandleMessageAsync(SocketConnection connection, DataEvent<TResponse> message) => Task.FromResult(new CallResult<TResponse>(message.Data));
/// <inheritdoc /> /// <inheritdoc />
public override void Timeout() public override void Timeout()

View File

@ -13,6 +13,7 @@ using System.Text;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using CryptoExchange.Net.Converters; using CryptoExchange.Net.Converters;
using System.Diagnostics; using System.Diagnostics;
using CryptoExchange.Net.Sockets.MessageParsing;
namespace CryptoExchange.Net.Sockets namespace CryptoExchange.Net.Sockets
{ {
@ -49,17 +50,24 @@ namespace CryptoExchange.Net.Sockets
/// <summary> /// <summary>
/// Unhandled message event /// Unhandled message event
/// </summary> /// </summary>
public event Action<BaseParsedMessage>? UnhandledMessage; public event Action<SocketMessage>? UnhandledMessage;
/// <summary> /// <summary>
/// Unparsed message event /// Unparsed message event
/// </summary> /// </summary>
public event Action<byte[]>? UnparsedMessage; public event Action<byte[]>? UnparsedMessage; // TODO not linked up
/// <summary> /// <summary>
/// The amount of subscriptions on this connection /// The amount of subscriptions on this connection
/// </summary> /// </summary>
public int UserSubscriptionCount => _listenerManager.GetSubscriptions().Count(h => h.UserSubscription); public int UserSubscriptionCount
{
get
{
lock(_listenersLock)
return _listeners.OfType<Subscription>().Count(h => h.UserSubscription);
}
}
/// <summary> /// <summary>
/// Get a copy of the current message subscriptions /// Get a copy of the current message subscriptions
@ -68,7 +76,8 @@ namespace CryptoExchange.Net.Sockets
{ {
get get
{ {
return _listenerManager.GetSubscriptions().Where(h => h.UserSubscription).ToArray(); lock(_listenersLock)
return _listeners.OfType<Subscription>().Where(h => h.UserSubscription).ToArray();
} }
} }
@ -128,7 +137,7 @@ namespace CryptoExchange.Net.Sockets
if (_pausedActivity != value) if (_pausedActivity != value)
{ {
_pausedActivity = value; _pausedActivity = value;
_logger.Log(LogLevel.Information, $"Socket {SocketId} Paused activity: " + value); _logger.Log(LogLevel.Information, $"Socket {SocketId} paused activity: " + value);
if(_pausedActivity) _ = Task.Run(() => ActivityPaused?.Invoke()); if(_pausedActivity) _ = Task.Run(() => ActivityPaused?.Invoke());
else _ = Task.Run(() => ActivityUnpaused?.Invoke()); else _ = Task.Run(() => ActivityUnpaused?.Invoke());
} }
@ -153,7 +162,8 @@ namespace CryptoExchange.Net.Sockets
} }
private bool _pausedActivity; private bool _pausedActivity;
private readonly SocketListenerManager _listenerManager; private readonly object _listenersLock;
private readonly List<IMessageProcessor> _listeners;
private readonly ILogger _logger; private readonly ILogger _logger;
private SocketStatus _status; private SocketStatus _status;
@ -186,57 +196,67 @@ namespace CryptoExchange.Net.Sockets
_socket.OnError += HandleErrorAsync; _socket.OnError += HandleErrorAsync;
_socket.GetReconnectionUrl = GetReconnectionUrlAsync; _socket.GetReconnectionUrl = GetReconnectionUrlAsync;
_listenerManager = new SocketListenerManager(_logger, SocketId); _listenersLock = new object();
_listeners = new List<IMessageProcessor>();
} }
/// <summary> /// <summary>
/// Handler for a socket opening /// Handler for a socket opening
/// </summary> /// </summary>
protected virtual async Task HandleOpenAsync() protected virtual Task HandleOpenAsync()
{ {
Status = SocketStatus.Connected; Status = SocketStatus.Connected;
PausedActivity = false; PausedActivity = false;
return Task.CompletedTask;
} }
/// <summary> /// <summary>
/// Handler for a socket closing without reconnect /// Handler for a socket closing without reconnect
/// </summary> /// </summary>
protected virtual async Task HandleCloseAsync() protected virtual Task HandleCloseAsync()
{ {
Status = SocketStatus.Closed; Status = SocketStatus.Closed;
Authenticated = false; Authenticated = false;
foreach (var subscription in _listenerManager.GetSubscriptions()) lock (_listenersLock)
subscription.Confirmed = false;
foreach (var query in _listenerManager.GetQueries())
{ {
query.Fail("Connection interupted"); foreach (var subscription in _listeners.OfType<Subscription>())
_listenerManager.Remove(query); subscription.Confirmed = false;
foreach (var query in _listeners.OfType<Query>())
{
query.Fail("Connection interupted");
_listeners.Remove(query);
}
} }
Task.Run(() => ConnectionClosed?.Invoke()); _ = Task.Run(() => ConnectionClosed?.Invoke());
return Task.CompletedTask;
} }
/// <summary> /// <summary>
/// Handler for a socket losing conenction and starting reconnect /// Handler for a socket losing conenction and starting reconnect
/// </summary> /// </summary>
protected virtual async Task HandleReconnectingAsync() protected virtual Task HandleReconnectingAsync()
{ {
Status = SocketStatus.Reconnecting; Status = SocketStatus.Reconnecting;
DisconnectTime = DateTime.UtcNow; DisconnectTime = DateTime.UtcNow;
Authenticated = false; Authenticated = false;
foreach (var subscription in _listenerManager.GetSubscriptions()) lock (_listenersLock)
subscription.Confirmed = false;
foreach (var query in _listenerManager.GetQueries())
{ {
query.Fail("Connection interupted"); foreach (var subscription in _listeners.OfType<Subscription>())
_listenerManager.Remove(query); subscription.Confirmed = false;
foreach (var query in _listeners.OfType<Query>())
{
query.Fail("Connection interupted");
_listeners.Remove(query);
}
} }
_ = Task.Run(() => ConnectionLost?.Invoke()); _ = Task.Run(() => ConnectionLost?.Invoke());
return Task.CompletedTask;
} }
/// <summary> /// <summary>
@ -251,22 +271,26 @@ namespace CryptoExchange.Net.Sockets
/// <summary> /// <summary>
/// Handler for a socket which has reconnected /// Handler for a socket which has reconnected
/// </summary> /// </summary>
protected virtual async Task HandleReconnectedAsync() protected virtual Task HandleReconnectedAsync()
{ {
Status = SocketStatus.Resubscribing; Status = SocketStatus.Resubscribing;
foreach (var query in _listenerManager.GetQueries()) lock (_listenersLock)
{ {
query.Fail("Connection interupted"); foreach (var query in _listeners.OfType<Query>())
_listenerManager.Remove(query); {
query.Fail("Connection interupted");
_listeners.Remove(query);
}
} }
// Can't wait for this as it would cause a deadlock
_ = Task.Run(async () => _ = Task.Run(async () =>
{ {
var reconnectSuccessful = await ProcessReconnectAsync().ConfigureAwait(false); var reconnectSuccessful = await ProcessReconnectAsync().ConfigureAwait(false);
if (!reconnectSuccessful) if (!reconnectSuccessful)
{ {
_logger.Log(LogLevel.Warning, $"Socket {SocketId} Failed reconnect processing: {reconnectSuccessful.Error}, reconnecting again"); _logger.Log(LogLevel.Warning, $"Socket {SocketId} failed reconnect processing: {reconnectSuccessful.Error}, reconnecting again");
_ = _socket.ReconnectAsync().ConfigureAwait(false); _ = _socket.ReconnectAsync().ConfigureAwait(false);
} }
else else
@ -279,34 +303,44 @@ namespace CryptoExchange.Net.Sockets
}); });
} }
}); });
return Task.CompletedTask;
} }
/// <summary> /// <summary>
/// Handler for an error on a websocket /// Handler for an error on a websocket
/// </summary> /// </summary>
/// <param name="e">The exception</param> /// <param name="e">The exception</param>
protected virtual async Task HandleErrorAsync(Exception e) protected virtual Task HandleErrorAsync(Exception e)
{ {
if (e is WebSocketException wse) if (e is WebSocketException wse)
_logger.Log(LogLevel.Warning, $"Socket {SocketId} error: Websocket error code {wse.WebSocketErrorCode}, details: " + e.ToLogString()); _logger.Log(LogLevel.Warning, $"Socket {SocketId} error: Websocket error code {wse.WebSocketErrorCode}, details: " + e.ToLogString());
else else
_logger.Log(LogLevel.Warning, $"Socket {SocketId} error: " + e.ToLogString()); _logger.Log(LogLevel.Warning, $"Socket {SocketId} error: " + e.ToLogString());
return Task.CompletedTask;
} }
/// <summary> /// <summary>
/// Handler for whenever a request is sent over the websocket /// Handler for whenever a request is sent over the websocket
/// </summary> /// </summary>
/// <param name="requestId">Id of the request sent</param> /// <param name="requestId">Id of the request sent</param>
protected virtual async Task HandleRequestSentAsync(int requestId) protected virtual Task HandleRequestSentAsync(int requestId)
{ {
var query = _listenerManager.GetById<BaseQuery>(requestId); Query query;
lock (_listenersLock)
{
query = _listeners.OfType<Query>().FirstOrDefault(x => x.Id == requestId);
}
if (query == null) if (query == null)
{ {
_logger.Log(LogLevel.Debug, $"Socket {SocketId} - msg {requestId} - message sent, but not pending"); _logger.Log(LogLevel.Debug, $"Socket {SocketId} msg {requestId} - message sent, but not pending");
return; return Task.CompletedTask;
} }
query.IsSend(ApiClient.ClientOptions.RequestTimeout); query.IsSend(ApiClient.ClientOptions.RequestTimeout);
return Task.CompletedTask;
} }
/// <summary> /// <summary>
@ -317,87 +351,74 @@ namespace CryptoExchange.Net.Sockets
/// <returns></returns> /// <returns></returns>
protected virtual async Task HandleStreamMessage(WebSocketMessageType type, Stream stream) protected virtual async Task HandleStreamMessage(WebSocketMessageType type, Stream stream)
{ {
var result = ReadJson(type, stream); // 1. Decrypt/Preprocess if necessary
if (result == null) stream = ApiClient.PreprocessStreamMessage(type, stream);
{
// Not able to parse at all
var buffer = new byte[stream.Length];
stream.Position = 0;
stream.Read(buffer, 0, buffer.Length);
_logger.LogDebug($"Socket {SocketId} Failed to parse data: {Encoding.UTF8.GetString(buffer)}");
UnparsedMessage?.Invoke(buffer); // 2. Read data into accessor
var messageData = new JsonNetMessageData(stream); // TODO if we let the implementation create this we can switch per implementation
var message = new SocketMessage(DateTime.UtcNow, messageData);
if (ApiClient.ApiOptions.OutputOriginalData ?? ApiClient.ClientOptions.OutputOriginalData)
{
stream.Position = 0;
using var textReader = new StreamReader(stream, Encoding.UTF8, false, 1024, true);
message.RawData = textReader.ReadToEnd();
_logger.LogTrace("Socket {SocketId} received {Data}", SocketId, message.RawData);
}
// 3. Determine the subscription interested in the messsage
var listenId = ApiClient.GetListenerIdentifier(message);
List<IMessageProcessor> processors;
lock(_listenersLock)
processors = _listeners.Where(s => s.ListenerIdentifiers.Contains(listenId)).ToList();
if (!processors.Any())
{
_logger.LogWarning("Socket {SocketId} received message not matched to any processor", SocketId);
UnhandledMessage?.Invoke(message);
return; return;
} }
if (result.OriginalData != null) _logger.LogTrace("Socket {SocketId} {Count} processors matched to message with listener identifier {ListenerId}", SocketId, processors.Count, listenId);
_logger.LogDebug($"Socket {SocketId} Data received: {result.OriginalData}"); foreach (var processor in processors)
if (!result.Parsed)
{ {
// Not able to determine the message type for the message // 4. Determine the type to deserialize to
_logger.LogWarning("Message not matched to type"); var messageType = processor.GetMessageType(message);
return; if (messageType == null)
} {
_logger.LogWarning("Socket {SocketId} received message not recognized by handler {Id}", SocketId, processor.Id);
continue;
}
if (!await _listenerManager.InvokeListenersAsync(this, result.StreamIdentifier, result).ConfigureAwait(false)) // 5. Deserialize the message
{ object deserialized;
// Not able to find a listener for this message try
stream.Position = 0; {
var unhandledBuffer = new byte[stream.Length]; deserialized = message.Deserialize(messageType);
stream.Read(unhandledBuffer, 0, unhandledBuffer.Length); }
_logger.Log(LogLevel.Warning, $"Socket {SocketId} Message unidentified. Id: {result.StreamIdentifier.ToLowerInvariant()}, listening ids: [{string.Join(", ", _listenerManager.GetListenIds())}], Message: {Encoding.UTF8.GetString(unhandledBuffer)} "); catch (Exception ex)
UnhandledMessage?.Invoke(result); {
return; _logger.LogWarning("Socket {SocketId} failed to deserialize message to type {Type}: {Exception}", SocketId, messageType.Name, ex.ToLogString());
continue;
}
// 6. Hand of the message to the subscription
try
{
await processor.HandleAsync(this, new DataEvent<object>(deserialized, null, message.RawData, message.ReceiveTime, null)).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.LogWarning("Socket {SocketId} user message processing failed: {Exception}", SocketId, ex.ToLogString());
if (processor is Subscription subscription)
subscription.InvokeExceptionHandler(ex);
}
} }
stream.Dispose(); stream.Dispose();
} }
/// <summary>
/// Read a message from stream
/// </summary>
/// <param name="websocketMessageType"></param>
/// <param name="stream"></param>
/// <returns></returns>
protected virtual BaseParsedMessage? ReadJson(WebSocketMessageType websocketMessageType, Stream stream)
{
// Start reading the data
// Once we reach the properties that identify the message we save those in a dict
// Once all id properties have been read callback to see what the deserialization type should be
// Deserialize to the correct type
if (ApiClient.Pipeline.PreProcessCallback != null)
stream = ApiClient.Pipeline.PreProcessCallback(websocketMessageType, stream);
var accessor = new JTokenAccessor(stream);
if (accessor == null)
return null;
var streamIdentity = ApiClient.Pipeline.GetStreamIdentifier(accessor);
if (streamIdentity == null)
return null;
var typeIdentity = ApiClient.Pipeline.GetTypeIdentifier(accessor, streamIdentity);
var typeResult = _listenerManager.IdToType(streamIdentity, typeIdentity);
if (typeResult == null)
return null;
var idInstance = accessor.Instantiate(typeResult);
if (ApiClient.ApiOptions.OutputOriginalData ?? ApiClient.ClientOptions.OutputOriginalData)
{
var buffer2 = new byte[stream.Length];
stream.Position = 0;
stream.Read(buffer2, 0, buffer2.Length);
idInstance.OriginalData = Encoding.UTF8.GetString(buffer2);
}
idInstance.StreamIdentifier = streamIdentity;
idInstance.TypeIdentifier = typeIdentity;
idInstance.Parsed = true;
return idInstance;
}
/// <summary> /// <summary>
/// Connect the websocket /// Connect the websocket
/// </summary> /// </summary>
@ -428,10 +449,13 @@ namespace CryptoExchange.Net.Sockets
if (ApiClient.socketConnections.ContainsKey(SocketId)) if (ApiClient.socketConnections.ContainsKey(SocketId))
ApiClient.socketConnections.TryRemove(SocketId, out _); ApiClient.socketConnections.TryRemove(SocketId, out _);
foreach (var subscription in _listenerManager.GetSubscriptions()) lock (_listenersLock)
{ {
if (subscription.CancellationTokenRegistration.HasValue) foreach (var subscription in _listeners.OfType<Subscription>())
subscription.CancellationTokenRegistration.Value.Dispose(); {
if (subscription.CancellationTokenRegistration.HasValue)
subscription.CancellationTokenRegistration.Value.Dispose();
}
} }
await _socket.CloseAsync().ConfigureAwait(false); await _socket.CloseAsync().ConfigureAwait(false);
@ -446,8 +470,11 @@ namespace CryptoExchange.Net.Sockets
/// <returns></returns> /// <returns></returns>
public async Task CloseAsync(Subscription subscription, bool unsubEvenIfNotConfirmed = false) public async Task CloseAsync(Subscription subscription, bool unsubEvenIfNotConfirmed = false)
{ {
if (!_listenerManager.Contains(subscription)) lock (_listenersLock)
return; {
if (!_listeners.Contains(subscription))
return;
}
subscription.Closed = true; subscription.Closed = true;
@ -467,9 +494,13 @@ namespace CryptoExchange.Net.Sockets
return; return;
} }
var shouldCloseConnection = _listenerManager.GetSubscriptions().All(r => !r.UserSubscription || r.Closed); bool shouldCloseConnection;
if (shouldCloseConnection) lock (_listenersLock)
Status = SocketStatus.Closing; {
shouldCloseConnection = _listeners.OfType<Subscription>().All(r => !r.UserSubscription || r.Closed);
if (shouldCloseConnection)
Status = SocketStatus.Closing;
}
if (shouldCloseConnection) if (shouldCloseConnection)
{ {
@ -477,7 +508,8 @@ namespace CryptoExchange.Net.Sockets
await CloseAsync().ConfigureAwait(false); await CloseAsync().ConfigureAwait(false);
} }
_listenerManager.Remove(subscription); lock (_listenersLock)
_listeners.Remove(subscription);
} }
/// <summary> /// <summary>
@ -504,7 +536,8 @@ namespace CryptoExchange.Net.Sockets
if (Status != SocketStatus.None && Status != SocketStatus.Connected) if (Status != SocketStatus.None && Status != SocketStatus.Connected)
return false; return false;
_listenerManager.Add(subscription); lock (_listenersLock)
_listeners.Add(subscription);
if (subscription.UserSubscription) if (subscription.UserSubscription)
_logger.Log(LogLevel.Debug, $"Socket {SocketId} adding new subscription with id {subscription.Id}, total subscriptions on connection: {UserSubscriptionCount}"); _logger.Log(LogLevel.Debug, $"Socket {SocketId} adding new subscription with id {subscription.Id}, total subscriptions on connection: {UserSubscriptionCount}");
@ -515,21 +548,29 @@ namespace CryptoExchange.Net.Sockets
/// Get a subscription on this connection by id /// Get a subscription on this connection by id
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="id"></param>
public Subscription? GetSubscription(int id) => _listenerManager.GetSubscriptions().SingleOrDefault(s => s.Id == id); public Subscription? GetSubscription(int id)
{
lock (_listenersLock)
return _listeners.OfType<Subscription>().SingleOrDefault(s => s.Id == id);
}
/// <summary> /// <summary>
/// Get a subscription on this connection by its subscribe request /// Get a subscription on this connection by its subscribe request
/// </summary> /// </summary>
/// <param name="predicate">Filter for a request</param> /// <param name="predicate">Filter for a request</param>
/// <returns></returns> /// <returns></returns>
public Subscription? GetSubscriptionByRequest(Func<object?, bool> predicate) => _listenerManager.GetSubscriptions().SingleOrDefault(s => predicate(s)); public Subscription? GetSubscriptionByRequest(Func<object?, bool> predicate)
{
lock (_listenersLock)
return _listeners.OfType<Subscription>().SingleOrDefault(s => predicate(s));
}
/// <summary> /// <summary>
/// Send a query request and wait for an answer /// Send a query request and wait for an answer
/// </summary> /// </summary>
/// <param name="query">Query to send</param> /// <param name="query">Query to send</param>
/// <param name="onFinished">Action to run when query finishes</param>
/// <returns></returns> /// <returns></returns>
public virtual async Task<CallResult> SendAndWaitQueryAsync(BaseQuery query, Action? onFinished = null) public virtual async Task<CallResult> SendAndWaitQueryAsync(Query query, Action? onFinished = null)
{ {
await SendAndWaitIntAsync(query, onFinished).ConfigureAwait(false); await SendAndWaitIntAsync(query, onFinished).ConfigureAwait(false);
return query.Result ?? new CallResult(new ServerError("Timeout")); return query.Result ?? new CallResult(new ServerError("Timeout"));
@ -540,6 +581,7 @@ namespace CryptoExchange.Net.Sockets
/// </summary> /// </summary>
/// <typeparam name="T">Query response type</typeparam> /// <typeparam name="T">Query response type</typeparam>
/// <param name="query">Query to send</param> /// <param name="query">Query to send</param>
/// <param name="onFinished">Action to run when query finishes</param>
/// <returns></returns> /// <returns></returns>
public virtual async Task<CallResult<T>> SendAndWaitQueryAsync<T>(Query<T> query, Action? onFinished = null) public virtual async Task<CallResult<T>> SendAndWaitQueryAsync<T>(Query<T> query, Action? onFinished = null)
{ {
@ -547,9 +589,11 @@ namespace CryptoExchange.Net.Sockets
return query.TypedResult ?? new CallResult<T>(new ServerError("Timeout")); return query.TypedResult ?? new CallResult<T>(new ServerError("Timeout"));
} }
private async Task SendAndWaitIntAsync(BaseQuery query, Action onFinished) private async Task SendAndWaitIntAsync(Query query, Action? onFinished)
{ {
_listenerManager.Add(query); lock(_listenersLock)
_listeners.Add(query);
var sendOk = Send(query.Id, query.Request, query.Weight); var sendOk = Send(query.Id, query.Request, query.Weight);
if (!sendOk) if (!sendOk)
{ {
@ -600,7 +644,7 @@ namespace CryptoExchange.Net.Sockets
/// <param name="requestId">The id of the request</param> /// <param name="requestId">The id of the request</param>
public virtual bool Send(int requestId, string data, int weight) public virtual bool Send(int requestId, string data, int weight)
{ {
_logger.Log(LogLevel.Trace, $"Socket {SocketId} - msg {requestId} - sending messsage: {data}"); _logger.Log(LogLevel.Trace, $"Socket {SocketId} msg {requestId} - sending messsage: {data}");
try try
{ {
_socket.Send(requestId, data, weight); _socket.Send(requestId, data, weight);
@ -617,16 +661,20 @@ namespace CryptoExchange.Net.Sockets
if (!_socket.IsOpen) if (!_socket.IsOpen)
return new CallResult<bool>(new WebError("Socket not connected")); return new CallResult<bool>(new WebError("Socket not connected"));
var anySubscriptions = _listenerManager.GetSubscriptions().Any(s => s.UserSubscription); bool anySubscriptions;
lock (_listenersLock)
anySubscriptions = _listeners.OfType<Subscription>().Any(s => s.UserSubscription);
if (!anySubscriptions) if (!anySubscriptions)
{ {
// No need to resubscribe anything // No need to resubscribe anything
_logger.Log(LogLevel.Debug, $"Socket {SocketId} Nothing to resubscribe, closing connection"); _logger.Log(LogLevel.Debug, $"Socket {SocketId} nothing to resubscribe, closing connection");
_ = _socket.CloseAsync(); _ = _socket.CloseAsync();
return new CallResult<bool>(true); return new CallResult<bool>(true);
} }
var anyAuthenticated = _listenerManager.GetSubscriptions().Any(s => s.Authenticated); bool anyAuthenticated;
lock (_listenersLock)
anyAuthenticated = _listeners.OfType<Subscription>().Any(s => s.Authenticated);
if (anyAuthenticated) if (anyAuthenticated)
{ {
// If we reconnected a authenticated connection we need to re-authenticate // If we reconnected a authenticated connection we need to re-authenticate
@ -642,7 +690,9 @@ namespace CryptoExchange.Net.Sockets
} }
// Get a list of all subscriptions on the socket // Get a list of all subscriptions on the socket
var subList = _listenerManager.GetSubscriptions(); List<Subscription> subList;
lock (_listenersLock)
subList = _listeners.OfType<Subscription>().ToList();
foreach(var subscription in subList) foreach(var subscription in subList)
{ {
@ -650,7 +700,7 @@ namespace CryptoExchange.Net.Sockets
var result = await ApiClient.RevitalizeRequestAsync(subscription).ConfigureAwait(false); var result = await ApiClient.RevitalizeRequestAsync(subscription).ConfigureAwait(false);
if (!result) if (!result)
{ {
_logger.Log(LogLevel.Warning, $"Socket {SocketId} Failed request revitalization: " + result.Error); _logger.Log(LogLevel.Warning, $"Socket {SocketId} failed request revitalization: " + result.Error);
return result.As(false); return result.As(false);
} }
} }
@ -670,8 +720,7 @@ namespace CryptoExchange.Net.Sockets
taskList.Add(SendAndWaitQueryAsync(subQuery, () => taskList.Add(SendAndWaitQueryAsync(subQuery, () =>
{ {
subscription.HandleSubQueryResponse(subQuery.Response); subscription.HandleSubQueryResponse(subQuery.Response!);
_listenerManager.Reset(subscription);
})); }));
} }
@ -710,7 +759,7 @@ namespace CryptoExchange.Net.Sockets
return new CallResult(null); return new CallResult(null);
var result = await SendAndWaitQueryAsync(subQuery).ConfigureAwait(false); var result = await SendAndWaitQueryAsync(subQuery).ConfigureAwait(false);
subscription.HandleSubQueryResponse(subQuery.Response); subscription.HandleSubQueryResponse(subQuery.Response!);
return result; return result;
} }

View File

@ -1,170 +0,0 @@
using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.Objects.Sockets;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace CryptoExchange.Net.Sockets
{
public class SocketListenerManager
{
private ILogger _logger;
private int _socketId;
private object _lock = new object();
//private Dictionary<int, IMessageProcessor> _idMap;
//private Dictionary<string, Dictionary<string, Type>> _typeMap;
private Dictionary<string, List<IMessageProcessor>> _listeners;
public SocketListenerManager(ILogger logger, int socketId)
{
//_idMap = new Dictionary<int, IMessageProcessor>();
_listeners = new Dictionary<string, List<IMessageProcessor>>();
//_typeMap = new Dictionary<string, Dictionary<string, Type>>();
_logger = logger;
_socketId = socketId;
}
public Type? IdToType(string streamIdentifier, string typeIdentifier)
{
lock (_lock)
{
_listeners.TryGetValue(streamIdentifier, out var listeners);
if (listeners == null)
return null;
var result = listeners.SelectMany(l => l.TypeMapping).FirstOrDefault(x => x.Key == (typeIdentifier ?? ""));
return result.Value;
}
}
public List<string> GetListenIds()
{
lock(_lock)
return _listeners.Keys.ToList();
}
public void Add(IMessageProcessor processor)
{
lock (_lock)
{
if (processor.StreamIdentifiers?.Any() == true)
{
foreach (var identifier in processor.StreamIdentifiers)
{
if (!_listeners.TryGetValue(identifier, out var list))
{
list = new List<IMessageProcessor>();
_listeners.Add(identifier, list);
}
list.Add(processor);
}
}
}
}
public void Reset(IMessageProcessor processor)
{
lock (_lock)
{
//Debug.WriteLine("4 Resetting");
Remove(processor);
Add(processor);
}
}
public async Task<bool> InvokeListenersAsync(SocketConnection connection, string id, BaseParsedMessage data)
{
List<IMessageProcessor> listeners;
lock (_lock)
{
if (!_listeners.TryGetValue(id, out var idListeners))
return false;
listeners = idListeners.Where(i => data.TypeIdentifier == null || i.TypeMapping.ContainsKey(data.TypeIdentifier)).ToList();
}
foreach (var listener in listeners)
{
_logger.Log(LogLevel.Trace, $"Socket {_socketId} Message mapped to processor {listener.Id} with identifier {data.StreamIdentifier}");
if (listener is BaseQuery query)
{
Remove(listener);
if (query?.Completed == true)
{
// Answer to a timed out request
_logger.Log(LogLevel.Warning, $"Socket {_socketId} Received after request timeout. Consider increasing the RequestTimeout");
}
}
// Matched based on identifier
var userSw = Stopwatch.StartNew();
var dataEvent = new DataEvent<BaseParsedMessage>(data, null, data.OriginalData, DateTime.UtcNow, null);
try
{
await listener.HandleMessageAsync(connection, dataEvent).ConfigureAwait(false);
}
catch (Exception ex)
{
// TODO
}
userSw.Stop();
if (userSw.ElapsedMilliseconds > 500)
{
_logger.Log(LogLevel.Debug, $"Socket {_socketId} {(listener is Subscription ? "subscription " : "query " + listener!.Id)} message processing slow ({(int)userSw.ElapsedMilliseconds}ms), consider offloading data handling to another thread. " +
"Data from this socket may arrive late or not at all if message processing is continuously slow.");
}
}
return true;
}
public T? GetById<T>(int id) where T : BaseQuery
{
lock (_lock)
{
var val = _listeners.Values.SelectMany(x => x).FirstOrDefault(x => x.Id == id);
return (T)val;
}
}
public List<Subscription> GetSubscriptions()
{
lock (_lock)
return _listeners.Values.SelectMany(v => v.OfType<Subscription>()).Distinct().ToList();
}
public List<BaseQuery> GetQueries()
{
lock (_lock)
return _listeners.Values.SelectMany(v => v.OfType<BaseQuery>()).ToList();
}
public bool Contains(IMessageProcessor processor)
{
lock (_lock)
return _listeners.Any(l => l.Value.Contains(processor));
}
public void Remove(IMessageProcessor processor)
{
lock (_lock)
{
if (processor.StreamIdentifiers?.Any() != true)
return;
foreach(var kv in _listeners)
{
if (kv.Value.Contains(processor))
kv.Value.Remove(processor);
}
}
}
}
}

View File

@ -0,0 +1,45 @@
using CryptoExchange.Net.Sockets.MessageParsing.Interfaces;
using System;
namespace CryptoExchange.Net.Sockets
{
/// <summary>
/// Message received from the websocket
/// </summary>
public class SocketMessage
{
/// <summary>
/// Message receive time
/// </summary>
public DateTime ReceiveTime { get; set; }
/// <summary>
/// The message data
/// </summary>
public IMessageAccessor Message { get; set; }
/// <summary>
/// Raw string data
/// </summary>
public string? RawData { get; set; }
/// <summary>
/// ctor
/// </summary>
/// <param name="receiveTime"></param>
/// <param name="message"></param>
public SocketMessage(DateTime receiveTime, IMessageAccessor message)
{
ReceiveTime = receiveTime;
Message = message;
}
/// <summary>
/// Deserialize the message to a type
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public object Deserialize(Type type)
{
return Message.Deserialize(type);
}
}
}

View File

@ -55,9 +55,9 @@ namespace CryptoExchange.Net.Sockets
public bool Authenticated { get; } public bool Authenticated { get; }
/// <summary> /// <summary>
/// Strings to identify this subscription with /// Strings to match this subscription to a received message
/// </summary> /// </summary>
public abstract List<string> StreamIdentifiers { get; set; } public abstract HashSet<string> ListenerIdentifiers { get; set; }
/// <summary> /// <summary>
/// Cancellation token registration /// Cancellation token registration
@ -69,7 +69,12 @@ namespace CryptoExchange.Net.Sockets
/// </summary> /// </summary>
public event Action<Exception>? Exception; public event Action<Exception>? Exception;
public abstract Dictionary<string, Type> TypeMapping { get; } /// <summary>
/// Get the deserialization type for this message
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public abstract Type? GetMessageType(SocketMessage message);
/// <summary> /// <summary>
/// ctor /// ctor
@ -86,21 +91,36 @@ namespace CryptoExchange.Net.Sockets
} }
/// <summary> /// <summary>
/// Get the subscribe object to send when subscribing /// Get the subscribe query to send when subscribing
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public abstract BaseQuery? GetSubQuery(SocketConnection connection); public abstract Query? GetSubQuery(SocketConnection connection);
public virtual void HandleSubQueryResponse(BaseParsedMessage message) { }
public virtual void HandleUnsubQueryResponse(BaseParsedMessage message) { }
/// <summary> /// <summary>
/// Get the unsubscribe object to send when unsubscribing /// Handle a subscription query response
/// </summary>
/// <param name="message"></param>
public virtual void HandleSubQueryResponse(object message) { }
/// <summary>
/// Handle an unsubscription query response
/// </summary>
/// <param name="message"></param>
public virtual void HandleUnsubQueryResponse(object message) { }
/// <summary>
/// Get the unsubscribe query to send when unsubscribing
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public abstract BaseQuery? GetUnsubQuery(); public abstract Query? GetUnsubQuery();
public async Task<CallResult> HandleMessageAsync(SocketConnection connection, DataEvent<BaseParsedMessage> message) /// <summary>
/// Handle an update message
/// </summary>
/// <param name="connection"></param>
/// <param name="message"></param>
/// <returns></returns>
public async Task<CallResult> HandleAsync(SocketConnection connection, DataEvent<object> message)
{ {
ConnectionInvocations++; ConnectionInvocations++;
TotalInvocations++; TotalInvocations++;
@ -110,9 +130,10 @@ namespace CryptoExchange.Net.Sockets
/// <summary> /// <summary>
/// Handle the update message /// Handle the update message
/// </summary> /// </summary>
/// <param name="connection"></param>
/// <param name="message"></param> /// <param name="message"></param>
/// <returns></returns> /// <returns></returns>
public abstract Task<CallResult> DoHandleMessageAsync(SocketConnection connection, DataEvent<BaseParsedMessage> message); public abstract Task<CallResult> DoHandleMessageAsync(SocketConnection connection, DataEvent<object> message);
/// <summary> /// <summary>
/// Invoke the exception event /// Invoke the exception event
@ -124,24 +145,9 @@ namespace CryptoExchange.Net.Sockets
} }
} }
/// <inheritdoc />
public abstract class Subscription<TQuery> : Subscription<TQuery, TQuery>
{
/// <summary>
/// ctor
/// </summary>
/// <param name="logger"></param>
/// <param name="authenticated"></param>
protected Subscription(ILogger logger, bool authenticated) : base(logger, authenticated)
{
}
}
/// <inheritdoc /> /// <inheritdoc />
public abstract class Subscription<TSubResponse, TUnsubResponse> : Subscription public abstract class Subscription<TSubResponse, TUnsubResponse> : Subscription
{ {
//public override Func<string, Type> ExpectedTypeDelegate => (x) => typeof(TEvent);
/// <summary> /// <summary>
/// ctor /// ctor
/// </summary> /// </summary>
@ -152,18 +158,24 @@ namespace CryptoExchange.Net.Sockets
} }
/// <inheritdoc /> /// <inheritdoc />
//public override Task<CallResult> DoHandleMessageAsync(SocketConnection connection, DataEvent<BaseParsedMessage> message) public override void HandleSubQueryResponse(object message)
// => HandleEventAsync(connection, message.As((ParsedMessage<TEvent>)message.Data)); => HandleSubQueryResponse((TSubResponse)message);
public override void HandleSubQueryResponse(BaseParsedMessage message) /// <summary>
=> HandleSubQueryResponse((ParsedMessage<TSubResponse>)message); /// Handle a subscription query response
/// </summary>
/// <param name="message"></param>
public virtual void HandleSubQueryResponse(TSubResponse message) { }
public virtual void HandleSubQueryResponse(ParsedMessage<TSubResponse> message) { } /// <inheritdoc />
public override void HandleUnsubQueryResponse(object message)
=> HandleUnsubQueryResponse((TUnsubResponse)message);
public override void HandleUnsubQueryResponse(BaseParsedMessage message) /// <summary>
=> HandleUnsubQueryResponse((ParsedMessage<TUnsubResponse>)message); /// Handle an unsubscription query response
/// </summary>
public virtual void HandleUnsubQueryResponse(ParsedMessage<TUnsubResponse> message) { } /// <param name="message"></param>
public virtual void HandleUnsubQueryResponse(TUnsubResponse message) { }
} }
} }

View File

@ -22,26 +22,37 @@ namespace CryptoExchange.Net.Sockets
} }
/// <inheritdoc /> /// <inheritdoc />
public override BaseQuery? GetSubQuery(SocketConnection connection) => null; public override Query? GetSubQuery(SocketConnection connection) => null;
/// <inheritdoc /> /// <inheritdoc />
public override BaseQuery? GetUnsubQuery() => null; public override Query? GetUnsubQuery() => null;
} }
/// <inheritdoc />
public abstract class SystemSubscription<T> : SystemSubscription public abstract class SystemSubscription<T> : SystemSubscription
{ {
public override Dictionary<string, Type> TypeMapping => new Dictionary<string, Type> /// <inheritdoc />
{ public override Type GetMessageType(SocketMessage message) => typeof(T);
{ "", typeof(T) }
};
public override Task<CallResult> DoHandleMessageAsync(SocketConnection connection, DataEvent<BaseParsedMessage> message) /// <inheritdoc />
=> HandleMessageAsync(connection, message.As((ParsedMessage<T>)message.Data)); public override Task<CallResult> DoHandleMessageAsync(SocketConnection connection, DataEvent<object> message)
=> HandleMessageAsync(connection, message.As((T)message.Data));
/// <summary>
/// ctor
/// </summary>
/// <param name="logger"></param>
/// <param name="authenticated"></param>
protected SystemSubscription(ILogger logger, bool authenticated) : base(logger, authenticated) protected SystemSubscription(ILogger logger, bool authenticated) : base(logger, authenticated)
{ {
} }
public abstract Task<CallResult> HandleMessageAsync(SocketConnection connection, DataEvent<ParsedMessage<T>> message); /// <summary>
/// Handle an update message
/// </summary>
/// <param name="connection"></param>
/// <param name="message"></param>
/// <returns></returns>
public abstract Task<CallResult> HandleMessageAsync(SocketConnection connection, DataEvent<T> message);
} }
} }