diff --git a/CryptoExchange.Net.Protobuf/Converters/Protobuf/DynamicProtobufConverter.cs b/CryptoExchange.Net.Protobuf/Converters/Protobuf/DynamicProtobufConverter.cs deleted file mode 100644 index d9f93b4..0000000 --- a/CryptoExchange.Net.Protobuf/Converters/Protobuf/DynamicProtobufConverter.cs +++ /dev/null @@ -1,22 +0,0 @@ -//using CryptoExchange.Net.Converters.MessageParsing; -//using CryptoExchange.Net.Converters.MessageParsing.DynamicConverters; -//using CryptoExchange.Net.Objects; -//using LightProto; -//using System; -//using System.Collections.Generic; -//using System.Net.WebSockets; -//using System.Text; - -//namespace CryptoExchange.Net.Protobuf.Converters.Protobuf -//{ -// public abstract class DynamicProtobufConverter : ISocketMessageHandler -// { -// public object Deserialize(ReadOnlySpan data, Type type) -// { -// var result = Serializer.Deserialize(data); -// return result; -// } - -// public abstract string GetMessageIdentifier(ReadOnlySpan data, WebSocketMessageType? webSocketMessageType); -// } -//} diff --git a/CryptoExchange.Net/Converters/SystemTextJson/ArrayConverter.cs b/CryptoExchange.Net/Converters/SystemTextJson/ArrayConverter.cs index d45a41c..d3d611a 100644 --- a/CryptoExchange.Net/Converters/SystemTextJson/ArrayConverter.cs +++ b/CryptoExchange.Net/Converters/SystemTextJson/ArrayConverter.cs @@ -1,11 +1,12 @@ -using System; +using CryptoExchange.Net.Exceptions; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Linq; using System.Reflection; -using System.Text.Json.Serialization; using System.Text.Json; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; +using System.Text.Json.Serialization; using System.Threading; namespace CryptoExchange.Net.Converters.SystemTextJson @@ -109,7 +110,7 @@ namespace CryptoExchange.Net.Converters.SystemTextJson #endif { if (reader.TokenType != JsonTokenType.StartArray) - throw new Exception("Not an array"); + throw new CeDeserializationException("Not an array"); int index = 0; while (reader.Read()) @@ -158,7 +159,7 @@ namespace CryptoExchange.Net.Converters.SystemTextJson JsonTokenType.String => reader.GetString(), JsonTokenType.Number => reader.GetDecimal(), JsonTokenType.StartObject => JsonSerializer.Deserialize(ref reader, attribute.TargetType, options), - _ => throw new NotImplementedException($"Array deserialization of type {reader.TokenType} not supported"), + _ => throw new CeDeserializationException($"Array deserialization of type {reader.TokenType} not supported"), }; } diff --git a/CryptoExchange.Net/Exceptions/CeDeserializationException.cs b/CryptoExchange.Net/Exceptions/CeDeserializationException.cs new file mode 100644 index 0000000..6b1caf0 --- /dev/null +++ b/CryptoExchange.Net/Exceptions/CeDeserializationException.cs @@ -0,0 +1,24 @@ +using System; + +namespace CryptoExchange.Net.Exceptions +{ + /// + /// Exception during deserialization + /// + public class CeDeserializationException : Exception + { + /// + /// ctor + /// + public CeDeserializationException(string message) : base(message) + { + } + + /// + /// ctor + /// + public CeDeserializationException(string message, Exception innerException) : base(message, innerException) + { + } + } +} diff --git a/CryptoExchange.Net/Objects/Sockets/HighPerfUpdateSubscription.cs b/CryptoExchange.Net/Objects/Sockets/HighPerfUpdateSubscription.cs index 9cc21a9..0d28d0f 100644 --- a/CryptoExchange.Net/Objects/Sockets/HighPerfUpdateSubscription.cs +++ b/CryptoExchange.Net/Objects/Sockets/HighPerfUpdateSubscription.cs @@ -97,14 +97,5 @@ namespace CryptoExchange.Net.Objects.Sockets { return _connection.CloseAsync(_subscription); } - - /// - /// Unsubscribe a subscription - /// - /// - internal async Task UnsubscribeAsync() - { - await _connection.UnsubscribeAsync(_subscription).ConfigureAwait(false); - } } } diff --git a/CryptoExchange.Net/Sockets/HighPerf/HighPerfJsonSocketConnection.cs b/CryptoExchange.Net/Sockets/HighPerf/HighPerfJsonSocketConnection.cs index 7a2a495..c80a122 100644 --- a/CryptoExchange.Net/Sockets/HighPerf/HighPerfJsonSocketConnection.cs +++ b/CryptoExchange.Net/Sockets/HighPerf/HighPerfJsonSocketConnection.cs @@ -1,4 +1,5 @@ using CryptoExchange.Net.Clients; +using CryptoExchange.Net.Exceptions; using CryptoExchange.Net.Objects.Sockets; using CryptoExchange.Net.Sockets.Default.Interfaces; using Microsoft.Extensions.Logging; @@ -37,14 +38,21 @@ namespace CryptoExchange.Net.Sockets.HighPerf { try { + while (!ct.IsCancellationRequested) + { + try + { #pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code #pragma warning disable IL3050 // Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling. - await foreach (var update in JsonSerializer.DeserializeAsyncEnumerable(_pipe.Reader, true, _jsonOptions, ct).ConfigureAwait(false)) + await foreach (var update in JsonSerializer.DeserializeAsyncEnumerable(_pipe.Reader, true, _jsonOptions, ct).ConfigureAwait(false)) #pragma warning restore IL3050 // Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling. #pragma warning restore IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code - { - foreach(var sub in _typedSubscriptions) - DelegateToSubscription(_typedSubscriptions[0], update!); + { + foreach (var sub in _typedSubscriptions) + DelegateToSubscription(_typedSubscriptions[0], update!); + } + } + catch (CeDeserializationException) { } // Might just be a different message, ignore } } catch (OperationCanceledException) { } diff --git a/CryptoExchange.Net/Sockets/HighPerf/HighPerfSocketConnection.cs b/CryptoExchange.Net/Sockets/HighPerf/HighPerfSocketConnection.cs index 1646d2e..afc15de 100644 --- a/CryptoExchange.Net/Sockets/HighPerf/HighPerfSocketConnection.cs +++ b/CryptoExchange.Net/Sockets/HighPerf/HighPerfSocketConnection.cs @@ -240,24 +240,8 @@ namespace CryptoExchange.Net.Sockets.HighPerf if (subscription.CancellationTokenRegistration.HasValue) subscription.CancellationTokenRegistration.Value.Dispose(); - var anyOtherSubscriptions = Subscriptions.Any(x => x != subscription); - - if (anyOtherSubscriptions) - await UnsubscribeAsync(subscription).ConfigureAwait(false); - - if (Status == SocketStatus.Closing) - { - _logger.AlreadyClosing(SocketId); - return; - } - - if (!anyOtherSubscriptions) - { - Status = SocketStatus.Closing; - _logger.ClosingNoMoreSubscriptions(SocketId); - await CloseAsync().ConfigureAwait(false); - } - + Status = SocketStatus.Closing; + await CloseAsync().ConfigureAwait(false); } /// @@ -311,7 +295,6 @@ namespace CryptoExchange.Net.Sockets.HighPerf return new CallResult(new WebError("Failed to send message, socket no longer open")); } - _logger.SendingByteData(SocketId, 0, data.Length); try { if (!await _socket.SendAsync(data).ConfigureAwait(false)) @@ -344,7 +327,6 @@ namespace CryptoExchange.Net.Sockets.HighPerf return new CallResult(new WebError("Failed to send message, socket no longer open")); } - _logger.SendingData(SocketId, 0, data); try { if (!await _socket.SendAsync(data).ConfigureAwait(false)) @@ -358,16 +340,6 @@ namespace CryptoExchange.Net.Sockets.HighPerf } } - internal async Task UnsubscribeAsync(HighPerfSubscription subscription) - { - var unsubscribeRequest = subscription.CreateUnsubscriptionQuery(this); - if (unsubscribeRequest == null) - return; - - await SendAsync(unsubscribeRequest).ConfigureAwait(false); - _logger.SubscriptionUnsubscribed(SocketId, subscription.Id); - } - /// /// Periodically sends data over a socket connection /// diff --git a/CryptoExchange.Net/Sockets/HighPerf/HighPerfSubscription.cs b/CryptoExchange.Net/Sockets/HighPerf/HighPerfSubscription.cs index 2630213..874f5e4 100644 --- a/CryptoExchange.Net/Sockets/HighPerf/HighPerfSubscription.cs +++ b/CryptoExchange.Net/Sockets/HighPerf/HighPerfSubscription.cs @@ -33,11 +33,6 @@ namespace CryptoExchange.Net.Sockets.HighPerf /// public object? SubscriptionQuery { get; private set; } - /// - /// The unsubscribe query for this subscription - /// - public object? UnsubscriptionQuery { get; private set; } - /// /// ctor /// @@ -62,22 +57,6 @@ namespace CryptoExchange.Net.Sockets.HighPerf /// protected abstract object? GetSubQuery(HighPerfSocketConnection connection); - /// - /// Create a new unsubscription query - /// - public object? CreateUnsubscriptionQuery(HighPerfSocketConnection connection) - { - var query = GetUnsubQuery(connection); - UnsubscriptionQuery = query; - return query; - } - - /// - /// Get the unsubscribe query to send when unsubscribing - /// - /// - protected abstract object? GetUnsubQuery(HighPerfSocketConnection connection); - /// /// Invoke the exception event ///