Skip to content

Commit b62b60f

Browse files
committed
connection reroute test cases
1 parent 3cbbbcb commit b62b60f

35 files changed

+811
-1538
lines changed

.editorconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ csharp_new_line_before_members_in_object_initializers = true
5151
csharp_new_line_before_members_in_anonymous_types = true
5252

5353
# Namespace settings
54-
csharp_style_namespace_declarations = file_scoped:silent
54+
csharp_style_namespace_declarations = file_scoped:error
5555

5656
# Brace settings
5757
csharp_prefer_braces = true:silent# Prefer curly braces even for one line of code
@@ -102,6 +102,7 @@ csharp_prefer_simple_default_expression = true:suggestion
102102
csharp_style_prefer_local_over_anonymous_function = true:suggestion
103103
csharp_style_prefer_index_operator = true:suggestion
104104
csharp_style_prefer_range_operator = true:suggestion
105+
csharp_space_around_binary_operators = before_and_after
105106

106107
[*.{xml,config,*proj,nuspec,props,resx,targets,yml,tasks}]
107108
indent_size = 2

samples/ChatSample/ChatSample.Net70/Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
14
using ClientResultSample;
5+
26
using Microsoft.AspNetCore.SignalR;
37

48
var builder = WebApplication.CreateBuilder(args);

src/Microsoft.Azure.SignalR.AspNet/ServerConnections/ServiceConnection.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft. All rights reserved.
1+
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System;
@@ -10,6 +10,7 @@
1010
using System.Text;
1111
using System.Threading;
1212
using System.Threading.Tasks;
13+
1314
using Microsoft.AspNetCore.Connections;
1415
using Microsoft.Azure.SignalR.Protocol;
1516
using Microsoft.Extensions.Logging;
@@ -21,13 +22,13 @@ internal partial class ServiceConnection : ServiceConnectionBase
2122
{
2223
private const string ReconnectMessage = "asrs:reconnect";
2324

24-
private static readonly Dictionary<string, string> CustomHeader = new Dictionary<string, string>
25-
{{Constants.AsrsUserAgent, ProductInfo.GetProductInfo()}};
25+
private static readonly Dictionary<string, string> CustomHeader = new() {
26+
{Constants.AsrsUserAgent, ProductInfo.GetProductInfo()}
27+
};
2628

2729
private static readonly TimeSpan CloseApplicationTimeout = TimeSpan.FromSeconds(5);
2830

29-
private readonly ConcurrentDictionary<string, ClientConnectionContext> _clientConnections =
30-
new ConcurrentDictionary<string, ClientConnectionContext>(StringComparer.Ordinal);
31+
private readonly ConcurrentDictionary<string, ClientConnectionContext> _clientConnections = new(StringComparer.Ordinal);
3132

3233
private readonly IConnectionFactory _connectionFactory;
3334

@@ -75,6 +76,11 @@ public override bool TryRemoveClientConnection(string connectionId, out IClientC
7576
return r;
7677
}
7778

79+
public override Task CloseClientConnections(CancellationToken token)
80+
{
81+
throw new NotSupportedException();
82+
}
83+
7884
protected override Task<ConnectionContext> CreateConnection(string target = null)
7985
{
8086
return _connectionFactory.ConnectAsync(HubEndpoint, TransferFormat.Binary, ConnectionId, target,
@@ -97,11 +103,6 @@ protected override Task CleanupClientConnections(string fromInstanceId = null)
97103
return Task.CompletedTask;
98104
}
99105

100-
public override Task CloseClientConnections(CancellationToken token)
101-
{
102-
throw new NotSupportedException();
103-
}
104-
105106
protected override Task OnClientConnectedAsync(OpenConnectionMessage openConnectionMessage)
106107
{
107108
// Create empty transport with only channel for async processing messages

src/Microsoft.Azure.SignalR.Common/ClientInvocation/DummyClientInvocationManager.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft. All rights reserved.
1+
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System;
@@ -8,13 +8,14 @@ namespace Microsoft.Azure.SignalR
88
internal sealed class DummyClientInvocationManager : IClientInvocationManager
99
{
1010
public ICallerClientResultsManager Caller => throw new NotSupportedException();
11+
1112
public IRoutedClientResultsManager Router => throw new NotSupportedException();
1213

1314
public DummyClientInvocationManager()
1415
{
1516
}
1617

17-
public void CleanupInvocationsByConnection(string connectionId) => throw new NotSupportedException();
18+
public void CleanupInvocationsByConnection(string connectionId) { }
1819

1920
public bool TryGetInvocationReturnType(string invocationId, out Type type) => throw new NotSupportedException();
2021
}

src/Microsoft.Azure.SignalR.Common/Constants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,4 @@ public static class HttpClientNames
167167

168168
public const string InternalDefault = "InternalDefault";
169169
}
170-
}
170+
}

