Refactored a few for WebSocketServer.cs

This commit is contained in:
sta 2015-06-26 17:20:35 +09:00
parent 7559c5c8f2
commit 3764de92e7

View File

@ -84,22 +84,22 @@ namespace WebSocketSharp.Server
/// Initializes a new instance of the <see cref="WebSocketServer"/> class. /// Initializes a new instance of the <see cref="WebSocketServer"/> class.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// An instance initialized by this constructor listens for the incoming connection requests /// An instance initialized by this constructor listens for the incoming
/// on port 80. /// connection requests on port 80.
/// </remarks> /// </remarks>
public WebSocketServer () public WebSocketServer ()
: this (System.Net.IPAddress.Any, 80, false)
{ {
init (System.Net.IPAddress.Any, 80, false);
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="WebSocketServer"/> class with the specified /// Initializes a new instance of the <see cref="WebSocketServer"/> class with
/// <paramref name="port"/>. /// the specified <paramref name="port"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// <para> /// <para>
/// An instance initialized by this constructor listens for the incoming connection requests /// An instance initialized by this constructor listens for the incoming
/// on <paramref name="port"/>. /// connection requests on <paramref name="port"/>.
/// </para> /// </para>
/// <para> /// <para>
/// If <paramref name="port"/> is 443, that instance provides a secure connection. /// If <paramref name="port"/> is 443, that instance provides a secure connection.
@ -109,37 +109,49 @@ namespace WebSocketSharp.Server
/// An <see cref="int"/> that represents the port number on which to listen. /// An <see cref="int"/> that represents the port number on which to listen.
/// </param> /// </param>
/// <exception cref="ArgumentOutOfRangeException"> /// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="port"/> isn't between 1 and 65535. /// <paramref name="port"/> isn't between 1 and 65535 inclusive.
/// </exception> /// </exception>
public WebSocketServer (int port) public WebSocketServer (int port)
: this (System.Net.IPAddress.Any, port, port == 443)
{ {
if (!port.IsPortNumber ())
throw new ArgumentOutOfRangeException (
"port", "Not between 1 and 65535 inclusive: " + port);
init (System.Net.IPAddress.Any, port, port == 443);
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="WebSocketServer"/> class with the specified /// Initializes a new instance of the <see cref="WebSocketServer"/> class with
/// WebSocket URL. /// the specified WebSocket URL.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// <para> /// <para>
/// An instance initialized by this constructor listens for the incoming connection requests /// An instance initialized by this constructor listens for the incoming
/// on the port in <paramref name="url"/>. /// connection requests on the port in <paramref name="url"/>.
/// </para> /// </para>
/// <para> /// <para>
/// If <paramref name="url"/> doesn't include a port, either port 80 or 443 is used on which /// If <paramref name="url"/> doesn't include a port, either port 80 or 443 is used on
/// to listen. It's determined by the scheme (ws or wss) in <paramref name="url"/>. /// which to listen. It's determined by the scheme (ws or wss) in <paramref name="url"/>.
/// (Port 80 if the scheme is ws.) /// (Port 80 if the scheme is ws.)
/// </para> /// </para>
/// </remarks> /// </remarks>
/// <param name="url"> /// <param name="url">
/// A <see cref="string"/> that represents the WebSocket URL of the server. /// A <see cref="string"/> that represents the WebSocket URL of the server.
/// </param> /// </param>
/// <exception cref="ArgumentException">
/// <paramref name="url"/> is invalid.
/// </exception>
/// <exception cref="ArgumentNullException"> /// <exception cref="ArgumentNullException">
/// <paramref name="url"/> is <see langword="null"/>. /// <paramref name="url"/> is <see langword="null"/>.
/// </exception> /// </exception>
/// <exception cref="ArgumentException">
/// <para>
/// <paramref name="url"/> is empty.
/// </para>
/// <para>
/// -or-
/// </para>
/// <para>
/// <paramref name="url"/> is invalid.
/// </para>
/// </exception>
public WebSocketServer (string url) public WebSocketServer (string url)
{ {
if (url == null) if (url == null)
@ -148,27 +160,26 @@ namespace WebSocketSharp.Server
if (url.Length == 0) if (url.Length == 0)
throw new ArgumentException ("An empty string.", "url"); throw new ArgumentException ("An empty string.", "url");
Uri uri;
string msg; string msg;
if (!tryCreateUri (url, out _uri, out msg)) if (!tryCreateUri (url, out uri, out msg))
throw new ArgumentException (msg, "url"); throw new ArgumentException (msg, "url");
_address = _uri.DnsSafeHost.ToIPAddress (); var addr = uri.DnsSafeHost.ToIPAddress ();
if (_address == null || !_address.IsLocal ()) if (!addr.IsLocal ())
throw new ArgumentException ("The host part isn't a local host name: " + url, "url"); throw new ArgumentException ("The host part isn't a local host name: " + url, "url");
_port = _uri.Port; _uri = uri;
_secure = _uri.Scheme == "wss"; init (addr, uri.Port, uri.Scheme == "wss");
init ();
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="WebSocketServer"/> class with the specified /// Initializes a new instance of the <see cref="WebSocketServer"/> class with
/// <paramref name="port"/> and <paramref name="secure"/>. /// the specified <paramref name="port"/> and <paramref name="secure"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// An instance initialized by this constructor listens for the incoming connection requests /// An instance initialized by this constructor listens for the incoming
/// on <paramref name="port"/>. /// connection requests on <paramref name="port"/>.
/// </remarks> /// </remarks>
/// <param name="port"> /// <param name="port">
/// An <see cref="int"/> that represents the port number on which to listen. /// An <see cref="int"/> that represents the port number on which to listen.
@ -181,7 +192,7 @@ namespace WebSocketSharp.Server
/// Pair of <paramref name="port"/> and <paramref name="secure"/> is invalid. /// Pair of <paramref name="port"/> and <paramref name="secure"/> is invalid.
/// </exception> /// </exception>
/// <exception cref="ArgumentOutOfRangeException"> /// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="port"/> isn't between 1 and 65535. /// <paramref name="port"/> isn't between 1 and 65535 inclusive.
/// </exception> /// </exception>
public WebSocketServer (int port, bool secure) public WebSocketServer (int port, bool secure)
: this (System.Net.IPAddress.Any, port, secure) : this (System.Net.IPAddress.Any, port, secure)
@ -189,13 +200,13 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="WebSocketServer"/> class with the specified /// Initializes a new instance of the <see cref="WebSocketServer"/> class with
/// <paramref name="address"/> and <paramref name="port"/>. /// the specified <paramref name="address"/> and <paramref name="port"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// <para> /// <para>
/// An instance initialized by this constructor listens for the incoming connection requests /// An instance initialized by this constructor listens for the incoming
/// on <paramref name="port"/>. /// connection requests on <paramref name="port"/>.
/// </para> /// </para>
/// <para> /// <para>
/// If <paramref name="port"/> is 443, that instance provides a secure connection. /// If <paramref name="port"/> is 443, that instance provides a secure connection.
@ -207,14 +218,14 @@ namespace WebSocketSharp.Server
/// <param name="port"> /// <param name="port">
/// An <see cref="int"/> that represents the port number on which to listen. /// An <see cref="int"/> that represents the port number on which to listen.
/// </param> /// </param>
/// <exception cref="ArgumentException">
/// <paramref name="address"/> isn't a local IP address.
/// </exception>
/// <exception cref="ArgumentNullException"> /// <exception cref="ArgumentNullException">
/// <paramref name="address"/> is <see langword="null"/>. /// <paramref name="address"/> is <see langword="null"/>.
/// </exception> /// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="address"/> isn't a local IP address.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException"> /// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="port"/> isn't between 1 and 65535. /// <paramref name="port"/> isn't between 1 and 65535 inclusive.
/// </exception> /// </exception>
public WebSocketServer (System.Net.IPAddress address, int port) public WebSocketServer (System.Net.IPAddress address, int port)
: this (address, port, port == 443) : this (address, port, port == 443)
@ -222,12 +233,13 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="WebSocketServer"/> class with the specified /// Initializes a new instance of the <see cref="WebSocketServer"/> class with
/// <paramref name="address"/>, <paramref name="port"/>, and <paramref name="secure"/>. /// the specified <paramref name="address"/>, <paramref name="port"/>, and
/// <paramref name="secure"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// An instance initialized by this constructor listens for the incoming connection requests on /// An instance initialized by this constructor listens for the incoming
/// <paramref name="port"/>. /// connection requests on <paramref name="port"/>.
/// </remarks> /// </remarks>
/// <param name="address"> /// <param name="address">
/// A <see cref="System.Net.IPAddress"/> that represents the local IP address of the server. /// A <see cref="System.Net.IPAddress"/> that represents the local IP address of the server.
@ -272,12 +284,7 @@ namespace WebSocketSharp.Server
throw new ArgumentException ( throw new ArgumentException (
String.Format ("An invalid pair of 'port' and 'secure': {0}, {1}", port, secure)); String.Format ("An invalid pair of 'port' and 'secure': {0}, {1}", port, secure));
_address = address; init (address, port, secure);
_port = port;
_secure = secure;
_uri = "/".ToUri ();
init ();
} }
#endregion #endregion
@ -599,13 +606,16 @@ namespace WebSocketSharp.Server
: null; : null;
} }
private void init () private void init (System.Net.IPAddress address, int port, bool secure)
{ {
_address = address;
_port = port;
_secure = secure;
_listener = new TcpListener (address, port);
_authSchemes = AuthenticationSchemes.Anonymous; _authSchemes = AuthenticationSchemes.Anonymous;
_listener = new TcpListener (_address, _port);
_logger = new Logger (); _logger = new Logger ();
_services = new WebSocketServiceManager (_logger); _services = new WebSocketServiceManager (_logger);
_state = ServerState.Ready;
_sync = new object (); _sync = new object ();
} }
@ -617,7 +627,7 @@ namespace WebSocketSharp.Server
return; return;
} }
if (_uri.IsAbsoluteUri) { if (_uri != null && _uri.IsAbsoluteUri) {
var actual = uri.DnsSafeHost; var actual = uri.DnsSafeHost;
var expected = _uri.DnsSafeHost; var expected = _uri.DnsSafeHost;
if (Uri.CheckHostName (actual) == UriHostNameType.Dns && if (Uri.CheckHostName (actual) == UriHostNameType.Dns &&