diff --git a/websocket-sharp/Ext.cs b/websocket-sharp/Ext.cs index 146e0e0f..484eb117 100644 --- a/websocket-sharp/Ext.cs +++ b/websocket-sharp/Ext.cs @@ -817,6 +817,11 @@ namespace WebSocketSharp } } + internal static List ToList (this IEnumerable source) + { + return new List (source); + } + internal static ushort ToUInt16 (this byte [] src, ByteOrder srcOrder) { return BitConverter.ToUInt16 (src.ToHostOrder (srcOrder), 0); diff --git a/websocket-sharp/Server/WebSocketServiceManager.cs b/websocket-sharp/Server/WebSocketServiceManager.cs index dd35de22..072cccd6 100644 --- a/websocket-sharp/Server/WebSocketServiceManager.cs +++ b/websocket-sharp/Server/WebSocketServiceManager.cs @@ -87,24 +87,26 @@ namespace WebSocketSharp.Server } /// - /// Gets the collection of the WebSocket service hosts. + /// Gets the collection of every information in the Websocket services provided by the server. /// /// - /// An IEnumerable<WebSocketServiceHost> that contains the collection of the WebSocket - /// service hosts. + /// An IEnumerable<WebSocketServiceHost> that contains the collection of every + /// information in the Websocket services. /// public IEnumerable Hosts { get { - return copyHosts ().Values; + lock (_sync) { + return _hosts.Values.ToList (); + } } } /// - /// Gets a WebSocket service host with the specified . + /// Gets the information in a WebSocket service with the specified . /// /// - /// A instance that represents the WebSocket service host if - /// it's successfully found; otherwise, . + /// A instance that provides the access to the WebSocket + /// service if it's successfully found; otherwise, . /// /// /// A that represents the absolute path to the WebSocket service to find. @@ -152,7 +154,9 @@ namespace WebSocketSharp.Server /// public IEnumerable Paths { get { - return copyHosts ().Keys; + lock (_sync) { + return _hosts.Keys.ToList (); + } } } @@ -253,13 +257,6 @@ namespace WebSocketSharp.Server return result; } - private Dictionary copyHosts () - { - lock (_sync) { - return new Dictionary (_hosts); - } - } - #endregion #region Internal Methods @@ -328,7 +325,6 @@ namespace WebSocketSharp.Server host.Sessions.Stop (args, frameAsBytes); _hosts.Clear (); - _state = ServerState.STOP; } } @@ -555,7 +551,8 @@ namespace WebSocketSharp.Server } /// - /// Tries to get a WebSocket service host with the specified . + /// Tries to get the information in a WebSocket service with the specified + /// . /// /// /// true if the WebSocket service is successfully found; otherwise, false. @@ -564,9 +561,9 @@ namespace WebSocketSharp.Server /// A that represents the absolute path to the WebSocket service to find. /// /// - /// When this method returns, a instance that represents the - /// WebSocket service host if it's successfully found; otherwise, . This - /// parameter is passed uninitialized. + /// When this method returns, a instance that + /// provides the access to the WebSocket service if it's successfully found; + /// otherwise, . This parameter is passed uninitialized. /// public bool TryGetServiceHost (string path, out WebSocketServiceHost host) { diff --git a/websocket-sharp/Server/WebSocketSessionManager.cs b/websocket-sharp/Server/WebSocketSessionManager.cs index 066d3a60..d0f7811d 100644 --- a/websocket-sharp/Server/WebSocketSessionManager.cs +++ b/websocket-sharp/Server/WebSocketSessionManager.cs @@ -42,7 +42,7 @@ namespace WebSocketSharp.Server { #region Private Static Fields - private static readonly List _emptySessions; + private static readonly Dictionary _emptySessions; #endregion @@ -63,7 +63,7 @@ namespace WebSocketSharp.Server static WebSocketSessionManager () { - _emptySessions = new List (); + _emptySessions = new Dictionary (); } #endregion @@ -138,7 +138,12 @@ namespace WebSocketSharp.Server /// public IEnumerable IDs { get { - return copySessions ().Keys; + if (_state == ServerState.SHUTDOWN) + return _emptySessions.Keys; + + lock (_sync) { + return _sessions.Keys.ToList (); + } } } @@ -158,11 +163,11 @@ namespace WebSocketSharp.Server } /// - /// Gets a session information with the specified in the WebSocket + /// Gets the information in a session with the specified in the WebSocket /// service. /// /// - /// A instance that represents the session information if it's + /// A instance that provides the access to the session if it's /// successfully found; otherwise, . /// /// @@ -200,18 +205,20 @@ namespace WebSocketSharp.Server } /// - /// Gets the collection of the session informations in the Websocket service. + /// Gets the collection of every information in the sessions in the Websocket service. /// /// - /// An IEnumerable<IWebSocketSession> that contains the collection of the session - /// informations. + /// An IEnumerable<IWebSocketSession> that contains the collection of every information + /// in the sessions. /// public IEnumerable Sessions { get { if (_state == ServerState.SHUTDOWN) - return _emptySessions; + return _emptySessions.Values; - return copySessions ().Values; + lock (_sync) { + return _sessions.Values.ToList (); + } } } @@ -266,13 +273,6 @@ namespace WebSocketSharp.Server state => broadcast (opcode, stream, completed)); } - private Dictionary copySessions () - { - lock (_sync) { - return new Dictionary (_sessions); - } - } - private static string createID () { return Guid.NewGuid ().ToString ("N"); @@ -380,7 +380,7 @@ namespace WebSocketSharp.Server _state = ServerState.SHUTDOWN; _sweepTimer.Enabled = false; - foreach (var session in copySessions ().Values) + foreach (var session in _sessions.Values.ToList ()) session.Context.WebSocket.Close (args, frame, 1000); _state = ServerState.STOP; @@ -609,7 +609,7 @@ namespace WebSocketSharp.Server /// A that represents the ID of the session to close. /// /// - /// A that indicates the status code for closure. + /// A that represents the status code indicating the reason for closure. /// /// /// A that represents the reason for closure. @@ -629,8 +629,8 @@ namespace WebSocketSharp.Server /// A that represents the ID of the session to close. /// /// - /// One of the enum values, indicates the status code for - /// closure. + /// One of the enum values, represents the status code indicating + /// the reason for closure. /// /// /// A that represents the reason for closure. @@ -826,7 +826,7 @@ namespace WebSocketSharp.Server } /// - /// Tries to get a session information with the specified in the + /// Tries to get the information in a session with the specified in the /// WebSocket service. /// /// @@ -836,8 +836,8 @@ namespace WebSocketSharp.Server /// A that represents the ID of the session to find. /// /// - /// When this method returns, a instance that represents the - /// session information if it's successfully found; otherwise, . This + /// When this method returns, a instance that provides the + /// access to the session if it's successfully found; otherwise, . This /// parameter is passed uninitialized. /// public bool TryGetSession (string id, out IWebSocketSession session)