From cb8d1e75f654885c5148a85464cfdaa8d2414273 Mon Sep 17 00:00:00 2001 From: sta Date: Mon, 29 Jun 2015 16:20:00 +0900 Subject: [PATCH] Refactored a few for HttpServer.cs --- websocket-sharp/Server/HttpServer.cs | 186 ++++++++++++++------------- 1 file changed, 95 insertions(+), 91 deletions(-) diff --git a/websocket-sharp/Server/HttpServer.cs b/websocket-sharp/Server/HttpServer.cs index c078e549..6ff46a77 100644 --- a/websocket-sharp/Server/HttpServer.cs +++ b/websocket-sharp/Server/HttpServer.cs @@ -80,18 +80,18 @@ namespace WebSocketSharp.Server /// An instance initialized by this constructor listens for the incoming requests on port 80. /// public HttpServer () - : this (80, false) { + init ("*", 80, false); } /// - /// Initializes a new instance of the class with the specified - /// . + /// Initializes a new instance of the class with + /// the specified . /// /// /// - /// An instance initialized by this constructor listens for the incoming requests - /// on . + /// An instance initialized by this constructor listens for the incoming + /// requests on . /// /// /// If is 443, that instance provides a secure connection. @@ -101,20 +101,24 @@ namespace WebSocketSharp.Server /// An that represents the port number on which to listen. /// /// - /// isn't between 1 and 65535. + /// isn't between 1 and 65535 inclusive. /// public HttpServer (int port) - : this (port, port == 443) { + if (!port.IsPortNumber ()) + throw new ArgumentOutOfRangeException ( + "port", "Not between 1 and 65535 inclusive: " + port); + + init ("*", port, port == 443); } /// - /// Initializes a new instance of the class with the specified - /// and . + /// Initializes a new instance of the class with + /// the specified and . /// /// - /// An instance initialized by this constructor listens for the incoming requests - /// on . + /// An instance initialized by this constructor listens for the incoming + /// requests on . /// /// /// An that represents the port number on which to listen. @@ -127,30 +131,19 @@ namespace WebSocketSharp.Server /// Pair of and is invalid. /// /// - /// isn't between 1 and 65535. + /// isn't between 1 and 65535 inclusive. /// public HttpServer (int port, bool secure) { if (!port.IsPortNumber ()) - throw new ArgumentOutOfRangeException ("port", "Not between 1 and 65535: " + port); + throw new ArgumentOutOfRangeException ( + "port", "Not between 1 and 65535 inclusive: " + port); if ((port == 80 && secure) || (port == 443 && !secure)) throw new ArgumentException ( String.Format ("An invalid pair of 'port' and 'secure': {0}, {1}", port, secure)); - _port = port; - _secure = secure; - _listener = new HttpListener (); - _logger = _listener.Log; - _services = new WebSocketServiceManager (_logger); - _state = ServerState.Ready; - _sync = new object (); - - var os = Environment.OSVersion; - _windows = os.Platform != PlatformID.Unix && os.Platform != PlatformID.MacOSX; - - var pref = String.Format ("http{0}://*:{1}/", _secure ? "s" : "", _port); - _listener.Prefixes.Add (pref); + init ("*", port, secure); } #endregion @@ -206,8 +199,8 @@ namespace WebSocketSharp.Server } /// - /// Gets or sets a value indicating whether the server cleans up the inactive sessions - /// in the WebSocket services periodically. + /// Gets or sets a value indicating whether the server cleans up + /// the inactive sessions in the WebSocket services periodically. /// /// /// true if the server cleans up the inactive sessions every 60 seconds; @@ -262,8 +255,8 @@ namespace WebSocketSharp.Server /// Gets or sets the name of the realm associated with the server. /// /// - /// A that represents the name of the realm. - /// The default value is "SECRET AREA". + /// A that represents the name of the realm. The default value is + /// "SECRET AREA". /// public string Realm { get { @@ -282,12 +275,12 @@ namespace WebSocketSharp.Server } /// - /// Gets or sets a value indicating whether the server is allowed to be bound to an address - /// that is already in use. + /// Gets or sets a value indicating whether the server is allowed to be bound to + /// an address that is already in use. /// /// - /// If you would like to resolve to wait for socket in TIME_WAIT state, you should set - /// this property to true. + /// If you would like to resolve to wait for socket in TIME_WAIT state, + /// you should set this property to true. /// /// /// true if the server is allowed to be bound to an address that is already in use; @@ -318,9 +311,7 @@ namespace WebSocketSharp.Server /// public string RootPath { get { - return _rootPath != null && _rootPath.Length > 0 - ? _rootPath - : (_rootPath = "./Public"); + return _rootPath != null && _rootPath.Length > 0 ? _rootPath : (_rootPath = "./Public"); } set { @@ -339,8 +330,8 @@ namespace WebSocketSharp.Server /// optionally the client for secure connection. /// /// - /// A that represents the configuration used - /// to authenticate the server and optionally the client for secure connection. + /// A that represents the configuration used to + /// authenticate the server and optionally the client for secure connection. /// public ServerSslConfiguration SslConfiguration { get { @@ -363,9 +354,9 @@ namespace WebSocketSharp.Server /// authenticate a client. /// /// - /// A Func<, > delegate that - /// references the method(s) used to find the credentials. The default value is a function - /// that only returns . + /// A Func<, > delegate that + /// references the method(s) used to find the credentials. The default value is a function that + /// only returns . /// public Func UserCredentialsFinder { get { @@ -498,9 +489,24 @@ namespace WebSocketSharp.Server return null; } - return !(usr || port) - ? "The secure connection requires a server certificate." - : null; + return !(usr || port) ? "The secure connection requires a server certificate." : null; + } + + private void init (string hostname, int port, bool secure) + { + _port = port; + _secure = secure; + + _listener = new HttpListener (); + _listener.Prefixes.Add ( + String.Format ("http{0}://{1}:{2}/", secure ? "s" : "", hostname, port)); + + _logger = _listener.Log; + _services = new WebSocketServiceManager (_logger); + _sync = new object (); + + var os = Environment.OSVersion; + _windows = os.Platform != PlatformID.Unix && os.Platform != PlatformID.MacOSX; } private void processRequest (HttpListenerContext context) @@ -567,7 +573,7 @@ namespace WebSocketSharp.Server }); } catch (HttpListenerException ex) { - _logger.Warn ("Receiving has been stopped.\nreason: " + ex.Message); + _logger.Warn ("Receiving has been stopped.\n reason: " + ex.Message); break; } catch (Exception ex) { @@ -598,6 +604,46 @@ namespace WebSocketSharp.Server #region Public Methods + /// + /// Adds the WebSocket service with the specified behavior, , + /// and . + /// + /// + /// + /// This method converts to URL-decoded string, + /// and removes '/' from tail end of . + /// + /// + /// returns an initialized specified typed + /// instance. + /// + /// + /// + /// A that represents the absolute path to the service to add. + /// + /// + /// A Func<T> delegate that references the method used to initialize + /// a new specified typed instance (a new + /// instance). + /// + /// + /// The type of the behavior of the service to add. The TBehavior must inherit + /// the class. + /// + public void AddWebSocketService (string path, Func initializer) + where TBehavior : WebSocketBehavior + { + var msg = path.CheckIfValidServicePath () ?? + (initializer == null ? "'initializer' is null." : null); + + if (msg != null) { + _logger.Error (msg); + return; + } + + _services.Add (path, initializer); + } + /// /// Adds a WebSocket service with the specified behavior and . /// @@ -619,46 +665,6 @@ namespace WebSocketSharp.Server AddWebSocketService (path, () => new TBehaviorWithNew ()); } - /// - /// Adds the WebSocket service with the specified behavior, , - /// and . - /// - /// - /// - /// This method converts to URL-decoded string, - /// and removes '/' from tail end of . - /// - /// - /// returns an initialized specified typed - /// instance. - /// - /// - /// - /// A that represents the absolute path to the service to add. - /// - /// - /// A Func<T> delegate that references the method used to initialize a new specified - /// typed instance (a new - /// instance). - /// - /// - /// The type of the behavior of the service to add. The TBehavior must inherit - /// the class. - /// - public void AddWebSocketService (string path, Func initializer) - where TBehavior : WebSocketBehavior - { - var msg = path.CheckIfValidServicePath () ?? - (initializer == null ? "'initializer' is null." : null); - - if (msg != null) { - _logger.Error (msg); - return; - } - - _services.Add (path, initializer); - } - /// /// Gets the contents of the file with the specified . /// @@ -675,9 +681,7 @@ namespace WebSocketSharp.Server if (_windows) path = path.Replace ("/", "\\"); - return File.Exists (path) - ? File.ReadAllBytes (path) - : null; + return File.Exists (path) ? File.ReadAllBytes (path) : null; } /// @@ -784,8 +788,8 @@ namespace WebSocketSharp.Server /// used to stop the WebSocket services. /// /// - /// One of the enum values, represents the status code - /// indicating the reason for the stop. + /// One of the enum values, represents the status code indicating + /// the reason for the stop. /// /// /// A that represents the reason for the stop.