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:
Loretta 2026-03-06 14:26:48 +01:00
parent 155cef4500
commit 4ab8ede6ca
4 changed files with 24 additions and 20 deletions

View File

@ -18,7 +18,7 @@ public static partial class AcBinaryDeserializer
private Dictionary<int, object>? _objectReferences;
private Dictionary<int, string>? _stringCache;
private readonly byte _minStringInternLength;
private readonly bool _useStringCaching;
private bool _useStringCaching;
private readonly int _maxCachedStringLength;
public bool HasMetadata { get; private set; }
@ -107,6 +107,8 @@ public static partial class AcBinaryDeserializer
if (hasInternTable)
{
_useStringCaching = false;
var internCount = (int)ReadVarUInt();
_internedStrings = new List<string>(internCount);
for (var i = 0; i < internCount; i++)

View File

@ -2,8 +2,8 @@ namespace AyCode.Core.Serializers.Jsons;
public enum AcSerializerType : byte
{
Json = 0,
Binary = 1,
Binary = 0,
Json = 1,
Toon = 2,
}

View File

@ -1,6 +1,7 @@
using AyCode.Core.Extensions;
using AyCode.Core.Helpers;
using AyCode.Core.Loggers;
using AyCode.Core.Serializers.Binaries;
using AyCode.Core.Serializers.Jsons;
using AyCode.Services.SignalRs;
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)
{
var response = new SignalResponseDataMessage(messageTag, SignalResponseStatus.Success, content, AcJsonSerializerOptions.Default);
var response = new SignalResponseDataMessage(messageTag, SignalResponseStatus.Success, content, AcBinarySerializerOptions.Default);
var responseBytes = response.ToBinary();
Logger.Info($"[{responseBytes.Length / 1024}kb] Server sending to client; {ConstHelper.NameByValue<TSignalRTags>(messageTag)}");

View File

@ -136,7 +136,7 @@ public enum SignalResponseStatus : byte
/// <summary>
/// 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.
/// </summary>
public sealed class SignalResponseDataMessage : ISignalResponseMessage, IDisposable
@ -182,15 +182,23 @@ public sealed class SignalResponseDataMessage : ISignalResponseMessage, IDisposa
if (_cachedResponseData != null) return (T)_cachedResponseData;
if (ResponseData == null) return default;
if (DataSerializerType == AcSerializerType.Binary)
{
try
{
if (DataSerializerType == AcSerializerType.Binary)
{
// Log diagnostics if enabled
LogResponseDataDiagnostics<T>();
return (T)(_cachedResponseData = ResponseData.BinaryTo<T>()!);
}
// Decompress GZip to pooled buffer and deserialize directly
EnsureDecompressed();
var result = AcJsonDeserializer.Deserialize<T>(new ReadOnlySpan<byte>(_rentedDecompressedBuffer, 0, _decompressedLength));
_cachedResponseData = result;
return result;
}
catch (Exception ex)
{
// Log detailed error diagnostics
@ -199,13 +207,6 @@ public sealed class SignalResponseDataMessage : ISignalResponseMessage, IDisposa
}
}
// Decompress Brotli to pooled buffer and deserialize directly
EnsureDecompressed();
var result = AcJsonDeserializer.Deserialize<T>(new ReadOnlySpan<byte>(_rentedDecompressedBuffer, 0, _decompressedLength));
_cachedResponseData = result;
return result;
}
private void LogResponseDataDiagnostics<T>()
{
if (DiagnosticLogger == null || ResponseData == null) return;