1
0
mirror of https://github.com/JKorf/CryptoExchange.Net.git synced 2026-08-12 08:53:01 +00:00

Compare commits

..

20 Commits

Author SHA1 Message Date
JKorf 68067d6258 Updated to version 7.10.0 2024-07-26 14:29:07 +02:00
JKorf b309deb0c4 Small fixed/improvements. Added deflate stream method 2024-07-25 18:47:33 +02:00
JKorf e1dafdf0dd Added AddSecondsString and AddOptionalSecondsString method to ParameterCollection 2024-07-24 10:22:35 +02:00
JKorf fd7b5f0f0f Added RestIntergrationTest base class for running integration tests 2024-07-23 16:58:10 +02:00
JKorf 3b735d66fd Improved socket reconnect logic so unsubscribing while processing resubscribing won't result in a ghost subscription 2024-07-23 12:01:31 +02:00
JKorf 3cd505ac8b Fixed order book logging 2024-07-23 12:00:15 +02:00
JKorf 81d856d78d Fixed System.Text.Json array converter deserialization when skipping indexes, added NumberStringConverter 2024-07-23 12:00:08 +02:00
JKorf 11c1ad871a Docs 2024-07-19 10:15:21 +02:00
JKorf ffcb7db8ff Updated to version 7.9.0 2024-07-16 19:01:03 +02:00
JKorf 02432e5109 Added small performance improvements in SystemTextJsonMessageAccessor 2024-07-16 15:54:11 +02:00
JKorf a85bfb4432 Added support for requiring multiple responses for queries, fixed possible exception when closing connection, added ToString override DataEvent object 2024-07-16 15:23:09 +02:00
JKorf 17d85fdd85 Added WebCallResult As<T> methods for untyped WebCallResult 2024-07-16 08:35:19 +02:00
JKorf 5e0733d7f4 Fixed trying to authenticate dedicated connection when no credentials are set 2024-07-14 20:59:22 +02:00
JKorf 28d5287bd4 Changed logging extensions to public so overridden methods can access them 2024-07-11 19:19:35 +02:00
JKorf ef5097589a Test compare improvements 2024-07-10 16:57:30 +02:00
JKorf f287ec1fa4 Added some checks socket connections 2024-07-10 14:12:07 +02:00
JKorf 28da93af9d Test fixes 2024-07-10 10:18:58 +02:00
JKorf 0d5bdf5095 Bumped System.Text.Json package to 8.0.4 2024-07-10 10:09:36 +02:00
JKorf 8dac3d7aa6 Refactor to prevent usage of thread block ManualResetEvent in socket message handling 2024-07-09 16:50:08 +02:00
JKorf 6951f31be7 Fix ArrayConverter error for nullable types 2024-07-05 16:34:58 +02:00
33 changed files with 588 additions and 220 deletions
@@ -58,9 +58,7 @@ namespace CryptoExchange.Net.UnitTests
options.ReconnectInterval = TimeSpan.Zero;
});
var socket = client.CreateSocket();
socket.ShouldReconnect = true;
socket.CanConnect = true;
socket.DisconnectTime = DateTime.UtcNow;
var sub = new SocketConnection(new TraceLogger(), client.SubClient, socket, null);
var rstEvent = new ManualResetEvent(false);
Dictionary<string, string> result = null;
@@ -75,7 +73,7 @@ namespace CryptoExchange.Net.UnitTests
sub.AddSubscription(subObj);
// act
socket.InvokeMessage("{\"property\": \"123\", \"topic\": \"topic\"}");
socket.InvokeMessage("{\"property\": \"123\", \"action\": \"update\", \"topic\": \"topic\"}");
rstEvent.WaitOne(1000);
// assert
@@ -93,9 +91,7 @@ namespace CryptoExchange.Net.UnitTests
options.SubOptions.OutputOriginalData = enabled;
});
var socket = client.CreateSocket();
socket.ShouldReconnect = true;
socket.CanConnect = true;
socket.DisconnectTime = DateTime.UtcNow;
var sub = new SocketConnection(new TraceLogger(), client.SubClient, socket, null);
var rstEvent = new ManualResetEvent(false);
string original = null;
@@ -107,7 +103,7 @@ namespace CryptoExchange.Net.UnitTests
rstEvent.Set();
});
sub.AddSubscription(subObj);
var msgToSend = JsonConvert.SerializeObject(new { topic = "topic", property = 123 });
var msgToSend = JsonConvert.SerializeObject(new { topic = "topic", action = "update", property = 123 });
// act
socket.InvokeMessage(msgToSend);
@@ -202,7 +198,7 @@ namespace CryptoExchange.Net.UnitTests
// act
var sub = client.SubClient.SubscribeToSomethingAsync(channel, onUpdate => {}, ct: default);
socket.InvokeMessage(JsonConvert.SerializeObject(new { channel, status = "error" }));
socket.InvokeMessage(JsonConvert.SerializeObject(new { channel, action = "subscribe", status = "error" }));
await sub;
// assert
@@ -225,7 +221,7 @@ namespace CryptoExchange.Net.UnitTests
// act
var sub = client.SubClient.SubscribeToSomethingAsync(channel, onUpdate => {}, ct: default);
socket.InvokeMessage(JsonConvert.SerializeObject(new { channel, status = "confirmed" }));
socket.InvokeMessage(JsonConvert.SerializeObject(new { channel, action = "subscribe", status = "confirmed" }));
await sub;
// assert
@@ -10,6 +10,10 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations.Sockets
{
internal class SubResponse
{
[JsonProperty("action")]
public string Action { get; set; } = null!;
[JsonProperty("channel")]
public string Channel { get; set; } = null!;
@@ -19,6 +23,9 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations.Sockets
internal class UnsubResponse
{
[JsonProperty("action")]
public string Action { get; set; } = null!;
[JsonProperty("status")]
public string Status { get; set; } = null!;
}
@@ -29,7 +36,7 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations.Sockets
public TestChannelQuery(string channel, string request, bool authenticated, int weight = 1) : base(request, authenticated, weight)
{
ListenerIdentifiers = new HashSet<string> { channel };
ListenerIdentifiers = new HashSet<string> { request + "-" + channel };
}
public override CallResult<SubResponse> HandleMessage(SocketConnection connection, DataEvent<SubResponse> message)
@@ -15,7 +15,7 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations.Sockets
{
private readonly Action<DataEvent<T>> _handler;
public override HashSet<string> ListenerIdentifiers { get; set; } = new HashSet<string> { "topic" };
public override HashSet<string> ListenerIdentifiers { get; set; } = new HashSet<string> { "update-topic" };
public TestSubscription(ILogger logger, Action<DataEvent<T>> handler) : base(logger, false)
{
@@ -1,131 +1,132 @@
using System;
using System.IO;
using System.Net.WebSockets;
using System.Security.Authentication;
using System.Text;
using System.Threading.Tasks;
using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.Objects;
//using System;
//using System.IO;
//using System.Net.WebSockets;
//using System.Security.Authentication;
//using System.Text;
//using System.Threading.Tasks;
//using CryptoExchange.Net.Interfaces;
//using CryptoExchange.Net.Objects;
namespace CryptoExchange.Net.UnitTests.TestImplementations
{
public class TestSocket: IWebsocket
{
public bool CanConnect { get; set; }
public bool Connected { get; set; }
//namespace CryptoExchange.Net.UnitTests.TestImplementations
//{
// public class TestSocket: IWebsocket
// {
// public bool CanConnect { get; set; }
// public bool Connected { get; set; }
public event Func<Task> OnClose;
#pragma warning disable 0067
public event Func<Task> OnReconnected;
public event Func<Task> OnReconnecting;
public event Func<int, Task> OnRequestRateLimited;
#pragma warning restore 0067
public event Func<int, Task> OnRequestSent;
public event Action<WebSocketMessageType, ReadOnlyMemory<byte>> OnStreamMessage;
public event Func<Exception, Task> OnError;
public event Func<Task> OnOpen;
public Func<Task<Uri>> GetReconnectionUrl { get; set; }
// public event Func<Task> OnClose;
//#pragma warning disable 0067
// public event Func<Task> OnReconnected;
// public event Func<Task> OnReconnecting;
// public event Func<int, Task> OnRequestRateLimited;
//#pragma warning restore 0067
// public event Func<int, Task> OnRequestSent;
// public event Func<WebSocketMessageType, ReadOnlyMemory<byte>, Task> OnStreamMessage;
// public event Func<Exception, Task> OnError;
// public event Func<Task> OnOpen;
// public Func<Task<Uri>> GetReconnectionUrl { get; set; }
public int Id { get; }
public bool ShouldReconnect { get; set; }
public TimeSpan Timeout { get; set; }
public Func<string, string> DataInterpreterString { get; set; }
public Func<byte[], string> DataInterpreterBytes { get; set; }
public DateTime? DisconnectTime { get; set; }
public string Url { get; }
public bool IsClosed => !Connected;
public bool IsOpen => Connected;
public bool PingConnection { get; set; }
public TimeSpan PingInterval { get; set; }
public SslProtocols SSLProtocols { get; set; }
public Encoding Encoding { get; set; }
// public int Id { get; }
// public bool ShouldReconnect { get; set; }
// public TimeSpan Timeout { get; set; }
// public Func<string, string> DataInterpreterString { get; set; }
// public Func<byte[], string> DataInterpreterBytes { get; set; }
// public DateTime? DisconnectTime { get; set; }
// public string Url { get; }
// public bool IsClosed => !Connected;
// public bool IsOpen => Connected;
// public bool PingConnection { get; set; }
// public TimeSpan PingInterval { get; set; }
// public SslProtocols SSLProtocols { get; set; }
// public Encoding Encoding { get; set; }
public int ConnectCalls { get; private set; }
public bool Reconnecting { get; set; }
public string Origin { get; set; }
public int? RatelimitPerSecond { get; set; }
// public int ConnectCalls { get; private set; }
// public bool Reconnecting { get; set; }
// public string Origin { get; set; }
// public int? RatelimitPerSecond { get; set; }
public double IncomingKbps => throw new NotImplementedException();
// public double IncomingKbps => throw new NotImplementedException();
public Uri Uri => new Uri("");
// public Uri Uri => new Uri("");
public TimeSpan KeepAliveInterval { get; set; }
// public TimeSpan KeepAliveInterval { get; set; }
public static int lastId = 0;
public static object lastIdLock = new object();
// public static int lastId = 0;
// public static object lastIdLock = new object();
public TestSocket()
{
lock (lastIdLock)
{
Id = lastId + 1;
lastId++;
}
}
// public TestSocket()
// {
// lock (lastIdLock)
// {
// Id = lastId + 1;
// lastId++;
// }
// }
public Task<CallResult> ConnectAsync()
{
Connected = CanConnect;
ConnectCalls++;
if (CanConnect)
InvokeOpen();
return Task.FromResult(CanConnect ? new CallResult(null) : new CallResult(new CantConnectError()));
}
// public Task<CallResult> ConnectAsync()
// {
// Connected = CanConnect;
// ConnectCalls++;
// if (CanConnect)
// InvokeOpen();
// return Task.FromResult(CanConnect ? new CallResult(null) : new CallResult(new CantConnectError()));
// }
public void Send(int requestId, string data, int weight)
{
if(!Connected)
throw new Exception("Socket not connected");
OnRequestSent?.Invoke(requestId);
}
// public bool Send(int requestId, string data, int weight)
// {
// if(!Connected)
// throw new Exception("Socket not connected");
// OnRequestSent?.Invoke(requestId);
// return true;
// }
public void Reset()
{
}
// public void Reset()
// {
// }
public Task CloseAsync()
{
Connected = false;
DisconnectTime = DateTime.UtcNow;
OnClose?.Invoke();
return Task.FromResult(0);
}
// public Task CloseAsync()
// {
// Connected = false;
// DisconnectTime = DateTime.UtcNow;
// OnClose?.Invoke();
// return Task.FromResult(0);
// }
public void SetProxy(string host, int port)
{
throw new NotImplementedException();
}
public void Dispose()
{
}
// public void SetProxy(string host, int port)
// {
// throw new NotImplementedException();
// }
// public void Dispose()
// {
// }
public void InvokeClose()
{
Connected = false;
DisconnectTime = DateTime.UtcNow;
Reconnecting = true;
OnClose?.Invoke();
}
// public void InvokeClose()
// {
// Connected = false;
// DisconnectTime = DateTime.UtcNow;
// Reconnecting = true;
// OnClose?.Invoke();
// }
public void InvokeOpen()
{
OnOpen?.Invoke();
}
// public void InvokeOpen()
// {
// OnOpen?.Invoke();
// }
public void InvokeMessage(string data)
{
OnStreamMessage?.Invoke(WebSocketMessageType.Text, new ReadOnlyMemory<byte>(Encoding.UTF8.GetBytes(data)));
}
// public void InvokeMessage(string data)
// {
// OnStreamMessage?.Invoke(WebSocketMessageType.Text, new ReadOnlyMemory<byte>(Encoding.UTF8.GetBytes(data))).Wait();
// }
public void SetProxy(ApiProxy proxy)
{
throw new NotImplementedException();
}
// public void SetProxy(ApiProxy proxy)
// {
// throw new NotImplementedException();
// }
public void InvokeError(Exception error)
{
OnError?.Invoke(error);
}
public Task ReconnectAsync() => Task.CompletedTask;
}
}
// public void InvokeError(Exception error)
// {
// OnError?.Invoke(error);
// }
// public Task ReconnectAsync() => Task.CompletedTask;
// }
//}
@@ -13,11 +13,11 @@ using CryptoExchange.Net.Sockets;
using CryptoExchange.Net.UnitTests.TestImplementations.Sockets;
using Microsoft.Extensions.Logging;
using Moq;
using Newtonsoft.Json.Linq;
using CryptoExchange.Net.Testing.Implementations;
namespace CryptoExchange.Net.UnitTests.TestImplementations
{
public class TestSocketClient: BaseSocketClient
internal class TestSocketClient: BaseSocketClient
{
public TestSubSocketClient SubClient { get; }
@@ -41,12 +41,12 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations
SubClient = AddApiClient(new TestSubSocketClient(options, options.SubOptions));
SubClient.SocketFactory = new Mock<IWebsocketFactory>().Object;
Mock.Get(SubClient.SocketFactory).Setup(f => f.CreateWebsocket(It.IsAny<ILogger>(), It.IsAny<WebSocketParameters>())).Returns(new TestSocket());
Mock.Get(SubClient.SocketFactory).Setup(f => f.CreateWebsocket(It.IsAny<ILogger>(), It.IsAny<WebSocketParameters>())).Returns(new TestSocket("https://test.com"));
}
public TestSocket CreateSocket()
{
Mock.Get(SubClient.SocketFactory).Setup(f => f.CreateWebsocket(It.IsAny<ILogger>(), It.IsAny<WebSocketParameters>())).Returns(new TestSocket());
Mock.Get(SubClient.SocketFactory).Setup(f => f.CreateWebsocket(It.IsAny<ILogger>(), It.IsAny<WebSocketParameters>())).Returns(new TestSocket("https://test.com"));
return (TestSocket)SubClient.CreateSocketInternal("https://localhost:123/");
}
@@ -75,6 +75,7 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations
public class TestSubSocketClient : SocketApiClient
{
private MessagePath _channelPath = MessagePath.Get().Property("channel");
private MessagePath _actionPath = MessagePath.Get().Property("action");
private MessagePath _topicPath = MessagePath.Get().Property("topic");
public Subscription TestSubscription { get; private set; } = null;
@@ -110,7 +111,7 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations
var id = message.GetValue<string>(_channelPath);
id ??= message.GetValue<string>(_topicPath);
return id;
return message.GetValue<string>(_actionPath) + "-" + id;
}
public Task<CallResult<UpdateSubscription>> SubscribeToSomethingAsync(string channel, Action<DataEvent<string>> onUpdate, CancellationToken ct)
+1 -1
View File
@@ -228,7 +228,7 @@ namespace CryptoExchange.Net.Clients
uriParameters,
bodyParameters,
additionalHeaders);
_logger.RestApiSendRequest(request.RequestId, definition, request.Content, request.Uri.Query, string.Join(", ", request.GetHeaders().Select(h => h.Key + $"=[{string.Join(",", h.Value)}]")));
_logger.RestApiSendRequest(request.RequestId, definition, request.Content, string.IsNullOrEmpty(request.Uri.Query) ? "-" : request.Uri.Query, string.Join(", ", request.GetHeaders().Select(h => h.Key + $"=[{string.Join(",", h.Value)}]")));
TotalRequestsMade++;
var result = await GetResponseAsync<T>(request, definition.RateLimitGate, cancellationToken).ConfigureAwait(false);
if (!result)
@@ -189,7 +189,10 @@ namespace CryptoExchange.Net.Clients
return new CallResult<UpdateSubscription>(new InvalidOperationError("Client disposed, can't subscribe"));
if (subscription.Authenticated && AuthenticationProvider == null)
{
_logger.LogWarning("Failed to subscribe, private subscription but no API credentials set");
return new CallResult<UpdateSubscription>(new NoApiCredentialsError());
}
SocketConnection socketConnection;
var released = false;
@@ -251,7 +254,7 @@ namespace CryptoExchange.Net.Clients
return new CallResult<UpdateSubscription>(new ServerError("Socket is paused"));
}
var waitEvent = new ManualResetEvent(false);
var waitEvent = new AsyncResetEvent(false);
var subQuery = subscription.GetSubQuery(socketConnection);
if (subQuery != null)
{
@@ -269,7 +272,7 @@ namespace CryptoExchange.Net.Clients
{
_logger.FailedToSubscribe(socketConnection.SocketId, subResult.Error?.ToString());
// If this was a timeout we still need to send an unsubscribe to prevent messages coming in later
await socketConnection.CloseAsync(subscription, isTimeout).ConfigureAwait(false);
await socketConnection.CloseAsync(subscription).ConfigureAwait(false);
return new CallResult<UpdateSubscription>(subResult.Error!);
}
}
@@ -786,9 +789,10 @@ namespace CryptoExchange.Net.Clients
/// <summary>
/// Preprocess a stream message
/// </summary>
/// <param name="connection"></param>
/// <param name="type"></param>
/// <param name="data"></param>
/// <returns></returns>
public virtual ReadOnlyMemory<byte> PreprocessStreamMessage(WebSocketMessageType type, ReadOnlyMemory<byte> data) => data;
public virtual ReadOnlyMemory<byte> PreprocessStreamMessage(SocketConnection connection, WebSocketMessageType type, ReadOnlyMemory<byte> data) => data;
}
}
@@ -32,6 +32,7 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
public ArrayPropertyAttribute ArrayProperty { get; set; } = null!;
public Type? JsonConverterType { get; set; }
public bool DefaultDeserialization { get; set; }
public Type TargetType { get; set; } = null!;
}
private class ArrayConverterInner<T> : JsonConverter<T>
@@ -70,7 +71,8 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
ArrayProperty = att,
PropertyInfo = property,
DefaultDeserialization = property.GetCustomAttribute<JsonConversionAttribute>() != null,
JsonConverterType = property.GetCustomAttribute<JsonConverterAttribute>()?.ConverterType
JsonConverterType = property.GetCustomAttribute<JsonConverterAttribute>()?.ConverterType,
TargetType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType
});
}
@@ -94,10 +96,12 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
var attribute = attributes.SingleOrDefault(a => a.ArrayProperty.Index == index);
if (attribute == null)
{
index++;
continue;
}
var targetType = attribute.PropertyInfo.PropertyType;
var targetType = attribute.TargetType;
object? value = null;
if (attribute.JsonConverterType != null)
{
@@ -124,7 +128,7 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
};
}
attribute.PropertyInfo.SetValue(result, value == null ? null : Convert.ChangeType(value, attribute.PropertyInfo.PropertyType, CultureInfo.InvariantCulture));
attribute.PropertyInfo.SetValue(result, value == null ? null : Convert.ChangeType(value, targetType, CultureInfo.InvariantCulture));
index++;
}
@@ -0,0 +1,31 @@
using System;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace CryptoExchange.Net.Converters.SystemTextJson
{
/// <summary>
/// Read string or number as string
/// </summary>
public class NumberStringConverter : JsonConverter<string?>
{
/// <inheritdoc />
public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
return null;
if (reader.TokenType == JsonTokenType.Number)
return reader.GetInt64().ToString();
return reader.GetString();
}
/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, string? value, JsonSerializerOptions options)
{
writer.WriteStringValue(value);
}
}
}
@@ -242,6 +242,7 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
{
_stream?.Dispose();
_stream = null;
_document?.Dispose();
_document = null;
}
@@ -261,6 +262,14 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
try
{
var firstByte = data.Span[0];
if (firstByte != 0x7b && firstByte != 0x5b)
{
// Value doesn't start with `{` or `[`, prevent deserialization attempt as it's slow
IsJson = false;
return new CallResult(new ServerError("Not a json value"));
}
_document = JsonDocument.Parse(data);
IsJson = true;
return new CallResult(null);
@@ -289,6 +298,7 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
public override void Clear()
{
_bytes = null;
_document?.Dispose();
_document = null;
}
}
+4 -5
View File
@@ -6,9 +6,9 @@
<PackageId>CryptoExchange.Net</PackageId>
<Authors>JKorf</Authors>
<Description>CryptoExchange.Net is a base library which is used to implement different cryptocurrency (exchange) API's. It provides a standardized way of implementing different API's, which results in a very similar experience for users of the API implementations.</Description>
<PackageVersion>7.8.0</PackageVersion>
<AssemblyVersion>7.8.0</AssemblyVersion>
<FileVersion>7.8.0</FileVersion>
<PackageVersion>7.10.0</PackageVersion>
<AssemblyVersion>7.10.0</AssemblyVersion>
<FileVersion>7.10.0</FileVersion>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageTags>OKX;OKX.Net;Mexc;Mexc.Net;Kucoin;Kucoin.Net;Kraken;Kraken.Net;Huobi;Huobi.Net;CoinEx;CoinEx.Net;Bybit;Bybit.Net;Bitget;Bitget.Net;Bitfinex;Bitfinex.Net;Binance;Binance.Net;CryptoCurrency;CryptoCurrency Exchange</PackageTags>
<RepositoryType>git</RepositoryType>
@@ -53,12 +53,11 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="System.Text.Json" Version="8.0.3" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>
</Project>
+18 -1
View File
@@ -453,7 +453,7 @@ namespace CryptoExchange.Net
}
/// <summary>
/// Decompress using Gzip
/// Decompress using GzipStream
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
@@ -467,6 +467,23 @@ namespace CryptoExchange.Net
deflateStream.CopyTo(decompressedStream);
return new ReadOnlyMemory<byte>(decompressedStream.GetBuffer(), 0, (int)decompressedStream.Length);
}
/// <summary>
/// Decompress using DeflateStream
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static ReadOnlyMemory<byte> Decompress(this ReadOnlyMemory<byte> input)
{
var output = new MemoryStream();
using (var compressStream = new MemoryStream(input.ToArray()))
using (var decompressor = new DeflateStream(compressStream, CompressionMode.Decompress))
decompressor.CopyTo(output);
output.Position = 0;
return new ReadOnlyMemory<byte>(output.GetBuffer(), 0, (int)output.Length);
}
}
}
@@ -3,6 +3,7 @@ using CryptoExchange.Net.Objects.Sockets;
using CryptoExchange.Net.Sockets;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace CryptoExchange.Net.Interfaces
{
@@ -25,7 +26,7 @@ namespace CryptoExchange.Net.Interfaces
/// <param name="connection"></param>
/// <param name="message"></param>
/// <returns></returns>
CallResult Handle(SocketConnection connection, DataEvent<object> message);
Task<CallResult> Handle(SocketConnection connection, DataEvent<object> message);
/// <summary>
/// Get the type the message should be deserialized to
/// </summary>
+2 -2
View File
@@ -17,7 +17,7 @@ namespace CryptoExchange.Net.Interfaces
/// <summary>
/// Websocket message received event
/// </summary>
event Action<WebSocketMessageType, ReadOnlyMemory<byte>> OnStreamMessage;
event Func<WebSocketMessageType, ReadOnlyMemory<byte>, Task> OnStreamMessage;
/// <summary>
/// Websocket sent event, RequestId as parameter
/// </summary>
@@ -78,7 +78,7 @@ namespace CryptoExchange.Net.Interfaces
/// <param name="id"></param>
/// <param name="data"></param>
/// <param name="weight"></param>
void Send(int id, string data, int weight);
bool Send(int id, string data, int weight);
/// <summary>
/// Reconnect the socket
/// </summary>
@@ -3,7 +3,8 @@ using System;
namespace CryptoExchange.Net.Logging.Extensions
{
internal static class CryptoExchangeWebSocketClientLoggingExtension
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
public static class CryptoExchangeWebSocketClientLoggingExtension
{
private static readonly Action<ILogger, int, Exception?> _connecting;
private static readonly Action<ILogger, int, string, Exception?> _connectionFailed;
@@ -151,7 +152,7 @@ namespace CryptoExchange.Net.Logging.Extensions
"[Sckt {SocketId}] discarding incomplete message of {NumBytes} bytes");
_receiveLoopStoppedWithException = LoggerMessage.Define<int>(
LogLevel.Warning,
LogLevel.Error,
new EventId(1024, "ReceiveLoopStoppedWithException"),
"[Sckt {SocketId}] receive loop stopped with exception");
@@ -3,7 +3,8 @@ using System;
namespace CryptoExchange.Net.Logging.Extensions
{
internal static class RateLimitGateLoggingExtensions
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
public static class RateLimitGateLoggingExtensions
{
private static readonly Action<ILogger, int, string, string, string, Exception?> _rateLimitRequestFailed;
private static readonly Action<ILogger, int, string, string, Exception?> _rateLimitConnectionFailed;
@@ -6,7 +6,8 @@ using System.Net.Http;
namespace CryptoExchange.Net.Logging.Extensions
{
internal static class RestApiClientLoggingExtensions
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
public static class RestApiClientLoggingExtensions
{
private static readonly Action<ILogger, int?, int?, long, string?, Exception?> _restApiErrorReceived;
private static readonly Action<ILogger, int?, int?, long, string?, Exception?> _restApiResponseReceived;
@@ -3,7 +3,8 @@ using System;
namespace CryptoExchange.Net.Logging.Extensions
{
internal static class SocketApiClientLoggingExtension
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
public static class SocketApiClientLoggingExtension
{
private static readonly Action<ILogger, int, Exception?> _failedToAddSubscriptionRetryOnDifferentConnection;
private static readonly Action<ILogger, int, Exception?> _hasBeenPausedCantSubscribeAtThisMoment;
@@ -4,7 +4,8 @@ using Microsoft.Extensions.Logging;
namespace CryptoExchange.Net.Logging.Extensions
{
internal static class SocketConnectionLoggingExtension
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
public static class SocketConnectionLoggingExtension
{
private static readonly Action<ILogger, int, bool, Exception?> _activityPaused;
private static readonly Action<ILogger, int, Sockets.SocketConnection.SocketStatus, Sockets.SocketConnection.SocketStatus, Exception?> _socketStatusChanged;
@@ -4,7 +4,9 @@ using Microsoft.Extensions.Logging;
namespace CryptoExchange.Net.Logging.Extensions
{
internal static class SymbolOrderBookLoggingExtensions
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
public static class SymbolOrderBookLoggingExtensions
{
private static readonly Action<ILogger, string, string, OrderBookStatus, OrderBookStatus, Exception?> _orderBookStatusChanged;
private static readonly Action<ILogger, string, string, Exception?> _orderBookStarting;
+22
View File
@@ -273,6 +273,28 @@ namespace CryptoExchange.Net.Objects
return new WebCallResult(ResponseStatusCode, ResponseHeaders, ResponseTime, RequestId, RequestUrl, RequestBody, RequestMethod, RequestHeaders, error);
}
/// <summary>
/// Copy the WebCallResult to a new data type
/// </summary>
/// <typeparam name="K">The new type</typeparam>
/// <param name="data">The data of the new type</param>
/// <returns></returns>
public WebCallResult<K> As<K>([AllowNull] K data)
{
return new WebCallResult<K>(ResponseStatusCode, ResponseHeaders, ResponseTime, 0, null, RequestId, RequestUrl, RequestBody, RequestMethod, RequestHeaders, ResultDataSource.Server, data, Error);
}
/// <summary>
/// Copy the WebCallResult to a new data type
/// </summary>
/// <typeparam name="K">The new type</typeparam>
/// <param name="error">The error returned</param>
/// <returns></returns>
public WebCallResult<K> AsError<K>(Error error)
{
return new WebCallResult<K>(ResponseStatusCode, ResponseHeaders, ResponseTime, 0, null, RequestId, RequestUrl, RequestBody, RequestMethod, RequestHeaders, ResultDataSource.Server, default, error);
}
/// <inheritdoc />
public override string ToString()
{
@@ -149,6 +149,27 @@ namespace CryptoExchange.Net.Objects
Add(key, DateTimeConverter.ConvertToSeconds(value));
}
/// <summary>
/// Add a datetime value as string seconds timestamp
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void AddSecondsString(string key, DateTime value)
{
Add(key, DateTimeConverter.ConvertToSeconds(value).ToString());
}
/// <summary>
/// Add a datetime value as string seconds timestamp. Not added if value is null
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void AddOptionalSecondsString(string key, DateTime? value)
{
if (value != null)
Add(key, DateTimeConverter.ConvertToSeconds(value).ToString());
}
/// <summary>
/// Add an enum value as the string value as mapped using the <see cref="MapAttribute" />
/// </summary>
@@ -167,7 +188,7 @@ namespace CryptoExchange.Net.Objects
public void AddEnumAsInt<T>(string key, T value)
{
var stringVal = EnumConverter.GetString(value);
Add(key, EnumConverter.GetString(int.Parse(stringVal))!);
Add(key, int.Parse(stringVal)!);
}
/// <summary>
@@ -144,5 +144,11 @@ namespace CryptoExchange.Net.Objects.Sockets
{
return new CallResult<K>(default, OriginalData, error);
}
/// <inheritdoc />
public override string ToString()
{
return $"{StreamId} - {(Symbol == null ? "" : (Symbol + " - "))}{(UpdateType == null ? "" : (UpdateType + " - "))}{Data}";
}
}
}
@@ -810,7 +810,7 @@ namespace CryptoExchange.Net.OrderBook
{
if (lastUpdateId <= LastSequenceNumber)
{
_logger.OrderBookUpdateSkipped(Api, Symbol, firstUpdateId, lastUpdateId);
_logger.OrderBookUpdateSkipped(Api, Symbol, lastUpdateId, LastSequenceNumber);
return;
}
@@ -108,7 +108,7 @@ namespace CryptoExchange.Net.Sockets
public event Func<Task>? OnClose;
/// <inheritdoc />
public event Action<WebSocketMessageType, ReadOnlyMemory<byte>>? OnStreamMessage;
public event Func<WebSocketMessageType, ReadOnlyMemory<byte>, Task>? OnStreamMessage;
/// <inheritdoc />
public event Func<int, Task>? OnRequestSent;
@@ -245,7 +245,8 @@ namespace CryptoExchange.Net.Sockets
await Task.Delay(50).ConfigureAwait(false);
await _closeTask.ConfigureAwait(false);
_closeTask = null;
if (!_stopRequested)
_closeTask = null;
if (Parameters.ReconnectPolicy == ReconnectPolicy.Disabled)
{
@@ -322,15 +323,16 @@ namespace CryptoExchange.Net.Sockets
}
/// <inheritdoc />
public virtual void Send(int id, string data, int weight)
public virtual bool Send(int id, string data, int weight)
{
if (_ctsSource.IsCancellationRequested)
return;
if (_ctsSource.IsCancellationRequested || _processState != ProcessState.Processing)
return false;
var bytes = Parameters.Encoding.GetBytes(data);
_logger.SocketAddingBytesToSendBuffer(Id, id, bytes);
_sendBuffer.Enqueue(new SendItem { Id = id, Weight = weight, Bytes = bytes });
_sendEvent.Set();
return true;
}
/// <inheritdoc />
@@ -389,9 +391,7 @@ namespace CryptoExchange.Net.Sockets
if (_disposed)
return;
//_closeState = CloseState.Closing;
_ctsSource.Cancel();
_sendEvent.Set();
if (_socket.State == WebSocketState.Open)
{
@@ -436,6 +436,7 @@ namespace CryptoExchange.Net.Sockets
_disposed = true;
_socket.Dispose();
_ctsSource?.Dispose();
_sendEvent.Dispose();
_logger.SocketDisposed(Id);
}
@@ -450,10 +451,15 @@ namespace CryptoExchange.Net.Sockets
{
while (true)
{
if (_ctsSource.IsCancellationRequested)
try
{
if (!_sendBuffer.Any())
await _sendEvent.WaitAsync(ct: _ctsSource.Token).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
break;
await _sendEvent.WaitAsync().ConfigureAwait(false);
}
if (_ctsSource.IsCancellationRequested)
break;
@@ -507,7 +513,8 @@ namespace CryptoExchange.Net.Sockets
// Make sure we at least let the owner know there was an error
_logger.SocketSendLoopStoppedWithException(Id, e.Message, e);
await (OnError?.Invoke(e) ?? Task.CompletedTask).ConfigureAwait(false);
throw;
if (_closeTask?.IsCompleted != false)
_closeTask = CloseInternalAsync();
}
finally
{
@@ -582,7 +589,7 @@ namespace CryptoExchange.Net.Sockets
{
// Received a complete message and it's not multi part
_logger.SocketReceivedSingleMessage(Id, receiveResult.Count);
ProcessData(receiveResult.MessageType, new ReadOnlyMemory<byte>(buffer.Array, buffer.Offset, receiveResult.Count));
await ProcessData(receiveResult.MessageType, new ReadOnlyMemory<byte>(buffer.Array, buffer.Offset, receiveResult.Count)).ConfigureAwait(false);
}
else
{
@@ -617,7 +624,7 @@ namespace CryptoExchange.Net.Sockets
{
_logger.SocketReassembledMessage(Id, multipartStream!.Length);
// Get the underlying buffer of the memorystream holding the written data and delimit it (GetBuffer return the full array, not only the written part)
ProcessData(receiveResult.MessageType, new ReadOnlyMemory<byte>(multipartStream.GetBuffer(), 0, (int)multipartStream.Length));
await ProcessData(receiveResult.MessageType, new ReadOnlyMemory<byte>(multipartStream.GetBuffer(), 0, (int)multipartStream.Length)).ConfigureAwait(false);
}
else
{
@@ -633,7 +640,8 @@ namespace CryptoExchange.Net.Sockets
// Make sure we at least let the owner know there was an error
_logger.SocketReceiveLoopStoppedWithException(Id, e);
await (OnError?.Invoke(e) ?? Task.CompletedTask).ConfigureAwait(false);
throw;
if (_closeTask?.IsCompleted != false)
_closeTask = CloseInternalAsync();
}
finally
{
@@ -647,10 +655,10 @@ namespace CryptoExchange.Net.Sockets
/// <param name="type"></param>
/// <param name="data"></param>
/// <returns></returns>
protected void ProcessData(WebSocketMessageType type, ReadOnlyMemory<byte> data)
protected async Task ProcessData(WebSocketMessageType type, ReadOnlyMemory<byte> data)
{
LastActionTime = DateTime.UtcNow;
OnStreamMessage?.Invoke(type, data);
await (OnStreamMessage?.Invoke(type, data) ?? Task.CompletedTask).ConfigureAwait(false);
}
/// <summary>
@@ -691,7 +699,6 @@ namespace CryptoExchange.Net.Sockets
// any exception here will stop the timeout checking, but do so silently unless the socket get's stopped.
// Make sure we at least let the owner know there was an error
await (OnError?.Invoke(e) ?? Task.CompletedTask).ConfigureAwait(false);
throw;
}
}
@@ -716,10 +723,14 @@ namespace CryptoExchange.Net.Sockets
var checkTime = DateTime.UtcNow;
if (checkTime - _lastReceivedMessagesUpdate > TimeSpan.FromSeconds(1))
{
foreach (var msg in _receivedMessages.ToList()) // To list here because we're removing from the list
for (var i = 0; i < _receivedMessages.Count; i++)
{
var msg = _receivedMessages[i];
if (checkTime - msg.Timestamp > TimeSpan.FromSeconds(3))
{
_receivedMessages.Remove(msg);
i--;
}
}
_lastReceivedMessagesUpdate = checkTime;
+33 -9
View File
@@ -24,6 +24,17 @@ namespace CryptoExchange.Net.Sockets
/// </summary>
public bool Completed { get; set; }
/// <summary>
/// The number of required responses. Can be more than 1 when for example subscribing multiple symbols streams in a single request,
/// and each symbol receives it's own confirmation response
/// </summary>
public int RequiredResponses { get; set; } = 1;
/// <summary>
/// The current number of responses received on this query
/// </summary>
public int CurrentResponses { get; set; }
/// <summary>
/// Timestamp of when the request was send
/// </summary>
@@ -42,7 +53,7 @@ namespace CryptoExchange.Net.Sockets
/// <summary>
/// Wait event for the calling message processing thread
/// </summary>
public ManualResetEvent? ContinueAwaiter { get; set; }
public AsyncResetEvent? ContinueAwaiter { get; set; }
/// <summary>
/// Strings to match this query to a received message
@@ -108,7 +119,7 @@ namespace CryptoExchange.Net.Sockets
}
/// <summary>
/// Wait untill timeout or the request is competed
/// Wait until timeout or the request is completed
/// </summary>
/// <param name="timeout"></param>
/// <param name="ct">Cancellation token</param>
@@ -135,7 +146,7 @@ namespace CryptoExchange.Net.Sockets
/// <param name="message"></param>
/// <param name="connection"></param>
/// <returns></returns>
public abstract CallResult Handle(SocketConnection connection, DataEvent<object> message);
public abstract Task<CallResult> Handle(SocketConnection connection, DataEvent<object> message);
}
@@ -165,13 +176,26 @@ namespace CryptoExchange.Net.Sockets
}
/// <inheritdoc />
public override CallResult Handle(SocketConnection connection, DataEvent<object> message)
public override async Task<CallResult> Handle(SocketConnection connection, DataEvent<object> message)
{
Completed = true;
Response = message.Data;
Result = HandleMessage(connection, message.As((TServerResponse)message.Data));
_event.Set();
ContinueAwaiter?.WaitOne();
CurrentResponses++;
if (CurrentResponses == RequiredResponses)
{
Completed = true;
Response = message.Data;
}
if (Result?.Success != false)
// If an error result is already set don't override that
Result = HandleMessage(connection, message.As((TServerResponse)message.Data));
if (CurrentResponses == RequiredResponses)
{
_event.Set();
if (ContinueAwaiter != null)
await ContinueAwaiter.WaitAsync().ConfigureAwait(false);
}
return Result;
}
+40 -30
View File
@@ -413,14 +413,14 @@ namespace CryptoExchange.Net.Sockets
/// <param name="data"></param>
/// <param name="type"></param>
/// <returns></returns>
protected virtual void HandleStreamMessage(WebSocketMessageType type, ReadOnlyMemory<byte> data)
protected virtual async Task HandleStreamMessage(WebSocketMessageType type, ReadOnlyMemory<byte> data)
{
var sw = Stopwatch.StartNew();
var receiveTime = DateTime.UtcNow;
string? originalData = null;
// 1. Decrypt/Preprocess if necessary
data = ApiClient.PreprocessStreamMessage(type, data);
data = ApiClient.PreprocessStreamMessage(this, type, data);
// 2. Read data into accessor
_accessor.Read(data);
@@ -507,7 +507,9 @@ namespace CryptoExchange.Net.Sockets
try
{
var innerSw = Stopwatch.StartNew();
processor.Handle(this, new DataEvent<object>(deserialized, null, null, originalData, receiveTime, null));
await processor.Handle(this, new DataEvent<object>(deserialized, null, null, originalData, receiveTime, null)).ConfigureAwait(false);
if (processor is Query query && query.RequiredResponses != 1)
_logger.LogDebug($"[Sckt {SocketId}] [Req {query.Id}] responses: {query.CurrentResponses}/{query.RequiredResponses}");
totalUserTime += (int)innerSw.ElapsedMilliseconds;
}
catch (Exception ex)
@@ -573,9 +575,8 @@ namespace CryptoExchange.Net.Sockets
/// Close a subscription on this connection. If all subscriptions on this connection are closed the connection gets closed as well
/// </summary>
/// <param name="subscription">Subscription to close</param>
/// <param name="unsubEvenIfNotConfirmed">Whether to send an unsub request even if the subscription wasn't confirmed</param>
/// <returns></returns>
public async Task CloseAsync(Subscription subscription, bool unsubEvenIfNotConfirmed = false)
public async Task CloseAsync(Subscription subscription)
{
subscription.Closed = true;
@@ -596,7 +597,7 @@ namespace CryptoExchange.Net.Sockets
lock (_listenersLock)
needUnsub = _listeners.Contains(subscription);
if (needUnsub && (unsubEvenIfNotConfirmed || subscription.Confirmed) && _socket.IsOpen)
if (needUnsub && _socket.IsOpen)
await UnsubscribeAsync(subscription).ConfigureAwait(false);
}
else
@@ -697,7 +698,7 @@ namespace CryptoExchange.Net.Sockets
/// <param name="continueEvent">Wait event for when the socket message handler can continue</param>
/// <param name="ct">Cancellation token</param>
/// <returns></returns>
public virtual async Task<CallResult> SendAndWaitQueryAsync(Query query, ManualResetEvent? continueEvent = null, CancellationToken ct = default)
public virtual async Task<CallResult> SendAndWaitQueryAsync(Query query, AsyncResetEvent? continueEvent = null, CancellationToken ct = default)
{
await SendAndWaitIntAsync(query, continueEvent, ct).ConfigureAwait(false);
return query.Result ?? new CallResult(new ServerError("Timeout"));
@@ -712,13 +713,13 @@ namespace CryptoExchange.Net.Sockets
/// <param name="continueEvent">Wait event for when the socket message handler can continue</param>
/// <param name="ct">Cancellation token</param>
/// <returns></returns>
public virtual async Task<CallResult<THandlerResponse>> SendAndWaitQueryAsync<TServerResponse, THandlerResponse>(Query<TServerResponse, THandlerResponse> query, ManualResetEvent? continueEvent = null, CancellationToken ct = default)
public virtual async Task<CallResult<THandlerResponse>> SendAndWaitQueryAsync<TServerResponse, THandlerResponse>(Query<TServerResponse, THandlerResponse> query, AsyncResetEvent? continueEvent = null, CancellationToken ct = default)
{
await SendAndWaitIntAsync(query, continueEvent, ct).ConfigureAwait(false);
return query.TypedResult ?? new CallResult<THandlerResponse>(new ServerError("Timeout"));
}
private async Task SendAndWaitIntAsync(Query query, ManualResetEvent? continueEvent, CancellationToken ct = default)
private async Task SendAndWaitIntAsync(Query query, AsyncResetEvent? continueEvent, CancellationToken ct = default)
{
lock(_listenersLock)
_listeners.Add(query);
@@ -802,7 +803,9 @@ namespace CryptoExchange.Net.Sockets
_logger.SendingData(SocketId, requestId, data);
try
{
_socket.Send(requestId, data, weight);
if (!_socket.Send(requestId, data, weight))
return new CallResult(new WebError("Failed to send message, connection not open"));
return new CallResult(null);
}
catch(Exception ex)
@@ -832,7 +835,11 @@ namespace CryptoExchange.Net.Sockets
bool anyAuthenticated;
lock (_listenersLock)
anyAuthenticated = _listeners.OfType<Subscription>().Any(s => s.Authenticated) || DedicatedRequestConnection;
{
anyAuthenticated = _listeners.OfType<Subscription>().Any(s => s.Authenticated)
|| (DedicatedRequestConnection && ApiClient.AuthenticationProvider != null);
}
if (anyAuthenticated)
{
// If we reconnected a authenticated connection we need to re-authenticate
@@ -847,36 +854,37 @@ namespace CryptoExchange.Net.Sockets
_logger.AuthenticationSucceeded(SocketId);
}
// Get a list of all subscriptions on the socket
List<Subscription> subList;
lock (_listenersLock)
subList = _listeners.OfType<Subscription>().ToList();
foreach(var subscription in subList)
{
subscription.ConnectionInvocations = 0;
var result = await ApiClient.RevitalizeRequestAsync(subscription).ConfigureAwait(false);
if (!result)
{
_logger.FailedRequestRevitalization(SocketId, result.Error?.ToString());
return result;
}
}
// Foreach subscription which is subscribed by a subscription request we will need to resend that request to resubscribe
for (var i = 0; i < subList.Count; i += ApiClient.ClientOptions.MaxConcurrentResubscriptionsPerSocket)
int batch = 0;
int batchSize = ApiClient.ClientOptions.MaxConcurrentResubscriptionsPerSocket;
while (true)
{
if (!_socket.IsOpen)
return new CallResult(new WebError("Socket not connected"));
List<Subscription> subList;
lock (_listenersLock)
subList = _listeners.OfType<Subscription>().Skip(batch * batchSize).Take(batchSize).ToList();
if (subList.Count == 0)
break;
var taskList = new List<Task<CallResult>>();
foreach (var subscription in subList.Skip(i).Take(ApiClient.ClientOptions.MaxConcurrentResubscriptionsPerSocket))
foreach (var subscription in subList)
{
subscription.ConnectionInvocations = 0;
var result = await ApiClient.RevitalizeRequestAsync(subscription).ConfigureAwait(false);
if (!result)
{
_logger.FailedRequestRevitalization(SocketId, result.Error?.ToString());
return result;
}
var subQuery = subscription.GetSubQuery(this);
if (subQuery == null)
continue;
var waitEvent = new ManualResetEvent(false);
var waitEvent = new AsyncResetEvent(false);
taskList.Add(SendAndWaitQueryAsync(subQuery, waitEvent).ContinueWith((r) =>
{
subscription.HandleSubQueryResponse(subQuery.Response!);
@@ -890,6 +898,8 @@ namespace CryptoExchange.Net.Sockets
await Task.WhenAll(taskList).ConfigureAwait(false);
if (taskList.Any(t => !t.Result.Success))
return taskList.First(t => !t.Result.Success).Result;
batch++;
}
if (!_socket.IsOpen)
+3 -2
View File
@@ -5,6 +5,7 @@ using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace CryptoExchange.Net.Sockets
{
@@ -122,11 +123,11 @@ namespace CryptoExchange.Net.Sockets
/// <param name="connection"></param>
/// <param name="message"></param>
/// <returns></returns>
public CallResult Handle(SocketConnection connection, DataEvent<object> message)
public Task<CallResult> Handle(SocketConnection connection, DataEvent<object> message)
{
ConnectionInvocations++;
TotalInvocations++;
return DoHandleMessage(connection, message);
return Task.FromResult(DoHandleMessage(connection, message));
}
/// <summary>
@@ -75,7 +75,10 @@ namespace CryptoExchange.Net.Testing.Comparers
var enumerator = list.GetEnumerator();
foreach (var jObj in jObjs)
{
enumerator.MoveNext();
if (!enumerator.MoveNext())
{
}
if (jObj.Type == JTokenType.Object)
{
foreach (var subProp in ((JObject)jObj).Properties())
@@ -272,6 +275,67 @@ namespace CryptoExchange.Net.Testing.Comparers
}
}
}
else if (propValue.Type == JTokenType.Array)
{
var jObjs = (JArray)propValue;
if (propertyValue is IEnumerable list)
{
var enumerator = list.GetEnumerator();
foreach (var jObj in jObjs)
{
if (!enumerator.MoveNext())
{
}
if (jObj.Type == JTokenType.Object)
{
foreach (var subProp in ((JObject)jObj).Properties())
{
if (ignoreProperties?.Contains(subProp.Name) == true)
continue;
CheckObject(method, subProp, enumerator.Current, ignoreProperties!);
}
}
else if (jObj.Type == JTokenType.Array)
{
var resultObj = enumerator.Current;
var resultProps = resultObj.GetType().GetProperties().Select(p => (p, p.GetCustomAttributes(typeof(ArrayPropertyAttribute), true).Cast<ArrayPropertyAttribute>().SingleOrDefault()));
var arrayConverterProperty = resultObj.GetType().GetCustomAttributes(typeof(JsonConverterAttribute), true).FirstOrDefault();
var jsonConverter = ((JsonConverterAttribute)arrayConverterProperty!).ConverterType;
if (jsonConverter != typeof(ArrayConverter))
// Not array converter?
continue;
int i = 0;
foreach (var item in jObj.Values())
{
var arrayProp = resultProps.SingleOrDefault(p => p.Item2!.Index == i).p;
if (arrayProp != null)
CheckPropertyValue(method, item, arrayProp.GetValue(resultObj), arrayProp.PropertyType, arrayProp.Name, "Array index " + i, ignoreProperties!);
i++;
}
}
else
{
var value = enumerator.Current;
if (value == default && ((JValue)jObj).Type != JTokenType.Null)
throw new Exception($"{method}: Array has no value while input json array has value {jObj}");
}
}
}
else
{
var resultProps = propertyValue.GetType().GetProperties().Select(p => (p, p.GetCustomAttributes(typeof(ArrayPropertyAttribute), true).Cast<ArrayPropertyAttribute>().SingleOrDefault()));
int i = 0;
foreach (var item in jObjs.Children())
{
var arrayProp = resultProps.Where(p => p.Item2 != null).SingleOrDefault(p => p.Item2!.Index == i).p;
if (arrayProp != null)
CheckPropertyValue(method, item, arrayProp.GetValue(propertyValue), arrayProp.PropertyType, arrayProp.Name, "Array index " + i, ignoreProperties!);
i++;
}
}
}
else
{
CheckValues(method, propertyName!, propertyType, (JValue)propValue, propertyValue);
@@ -307,7 +371,7 @@ namespace CryptoExchange.Net.Testing.Comparers
if (objectValue is DateTime time)
{
if (time != DateTimeConverter.ParseFromDouble(jsonValue.Value<long>()!))
throw new Exception($"{method}: {property} not equal: {jsonValue.Value<decimal>()} vs {time}");
throw new Exception($"{method}: {property} not equal: {DateTimeConverter.ParseFromDouble(jsonValue.Value<long>()!)} vs {time}");
}
else if (propertyType.IsEnum || Nullable.GetUnderlyingType(propertyType)?.IsEnum == true)
{
@@ -23,7 +23,7 @@ namespace CryptoExchange.Net.Testing.Implementations
public event Func<Exception, Task>? OnError;
#pragma warning restore 0067
public event Func<int, Task>? OnRequestSent;
public event Action<WebSocketMessageType, ReadOnlyMemory<byte>>? OnStreamMessage;
public event Func<WebSocketMessageType, ReadOnlyMemory<byte>, Task>? OnStreamMessage;
public event Func<Task>? OnOpen;
public int Id { get; }
@@ -33,9 +33,17 @@ namespace CryptoExchange.Net.Testing.Implementations
public Uri Uri { get; set; }
public Func<Task<Uri?>>? GetReconnectionUrl { get; set; }
public static int lastId = 0;
public static object lastIdLock = new object();
public TestSocket(string address)
{
Uri = new Uri(address);
lock (lastIdLock)
{
Id = lastId + 1;
lastId++;
}
}
public Task<CallResult> ConnectAsync()
@@ -44,13 +52,14 @@ namespace CryptoExchange.Net.Testing.Implementations
return Task.FromResult(CanConnect ? new CallResult(null) : new CallResult(new CantConnectError()));
}
public void Send(int requestId, string data, int weight)
public bool Send(int requestId, string data, int weight)
{
if (!Connected)
throw new Exception("Socket not connected");
OnRequestSent?.Invoke(requestId);
OnMessageSend?.Invoke(data);
return true;
}
public Task CloseAsync()
@@ -72,12 +81,12 @@ namespace CryptoExchange.Net.Testing.Implementations
public void InvokeMessage(string data)
{
OnStreamMessage?.Invoke(WebSocketMessageType.Text, new ReadOnlyMemory<byte>(Encoding.UTF8.GetBytes(data)));
OnStreamMessage?.Invoke(WebSocketMessageType.Text, new ReadOnlyMemory<byte>(Encoding.UTF8.GetBytes(data))).Wait();
}
public void InvokeMessage<T>(T data)
{
OnStreamMessage?.Invoke(WebSocketMessageType.Text, new ReadOnlyMemory<byte>(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data))));
OnStreamMessage?.Invoke(WebSocketMessageType.Text, new ReadOnlyMemory<byte>(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data)))).Wait();
}
public Task ReconnectAsync() => throw new NotImplementedException();
@@ -0,0 +1,100 @@
using CryptoExchange.Net.Objects;
using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace CryptoExchange.Net.Testing
{
/// <summary>
/// Base class for executing REST API integration tests
/// </summary>
/// <typeparam name="TClient">Client type</typeparam>
public abstract class RestIntergrationTest<TClient>
{
/// <summary>
/// Get a client instance
/// </summary>
/// <param name="loggerFactory"></param>
/// <returns></returns>
public abstract TClient GetClient(ILoggerFactory loggerFactory);
/// <summary>
/// Whether the test should be run. By default integration tests aren't executed, can be set to true to force execution.
/// </summary>
public virtual bool Run { get; set; }
/// <summary>
/// Whether API credentials are provided and thus authenticated calls can be executed. Should be set in the GetClient implementation.
/// </summary>
public bool Authenticated { get; set; }
/// <summary>
/// Create a client
/// </summary>
/// <returns></returns>
protected TClient CreateClient()
{
var fact = new LoggerFactory();
fact.AddProvider(new TraceLoggerProvider());
return GetClient(fact);
}
/// <summary>
/// Check if integration tests should be executed
/// </summary>
/// <returns></returns>
protected bool ShouldRun()
{
var integrationTests = Environment.GetEnvironmentVariable("INTEGRATION");
if (!Run && integrationTests != "1")
return false;
return true;
}
/// <summary>
/// Execute a REST endpoint call and check for any errors or warnings.
/// </summary>
/// <typeparam name="T">Type of response</typeparam>
/// <param name="expression">The call expression</param>
/// <param name="authRequest">Whether this is an authenticated request</param>
public async Task RunAndCheckResult<T>(Expression<Func<TClient, Task<WebCallResult<T>>>> expression, bool authRequest)
{
if (!ShouldRun())
return;
var client = CreateClient();
var expressionBody = (MethodCallExpression)expression.Body;
if (authRequest && !Authenticated)
{
Debug.WriteLine($"Skipping {expressionBody.Method.Name}, not authenticated");
return;
}
var listener = new EnumValueTraceListener();
Trace.Listeners.Add(listener);
WebCallResult<T> result;
try
{
result = await expression.Compile().Invoke(client).ConfigureAwait(false);
}
catch (Exception ex)
{
throw new Exception($"Method {expressionBody.Method.Name} threw an exception: " + ex.ToLogString());
}
finally
{
Trace.Listeners.Remove(listener);
}
if (!result.Success)
throw new Exception($"Method {expressionBody.Method.Name} returned error: " + result.Error);
Debug.WriteLine($"{expressionBody.Method.Name} {result}");
}
}
}
+23 -1
View File
@@ -1,4 +1,4 @@
# CryptoExchange.Net
# ![.CryptoExchange.Net](https://github.com/JKorf/CryptoExchange.Net/blob/ffcb7db8ff597c2f14982d68464015a748815580/CryptoExchange.Net/Icon/icon.png) CryptoExchange.Net
[![.NET](https://img.shields.io/github/actions/workflow/status/JKorf/CryptoExchange.Net/dotnet.yml?style=for-the-badge)](https://github.com/JKorf/CryptoExchange.Net/actions/workflows/dotnet.yml) [![Nuget downloads](https://img.shields.io/nuget/dt/CryptoExchange.Net.svg?style=for-the-badge)](https://www.nuget.org/packages/CryptoExchange.Net) ![License](https://img.shields.io/github/license/JKorf/CryptoExchange.Net?style=for-the-badge)
@@ -46,6 +46,28 @@ Make a one time donation in a crypto currency of your choice. If you prefer to d
Alternatively, sponsor me on Github using [Github Sponsors](https://github.com/sponsors/JKorf).
## Release notes
* Version 7.10.0 - 26 Jul 2024
* Added System.Text.Json NumberStringConverter
* Added integration testing base class
* Added AddSecondsString and AddOptionalSecondsString to ParameterCollection
* Added Decompress method for ReadOnlyMemory using non-GZip deflate
* Added SocketConnection parameter to SocketConnection PreprocessStreamMessage
* Fixed websocket reconnect/unsubscribe timing bug
* Fixed issue in System.Text.Json array object deserialization skipping property when skipping an index
* Fixed order book logging bug
* Fixed bug in ParameterCollection AddEnumAsInt
* Version 7.9.0 - 16 Jul 2024
* Added some checks in websocket connection handling
* Added As<T> and AsError<T> methods on untyped WebCallResult
* Updated System.Text.Json package to version 8.0.4 to fix vulnerability
* Updated websocket subscription response handling to remove the thread blocking ManualResetEvent usage
* Updated static logging classes access modifier from internal to public so they can be called in overriden methods
* Updated some testing object implementations
* Fixed authentication error when reconnecting an unauthenticated connection which was marked as dedicated query connection
* Small improvements in SystemTextJsonMessageAccessor
* Fixed System.Text.Json ArrayConverter implementation nullable value types handling
* Version 7.8.0 - 02 Jul 2024
* Updated single endpoint limit configuration
* Added LongConverter for nullable longs
+1 -1
View File
@@ -1389,7 +1389,7 @@ await client.UnsubscribeAllAsync();</code></pre>
============================ -->
<section id="idocs_common">
<h2>Common Clients</h2>
<p>The CryptoClients.Net client exposes some common client classes. These clients aim to make using the different API's easier.</p>
<p>The CryptoClients.Net library exposes two client classes. These clients aim to make using the different API's easier.</p>
<p><b>(I)ExchangeRestClient</b><br />
The <code>IExchangeRestClient</code> (or <code>ExchangeRestClient</code> when used directly) can be used to easily access REST clients for different API's.