1- using NetCoreStack . WebSockets . Interfaces ;
1+ using Microsoft . Extensions . Logging ;
2+ using Microsoft . Extensions . Options ;
3+ using NetCoreStack . WebSockets . Interfaces ;
24using NetCoreStack . WebSockets . Internal ;
35using Newtonsoft . Json ;
46using System ;
@@ -14,13 +16,25 @@ namespace NetCoreStack.WebSockets
1416{
1517 public class ConnectionManager : IConnectionManager
1618 {
17- protected IStreamCompressor Compressor { get ; }
18- protected ConcurrentDictionary < string , WebSocketTransport > Connections { get ; }
19-
20- public ConnectionManager ( IStreamCompressor compressor )
19+ private readonly InvocatorRegistry _invocatorRegistry ;
20+ private readonly ServerSocketsOptions _options ;
21+ private readonly IHandshakeStateTransport _initState ;
22+ private readonly ILoggerFactory _loggerFactory ;
23+ private readonly IStreamCompressor _compressor ;
24+ private readonly ConcurrentDictionary < string , WebSocketTransport > _connections ;
25+
26+ public ConnectionManager ( IStreamCompressor compressor ,
27+ InvocatorRegistry invocatorRegistry ,
28+ IOptions < ServerSocketsOptions > options ,
29+ IHandshakeStateTransport initState ,
30+ ILoggerFactory loggerFactory )
2131 {
22- Compressor = compressor ;
23- Connections = new ConcurrentDictionary < string , WebSocketTransport > ( ) ;
32+ _invocatorRegistry = invocatorRegistry ;
33+ _options = options . Value ;
34+ _initState = initState ;
35+ _loggerFactory = loggerFactory ;
36+ _compressor = compressor ;
37+ _connections = new ConcurrentDictionary < string , WebSocketTransport > ( ) ;
2438 }
2539
2640 private async Task < byte [ ] > PrepareBytesAsync ( byte [ ] input , JsonObject properties )
@@ -36,17 +50,7 @@ private async Task<byte[]> PrepareBytesAsync(byte[] input, JsonObject properties
3650 var bytesCount = input . Length ;
3751 input = propsBytes . Concat ( input ) . ToArray ( ) ;
3852
39- return await Compressor . CompressAsync ( input ) ;
40-
41- //if (input.Length > SocketsConstants.CompressorThreshold)
42- //{
43- // using (MemoryStream ms = new MemoryStream(input))
44- // {
45- // return await Compressor.CompressAsync(ms);
46- // }
47- //}
48- //else
49- // return input;
53+ return await _compressor . CompressAsync ( input ) ;
5054 }
5155
5256 private async Task SendAsync ( WebSocketTransport transport , WebSocketMessageDescriptor descriptor )
@@ -82,6 +86,31 @@ await transport.WebSocket.SendAsync(segments,
8286 token ) ;
8387 }
8488
89+ public async Task ConnectAsync ( WebSocket webSocket )
90+ {
91+ WebSocketTransport transport = new WebSocketTransport ( webSocket ) ;
92+ var connectionId = transport . ConnectionId ;
93+ var context = new WebSocketMessageContext ( ) ;
94+ context . Command = WebSocketCommands . Handshake ;
95+ context . Value = connectionId ;
96+ context . State = await _initState . GetStateAsync ( ) ;
97+ _connections . TryAdd ( connectionId , transport ) ;
98+
99+ await SendAsync ( connectionId , context ) ;
100+
101+ var receiverContext = new WebSocketReceiverContext
102+ {
103+ Compressor = _compressor ,
104+ ConnectionId = connectionId ,
105+ InvocatorRegistry = _invocatorRegistry ,
106+ LoggerFactory = _loggerFactory ,
107+ Options = _options ,
108+ WebSocket = webSocket
109+ } ;
110+ var receiver = new WebSocketReceiver ( receiverContext , CloseConnection ) ;
111+ await receiver . ReceiveAsync ( ) ;
112+ }
113+
85114 public async Task BroadcastAsync ( WebSocketMessageContext context )
86115 {
87116 if ( context == null )
@@ -94,7 +123,7 @@ public async Task BroadcastAsync(WebSocketMessageContext context)
94123 throw new ArgumentNullException ( nameof ( context . Value ) ) ;
95124 }
96125
97- if ( ! Connections . Any ( ) )
126+ if ( ! _connections . Any ( ) )
98127 {
99128 return ;
100129 }
@@ -107,15 +136,15 @@ public async Task BroadcastAsync(WebSocketMessageContext context)
107136 MessageType = WebSocketMessageType . Text
108137 } ;
109138
110- foreach ( var connection in Connections )
139+ foreach ( var connection in _connections )
111140 {
112141 await SendAsync ( connection . Value , descriptor ) ;
113142 }
114143 }
115144
116145 public async Task BroadcastBinaryAsync ( byte [ ] inputs , JsonObject properties )
117146 {
118- if ( ! Connections . Any ( ) )
147+ if ( ! _connections . Any ( ) )
119148 {
120149 return ;
121150 }
@@ -136,7 +165,7 @@ public async Task BroadcastBinaryAsync(byte[] inputs, JsonObject properties)
136165 if ( chunkedBytes . Length < SocketsConstants . ChunkSize )
137166 endOfMessage = true ;
138167
139- foreach ( var connection in Connections )
168+ foreach ( var connection in _connections )
140169 {
141170 await SendBinaryAsync ( connection . Value , chunkedBytes , endOfMessage , CancellationToken . None ) ;
142171 }
@@ -151,13 +180,13 @@ public async Task BroadcastBinaryAsync(byte[] inputs, JsonObject properties)
151180
152181 public async Task SendAsync ( string connectionId , WebSocketMessageContext context )
153182 {
154- if ( ! Connections . Any ( ) )
183+ if ( ! _connections . Any ( ) )
155184 {
156185 return ;
157186 }
158187
159188 WebSocketTransport transport = null ;
160- if ( ! Connections . TryGetValue ( connectionId , out transport ) )
189+ if ( ! _connections . TryGetValue ( connectionId , out transport ) )
161190 {
162191 throw new ArgumentOutOfRangeException ( nameof ( transport ) ) ;
163192 }
@@ -175,13 +204,13 @@ public async Task SendAsync(string connectionId, WebSocketMessageContext context
175204
176205 public async Task SendBinaryAsync ( string connectionId , byte [ ] input , JsonObject properties )
177206 {
178- if ( ! Connections . Any ( ) )
207+ if ( ! _connections . Any ( ) )
179208 {
180209 return ;
181210 }
182211
183212 WebSocketTransport transport = null ;
184- if ( ! Connections . TryGetValue ( connectionId , out transport ) )
213+ if ( ! _connections . TryGetValue ( connectionId , out transport ) )
185214 {
186215 throw new ArgumentOutOfRangeException ( nameof ( transport ) ) ;
187216 }
@@ -216,39 +245,18 @@ await transport.WebSocket.SendAsync(segments,
216245 }
217246 }
218247
219- public async Task SendAsync ( string connectionId , WebSocketMessageContext context , WebSocket webSocket )
248+ public void CloseConnection ( string connectionId )
220249 {
221- if ( string . IsNullOrEmpty ( connectionId ) )
222- {
223- throw new ArgumentNullException ( nameof ( connectionId ) ) ;
224- }
225-
226- if ( context == null )
227- {
228- throw new ArgumentNullException ( nameof ( context ) ) ;
229- }
230-
231- if ( webSocket == null )
232- {
233- throw new ArgumentNullException ( nameof ( webSocket ) ) ;
234- }
235-
236250 WebSocketTransport transport = null ;
237- if ( ! Connections . TryGetValue ( connectionId , out transport ) )
251+ if ( _connections . TryRemove ( connectionId , out transport ) )
238252 {
239- Connections . TryAdd ( connectionId , new WebSocketTransport ( webSocket ) ) ;
253+ transport . Dispose ( ) ;
240254 }
241-
242- await SendAsync ( connectionId , context ) ;
243255 }
244256
245- public void CloseConnection ( string connectionId )
257+ public void CloseConnection ( WebSocketReceiverContext context )
246258 {
247- WebSocketTransport transport = null ;
248- if ( Connections . TryRemove ( connectionId , out transport ) )
249- {
250- transport . Dispose ( ) ;
251- }
259+ CloseConnection ( context . ConnectionId ) ;
252260 }
253261 }
254262}
0 commit comments