@@ -47,7 +47,6 @@ public class WebSocketServiceManager
4747 private volatile bool _clean ;
4848 private readonly Dictionary < string , WebSocketServiceHost > _hosts ;
4949 private volatile ServerState _state ;
50- private readonly object _sync ;
5150 private TimeSpan _waitTime ;
5251
5352 internal WebSocketServiceManager ( int fragmentSize , bool keepClean = true )
@@ -56,7 +55,6 @@ internal WebSocketServiceManager(int fragmentSize, bool keepClean = true)
5655 _clean = keepClean ;
5756 _hosts = new Dictionary < string , WebSocketServiceHost > ( ) ;
5857 _state = ServerState . Ready ;
59- _sync = ( ( ICollection ) _hosts ) . SyncRoot ;
6058 _waitTime = TimeSpan . FromSeconds ( 1 ) ;
6159 }
6260
@@ -66,16 +64,7 @@ internal WebSocketServiceManager(int fragmentSize, bool keepClean = true)
6664 /// <value>
6765 /// An <see cref="int"/> that represents the number of the services.
6866 /// </value>
69- public int Count
70- {
71- get
72- {
73- lock ( _sync )
74- {
75- return _hosts . Count ;
76- }
77- }
78- }
67+ public int Count => _hosts . Count ;
7968
8069 /// <summary>
8170 /// Gets the host instances for the Websocket services.
@@ -84,16 +73,7 @@ public int Count
8473 /// An <c>IEnumerable<WebSocketServiceHost></c> instance that provides an enumerator
8574 /// which supports the iteration over the collection of the host instances for the services.
8675 /// </value>
87- public IEnumerable < WebSocketServiceHost > Hosts
88- {
89- get
90- {
91- lock ( _sync )
92- {
93- return _hosts . Values . ToList ( ) ;
94- }
95- }
96- }
76+ public IEnumerable < WebSocketServiceHost > Hosts => _hosts . Values . ToArray ( ) ;
9777
9878 /// <summary>
9979 /// Gets the WebSocket service host with the specified <paramref name="path"/>.
@@ -123,16 +103,7 @@ public WebSocketServiceHost this[string path]
123103 /// An <c>IEnumerable<string></c> instance that provides an enumerator which supports
124104 /// the iteration over the collection of the paths for the services.
125105 /// </value>
126- public IEnumerable < string > Paths
127- {
128- get
129- {
130- lock ( _sync )
131- {
132- return _hosts . Keys . ToList ( ) ;
133- }
134- }
135- }
106+ public IEnumerable < string > Paths => _hosts . Keys . ToArray ( ) ;
136107
137108 /// <summary>
138109 /// Gets the total number of the sessions in the WebSocket services.
@@ -163,14 +134,15 @@ public TimeSpan WaitTime
163134
164135 internal set
165136 {
166- lock ( _sync )
137+ if ( value == _waitTime )
167138 {
168- if ( value == _waitTime )
169- return ;
139+ return ;
140+ }
170141
171- _waitTime = value ;
172- foreach ( var host in _hosts . Values )
173- host . WaitTime = value ;
142+ _waitTime = value ;
143+ foreach ( var host in _hosts . Values . ToArray ( ) )
144+ {
145+ host . WaitTime = value ;
174146 }
175147 }
176148 }
@@ -306,46 +278,40 @@ private bool TryGetServiceHost(string path, out WebSocketServiceHost host)
306278 internal void Add < TBehavior > ( string path , Func < TBehavior > initializer )
307279 where TBehavior : WebSocketBehavior
308280 {
309- lock ( _sync )
310- {
311- path = HttpUtility . UrlDecode ( path ) . TrimEndSlash ( ) ;
312-
313- WebSocketServiceHost host ;
314- if ( _hosts . TryGetValue ( path , out host ) )
315- {
316- return ;
317- }
281+ path = HttpUtility . UrlDecode ( path ) . TrimEndSlash ( ) ;
318282
319- host = new WebSocketServiceHost < TBehavior > ( path , _fragmentSize , initializer ) ;
320- if ( ! _clean )
321- host . KeepClean = false ;
283+ WebSocketServiceHost host ;
284+ if ( _hosts . TryGetValue ( path , out host ) )
285+ {
286+ return ;
287+ }
322288
323- if ( _waitTime != host . WaitTime )
324- host . WaitTime = _waitTime ;
289+ host = new WebSocketServiceHost < TBehavior > ( path , _fragmentSize , initializer ) ;
290+ if ( ! _clean )
291+ {
292+ host . KeepClean = false ;
293+ }
325294
326- if ( _state == ServerState . Start )
327- host . Start ( ) ;
295+ host . WaitTime = _waitTime ;
328296
329- _hosts . Add ( path , host ) ;
297+ if ( _state == ServerState . Start )
298+ {
299+ host . Start ( ) ;
330300 }
301+
302+ _hosts . Add ( path , host ) ;
331303 }
332304
333305 internal bool InternalTryGetServiceHost ( string path , out WebSocketServiceHost host )
334306 {
335- bool res ;
336- lock ( _sync )
337- {
338- path = HttpUtility . UrlDecode ( path ) . TrimEndSlash ( ) ;
339- res = _hosts . TryGetValue ( path , out host ) ;
340- }
341-
342- return res ;
307+ path = HttpUtility . UrlDecode ( path ) . TrimEndSlash ( ) ;
308+ return _hosts . TryGetValue ( path , out host ) ;
343309 }
344310
345311 internal async Task < bool > Remove ( string path )
346312 {
347313 WebSocketServiceHost host ;
348- lock ( _sync )
314+ lock ( _hosts )
349315 {
350316 path = HttpUtility . UrlDecode ( path ) . TrimEndSlash ( ) ;
351317 if ( ! _hosts . TryGetValue ( path , out host ) )
@@ -366,30 +332,26 @@ internal async Task<bool> Remove(string path)
366332
367333 internal void Start ( )
368334 {
369- lock ( _sync )
335+ foreach ( var host in _hosts . Values . ToArray ( ) )
370336 {
371- foreach ( var host in _hosts . Values )
372- host . Start ( ) ;
373-
374- _state = ServerState . Start ;
337+ host . Start ( ) ;
375338 }
339+
340+ _state = ServerState . Start ;
376341 }
377342
378343 internal async Task Stop ( CloseEventArgs e , bool send , bool wait )
379344 {
380- using ( var ml = new MonitorLock ( _sync ) )
381- {
382- _state = ServerState . ShuttingDown ;
345+ _state = ServerState . ShuttingDown ;
383346
384- var bytes =
385- send ? await WebSocketFrame . CreateCloseFrame ( e . PayloadData , false ) . ToByteArray ( ) . ConfigureAwait ( false ) : null ;
347+ var bytes =
348+ send ? await WebSocketFrame . CreateCloseFrame ( e . PayloadData , false ) . ToByteArray ( ) . ConfigureAwait ( false ) : null ;
386349
387- var timeout = wait ? _waitTime : TimeSpan . Zero ;
388- var tasks = _hosts . Values . Select ( host => host . Sessions . Stop ( e , bytes , timeout ) ) ;
389- await Task . WhenAll ( tasks ) . ConfigureAwait ( false ) ;
390- _hosts . Clear ( ) ;
391- _state = ServerState . Stop ;
392- }
350+ var timeout = wait ? _waitTime : TimeSpan . Zero ;
351+ var tasks = _hosts . Values . ToArray ( ) . Select ( host => host . Sessions . Stop ( e , bytes , timeout ) ) ;
352+ await Task . WhenAll ( tasks ) . ConfigureAwait ( false ) ;
353+ _hosts . Clear ( ) ;
354+ _state = ServerState . Stop ;
393355 }
394356
395357 private async Task < bool > InnerBroadcast ( Opcode opcode , byte [ ] data )
0 commit comments