using System.Buffers;
using AyCode.Core.Serializers.Binaries;
namespace AyCode.Services.SignalRs;
///
/// 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.
///
public class AyCodeBinaryHubProtocol : AcBinaryHubProtocol
{
public AyCodeBinaryHubProtocol() { }
public AyCodeBinaryHubProtocol(AcBinarySerializerOptions options) : base(options) { }
protected override object CreateByteArrayResult(ReadOnlySpan data, Type targetType)
{
if (targetType == typeof(SignalData))
{
var rented = ArrayPool.Shared.Rent(data.Length);
data.CopyTo(rented);
return new SignalData(rented, data.Length, isRented: true);
}
return base.CreateByteArrayResult(data, targetType);
}
}