From a9d72dee4e98bc39d56d6aa6963c095d7cd45303 Mon Sep 17 00:00:00 2001 From: Jkorf Date: Tue, 17 Jun 2025 09:43:34 +0200 Subject: [PATCH] wip --- .../Protobuf/ProtobufMessageAccessor.cs | 65 ++++++++++++++----- .../Protobuf/ProtobufMessageSerializer.cs | 10 ++- .../CryptoExchange.Net.Protobuf.csproj | 6 ++ .../CryptoExchange.Net.Protobuf.xml | 13 ++-- .../TestImplementations/TestSocketClient.cs | 3 +- CryptoExchange.Net/Clients/RestApiClient.cs | 1 - CryptoExchange.Net/CryptoExchange.Net.csproj | 1 - .../Interfaces/IMessageAccessor.cs | 9 +++ .../Sockets/SocketConnection.cs | 8 +++ 9 files changed, 89 insertions(+), 27 deletions(-) diff --git a/CryptoExchange.Net.Protobuf/Converters/Protobuf/ProtobufMessageAccessor.cs b/CryptoExchange.Net.Protobuf/Converters/Protobuf/ProtobufMessageAccessor.cs index 8414246..d16efab 100644 --- a/CryptoExchange.Net.Protobuf/Converters/Protobuf/ProtobufMessageAccessor.cs +++ b/CryptoExchange.Net.Protobuf/Converters/Protobuf/ProtobufMessageAccessor.cs @@ -40,6 +40,10 @@ namespace CryptoExchange.Net.Converters.Protobuf /// The intermediate deserialization object /// protected TIntermediateType? _intermediateType; + /// + /// Runtime type model + /// + protected RuntimeTypeModel _model; /// public bool IsValid { get; set; } @@ -48,19 +52,20 @@ namespace CryptoExchange.Net.Converters.Protobuf public abstract bool OriginalDataAvailable { get; } /// - public object? Underlying => throw new NotImplementedException(); + public object? Underlying => _intermediateType; /// /// ctor /// - public ProtobufMessageAccessor() + public ProtobufMessageAccessor(RuntimeTypeModel model) { + _model = model; } /// public NodeType? GetNodeType() { - throw new Exception(""); + throw new NotImplementedException(); } /// @@ -82,7 +87,9 @@ namespace CryptoExchange.Net.Converters.Protobuf else if (step.Type == 1) { // property value +#pragma warning disable IL2075 // Type is already annotated value = value.GetType().GetProperty(step.Property!)?.GetValue(value); +#pragma warning restore } else { @@ -136,7 +143,9 @@ namespace CryptoExchange.Net.Converters.Protobuf else if (step.Type == 1) { // property value +#pragma warning disable IL2075 // Type is already annotated value = value.GetType().GetProperty(step.Property!)?.GetValue(value); +#pragma warning restore } else { @@ -150,8 +159,7 @@ namespace CryptoExchange.Net.Converters.Protobuf /// public T?[]? GetValues(MessagePath path) { - throw new Exception(""); - + throw new NotImplementedException(); } /// @@ -161,6 +169,10 @@ namespace CryptoExchange.Net.Converters.Protobuf public abstract void Clear(); /// +#if NET5_0_OR_GREATER + [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2092:RequiresUnreferencedCode", Justification = "JsonSerializerOptions provided here has TypeInfoResolver set")] + [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2095:RequiresUnreferencedCode", Justification = "JsonSerializerOptions provided here has TypeInfoResolver set")] +#endif public abstract CallResult Deserialize( #if NET5_0_OR_GREATER [DynamicallyAccessedMembers( @@ -181,10 +193,13 @@ namespace CryptoExchange.Net.Converters.Protobuf Type type, MessagePath? path = null); /// - public abstract CallResult Deserialize< - #if NET5_0_OR_GREATER - [DynamicallyAccessedMembers( + [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2092:RequiresUnreferencedCode", Justification = "JsonSerializerOptions provided here has TypeInfoResolver set")] + [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2095:RequiresUnreferencedCode", Justification = "JsonSerializerOptions provided here has TypeInfoResolver set")] +#endif + public abstract CallResult Deserialize< +#if NET5_0_OR_GREATER + [DynamicallyAccessedMembers( #if NET8_0_OR_GREATER DynamicallyAccessedMemberTypes.NonPublicConstructors | DynamicallyAccessedMemberTypes.PublicFields | @@ -232,11 +247,15 @@ namespace CryptoExchange.Net.Converters.Protobuf /// /// ctor /// - public ProtobufStreamMessageAccessor(): base() + public ProtobufStreamMessageAccessor(RuntimeTypeModel model) : base(model) { } /// +#if NET5_0_OR_GREATER + [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2092:RequiresUnreferencedCode", Justification = "JsonSerializerOptions provided here has TypeInfoResolver set")] + [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2095:RequiresUnreferencedCode", Justification = "JsonSerializerOptions provided here has TypeInfoResolver set")] +#endif public override CallResult Deserialize( #if NET5_0_OR_GREATER [DynamicallyAccessedMembers( @@ -258,7 +277,7 @@ namespace CryptoExchange.Net.Converters.Protobuf { try { - var result = Serializer.Deserialize(type, _stream); + var result = _model.Deserialize(type, _stream); return new CallResult(result); } catch (Exception ex) @@ -268,6 +287,10 @@ namespace CryptoExchange.Net.Converters.Protobuf } /// +#if NET5_0_OR_GREATER + [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2092:RequiresUnreferencedCode", Justification = "JsonSerializerOptions provided here has TypeInfoResolver set")] + [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2095:RequiresUnreferencedCode", Justification = "JsonSerializerOptions provided here has TypeInfoResolver set")] +#endif public override CallResult Deserialize< #if NET5_0_OR_GREATER [DynamicallyAccessedMembers( @@ -289,7 +312,7 @@ namespace CryptoExchange.Net.Converters.Protobuf { try { - var result = Serializer.Deserialize(_stream); + var result = _model.Deserialize(_stream); return new CallResult(result); } catch(Exception ex) @@ -320,7 +343,7 @@ namespace CryptoExchange.Net.Converters.Protobuf try { - _intermediateType = Serializer.Deserialize(_stream); + _intermediateType = _model.Deserialize(_stream); IsValid = true; return Task.FromResult(CallResult.SuccessResult); } @@ -328,7 +351,7 @@ namespace CryptoExchange.Net.Converters.Protobuf { // Not a json message IsValid = false; - return Task.FromResult(new CallResult(new DeserializeError("JsonError: " + ex.Message, ex))); + return Task.FromResult(new CallResult(new DeserializeError("ProtoBufError: " + ex.Message, ex))); } } @@ -380,11 +403,15 @@ namespace CryptoExchange.Net.Converters.Protobuf /// /// ctor /// - public ProtobufByteMessageAccessor() : base() + public ProtobufByteMessageAccessor(RuntimeTypeModel model) : base(model) { } /// +#if NET5_0_OR_GREATER + [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2092:RequiresUnreferencedCode", Justification = "JsonSerializerOptions provided here has TypeInfoResolver set")] + [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2095:RequiresUnreferencedCode", Justification = "JsonSerializerOptions provided here has TypeInfoResolver set")] +#endif public override CallResult Deserialize( #if NET5_0_OR_GREATER [DynamicallyAccessedMembers( @@ -408,7 +435,7 @@ namespace CryptoExchange.Net.Converters.Protobuf { using var stream = new MemoryStream(_bytes.ToArray()); stream.Position = 0; - var result = Serializer.Deserialize(type, stream); + var result = _model.Deserialize(type, stream); return new CallResult(result); } catch (Exception ex) @@ -418,6 +445,10 @@ namespace CryptoExchange.Net.Converters.Protobuf } /// +#if NET5_0_OR_GREATER + [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2092:RequiresUnreferencedCode", Justification = "JsonSerializerOptions provided here has TypeInfoResolver set")] + [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2095:RequiresUnreferencedCode", Justification = "JsonSerializerOptions provided here has TypeInfoResolver set")] +#endif #if NET5_0_OR_GREATER public override CallResult Deserialize< [DynamicallyAccessedMembers( @@ -441,7 +472,7 @@ namespace CryptoExchange.Net.Converters.Protobuf { try { - var result = Serializer.Deserialize(_bytes); + var result = _model.Deserialize(_bytes); return new CallResult(result); } catch (Exception ex) @@ -457,7 +488,7 @@ namespace CryptoExchange.Net.Converters.Protobuf try { - _intermediateType = Serializer.Deserialize(data); + _intermediateType = _model.Deserialize(data); IsValid = true; return CallResult.SuccessResult; } diff --git a/CryptoExchange.Net.Protobuf/Converters/Protobuf/ProtobufMessageSerializer.cs b/CryptoExchange.Net.Protobuf/Converters/Protobuf/ProtobufMessageSerializer.cs index 93c0deb..80df737 100644 --- a/CryptoExchange.Net.Protobuf/Converters/Protobuf/ProtobufMessageSerializer.cs +++ b/CryptoExchange.Net.Protobuf/Converters/Protobuf/ProtobufMessageSerializer.cs @@ -9,17 +9,21 @@ namespace CryptoExchange.Net.Converters.Protobuf /// public class ProtobufMessageSerializer : IByteMessageSerializer { - private readonly RuntimeTypeModel _model = RuntimeTypeModel.Create("CryptoExchange"); + private RuntimeTypeModel _model; /// /// ctor /// - public ProtobufMessageSerializer() + public ProtobufMessageSerializer(RuntimeTypeModel model) { - _model.UseImplicitZeroDefaults = false; + _model = model; } /// +#if NET5_0_OR_GREATER + [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2092:RequiresUnreferencedCode", Justification = "JsonSerializerOptions provided here has TypeInfoResolver set")] + [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2095:RequiresUnreferencedCode", Justification = "JsonSerializerOptions provided here has TypeInfoResolver set")] +#endif #if NET5_0_OR_GREATER public byte[] Serialize< [DynamicallyAccessedMembers( diff --git a/CryptoExchange.Net.Protobuf/CryptoExchange.Net.Protobuf.csproj b/CryptoExchange.Net.Protobuf/CryptoExchange.Net.Protobuf.csproj index 24cecd1..27c8837 100644 --- a/CryptoExchange.Net.Protobuf/CryptoExchange.Net.Protobuf.csproj +++ b/CryptoExchange.Net.Protobuf/CryptoExchange.Net.Protobuf.csproj @@ -29,6 +29,9 @@ + + true + true true @@ -39,6 +42,9 @@ CryptoExchange.Net.Protobuf.xml + + + diff --git a/CryptoExchange.Net.Protobuf/CryptoExchange.Net.Protobuf.xml b/CryptoExchange.Net.Protobuf/CryptoExchange.Net.Protobuf.xml index f5da907..0b7b110 100644 --- a/CryptoExchange.Net.Protobuf/CryptoExchange.Net.Protobuf.xml +++ b/CryptoExchange.Net.Protobuf/CryptoExchange.Net.Protobuf.xml @@ -14,6 +14,11 @@ The intermediate deserialization object + + + Runtime type model + + @@ -23,7 +28,7 @@ - + ctor @@ -60,7 +65,7 @@ - + ctor @@ -85,7 +90,7 @@ Protobuf byte message accessor - + ctor @@ -111,7 +116,7 @@ - + ctor diff --git a/CryptoExchange.Net.UnitTests/TestImplementations/TestSocketClient.cs b/CryptoExchange.Net.UnitTests/TestImplementations/TestSocketClient.cs index 561b88a..9df7288 100644 --- a/CryptoExchange.Net.UnitTests/TestImplementations/TestSocketClient.cs +++ b/CryptoExchange.Net.UnitTests/TestImplementations/TestSocketClient.cs @@ -17,6 +17,7 @@ using CryptoExchange.Net.Testing.Implementations; using CryptoExchange.Net.SharedApis; using Microsoft.Extensions.Options; using CryptoExchange.Net.Converters.SystemTextJson; +using System.Net.WebSockets; namespace CryptoExchange.Net.UnitTests.TestImplementations { @@ -98,7 +99,7 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations } - protected internal override IByteMessageAccessor CreateAccessor() => new SystemTextJsonByteMessageAccessor(new System.Text.Json.JsonSerializerOptions()); + protected internal override IByteMessageAccessor CreateAccessor(WebSocketMessageType type) => new SystemTextJsonByteMessageAccessor(new System.Text.Json.JsonSerializerOptions()); protected internal override IMessageSerializer CreateSerializer() => new SystemTextJsonMessageSerializer(new System.Text.Json.JsonSerializerOptions()); /// diff --git a/CryptoExchange.Net/Clients/RestApiClient.cs b/CryptoExchange.Net/Clients/RestApiClient.cs index 9ba63af..3d3532e 100644 --- a/CryptoExchange.Net/Clients/RestApiClient.cs +++ b/CryptoExchange.Net/Clients/RestApiClient.cs @@ -16,7 +16,6 @@ using CryptoExchange.Net.RateLimiting; using CryptoExchange.Net.RateLimiting.Interfaces; using CryptoExchange.Net.Requests; using Microsoft.Extensions.Logging; -using ProtoBuf; namespace CryptoExchange.Net.Clients { diff --git a/CryptoExchange.Net/CryptoExchange.Net.csproj b/CryptoExchange.Net/CryptoExchange.Net.csproj index de53174..007a4c7 100644 --- a/CryptoExchange.Net/CryptoExchange.Net.csproj +++ b/CryptoExchange.Net/CryptoExchange.Net.csproj @@ -52,7 +52,6 @@ - diff --git a/CryptoExchange.Net/Interfaces/IMessageAccessor.cs b/CryptoExchange.Net/Interfaces/IMessageAccessor.cs index f5a7b5b..1c3c28c 100644 --- a/CryptoExchange.Net/Interfaces/IMessageAccessor.cs +++ b/CryptoExchange.Net/Interfaces/IMessageAccessor.cs @@ -2,6 +2,7 @@ using CryptoExchange.Net.Objects; using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Threading.Tasks; @@ -59,12 +60,20 @@ namespace CryptoExchange.Net.Interfaces /// /// /// +#if NET5_0_OR_GREATER + [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2092:RequiresUnreferencedCode", Justification = "JsonSerializerOptions provided here has TypeInfoResolver set")] + [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2095:RequiresUnreferencedCode", Justification = "JsonSerializerOptions provided here has TypeInfoResolver set")] +#endif CallResult Deserialize(Type type, MessagePath? path = null); /// /// Deserialize the message into this type /// /// /// +#if NET5_0_OR_GREATER + [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2092:RequiresUnreferencedCode", Justification = "JsonSerializerOptions provided here has TypeInfoResolver set")] + [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2095:RequiresUnreferencedCode", Justification = "JsonSerializerOptions provided here has TypeInfoResolver set")] +#endif CallResult Deserialize(MessagePath? path = null); /// diff --git a/CryptoExchange.Net/Sockets/SocketConnection.cs b/CryptoExchange.Net/Sockets/SocketConnection.cs index a1d9502..991b674 100644 --- a/CryptoExchange.Net/Sockets/SocketConnection.cs +++ b/CryptoExchange.Net/Sockets/SocketConnection.cs @@ -480,6 +480,14 @@ namespace CryptoExchange.Net.Sockets _logger.FailedToParse(SocketId, result.Error!.Message); return; } + else + { + try + { + _logger.LogInformation("Valid: " + accessor.Underlying); + } + catch (Exception) { } + } // 3. Determine the identifying properties of this message var listenId = ApiClient.GetListenerIdentifier(accessor);