Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Sindo committed Jan 6, 2025
1 parent 68e8a85 commit 5105939
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 60 deletions.
4 changes: 2 additions & 2 deletions src/Microsoft.Azure.SignalR.Common/Utilities/AckHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Microsoft.Azure.SignalR
internal sealed class AckHandler : IDisposable
{
public static readonly AckHandler Singleton = new();
public static readonly ServiceModelProtocol _serviceModelProtocol = new();
public static readonly ServiceProtocol _serviceProtocol = new();
private readonly ConcurrentDictionary<int, IAckInfo> _acks = new();
private readonly Timer _timer;
private readonly TimeSpan _defaultAckTimeout;
Expand Down Expand Up @@ -226,7 +226,7 @@ public override bool Ack(AckStatus status, ReadOnlySequence<byte>? payload = nul

try
{
var result = _serviceModelProtocol.ParseModel<T>(payload.Value);
var result = _serviceProtocol.ParseMessagePayload<T>(payload.Value);
return _tcs.TrySetResult(result);
}
catch (Exception e)
Expand Down

This file was deleted.

29 changes: 29 additions & 0 deletions src/Microsoft.Azure.SignalR.Protocols/ServiceProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,35 @@ public class ServiceProtocol : IServiceProtocol
/// <inheritdoc />
public int Version => ProtocolVersion;

public T ParseMessagePayload<T>(ReadOnlySequence<byte> input) where T : notnull, new()
{
if (typeof(IMessagePackSerializable).IsAssignableFrom(typeof(T)))
{
var model = new T();
var reader = new MessagePackReader(input);
((IMessagePackSerializable)model).Load(ref reader, typeof(T).Name);
return model;
}
else
{
throw new NotSupportedException($"Type {typeof(T).Name} is not supported.");
}
}

public void WriteMessagePayload<T>(T model, IBufferWriter<byte> output) where T : notnull, new()
{
if (typeof(IMessagePackSerializable).IsAssignableFrom(typeof(T)))
{
var writer = new MessagePackWriter(output);
((IMessagePackSerializable)model).Serialize(ref writer);
writer.Flush();
}
else
{
throw new NotSupportedException($"Type {typeof(T).Name} is not supported.");
}
}

/// <inheritdoc />
public bool TryParseMessage(ref ReadOnlySequence<byte> input, out ServiceMessage? message)
{
Expand Down

0 comments on commit 5105939

Please sign in to comment.