src/Microsoft.Azure.SignalR.Common/DefaultServerNameProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft. All rights reserved.
1+
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System;
@@ -8,6 +8,7 @@ namespace Microsoft.Azure.SignalR
88
internal class DefaultServerNameProvider : IServerNameProvider
99
{
1010
private readonly string _name = $"{Environment.MachineName}_{Guid.NewGuid():N}";
11+
1112
public string GetName()
1213
{
1314
return _name;

src/Microsoft.Azure.SignalR.Common/ServiceConnections/ServiceConnectionBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft. All rights reserved.
1+
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System;
@@ -449,6 +449,7 @@ private Task ResumeClientConnectionAsync(IClientConnection clientConnection)
449449
{
450450
serviceConnection.TryRemoveClientConnection(clientConnection.ConnectionId, out _);
451451
}
452+
452453
if (TryAddClientConnection(clientConnection))
453454
{
454455
clientConnection.ServiceConnection = this;

src/Microsoft.Azure.SignalR.Management/ManagementConnectionFactory.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
// Copyright (c) Microsoft. All rights reserved.
1+
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System.Collections.Generic;
55
using System.Threading;
66
using System.Threading.Tasks;
7+
78
using Microsoft.AspNetCore.Connections;
89
using Microsoft.Extensions.Options;
910

1011
namespace Microsoft.Azure.SignalR.Management
1112
{
12-
internal class ManagementConnectionFactory : IConnectionFactory
13+
internal class ManagementConnectionFactory(IOptions<ServiceManagerOptions> context,
14+
ConnectionFactory connectionFactory) : IConnectionFactory
1315
{
14-
private readonly string _productInfo;
15-
private readonly ConnectionFactory _connectionFactory;
16-
17-
public ManagementConnectionFactory(IOptions<ServiceManagerOptions> context, ConnectionFactory connectionFactory)
18-
{
19-
_productInfo = context.Value.ProductInfo;
20-
_connectionFactory = connectionFactory;
21-
}
16+
private readonly string _productInfo = context.Value.ProductInfo;
17+
private readonly ConnectionFactory _connectionFactory = connectionFactory;
2218

23-
public Task<ConnectionContext> ConnectAsync(HubServiceEndpoint endpoint, TransferFormat transferFormat, string connectionId, string target, CancellationToken cancellationToken = default, IDictionary<string, string> headers = null)
19+
public Task<ConnectionContext> ConnectAsync(HubServiceEndpoint endpoint,
20+
TransferFormat transferFormat,
21+
string connectionId,
22+
string target,
23+
CancellationToken cancellationToken = default,
24+
IDictionary<string, string> headers = null)
2425
{
2526
if (headers == null)
2627
{

src/Microsoft.Azure.SignalR/ServerConnections/ServiceConnection.cs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft. All rights reserved.
1+
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System;
@@ -8,6 +8,7 @@
88
using System.Linq;
99
using System.Threading;
1010
using System.Threading.Tasks;
11+
1112
using Microsoft.AspNetCore.Connections;
1213
using Microsoft.AspNetCore.Http;
1314
using Microsoft.AspNetCore.SignalR;
@@ -27,16 +28,17 @@ internal partial class ServiceConnection : ServiceConnectionBase
2728

2829
// Fix issue: https://github.com/Azure/azure-signalr/issues/198
2930
// .NET Framework has restriction about reserved string as the header name like "User-Agent"
30-
private static readonly Dictionary<string, string> CustomHeader = new Dictionary<string, string> { { Constants.AsrsUserAgent, ProductInfo.GetProductInfo() } };
31+
private readonly Dictionary<string, string> _customHeader = new() {
32+
{ Constants.AsrsUserAgent, ProductInfo.GetProductInfo() }
33+
};
3134

3235
private readonly IConnectionFactory _connectionFactory;
3336

3437
private readonly IClientConnectionFactory _clientConnectionFactory;
3538

3639
private readonly IClientConnectionManager _clientConnectionManager;
3740

38-
private readonly ConcurrentDictionary<string, string> _connectionIds =
39-
new ConcurrentDictionary<string, string>(StringComparer.Ordinal);
41+
private readonly ConcurrentDictionary<string, string> _connectionIds = new(StringComparer.Ordinal);
4042

4143
private readonly string[] _pingMessages =
4244
new string[4] { ClientConnectionCountInHub, null, ClientConnectionCountInServiceConnection, null };
@@ -85,6 +87,11 @@ public ServiceConnection(IServiceProtocol serviceProtocol,
8587
_hubProtocolResolver = hubProtocolResolver;
8688
}
8789

90+
public void SetCustomHeader(string key, string val)
91+
{
92+
_customHeader[key] = val;
93+
}
94+
8895
public override bool TryAddClientConnection(IClientConnection connection)
8996
{
9097
var r = _clientConnectionManager.TryAddClientConnection(connection);
@@ -102,16 +109,6 @@ public override bool TryRemoveClientConnection(string connectionId, out IClientC
102109
return r;
103110
}
104111

105-
protected override Task<ConnectionContext> CreateConnection(string target = null)
106-
{
107-
return _connectionFactory.ConnectAsync(HubEndpoint, TransferFormat.Binary, ConnectionId, target, headers: CustomHeader);
108-
}
109-
110-
protected override Task DisposeConnection(ConnectionContext connection)
111-
{
112-
return _connectionFactory.DisposeAsync(connection);
113-
}
114-
115112
public override async Task CloseClientConnections(CancellationToken token)
116113
{
117114
var tasks = new List<Task>();
@@ -137,6 +134,16 @@ public override async Task CloseClientConnections(CancellationToken token)
137134
}
138135
}
139136

137+
protected override Task<ConnectionContext> CreateConnection(string target = null)
138+
{
139+
return _connectionFactory.ConnectAsync(HubEndpoint, TransferFormat.Binary, ConnectionId, target, headers: _customHeader);
140+
}
141+
142+
protected override Task DisposeConnection(ConnectionContext connection)
143+
{
144+
return _connectionFactory.DisposeAsync(connection);
145+
}
146+
140147
protected override Task CleanupClientConnections(string fromInstanceId = null)
141148
{
142149
// To gracefully complete client connections, let the client itself owns the connection lifetime
@@ -386,5 +393,4 @@ private Task OnConnectionReconnectAsync(ConnectionReconnectMessage message)
386393
}
387394
return Task.CompletedTask;
388395
}
389-
390396
}

src/Microsoft.Azure.SignalR/ServerConnections/ServiceConnectionFactory.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using System;
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
26
using Microsoft.AspNetCore.Connections;
37
using Microsoft.AspNetCore.Http;
48
using Microsoft.AspNetCore.SignalR;
@@ -59,7 +63,10 @@ public ServiceConnectionFactory(
5963
_hubProtocolResolver = hubProtocolResolver;
6064
}
6165

62-
public virtual IServiceConnection Create(HubServiceEndpoint endpoint, IServiceMessageHandler serviceMessageHandler, AckHandler ackHandler, ServiceConnectionType type)
66+
public virtual IServiceConnection Create(HubServiceEndpoint endpoint,
67+
IServiceMessageHandler serviceMessageHandler,
68+
AckHandler ackHandler,
69+
ServiceConnectionType type)
6370
{
6471
return new ServiceConnection(
6572
_serviceProtocol,

0 commit comments

Comments
 (0)