Skip to content

Commit 6df502e

Browse files
committed
update
1 parent 5348aab commit 6df502e

File tree

9 files changed

+57
-51
lines changed

9 files changed

+57
-51
lines changed

src/Components/Server/src/BlazorPack/BlazorPackHubProtocol.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.AspNetCore.Connections;
1313
using Microsoft.AspNetCore.Internal;
1414
using Microsoft.AspNetCore.SignalR;
15+
using Microsoft.AspNetCore.SignalR.Internal;
1516
using Microsoft.AspNetCore.SignalR.Protocol;
1617

1718
namespace Microsoft.AspNetCore.Components.Server.BlazorPack

src/Components/Server/src/BlazorPack/NonDefaultHubProtocol.cs renamed to src/Components/Server/src/BlazorPack/NonDefaultHubProtocolAttribute.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
using System;
55

6-
namespace Microsoft.AspNetCore.Components.Server.BlazorPack
6+
namespace Microsoft.AspNetCore.SignalR.Internal
77
{
88
// Tells SignalR not to add the IHubProtocol with this attribute to all hubs by default
99
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
10-
internal class NonDefaultHubProtocol : Attribute
10+
internal class NonDefaultHubProtocolAttribute : Attribute
1111
{
1212
}
1313
}

src/SignalR/perf/Microbenchmarks/RedisProtocolBenchmark.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
using System.Collections.Generic;
77
using BenchmarkDotNet.Attributes;
88
using Microsoft.AspNetCore.Connections;
9+
using Microsoft.AspNetCore.SignalR.Internal;
910
using Microsoft.AspNetCore.SignalR.Protocol;
1011
using Microsoft.AspNetCore.SignalR.StackExchangeRedis.Internal;
12+
using Microsoft.Extensions.Logging.Abstractions;
1113

