Refactored a few for WebSocketServer.cs
This commit is contained in:
		@@ -62,7 +62,7 @@ namespace WebSocketSharp.Server
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    private System.Net.IPAddress               _address;
 | 
					    private System.Net.IPAddress               _address;
 | 
				
			||||||
    private AuthenticationSchemes              _authSchemes;
 | 
					    private AuthenticationSchemes              _authSchemes;
 | 
				
			||||||
    private Func<IIdentity, NetworkCredential> _credentialsFinder;
 | 
					    private Func<IIdentity, NetworkCredential> _credFinder;
 | 
				
			||||||
    private TcpListener                        _listener;
 | 
					    private TcpListener                        _listener;
 | 
				
			||||||
    private Logger                             _logger;
 | 
					    private Logger                             _logger;
 | 
				
			||||||
    private int                                _port;
 | 
					    private int                                _port;
 | 
				
			||||||
@@ -234,8 +234,8 @@ namespace WebSocketSharp.Server
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /// <summary>
 | 
					    /// <summary>
 | 
				
			||||||
    /// Initializes a new instance of the <see cref="WebSocketServer"/> class with
 | 
					    /// Initializes a new instance of the <see cref="WebSocketServer"/> class with
 | 
				
			||||||
    /// the specified <paramref name="address"/>, <paramref name="port"/>, and
 | 
					    /// the specified <paramref name="address"/>, <paramref name="port"/>,
 | 
				
			||||||
    /// <paramref name="secure"/>.
 | 
					    /// and <paramref name="secure"/>.
 | 
				
			||||||
    /// </summary>
 | 
					    /// </summary>
 | 
				
			||||||
    /// <remarks>
 | 
					    /// <remarks>
 | 
				
			||||||
    /// An instance initialized by this constructor listens for the incoming
 | 
					    /// An instance initialized by this constructor listens for the incoming
 | 
				
			||||||
@@ -352,8 +352,8 @@ namespace WebSocketSharp.Server
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// <summary>
 | 
					    /// <summary>
 | 
				
			||||||
    /// Gets or sets a value indicating whether the server cleans up the inactive sessions
 | 
					    /// Gets or sets a value indicating whether the server cleans up
 | 
				
			||||||
    /// periodically.
 | 
					    /// the inactive sessions periodically.
 | 
				
			||||||
    /// </summary>
 | 
					    /// </summary>
 | 
				
			||||||
    /// <value>
 | 
					    /// <value>
 | 
				
			||||||
    /// <c>true</c> if the server cleans up the inactive sessions every 60 seconds;
 | 
					    /// <c>true</c> if the server cleans up the inactive sessions every 60 seconds;
 | 
				
			||||||
