Skip to content

Commit f50a128

Browse files
committed
TInvocator runtime factory context, tests, connector endpoint settings
1 parent 8794ab9 commit f50a128

32 files changed

+495
-151
lines changed

NetCoreStack.WebSockets.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26730.12
4+
VisualStudioVersion = 15.0.27130.2027
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{52C54080-F48B-4DBA-A418-8057611CAF6D}"
77
EndProject
@@ -27,6 +27,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleAppProxyClient", "te
2727
EndProject
2828
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreStack.WebSockets.ProxyClient", "src\NetCoreStack.WebSockets.ProxyClient\NetCoreStack.WebSockets.ProxyClient.csproj", "{1738B969-D294-4417-999C-A3AC3B04EFE4}"
2929
EndProject
30+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCoreStack.WebSockets.Tests", "test\NetCoreStack.WebSockets.Tests\NetCoreStack.WebSockets.Tests.csproj", "{8A756067-8C6F-475C-B2CF-97B582A4DB47}"
31+
EndProject
3032
Global
3133
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3234
Debug|Any CPU = Debug|Any CPU
@@ -61,6 +63,10 @@ Global
6163
{1738B969-D294-4417-999C-A3AC3B04EFE4}.Debug|Any CPU.Build.0 = Debug|Any CPU
6264
{1738B969-D294-4417-999C-A3AC3B04EFE4}.Release|Any CPU.ActiveCfg = Release|Any CPU
6365
{1738B969-D294-4417-999C-A3AC3B04EFE4}.Release|Any CPU.Build.0 = Release|Any CPU
66+
{8A756067-8C6F-475C-B2CF-97B582A4DB47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
67+
{8A756067-8C6F-475C-B2CF-97B582A4DB47}.Debug|Any CPU.Build.0 = Debug|Any CPU
68+
{8A756067-8C6F-475C-B2CF-97B582A4DB47}.Release|Any CPU.ActiveCfg = Release|Any CPU
69+
{8A756067-8C6F-475C-B2CF-97B582A4DB47}.Release|Any CPU.Build.0 = Release|Any CPU
6470
EndGlobalSection
6571
GlobalSection(SolutionProperties) = preSolution
6672
HideSolutionNode = FALSE
@@ -73,6 +79,7 @@ Global
7379
{7ACFEED0-C1D7-43DE-95D7-3D2CEFDF6F9F} = {A1704016-03C8-4917-9C50-AE9DDF55ECB7}
7480
{1AAECD28-5C1D-4ED6-8CBC-6FCAA3804ABF} = {A1704016-03C8-4917-9C50-AE9DDF55ECB7}
7581
{1738B969-D294-4417-999C-A3AC3B04EFE4} = {52C54080-F48B-4DBA-A418-8057611CAF6D}
82+
{8A756067-8C6F-475C-B2CF-97B582A4DB47} = {A1704016-03C8-4917-9C50-AE9DDF55ECB7}
7683
EndGlobalSection
7784
GlobalSection(ExtensibilityGlobals) = postSolution
7885
SolutionGuid = {F299AD98-9AB4-4C7B-B125-5E82EB9B8700}

src/NetCoreStack.WebSockets.ProxyClient/ApplicationBuilderExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public static IApplicationBuilder UseProxyWebSockets(this IApplicationBuilder ap
2424
IList<IWebSocketConnector> connectors = InvocatorFactory.GetConnectors(app.ApplicationServices);
2525
foreach (var connector in connectors)
2626
{
27+
InvocatorsHelper.EnsureHostPair(connector.GetInvocatorContext());
2728
appLifeTime.ApplicationStopping.Register(OnShutdown, connector);
2829
Task.Factory.StartNew(async () => await connector.ConnectAsync(cancellationTokenSource), TaskCreationOptions.LongRunning);
2930
}

src/NetCoreStack.WebSockets.ProxyClient/ClientWebSocketConnector.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,17 @@ public ClientWebSocketConnector(IServiceProvider serviceProvider,
4545
_loggerFactory = loggerFactory;
4646
}
4747

48-
protected abstract InvocatorContext CreateInvocatorContext();
48+
public abstract InvocatorContext GetInvocatorContext();
4949

5050
private async Task<WebSocketReceiver> TryConnectAsync(CancellationTokenSource cancellationTokenSource = null)
5151
{
52-
var invocatorContext = CreateInvocatorContext();
53-
var uri = new Uri($"ws://{invocatorContext.HostAddress}");
52+
var invocatorContext = GetInvocatorContext();
5453
_webSocket = new ClientWebSocket();
5554
_webSocket.Options.SetRequestHeader(SocketsConstants.ConnectorName, invocatorContext.ConnectorName);
5655
try
5756
{
5857
CancellationToken token = cancellationTokenSource != null ? cancellationTokenSource.Token : CancellationToken.None;
59-
await _webSocket.ConnectAsync(uri, token);
58+
await _webSocket.ConnectAsync(invocatorContext.Uri, token);
6059
}
6160
catch (Exception ex)
6261
{

src/NetCoreStack.WebSockets.ProxyClient/ClientWebSocketConnectorOfInvocator.cs

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Microsoft.Extensions.Logging;
2+
using NetCoreStack.WebSockets.Interfaces;
3+
using System;
4+
5+
namespace NetCoreStack.WebSockets.ProxyClient
6+
{
7+
public class ClientWebSocketConnectorOfT<TInvocator> : ClientWebSocketConnector,
8+
IWebSocketConnector<TInvocator> where TInvocator : IClientWebSocketCommandInvocator
9+
{
10+
private readonly IClientInvocatorContextFactory<TInvocator> _invocatorContextFactory;
11+
12+
public ProxyOptions<TInvocator> Options { get; }
13+
14+
public override InvocatorContext GetInvocatorContext()
15+
{
16+
return _invocatorContextFactory.CreateInvocatorContext();
17+
}
18+
19+
public ClientWebSocketConnectorOfT(IServiceProvider serviceProvider,
20+
IClientInvocatorContextFactory<TInvocator> invocatorContextFactory,
21+
IStreamCompressor compressor,
22+
ILoggerFactory loggerFactory)
23+
: base(serviceProvider, compressor, loggerFactory)
24+
{
25+
_invocatorContextFactory = invocatorContextFactory ?? throw new ArgumentNullException(nameof(invocatorContextFactory));
26+
}
27+
}
28+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.Extensions.Options;
2+
using System;
3+
4+
namespace NetCoreStack.WebSockets.ProxyClient
5+
{
6+
internal class DefaultClientInvocatorContextFactory<TInvocator> : IClientInvocatorContextFactory<TInvocator>
7+
where TInvocator : IClientWebSocketCommandInvocator
8+
{
9+
private readonly ProxyOptions<TInvocator> _proxyOptions;
10+
11+
public DefaultClientInvocatorContextFactory(IOptions<ProxyOptions<TInvocator>> options)
12+
{
13+
if (options == null)
14+
{
15+
throw new ArgumentNullException(nameof(options));
16+
}
17+
18+
_proxyOptions = options.Value;
19+
}
20+
21+
public InvocatorContext CreateInvocatorContext()
22+
{
23+
return new InvocatorContext(_proxyOptions.Invocator, _proxyOptions.ConnectorName, _proxyOptions.WebSocketHostAddress);
24+
}
25+
}
26+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace NetCoreStack.WebSockets.ProxyClient
2+
{
3+
public interface IClientInvocatorContextFactory<TInvocator> where TInvocator : IClientWebSocketCommandInvocator
4+
{
5+
InvocatorContext CreateInvocatorContext();
6+
}
7+
}

src/NetCoreStack.WebSockets.ProxyClient/IWebSocketConnector.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ public interface IWebSocketConnector
1111
Task ConnectAsync(CancellationTokenSource cancellationTokenSource);
1212
Task SendAsync(WebSocketMessageContext context);
1313
Task SendBinaryAsync(byte[] bytes);
14+
InvocatorContext GetInvocatorContext();
1415
}
1516

16-
public interface IWebSocketConnector<THandler> : IWebSocketConnector where THandler : IClientWebSocketCommandInvocator
17+
public interface IWebSocketConnector<TInvocator> : IWebSocketConnector where TInvocator : IClientWebSocketCommandInvocator
1718
{
18-
ProxyOptions<THandler> Options { get; }
19+
ProxyOptions<TInvocator> Options { get; }
1920
}
2021
}

src/NetCoreStack.WebSockets.ProxyClient/InvocatorRegistryHelper.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace NetCoreStack.WebSockets.ProxyClient
6+
{
7+
public static class InvocatorsHelper
8+
{
9+
private static readonly InvocatorTypes _invocators = new InvocatorTypes(StringComparer.OrdinalIgnoreCase);
10+
11+
private class InvocatorTypes : Dictionary<string, ConnectorHostPair>
12+
{
13+
public InvocatorTypes(IEqualityComparer<string> equalityComparer)
14+
:base(equalityComparer)
15+
{
16+
17+
}
18+
}
19+
20+
public static void EnsureHostPair(Type invocator, string connectorName, string hostAddress)
21+
{
22+
var connectorHostPair = new ConnectorHostPair(connectorName, hostAddress, invocator);
23+
var key = connectorHostPair.Key;
24+
25+
if (_invocators.TryGetValue(key, out ConnectorHostPair value))
26+
{
27+
// throw new InvalidOperationException($"\"{connectorName}\" is already registered with same Host and Invocator");
28+
return;
29+
}
30+
31+
_invocators.Add(key, connectorHostPair);
32+
}
33+
34+
public static void EnsureHostPair(InvocatorContext context)
35+
{
36+
if (context == null)
37+
{
38+
throw new ArgumentNullException(nameof(context));
39+
}
40+
41+
var invocator = context.Invocator;
42+
var connectorName = context.ConnectorName;
43+
var hostAddress = context.HostAddress;
44+
45+
EnsureHostPair(invocator, connectorName, hostAddress);
46+
}
47+
48+
public static List<Type> GetInvocators()
49+
{
50+
return _invocators.Select(p => p.Value.Invocator).ToList();
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)