From 403cf2a3f295003fae6768419d0d7057a8b12841 Mon Sep 17 00:00:00 2001 From: sta Date: Wed, 18 Sep 2013 17:44:14 +0900 Subject: [PATCH] Modified IWebSocketServiceHost.cs and WebSocketServiceHostManager.cs --- websocket-sharp/Server/HttpServer.cs | 2 +- .../Server/IWebSocketServiceHost.cs | 8 + websocket-sharp/Server/WebSocketServer.cs | 2 +- .../Server/WebSocketServiceHost.cs | 2 +- .../Server/WebSocketServiceHostManager.cs | 233 +++++++++--------- 5 files changed, 130 insertions(+), 117 deletions(-) diff --git a/websocket-sharp/Server/HttpServer.cs b/websocket-sharp/Server/HttpServer.cs index 76f10ca4..7269f7fb 100644 --- a/websocket-sharp/Server/HttpServer.cs +++ b/websocket-sharp/Server/HttpServer.cs @@ -390,7 +390,7 @@ namespace WebSocketSharp.Server var wsContext = context.AcceptWebSocket (); IWebSocketServiceHost host; - if (!_serviceHosts.TryGetServiceHost (wsContext.Path, out host)) + if (!_serviceHosts.TryGetServiceHostInternally (wsContext.Path, out host)) { context.Response.StatusCode = (int) HttpStatusCode.NotImplemented; return false; diff --git a/websocket-sharp/Server/IWebSocketServiceHost.cs b/websocket-sharp/Server/IWebSocketServiceHost.cs index e6b2f883..8732e40f 100644 --- a/websocket-sharp/Server/IWebSocketServiceHost.cs +++ b/websocket-sharp/Server/IWebSocketServiceHost.cs @@ -55,6 +55,14 @@ namespace WebSocketSharp.Server /// bool KeepClean { get; set; } + /// + /// Gets the path to the WebSocket service provided by the WebSocket service host. + /// + /// + /// A that contains an absolute path to the WebSocket service. + /// + string ServicePath { get; } + /// /// Gets the manager of the sessions to the WebSocket service host. /// diff --git a/websocket-sharp/Server/WebSocketServer.cs b/websocket-sharp/Server/WebSocketServer.cs index b4dabce9..0c44bde5 100644 --- a/websocket-sharp/Server/WebSocketServer.cs +++ b/websocket-sharp/Server/WebSocketServer.cs @@ -226,7 +226,7 @@ namespace WebSocketSharp.Server var path = context.Path; IWebSocketServiceHost host; - if (!_serviceHosts.TryGetServiceHost (path, out host)) + if (!_serviceHosts.TryGetServiceHostInternally (path, out host)) { websocket.Close (HttpStatusCode.NotImplemented); return; diff --git a/websocket-sharp/Server/WebSocketServiceHost.cs b/websocket-sharp/Server/WebSocketServiceHost.cs index 65c8dbab..c0680da7 100644 --- a/websocket-sharp/Server/WebSocketServiceHost.cs +++ b/websocket-sharp/Server/WebSocketServiceHost.cs @@ -223,7 +223,7 @@ namespace WebSocketSharp.Server } /// - /// Gets the path to the WebSocket service that the WebSocket service host provides. + /// Gets the path to the WebSocket service provided by the WebSocket service host. /// /// /// A that contains an absolute path to the WebSocket service. diff --git a/websocket-sharp/Server/WebSocketServiceHostManager.cs b/websocket-sharp/Server/WebSocketServiceHostManager.cs index dad24bcc..95ab6e96 100644 --- a/websocket-sharp/Server/WebSocketServiceHostManager.cs +++ b/websocket-sharp/Server/WebSocketServiceHostManager.cs @@ -66,44 +66,13 @@ namespace WebSocketSharp.Server #endregion - #region Internal Properties - - internal bool KeepClean { - get { - return _keepClean; - } - - set { - lock (_sync) - { - if (_keepClean ^ value) - { - _keepClean = value; - foreach (var host in _serviceHosts.Values) - host.KeepClean = value; - } - } - } - } - - internal IEnumerable ServiceHosts { - get { - lock (_sync) - { - return _serviceHosts.Values.ToList (); - } - } - } - - #endregion - #region Public Properties /// - /// Gets the connection count to the WebSocket services provided by the WebSocket server. + /// Gets the connection count to the every WebSocket service provided by the WebSocket server. /// /// - /// An that contains the connection count to the WebSocket services. + /// An that contains the connection count to the every WebSocket service. /// public int ConnectionCount { get { @@ -130,6 +99,76 @@ namespace WebSocketSharp.Server } } + /// + /// Gets the WebSocket service host with the specified . + /// + /// + /// A instance that represents the WebSocket service host + /// if it is successfully found; otherwise, . + /// + /// + /// A that contains an absolute path to the WebSocket service managed by + /// the WebSocket service host to get. + /// + public IWebSocketServiceHost this [string servicePath] { + get { + var msg = servicePath.CheckIfValidServicePath (); + if (msg != null) + { + _logger.Error (msg); + return null; + } + + IWebSocketServiceHost host; + if (!TryGetServiceHostInternally (servicePath, out host)) + _logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath); + + return host; + } + } + + /// + /// Gets a value indicating whether the manager cleans up periodically the every inactive session + /// to the WebSocket services provided by the WebSocket server. + /// + /// + /// true if the manager cleans up periodically the every inactive session to the WebSocket + /// services; otherwise, false. + /// + public bool KeepClean { + get { + return _keepClean; + } + + internal set { + lock (_sync) + { + if (_keepClean ^ value) + { + _keepClean = value; + foreach (var host in _serviceHosts.Values) + host.KeepClean = value; + } + } + } + } + + /// + /// Gets the collection of the WebSocket service hosts managed by the WebSocket server. + /// + /// + /// An IEnumerable<IWebSocketServiceHost> that contains the collection of the WebSocket + /// service hosts. + /// + public IEnumerable ServiceHosts { + get { + lock (_sync) + { + return _serviceHosts.Values.ToList (); + } + } + } + /// /// Gets the collection of every path to the WebSocket services provided by the WebSocket server. /// @@ -152,20 +191,12 @@ namespace WebSocketSharp.Server private Dictionary> broadping (byte [] data) { var result = new Dictionary> (); - foreach (var service in copy ()) - result.Add (service.Key, service.Value.Sessions.BroadpingInternally (data)); + foreach (var host in ServiceHosts) + result.Add (host.ServicePath, host.Sessions.BroadpingInternally (data)); return result; } - private Dictionary copy () - { - lock (_sync) - { - return new Dictionary (_serviceHosts); - } - } - #endregion #region Internal Methods @@ -190,7 +221,6 @@ namespace WebSocketSharp.Server internal bool Remove (string servicePath) { servicePath = HttpUtility.UrlDecode (servicePath).TrimEndSlash (); - IWebSocketServiceHost host; lock (_sync) { @@ -230,7 +260,7 @@ namespace WebSocketSharp.Server } } - internal bool TryGetServiceHost (string servicePath, out IWebSocketServiceHost serviceHost) + internal bool TryGetServiceHostInternally (string servicePath, out IWebSocketServiceHost serviceHost) { servicePath = HttpUtility.UrlDecode (servicePath).TrimEndSlash (); lock (_sync) @@ -306,7 +336,7 @@ namespace WebSocketSharp.Server } IWebSocketServiceHost host; - if (!TryGetServiceHost (servicePath, out host)) + if (!TryGetServiceHostInternally (servicePath, out host)) { _logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath); return false; @@ -339,7 +369,7 @@ namespace WebSocketSharp.Server } IWebSocketServiceHost host; - if (!TryGetServiceHost (servicePath, out host)) + if (!TryGetServiceHostInternally (servicePath, out host)) { _logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath); return false; @@ -412,7 +442,7 @@ namespace WebSocketSharp.Server } IWebSocketServiceHost host; - if (!TryGetServiceHost (servicePath, out host)) + if (!TryGetServiceHostInternally (servicePath, out host)) { _logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath); return null; @@ -450,7 +480,7 @@ namespace WebSocketSharp.Server } IWebSocketServiceHost host; - if (!TryGetServiceHost (servicePath, out host)) + if (!TryGetServiceHostInternally (servicePath, out host)) { _logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath); return null; @@ -479,7 +509,7 @@ namespace WebSocketSharp.Server } IWebSocketServiceHost host; - if (!TryGetServiceHost (servicePath, out host)) + if (!TryGetServiceHostInternally (servicePath, out host)) { _logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath); return; @@ -514,7 +544,7 @@ namespace WebSocketSharp.Server } IWebSocketServiceHost host; - if (!TryGetServiceHost (servicePath, out host)) + if (!TryGetServiceHostInternally (servicePath, out host)) { _logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath); return; @@ -549,7 +579,7 @@ namespace WebSocketSharp.Server } IWebSocketServiceHost host; - if (!TryGetServiceHost (servicePath, out host)) + if (!TryGetServiceHostInternally (servicePath, out host)) { _logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath); return; @@ -558,64 +588,6 @@ namespace WebSocketSharp.Server host.Sessions.CloseSession (code, reason, id); } - /// - /// Gets the connection count to the WebSocket service with the specified . - /// - /// - /// An that contains the connection count if the WebSocket service is successfully - /// found; otherwise, -1. - /// - /// - /// A that contains an absolute path to the WebSocket service to find. - /// - public int GetConnectionCount (string servicePath) - { - var msg = servicePath.CheckIfValidServicePath (); - if (msg != null) - { - _logger.Error (msg); - return -1; - } - - IWebSocketServiceHost host; - if (!TryGetServiceHost (servicePath, out host)) - { - _logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath); - return -1; - } - - return host.ConnectionCount; - } - - /// - /// Gets the manager of the sessions to the WebSocket service with the specified . - /// - /// - /// A if the WebSocket service is successfully found; - /// otherwise, . - /// - /// - /// A that contains an absolute path to the WebSocket service to find. - /// - public WebSocketSessionManager GetSessions (string servicePath) - { - var msg = servicePath.CheckIfValidServicePath (); - if (msg != null) - { - _logger.Error (msg); - return null; - } - - IWebSocketServiceHost host; - if (!TryGetServiceHost (servicePath, out host)) - { - _logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath); - return null; - } - - return host.Sessions; - } - /// /// Sends a Ping to the client associated with the specified and /// . @@ -640,7 +612,7 @@ namespace WebSocketSharp.Server } IWebSocketServiceHost host; - if (!TryGetServiceHost (servicePath, out host)) + if (!TryGetServiceHostInternally (servicePath, out host)) { _logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath); return false; @@ -676,7 +648,7 @@ namespace WebSocketSharp.Server } IWebSocketServiceHost host; - if (!TryGetServiceHost (servicePath, out host)) + if (!TryGetServiceHostInternally (servicePath, out host)) { _logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath); return false; @@ -711,7 +683,7 @@ namespace WebSocketSharp.Server } IWebSocketServiceHost host; - if (!TryGetServiceHost (servicePath, out host)) + if (!TryGetServiceHostInternally (servicePath, out host)) { _logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath); return false; @@ -746,7 +718,7 @@ namespace WebSocketSharp.Server } IWebSocketServiceHost host; - if (!TryGetServiceHost (servicePath, out host)) + if (!TryGetServiceHostInternally (servicePath, out host)) { _logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath); return false; @@ -755,6 +727,39 @@ namespace WebSocketSharp.Server return host.Sessions.SendTo (data, id); } + /// + /// Tries to get the WebSocket service host with the specified . + /// + /// + /// true if the WebSocket service host is successfully found; otherwise, false. + /// + /// + /// A that contains an absolute path to the WebSocket service managed by + /// the WebSocket service host to get. + /// + /// + /// When this method returns, a instance that represents + /// the WebSocket service host if it is successfully found; otherwise, . + /// This parameter is passed uninitialized. + /// + public bool TryGetServiceHost (string servicePath, out IWebSocketServiceHost serviceHost) + { + var msg = servicePath.CheckIfValidServicePath (); + if (msg != null) + { + _logger.Error (msg); + serviceHost = null; + + return false; + } + + var result = TryGetServiceHostInternally (servicePath, out serviceHost); + if (!result) + _logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath); + + return result; + } + #endregion } }