Switch to binary serialization, update compression to GZip
- Changed default SignalR message serialization from JSON to binary. - Updated AcSerializerType enum values: Binary=0, Json=1. - Disabled string caching when intern table is present. - Replaced Brotli with GZip for JSON compression in comments and logic. - Refactored SignalResponseDataMessage deserialization for improved error handling.
This commit is contained in:
parent
155cef4500
commit
4ab8ede6ca
|
|
@ -18,7 +18,7 @@ public static partial class AcBinaryDeserializer
|
||||||
private Dictionary<int, object>? _objectReferences;
|
private Dictionary<int, object>? _objectReferences;
|
||||||
private Dictionary<int, string>? _stringCache;
|
private Dictionary<int, string>? _stringCache;
|
||||||
private readonly byte _minStringInternLength;
|
private readonly byte _minStringInternLength;
|
||||||
private readonly bool _useStringCaching;
|
private bool _useStringCaching;
|
||||||
private readonly int _maxCachedStringLength;
|
private readonly int _maxCachedStringLength;
|
||||||
|
|
||||||
public bool HasMetadata { get; private set; }
|
public bool HasMetadata { get; private set; }
|
||||||
|
|
@ -107,6 +107,8 @@ public static partial class AcBinaryDeserializer
|
||||||
|
|
||||||
if (hasInternTable)
|
if (hasInternTable)
|
||||||
{
|
{
|
||||||
|
_useStringCaching = false;
|
||||||
|
|
||||||
var internCount = (int)ReadVarUInt();
|
var internCount = (int)ReadVarUInt();
|
||||||
_internedStrings = new List<string>(internCount);
|
_internedStrings = new List<string>(internCount);
|
||||||
for (var i = 0; i < internCount; i++)
|
for (var i = 0; i < internCount; i++)
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,8 @@ namespace AyCode.Core.Serializers.Jsons;
|
||||||
|
|
||||||
public enum AcSerializerType : byte
|
public enum AcSerializerType : byte
|
||||||
{
|
{
|
||||||
Json = 0,
|
Binary = 0,
|
||||||
Binary = 1,
|
Json = 1,
|
||||||
Toon = 2,
|
Toon = 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
using AyCode.Core.Extensions;
|
using AyCode.Core.Extensions;
|
||||||
using AyCode.Core.Helpers;
|
using AyCode.Core.Helpers;
|
||||||
using AyCode.Core.Loggers;
|
using AyCode.Core.Loggers;
|
||||||
|
using AyCode.Core.Serializers.Binaries;
|
||||||
using AyCode.Core.Serializers.Jsons;
|
using AyCode.Core.Serializers.Jsons;
|
||||||
using AyCode.Services.SignalRs;
|
using AyCode.Services.SignalRs;
|
||||||
using Microsoft.AspNetCore.SignalR;
|
using Microsoft.AspNetCore.SignalR;
|
||||||
|
|
@ -14,7 +15,7 @@ public abstract class AcSignalRSendToClientService<TSignalRHub, TSignalRTags, TL
|
||||||
|
|
||||||
protected virtual async Task SendMessageToClient(IAcSignalRHubItemServer sendTo, int messageTag, object? content)
|
protected virtual async Task SendMessageToClient(IAcSignalRHubItemServer sendTo, int messageTag, object? content)
|
||||||
{
|
{
|
||||||
var response = new SignalResponseDataMessage(messageTag, SignalResponseStatus.Success, content, AcJsonSerializerOptions.Default);
|
var response = new SignalResponseDataMessage(messageTag, SignalResponseStatus.Success, content, AcBinarySerializerOptions.Default);
|
||||||
var responseBytes = response.ToBinary();
|
var responseBytes = response.ToBinary();
|
||||||
|
|
||||||
Logger.Info($"[{responseBytes.Length / 1024}kb] Server sending to client; {ConstHelper.NameByValue<TSignalRTags>(messageTag)}");
|
Logger.Info($"[{responseBytes.Length / 1024}kb] Server sending to client; {ConstHelper.NameByValue<TSignalRTags>(messageTag)}");
|
||||||
|
|
|
||||||
|
|
@ -136,7 +136,7 @@ public enum SignalResponseStatus : byte
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Unified signal response message that supports both JSON and Binary serialization.
|
/// Unified signal response message that supports both JSON and Binary serialization.
|
||||||
/// JSON mode uses Brotli compression for reduced payload size.
|
/// JSON mode uses GZip compression for reduced payload size.
|
||||||
/// Optimized: uses pooled buffers for decompression, zero-copy deserialization path.
|
/// Optimized: uses pooled buffers for decompression, zero-copy deserialization path.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class SignalResponseDataMessage : ISignalResponseMessage, IDisposable
|
public sealed class SignalResponseDataMessage : ISignalResponseMessage, IDisposable
|
||||||
|
|
@ -182,28 +182,29 @@ public sealed class SignalResponseDataMessage : ISignalResponseMessage, IDisposa
|
||||||
if (_cachedResponseData != null) return (T)_cachedResponseData;
|
if (_cachedResponseData != null) return (T)_cachedResponseData;
|
||||||
if (ResponseData == null) return default;
|
if (ResponseData == null) return default;
|
||||||
|
|
||||||
if (DataSerializerType == AcSerializerType.Binary)
|
try
|
||||||
{
|
{
|
||||||
try
|
if (DataSerializerType == AcSerializerType.Binary)
|
||||||
{
|
{
|
||||||
// Log diagnostics if enabled
|
// Log diagnostics if enabled
|
||||||
LogResponseDataDiagnostics<T>();
|
LogResponseDataDiagnostics<T>();
|
||||||
|
|
||||||
return (T)(_cachedResponseData = ResponseData.BinaryTo<T>()!);
|
return (T)(_cachedResponseData = ResponseData.BinaryTo<T>()!);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
// Log detailed error diagnostics
|
|
||||||
LogResponseDataError<T>(ex);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decompress Brotli to pooled buffer and deserialize directly
|
// Decompress GZip to pooled buffer and deserialize directly
|
||||||
EnsureDecompressed();
|
EnsureDecompressed();
|
||||||
var result = AcJsonDeserializer.Deserialize<T>(new ReadOnlySpan<byte>(_rentedDecompressedBuffer, 0, _decompressedLength));
|
|
||||||
_cachedResponseData = result;
|
var result = AcJsonDeserializer.Deserialize<T>(new ReadOnlySpan<byte>(_rentedDecompressedBuffer, 0, _decompressedLength));
|
||||||
return result;
|
_cachedResponseData = result;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Log detailed error diagnostics
|
||||||
|
LogResponseDataError<T>(ex);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void LogResponseDataDiagnostics<T>()
|
private void LogResponseDataDiagnostics<T>()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue