Skip to content

Commit 1450e1b

Browse files
committed
client-server isolation, awaiter fix
1 parent a090b5d commit 1450e1b

33 files changed

+232
-85
lines changed

src/NetCoreStack.WebSockets.ProxyClient/ClientWebSocketConnector.cs

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

48-
public abstract InvocatorContext InvocatorContext { get; }
48+
public abstract ClientInvocatorContext InvocatorContext { get; }
4949

50-
private async Task<WebSocketReceiver> TryConnectAsync(CancellationTokenSource cancellationTokenSource = null)
50+
private async Task<ClientWebSocketReceiver> TryConnectAsync(CancellationTokenSource cancellationTokenSource = null)
5151
{
5252
_webSocket = new ClientWebSocket();
5353
_webSocket.Options.SetRequestHeader(NCSConstants.ConnectorName, InvocatorContext.ConnectorName);
@@ -62,15 +62,15 @@ private async Task<WebSocketReceiver> TryConnectAsync(CancellationTokenSource ca
6262
return null;
6363
}
6464

65-
var receiverContext = new WebSocketReceiverContext
65+
var receiverContext = new ClientWebSocketReceiverContext
6666
{
6767
Compressor = _compressor,
6868
InvocatorContext = InvocatorContext,
6969
LoggerFactory = _loggerFactory,
7070
WebSocket = _webSocket
7171
};
7272

73-
var receiver = new WebSocketReceiver(_serviceProvider, receiverContext, Close, (connectionId) => {
73+
var receiver = new ClientWebSocketReceiver(_serviceProvider, receiverContext, Close, (connectionId) => {
7474
_connectionId = connectionId;
7575
});
7676

@@ -82,7 +82,7 @@ public async Task ConnectAsync(CancellationTokenSource cancellationTokenSource =
8282
if (cancellationTokenSource == null)
8383
cancellationTokenSource = new CancellationTokenSource();
8484

85-
WebSocketReceiver receiver = null;
85+
ClientWebSocketReceiver receiver = null;
8686
while (!cancellationTokenSource.IsCancellationRequested)
8787
{
8888
receiver = await TryConnectAsync(cancellationTokenSource);
@@ -138,10 +138,10 @@ public async Task SendBinaryAsync(byte[] bytes)
138138
await _webSocket.SendAsync(segments, WebSocketMessageType.Binary, true, CancellationToken.None);
139139
}
140140

141-
internal void Close(WebSocketReceiverContext context)
141+
internal void Close(ClientWebSocketReceiverContext context)
142142
{
143143
context.WebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure,
144-
nameof(WebSocketReceiverContext),
144+
nameof(ClientWebSocketReceiverContext),
145145
CancellationToken.None);
146146
}
147147

src/NetCoreStack.WebSockets.ProxyClient/ClientWebSocketConnectorOfT.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class ClientWebSocketConnectorOfT<TInvocator> : ClientWebSocketConnector,
99
{
1010
private readonly IClientInvocatorContextFactory<TInvocator> _invocatorContextFactory;
1111

12-
public override InvocatorContext InvocatorContext { get; }
12+
public override ClientInvocatorContext InvocatorContext { get; }
1313

1414
public ClientWebSocketConnectorOfT(IServiceProvider serviceProvider,
1515
IClientInvocatorContextFactory<TInvocator> invocatorContextFactory,

src/NetCoreStack.WebSockets.ProxyClient/ClientWebSocketReceiver.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.Extensions.Logging;
2+
using NetCoreStack.WebSockets.Internal;
23
using Newtonsoft.Json;
34
using System;
45
using System.Collections.Generic;
@@ -7,19 +8,19 @@
78
using System.Threading;
89
using System.Threading.Tasks;
910

10-
namespace NetCoreStack.WebSockets.Internal
11+
namespace NetCoreStack.WebSockets.ProxyClient
1112
{
1213
public class ClientWebSocketReceiver
1314
{
1415
private readonly IServiceProvider _serviceProvider;
15-
private readonly WebSocketReceiverContext _context;
16-
private readonly Action<WebSocketReceiverContext> _closeCallback;
16+
private readonly ClientWebSocketReceiverContext _context;
17+
private readonly Action<ClientWebSocketReceiverContext> _closeCallback;
1718
private readonly Action<string> _handshakeCallback;
1819
private readonly ILogger<ClientWebSocketReceiver> _logger;
1920

20-
public ClientWebSocketReceiver(IServiceProvider serviceProvider,
21-
WebSocketReceiverContext context,
22-
Action<WebSocketReceiverContext> closeCallback,
21+
public ClientWebSocketReceiver(IServiceProvider serviceProvider,
22+
ClientWebSocketReceiverContext context,
23+
Action<ClientWebSocketReceiverContext> closeCallback,
2324
Action<string> handshakeCallback = null)
2425
{
2526
_serviceProvider = serviceProvider;
@@ -49,7 +50,10 @@ public async Task ReceiveAsync()
4950
}
5051

5152
var invocator = _context.GetInvocator(_serviceProvider);
52-
invocator?.InvokeAsync(context);
53+
if (invocator != null)
54+
{
55+
await invocator.InvokeAsync(context);
56+
}
5357
}
5458
catch (Exception ex)
5559
{
@@ -84,7 +88,10 @@ public async Task ReceiveAsync()
8488
{
8589
var context = await result.ToBinaryContextAsync(_context.Compressor, binaryResult);
8690
var invocator = _context.GetInvocator(_serviceProvider);
87-
invocator?.InvokeAsync(context);
91+
if (invocator != null)
92+
{
93+
await invocator.InvokeAsync(context);
94+
}
8895
}
8996
catch (Exception ex)
9097
{
@@ -99,11 +106,6 @@ public async Task ReceiveAsync()
99106
}
100107
catch (Exception ex)
101108
{
102-
if (ex is TaskCanceledException)
103-
{
104-
return;
105-
}
106-
107109
var dictionary = new Dictionary<string, string>();
108110
dictionary.Add(nameof(_context.ConnectionId), _context.ConnectionId);
109111

src/NetCoreStack.WebSockets.ProxyClient/DefaultClientInvocatorContextFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public DefaultClientInvocatorContextFactory(IOptions<ProxyOptions<TInvocator>> o
1818
_proxyOptions = options.Value;
1919
}
2020

21-
public InvocatorContext CreateInvocatorContext()
21+
public ClientInvocatorContext CreateInvocatorContext()
2222
{
23-
return new InvocatorContext(_proxyOptions.Invocator, _proxyOptions.ConnectorName, _proxyOptions.WebSocketHostAddress);
23+
return new ClientInvocatorContext(_proxyOptions.Invocator, _proxyOptions.ConnectorName, _proxyOptions.WebSocketHostAddress);
2424
}
2525
}
2626
}

src/NetCoreStack.WebSockets.ProxyClient/ApplicationBuilderExtensions.cs renamed to src/NetCoreStack.WebSockets.ProxyClient/Extensions/ApplicationBuilderExtensions.cs

File renamed without changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
3+
namespace NetCoreStack.WebSockets.ProxyClient
4+
{
5+
public static class ClientWebSocketReceiverExtensions
6+
{
7+
public static IClientWebSocketCommandInvocator GetInvocator(this ClientWebSocketReceiverContext context, IServiceProvider serviceProvider)
8+
{
9+
if (context.InvocatorContext != null)
10+
{
11+
var instance = serviceProvider.GetService(context.InvocatorContext.Invocator);
12+
if (instance != null && instance is IClientWebSocketCommandInvocator)
13+
{
14+
return (IClientWebSocketCommandInvocator)instance;
15+
}
16+
}
17+
18+
return null;
19+
}
20+
}
21+
}

src/NetCoreStack.WebSockets.ProxyClient/ConsoleApplicationBuilderExtensions.cs renamed to src/NetCoreStack.WebSockets.ProxyClient/Extensions/ConsoleApplicationBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Threading;
44
using System.Threading.Tasks;
55

6-
namespace NetCoreStack.WebSockets.ProxyClient.Console
6+
namespace NetCoreStack.WebSockets.ProxyClient
77
{
88
public static class ConsoleApplicationBuilderExtensions
99
{

src/NetCoreStack.WebSockets.ProxyClient/ServiceCollectionExtensions.cs renamed to src/NetCoreStack.WebSockets.ProxyClient/Extensions/ServiceCollectionExtensions.cs

File renamed without changes.

src/NetCoreStack.WebSockets.ProxyClient/IClientInvocatorContextFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
{
33
public interface IClientInvocatorContextFactory<TInvocator> where TInvocator : IClientWebSocketCommandInvocator
44
{
5-
InvocatorContext CreateInvocatorContext();
5+
ClientInvocatorContext CreateInvocatorContext();
66
}
77
}

src/NetCoreStack.WebSockets.ProxyClient/IWebSocketConnector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public interface IWebSocketConnector
1111
Task ConnectAsync(CancellationTokenSource cancellationTokenSource);
1212
Task SendAsync(WebSocketMessageContext context);
1313
Task SendBinaryAsync(byte[] bytes);
14-
InvocatorContext InvocatorContext { get; }
14+
ClientInvocatorContext InvocatorContext { get; }
1515
}
1616

1717
public interface IWebSocketConnector<TInvocator> : IWebSocketConnector where TInvocator : IClientWebSocketCommandInvocator

0 commit comments

Comments
 (0)