1214
namespace Microsoft.AspNetCore.SignalR.Microbenchmarks
1315
{
@@ -28,10 +30,10 @@ public class RedisProtocolBenchmark
2830
[GlobalSetup]
2931
public void GlobalSetup()
3032
{
31-
_protocol = new RedisProtocol(new [] {
32-
new DummyProtocol("protocol1"),
33-
new DummyProtocol("protocol2")
34-
});
33+
var resolver = new DefaultHubProtocolResolver(new List<IHubProtocol> { new DummyProtocol("protocol1"),
34+
new DummyProtocol("protocol2") }, NullLogger<DefaultHubProtocolResolver>.Instance);
35+
36+
_protocol = new RedisProtocol(new DefaultHubMessageSerializer(resolver, new List<string>() { "protocol1", "protocol2" }, hubSupportedProtocols: null));
3537

3638
_groupCommand = new RedisGroupCommand(id: 42, serverName: "Server", GroupAction.Add, groupName: "group", connectionId: "connection");
3739

src/SignalR/server/Core/src/HubOptionsSetup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public HubOptionsSetup(IEnumerable<IHubProtocol> protocols)
2727
{
2828
foreach (var hubProtocol in protocols)
2929
{
30-
if (hubProtocol.GetType().CustomAttributes.Where(a => a.AttributeType.Name == "NonDefaultHubProtocol").Any())
30+
if (hubProtocol.GetType().CustomAttributes.Where(a => a.AttributeType.FullName == "Microsoft.AspNetCore.SignalR.Internal.NonDefaultHubProtocolAttribute").Any())
3131
{
3232
continue;
3333
}

src/SignalR/server/SignalR/test/AddSignalRTests.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,15 +304,10 @@ public override Task SendUsersAsync(IReadOnlyList<string> userIds, string method
304304
}
305305
}
306306

307-
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
308-
internal class NonDefaultHubProtocol : Attribute
309-
{
310-
}
311-
312307
[NonDefaultHubProtocol]
313308
internal class CustomHubProtocol : IHubProtocol
314309
{
315-
public string Name => throw new NotImplementedException();
310+
public string Name => "custom";
316311

317312
public int Version => throw new NotImplementedException();
318313

@@ -339,3 +334,11 @@ public void WriteMessage(HubMessage message, IBufferWriter<byte> output)
339334
}
340335
}
341336
}
337+
338+
namespace Microsoft.AspNetCore.SignalR.Internal
339+
{
340+
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
341+
internal class NonDefaultHubProtocolAttribute : Attribute
342+
{
343+
}
344+
}

src/SignalR/server/StackExchangeRedis/src/Internal/RedisProtocol.cs

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,8 @@ namespace Microsoft.AspNetCore.SignalR.StackExchangeRedis.Internal
1515
{
1616
internal class RedisProtocol
1717
{
18-
private readonly IReadOnlyList<IHubProtocol> _protocols;
1918
private readonly DefaultHubMessageSerializer _messageSerializer;
2019

21-
public RedisProtocol(IReadOnlyList<IHubProtocol> protocols)
22-
{
23-
_protocols = protocols;
24-
}
25-
2620
public RedisProtocol(DefaultHubMessageSerializer messageSerializer)
2721
{
2822
_messageSerializer = messageSerializer;
@@ -174,34 +168,17 @@ private void WriteHubMessage(Stream stream, HubMessage message)
174168
// Written as a MessagePack 'map' where the keys are the name of the protocol (as a MessagePack 'str')
175169
// and the values are the serialized blob (as a MessagePack 'bin').
176170

177-
if (_protocols != null)
178-
{
179-
MessagePackBinary.WriteMapHeader(stream, _protocols.Count);
171+
var serializedHubMessages = _messageSerializer.SerializeMessage(message);
180172

181-
foreach (var protocol in _protocols)
182-
{
183-
MessagePackBinary.WriteString(stream, protocol.Name);
173+
MessagePackBinary.WriteMapHeader(stream, serializedHubMessages.Count);
184174

185-
var serialized = protocol.GetMessageBytes(message);
186-
var isArray = MemoryMarshal.TryGetArray(serialized, out var array);
187-
Debug.Assert(isArray);
188-
MessagePackBinary.WriteBytes(stream, array.Array, array.Offset, array.Count);
189-
}
190-
}
191-
else
175+
foreach (var serializedMessage in serializedHubMessages)
192176
{
193-
var serializedHubMessages = _messageSerializer.SerializeMessage(message);
194-
195-
MessagePackBinary.WriteMapHeader(stream, serializedHubMessages.Count);
177+
MessagePackBinary.WriteString(stream, serializedMessage.ProtocolName);
196178

197-
foreach (var serializedMessage in serializedHubMessages)
198-
{
199-
MessagePackBinary.WriteString(stream, serializedMessage.ProtocolName);
200-
201-
var isArray = MemoryMarshal.TryGetArray(serializedMessage.Serialized, out var array);
202-
Debug.Assert(isArray);
203-
MessagePackBinary.WriteBytes(stream, array.Array, array.Offset, array.Count);
204-
}
179+
var isArray = MemoryMarshal.TryGetArray(serializedMessage.Serialized, out var array);
180+
Debug.Assert(isArray);
181+
MessagePackBinary.WriteBytes(stream, array.Array, array.Offset, array.Count);
205182
}
206183
}
207184

src/SignalR/server/StackExchangeRedis/src/RedisHubLifetimeManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public RedisHubLifetimeManager(ILogger<RedisHubLifetimeManager<THub>> logger,
5757
}
5858
else
5959
{
60-
_protocol = new RedisProtocol(hubProtocolResolver.AllProtocols);
60+
var supportedProtocols = hubProtocolResolver.AllProtocols.Select(p => p.Name).ToList();
61+
_protocol = new RedisProtocol(new DefaultHubMessageSerializer(hubProtocolResolver, supportedProtocols, null));
6162
}
6263

6364
RedisLog.ConnectingToEndpoints(_logger, options.Value.Configuration.EndPoints, _serverName);

src/SignalR/server/StackExchangeRedis/test/DefaultHubMessageSerializerTests.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using Microsoft.AspNetCore.SignalR.Internal;
1010
using Microsoft.AspNetCore.SignalR.Protocol;
1111
using Microsoft.Extensions.Logging.Abstractions;
12-
using Microsoft.Extensions.Options;
1312
using Xunit;
1413

1514
namespace Microsoft.AspNetCore.SignalR.Tests.Internal
@@ -37,6 +36,29 @@ public void SerializeMessages(string testName)
3736
Assert.Equal(testData.Encoded, allBytes);
3837
}
3938

39+
[Fact]
40+
public void GlobalSupportedProtocolsOverriddenByHubSupportedProtocols()
41+
{
42+
var testData = _invocationTestData["Single supported protocol"];
43+
44+
var resolver = CreateHubProtocolResolver(new List<IHubProtocol> { new MessagePackHubProtocol(), new JsonHubProtocol() });
45+
46+
var serializer = new DefaultHubMessageSerializer(resolver, new List<string>() { "json" }, new List<string>() { "messagepack" });
47+
var serializedHubMessage = serializer.SerializeMessage(_testMessage);
48+
49+
Assert.Equal(1, serializedHubMessage.Count);
50+
51+
Assert.Equal(new List<byte>() { 0x0D,
52+
0x96,
53+
0x01,
54+
0x80,
55+
0xC0,
56+
0xA6, (byte)'t', (byte)'a', (byte)'r', (byte)'g', (byte)'e', (byte)'t',
57+
0x90,
58+
0x90 },
59+
serializedHubMessage[0].Serialized.ToArray());
60+
}
61+
4062
private IHubProtocolResolver CreateHubProtocolResolver(List<IHubProtocol> hubProtocols)
4163
{
4264
return new DefaultHubProtocolResolver(hubProtocols, NullLogger<DefaultHubProtocolResolver>.Instance);

src/SignalR/server/StackExchangeRedis/test/RedisProtocolTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class RedisProtocolTests
3232
public void ParseAck(string testName)
3333
{
3434
var testData = _ackTestData[testName];
35-
var protocol = new RedisProtocol(Array.Empty<IHubProtocol>());
35+
var protocol = new RedisProtocol(CreateHubMessageSerializer(new List<IHubProtocol>()));
3636

3737
var decoded = protocol.ReadAck(testData.Encoded);
3838

@@ -44,7 +44,7 @@ public void ParseAck(string testName)
4444
public void WriteAck(string testName)
4545
{
4646
var testData = _ackTestData[testName];
47-
var protocol = new RedisProtocol(Array.Empty<IHubProtocol>());
47+
var protocol = new RedisProtocol(CreateHubMessageSerializer(new List<IHubProtocol>()));
4848

4949
var encoded = protocol.WriteAck(testData.Decoded);
5050

@@ -64,7 +64,7 @@ public void WriteAck(string testName)
6464
public void ParseGroupCommand(string testName)
6565
{
6666
var testData = _groupCommandTestData[testName];
67-
var protocol = new RedisProtocol(Array.Empty<IHubProtocol>());
67+
var protocol = new RedisProtocol(CreateHubMessageSerializer(new List<IHubProtocol>()));
6868

6969
var decoded = protocol.ReadGroupCommand(testData.Encoded);
7070

@@ -80,7 +80,7 @@ public void ParseGroupCommand(string testName)
8080
public void WriteGroupCommand(string testName)
8181
{
8282
var testData = _groupCommandTestData[testName];
83-
var protocol = new RedisProtocol(Array.Empty<IHubProtocol>());
83+
var protocol = new RedisProtocol(CreateHubMessageSerializer(new List<IHubProtocol>()));
8484

8585
var encoded = protocol.WriteGroupCommand(testData.Decoded);
8686

@@ -140,7 +140,7 @@ public void ParseInvocation(string testName)
140140
{
141141
var testData = _invocationTestData[testName];
142142
var hubProtocols = new[] { new DummyHubProtocol("p1"), new DummyHubProtocol("p2") };
143-
var protocol = new RedisProtocol(hubProtocols);
143+
var protocol = new RedisProtocol(CreateHubMessageSerializer(hubProtocols.Cast<IHubProtocol>().ToList()));
144144

145145
var expected = testData.Decoded();
146146

@@ -171,7 +171,7 @@ public void ParseInvocation(string testName)
171171
public void WriteInvocation(string testName)
172172
{
173173
var testData = _invocationTestData[testName];
174-
var protocol = new RedisProtocol(new [] { new DummyHubProtocol("p1"), new DummyHubProtocol("p2") });
174+
var protocol = new RedisProtocol(CreateHubMessageSerializer(new List<IHubProtocol>() { new DummyHubProtocol("p1"), new DummyHubProtocol("p2") }));
175175

176176
// Actual invocation doesn't matter because we're using a dummy hub protocol.
177177
// But the dummy protocol will check that we gave it the test message to make sure everything flows through properly.

0 commit comments

Comments
 (0)