AyCode.Core/AyCode.Services/SignalRs/AyCodeBinaryHubProtocol.cs

28 lines
976 B
C#

using System.Buffers;
using AyCode.Core.Serializers.Binaries;
namespace AyCode.Services.SignalRs;
/// <summary>
/// Project-specific binary protocol. Uses ArrayPool for byte[] arguments
/// when the target type is SignalData (client receive path optimization).
/// Register this in PluginNopStartup.cs and AcSignalRClientBase instead of AcBinaryHubProtocol.
/// </summary>
public class AyCodeBinaryHubProtocol : AcBinaryHubProtocol
{
public AyCodeBinaryHubProtocol() { }
public AyCodeBinaryHubProtocol(AcBinarySerializerOptions options) : base(options) { }
protected override object CreateByteArrayResult(ReadOnlySpan<byte> data, Type targetType)
{
if (targetType == typeof(SignalData))
{
var rented = ArrayPool<byte>.Shared.Rent(data.Length);
data.CopyTo(rented);
return new SignalData(rented, data.Length, isRented: true);
}
return base.CreateByteArrayResult(data, targetType);
}
}