@@ -408,8 +408,8 @@ namespace WebSocketSharp.Server
 | 
				
			|||||||
    /// Gets or sets the name of the realm associated with the server.
 | 
					    /// Gets or sets the name of the realm associated with the server.
 | 
				
			||||||
    /// </summary>
 | 
					    /// </summary>
 | 
				
			||||||
    /// <value>
 | 
					    /// <value>
 | 
				
			||||||
    /// A <see cref="string"/> that represents the name of the realm.
 | 
					    /// A <see cref="string"/> that represents the name of the realm. The default value is
 | 
				
			||||||
    /// The default value is <c>"SECRET AREA"</c>.
 | 
					    /// <c>"SECRET AREA"</c>.
 | 
				
			||||||
    /// </value>
 | 
					    /// </value>
 | 
				
			||||||
    public string Realm {
 | 
					    public string Realm {
 | 
				
			||||||
      get {
 | 
					      get {
 | 
				
			||||||
@@ -428,12 +428,12 @@ namespace WebSocketSharp.Server
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// <summary>
 | 
					    /// <summary>
 | 
				
			||||||
    /// Gets or sets a value indicating whether the server is allowed to be bound to an address
 | 
					    /// Gets or sets a value indicating whether the server is allowed to be bound to
 | 
				
			||||||
    /// that is already in use.
 | 
					    /// an address that is already in use.
 | 
				
			||||||
    /// </summary>
 | 
					    /// </summary>
 | 
				
			||||||
    /// <remarks>
 | 
					    /// <remarks>
 | 
				
			||||||
    /// If you would like to resolve to wait for socket in <c>TIME_WAIT</c> state, you should set
 | 
					    /// If you would like to resolve to wait for socket in <c>TIME_WAIT</c> state,
 | 
				
			||||||
    /// this property to <c>true</c>.
 | 
					    /// you should set this property to <c>true</c>.
 | 
				
			||||||
    /// </remarks>
 | 
					    /// </remarks>
 | 
				
			||||||
    /// <value>
 | 
					    /// <value>
 | 
				
			||||||
    /// <c>true</c> if the server is allowed to be bound to an address that is already in use;
 | 
					    /// <c>true</c> if the server is allowed to be bound to an address that is already in use;
 | 
				
			||||||
@@ -460,8 +460,8 @@ namespace WebSocketSharp.Server
 | 
				
			|||||||
    /// optionally the client for secure connection.
 | 
					    /// optionally the client for secure connection.
 | 
				
			||||||
    /// </summary>
 | 
					    /// </summary>
 | 
				
			||||||
    /// <value>
 | 
					    /// <value>
 | 
				
			||||||
    /// A <see cref="ServerSslConfiguration"/> that represents the configuration used
 | 
					    /// A <see cref="ServerSslConfiguration"/> that represents the configuration used to
 | 
				
			||||||
    /// to authenticate the server and optionally the client for secure connection.
 | 
					    /// authenticate the server and optionally the client for secure connection.
 | 
				
			||||||
    /// </value>
 | 
					    /// </value>
 | 
				
			||||||
    public ServerSslConfiguration SslConfiguration {
 | 
					    public ServerSslConfiguration SslConfiguration {
 | 
				
			||||||
      get {
 | 
					      get {
 | 
				
			||||||
@@ -484,13 +484,13 @@ namespace WebSocketSharp.Server
 | 
				
			|||||||
    /// authenticate a client.
 | 
					    /// authenticate a client.
 | 
				
			||||||
    /// </summary>
 | 
					    /// </summary>
 | 
				
			||||||
    /// <value>
 | 
					    /// <value>
 | 
				
			||||||
    /// A Func<<see cref="IIdentity"/>, <see cref="NetworkCredential"/>> delegate that
 | 
					    /// A <c>Func<<see cref="IIdentity"/>, <see cref="NetworkCredential"/>></c> delegate that
 | 
				
			||||||
    /// references the method(s) used to find the credentials. The default value is a function
 | 
					    /// references the method(s) used to find the credentials. The default value is a function that
 | 
				
			||||||
    /// that only returns <see langword="null"/>.
 | 
					    /// only returns <see langword="null"/>.
 | 
				
			||||||
    /// </value>
 | 
					    /// </value>
 | 
				
			||||||
    public Func<IIdentity, NetworkCredential> UserCredentialsFinder {
 | 
					    public Func<IIdentity, NetworkCredential> UserCredentialsFinder {
 | 
				
			||||||
      get {
 | 
					      get {
 | 
				
			||||||
        return _credentialsFinder ?? (_credentialsFinder = identity => null);
 | 
					        return _credFinder ?? (_credFinder = identity => null);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      set {
 | 
					      set {
 | 
				
			||||||
@@ -500,7 +500,7 @@ namespace WebSocketSharp.Server
 | 
				
			|||||||
          return;
 | 
					          return;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        _credentialsFinder = value;
 | 
					        _credFinder = value;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -669,7 +669,7 @@ namespace WebSocketSharp.Server
 | 
				
			|||||||
            });
 | 
					            });
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        catch (SocketException ex) {
 | 
					        catch (SocketException ex) {
 | 
				
			||||||
          _logger.Warn ("Receiving has been stopped.\nreason: " + ex.Message);
 | 
					          _logger.Warn ("Receiving has been stopped.\n  reason: " + ex.Message);
 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        catch (Exception ex) {
 | 
					        catch (Exception ex) {
 | 
				
			||||||
@@ -719,6 +719,46 @@ namespace WebSocketSharp.Server
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    #region Public Methods
 | 
					    #region Public Methods
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// <summary>
 | 
				
			||||||
 | 
					    /// Adds a WebSocket service with the specified behavior, <paramref name="path"/>,
 | 
				
			||||||
 | 
					    /// and <paramref name="initializer"/>.
 | 
				
			||||||
 | 
					    /// </summary>
 | 
				
			||||||
 | 
					    /// <remarks>
 | 
				
			||||||
 | 
					    ///   <para>
 | 
				
			||||||
 | 
					    ///   This method converts <paramref name="path"/> to URL-decoded string,
 | 
				
			||||||
 | 
					    ///   and removes <c>'/'</c> from tail end of <paramref name="path"/>.
 | 
				
			||||||
 | 
					    ///   </para>
 | 
				
			||||||
 | 
					    ///   <para>
 | 
				
			||||||
 | 
					    ///   <paramref name="initializer"/> returns an initialized specified typed
 | 
				
			||||||
 | 
					    ///   <see cref="WebSocketBehavior"/> instance.
 | 
				
			||||||
 | 
					    ///   </para>
 | 
				
			||||||
 | 
					    /// </remarks>
 | 
				
			||||||
 | 
					    /// <param name="path">
 | 
				
			||||||
 | 
					    /// A <see cref="string"/> that represents the absolute path to the service to add.
 | 
				
			||||||
 | 
					    /// </param>
 | 
				
			||||||
 | 
					    /// <param name="initializer">
 | 
				
			||||||
 | 
					    /// A <c>Func<T></c> delegate that references the method used to initialize
 | 
				
			||||||
 | 
					    /// a new specified typed <see cref="WebSocketBehavior"/> instance (a new
 | 
				
			||||||
 | 
					    /// <see cref="IWebSocketSession"/> instance).
 | 
				
			||||||
 | 
					    /// </param>
 | 
				
			||||||
 | 
					    /// <typeparam name="TBehavior">
 | 
				
			||||||
 | 
					    /// The type of the behavior of the service to add. The TBehavior must inherit
 | 
				
			||||||
 | 
					    /// the <see cref="WebSocketBehavior"/> class.
 | 
				
			||||||
 | 
					    /// </typeparam>
 | 
				
			||||||
 | 
					    public void AddWebSocketService<TBehavior> (string path, Func<TBehavior> initializer)
 | 
				
			||||||
 | 
					      where TBehavior : WebSocketBehavior
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      var msg = path.CheckIfValidServicePath () ??
 | 
				
			||||||
 | 
					                (initializer == null ? "'initializer' is null." : null);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      if (msg != null) {
 | 
				
			||||||
 | 
					        _logger.Error (msg);
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      _services.Add<TBehavior> (path, initializer);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// <summary>
 | 
					    /// <summary>
 | 
				
			||||||
    /// Adds a WebSocket service with the specified behavior and <paramref name="path"/>.
 | 
					    /// Adds a WebSocket service with the specified behavior and <paramref name="path"/>.
 | 
				
			||||||
    /// </summary>
 | 
					    /// </summary>
 | 
				
			||||||
@@ -740,46 +780,6 @@ namespace WebSocketSharp.Server
 | 
				
			|||||||
      AddWebSocketService<TBehaviorWithNew> (path, () => new TBehaviorWithNew ());
 | 
					      AddWebSocketService<TBehaviorWithNew> (path, () => new TBehaviorWithNew ());
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// <summary>
 | 
					 | 
				
			||||||
    /// Adds a WebSocket service with the specified behavior, <paramref name="path"/>,
 | 
					 | 
				
			||||||
    /// and <paramref name="initializer"/>.
 | 
					 | 
				
			||||||
    /// </summary>
 | 
					 | 
				
			||||||
    /// <remarks>
 | 
					 | 
				
			||||||
    ///   <para>
 | 
					 | 
				
			||||||
    ///   This method converts <paramref name="path"/> to URL-decoded string,
 | 
					 | 
				
			||||||
    ///   and removes <c>'/'</c> from tail end of <paramref name="path"/>.
 | 
					 | 
				
			||||||
    ///   </para>
 | 
					 | 
				
			||||||
    ///   <para>
 | 
					 | 
				
			||||||
    ///   <paramref name="initializer"/> returns an initialized specified typed
 | 
					 | 
				
			||||||
    ///   <see cref="WebSocketBehavior"/> instance.
 | 
					 | 
				
			||||||
    ///   </para>
 | 
					 | 
				
			||||||
    /// </remarks>
 | 
					 | 
				
			||||||
    /// <param name="path">
 | 
					 | 
				
			||||||
    /// A <see cref="string"/> that represents the absolute path to the service to add.
 | 
					 | 
				
			||||||
    /// </param>
 | 
					 | 
				
			||||||
    /// <param name="initializer">
 | 
					 | 
				
			||||||
    /// A Func<T> delegate that references the method used to initialize a new specified
 | 
					 | 
				
			||||||
    /// typed <see cref="WebSocketBehavior"/> instance (a new <see cref="IWebSocketSession"/>
 | 
					 | 
				
			||||||
    /// instance).
 | 
					 | 
				
			||||||
    /// </param>
 | 
					 | 
				
			||||||
    /// <typeparam name="TBehavior">
 | 
					 | 
				
			||||||
    /// The type of the behavior of the service to add. The TBehavior must inherit
 | 
					 | 
				
			||||||
    /// the <see cref="WebSocketBehavior"/> class.
 | 
					 | 
				
			||||||
    /// </typeparam>
 | 
					 | 
				
			||||||
    public void AddWebSocketService<TBehavior> (string path, Func<TBehavior> initializer)
 | 
					 | 
				
			||||||
      where TBehavior : WebSocketBehavior
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
      var msg = path.CheckIfValidServicePath () ??
 | 
					 | 
				
			||||||
                (initializer == null ? "'initializer' is null." : null);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      if (msg != null) {
 | 
					 | 
				
			||||||
        _logger.Error (msg);
 | 
					 | 
				
			||||||
        return;
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      _services.Add<TBehavior> (path, initializer);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /// <summary>
 | 
					    /// <summary>
 | 
				
			||||||
    /// Removes the WebSocket service with the specified <paramref name="path"/>.
 | 
					    /// Removes the WebSocket service with the specified <paramref name="path"/>.
 | 
				
			||||||
    /// </summary>
 | 
					    /// </summary>
 | 
				
			||||||
@@ -845,8 +845,8 @@ namespace WebSocketSharp.Server
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// <summary>
 | 
					    /// <summary>
 | 
				
			||||||
    /// Stops receiving the WebSocket connection requests with the specified <see cref="ushort"/>
 | 
					    /// Stops receiving the WebSocket connection requests with
 | 
				
			||||||
    /// and <see cref="string"/>.
 | 
					    /// the specified <see cref="ushort"/> and <see cref="string"/>.
 | 
				
			||||||
    /// </summary>
 | 
					    /// </summary>
 | 
				
			||||||
    /// <param name="code">
 | 
					    /// <param name="code">
 | 
				
			||||||
    /// A <see cref="ushort"/> that represents the status code indicating the reason for the stop.
 | 
					    /// A <see cref="ushort"/> that represents the status code indicating the reason for the stop.
 | 
				
			||||||
@@ -879,12 +879,12 @@ namespace WebSocketSharp.Server
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// <summary>
 | 
					    /// <summary>
 | 
				
			||||||
    /// Stops receiving the WebSocket connection requests with the specified
 | 
					    /// Stops receiving the WebSocket connection requests with
 | 
				
			||||||
    /// <see cref="CloseStatusCode"/> and <see cref="string"/>.
 | 
					    /// the specified <see cref="CloseStatusCode"/> and <see cref="string"/>.
 | 
				
			||||||
    /// </summary>
 | 
					    /// </summary>
 | 
				
			||||||
    /// <param name="code">
 | 
					    /// <param name="code">
 | 
				
			||||||
    /// One of the <see cref="CloseStatusCode"/> enum values, represents the status code
 | 
					    /// One of the <see cref="CloseStatusCode"/> enum values, represents the status code indicating
 | 
				
			||||||
    /// indicating the reason for the stop.
 | 
					    /// the reason for the stop.
 | 
				
			||||||
    /// </param>
 | 
					    /// </param>
 | 
				
			||||||
    /// <param name="reason">
 | 
					    /// <param name="reason">
 | 
				
			||||||
    /// A <see cref="string"/> that represents the reason for the stop.
 | 
					    /// A <see cref="string"/> that represents the reason for the stop.
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user