Skip to content

Commit 0947122

Browse files
committed
add
1 parent f8901f9 commit 0947122

File tree

3 files changed

+31
-60
lines changed

3 files changed

+31
-60
lines changed

src/Microsoft.Azure.SignalR.Common/Utilities/AckHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Microsoft.Azure.SignalR
1717
internal sealed class AckHandler : IDisposable
1818
{
1919
public static readonly AckHandler Singleton = new();
20-
public static readonly ServiceModelProtocol _serviceModelProtocol = new();
20+
public static readonly ServiceProtocol _serviceProtocol = new();
2121
private readonly ConcurrentDictionary<int, IAckInfo> _acks = new();
2222
private readonly Timer _timer;
2323
private readonly TimeSpan _defaultAckTimeout;
@@ -226,7 +226,7 @@ public override bool Ack(AckStatus status, ReadOnlySequence<byte>? payload = nul
226226

227227
try
228228
{
229-
var result = _serviceModelProtocol.ParseModel<T>(payload.Value);
229+
var result = _serviceProtocol.ParseMessagePayload<T>(payload.Value);
230230
return _tcs.TrySetResult(result);
231231
}
232232
catch (Exception e)

src/Microsoft.Azure.SignalR.Protocols/Models/ServiceModelProtocol.cs

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/Microsoft.Azure.SignalR.Protocols/ServiceProtocol.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,35 @@ public class ServiceProtocol : IServiceProtocol
2121
/// <inheritdoc />
2222
public int Version => ProtocolVersion;
2323

24+
public T ParseMessagePayload<T>(ReadOnlySequence<byte> input) where T : notnull, new()
25+
{
26+
if (typeof(IMessagePackSerializable).IsAssignableFrom(typeof(T)))
27+
{
28+
var model = new T();
29+
var reader = new MessagePackReader(input);
30+
((IMessagePackSerializable)model).Load(ref reader, typeof(T).Name);
31+
return model;
32+
}
33+
else
34+
{
35+
throw new NotSupportedException($"Type {typeof(T).Name} is not supported.");
36+
}
37+
}
38+
39+
public void WriteMessagePayload<T>(T model, IBufferWriter<byte> output) where T : notnull, new()
40+
{
41+
if (typeof(IMessagePackSerializable).IsAssignableFrom(typeof(T)))
42+
{
43+
var writer = new MessagePackWriter(output);
44+
((IMessagePackSerializable)model).Serialize(ref writer);
45+
writer.Flush();
46+
}
47+
else
48+
{
49+
throw new NotSupportedException($"Type {typeof(T).Name} is not supported.");
50+
}
51+
}
52+
2453
/// <inheritdoc />
2554
public bool TryParseMessage(ref ReadOnlySequence<byte> input, out ServiceMessage? message)
2655
{

0 commit comments

Comments
 (0)