Refactored HttpServer.cs, WebSocketServer.cs

This commit is contained in:
sta 2014-02-04 16:15:37 +09:00
parent 2fe44b9279
commit 7a42c60578
2 changed files with 96 additions and 76 deletions

View File

@ -100,10 +100,10 @@ namespace WebSocketSharp.Server
/// </para> /// </para>
/// </remarks> /// </remarks>
/// <param name="port"> /// <param name="port">
/// An <see cref="int"/> that represents the port number 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"/> is not between 1 and 65535. /// <paramref name="port"/> isn't between 1 and 65535.
/// </exception> /// </exception>
public HttpServer (int port) public HttpServer (int port)
: this (port, port == 443 ? true : false) : this (port, port == 443 ? true : false)
@ -119,14 +119,14 @@ namespace WebSocketSharp.Server
/// requests on <paramref name="port"/>. /// requests on <paramref name="port"/>.
/// </remarks> /// </remarks>
/// <param name="port"> /// <param name="port">
/// An <see cref="int"/> that represents the port number to listen. /// An <see cref="int"/> that represents the port number on which to listen.
/// </param> /// </param>
/// <param name="secure"> /// <param name="secure">
/// A <see cref="bool"/> that indicates providing a secure connection or not. /// A <see cref="bool"/> that indicates providing a secure connection or not.
/// (<c>true</c> indicates providing a secure connection.) /// (<c>true</c> indicates providing a secure connection.)
/// </param> /// </param>
/// <exception cref="ArgumentOutOfRangeException"> /// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="port"/> is not between 1 and 65535. /// <paramref name="port"/> isn't between 1 and 65535.
/// </exception> /// </exception>
/// <exception cref="ArgumentException"> /// <exception cref="ArgumentException">
/// Pair of <paramref name="port"/> and <paramref name="secure"/> is invalid. /// Pair of <paramref name="port"/> and <paramref name="secure"/> is invalid.
@ -208,10 +208,10 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Gets a value indicating whether the server has been started. /// Gets a value indicating whether the server has started.
/// </summary> /// </summary>
/// <value> /// <value>
/// <c>true</c> if the server has been started; otherwise, <c>false</c>. /// <c>true</c> if the server has started; otherwise, <c>false</c>.
/// </value> /// </value>
public bool IsListening { public bool IsListening {
get { get {
@ -220,10 +220,10 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Gets a value indicating whether the server provides secure connection. /// Gets a value indicating whether the server provides a secure connection.
/// </summary> /// </summary>
/// <value> /// <value>
/// <c>true</c> if the server provides secure connection; otherwise, /// <c>true</c> if the server provides a secure connection; otherwise,
/// <c>false</c>. /// <c>false</c>.
/// </value> /// </value>
public bool IsSecure { public bool IsSecure {
@ -271,7 +271,7 @@ namespace WebSocketSharp.Server
/// Gets the port on which to listen for incoming requests. /// Gets the port on which to listen for incoming requests.
/// </summary> /// </summary>
/// <value> /// <value>
/// An <see cref="int"/> that represents the port number to listen. /// An <see cref="int"/> that represents the port number on which to listen.
/// </value> /// </value>
public int Port { public int Port {
get { get {
@ -432,52 +432,63 @@ namespace WebSocketSharp.Server
{ {
var args = new HttpRequestEventArgs (context); var args = new HttpRequestEventArgs (context);
var method = context.Request.HttpMethod; var method = context.Request.HttpMethod;
if (method == "GET" && OnGet != null) {
if (method == "GET") {
if (OnGet != null) {
OnGet (this, args); OnGet (this, args);
return; return;
} }
}
if (method == "HEAD" && OnHead != null) { else if (method == "HEAD") {
if (OnHead != null) {
OnHead (this, args); OnHead (this, args);
return; return;
} }
}
if (method == "POST" && OnPost != null) { else if (method == "POST") {
if (OnPost != null) {
OnPost (this, args); OnPost (this, args);
return; return;
} }
}
if (method == "PUT" && OnPut != null) { else if (method == "PUT") {
if (OnPut != null) {
OnPut (this, args); OnPut (this, args);
return; return;
} }
}
if (method == "DELETE" && OnDelete != null) { else if (method == "DELETE") {
if (OnDelete != null) {
OnDelete (this, args); OnDelete (this, args);
return; return;
} }
}
if (method == "OPTIONS" && OnOptions != null) { else if (method == "OPTIONS") {
if (OnOptions != null) {
OnOptions (this, args); OnOptions (this, args);
return; return;
} }
}
if (method == "TRACE" && OnTrace != null) { else if (method == "TRACE") {
if (OnTrace != null) {
OnTrace (this, args); OnTrace (this, args);
return; return;
} }
}
if (method == "CONNECT" && OnConnect != null) { else if (method == "CONNECT") {
if (OnConnect != null) {
OnConnect (this, args); OnConnect (this, args);
return; return;
} }
}
if (method == "PATCH" && OnPatch != null) { else if (method == "PATCH") {
if (OnPatch != null) {
OnPatch (this, args); OnPatch (this, args);
return; return;
} }
}
context.Response.Close (HttpStatusCode.NotImplemented); context.Response.StatusCode = (int) HttpStatusCode.NotImplemented;
} }
private void acceptRequestAsync (HttpListenerContext context) private void acceptRequestAsync (HttpListenerContext context)
@ -590,10 +601,10 @@ namespace WebSocketSharp.Server
_receiveRequestThread.Start (); _receiveRequestThread.Start ();
} }
private void stopListener (int timeOut) private void stopListener (int millisecondsTimeout)
{ {
_listener.Close (); _listener.Close ();
_receiveRequestThread.Join (timeOut); _receiveRequestThread.Join (millisecondsTimeout);
} }
#endregion #endregion

View File

@ -103,10 +103,10 @@ namespace WebSocketSharp.Server
/// </para> /// </para>
/// </remarks> /// </remarks>
/// <param name="port"> /// <param name="port">
/// An <see cref="int"/> that represents the port number 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"/> is not between 1 and 65535. /// <paramref name="port"/> isn't between 1 and 65535.
/// </exception> /// </exception>
public WebSocketServer (int port) public WebSocketServer (int port)
: this (System.Net.IPAddress.Any, port) : this (System.Net.IPAddress.Any, port)
@ -118,8 +118,15 @@ namespace WebSocketSharp.Server
/// with the specified WebSocket URL. /// with the specified WebSocket URL.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// <para>
/// An instance initialized by this constructor listens for the incoming /// An instance initialized by this constructor listens for the incoming
/// connection requests on the port number of <paramref name="url"/>. /// connection requests on the port (if any) in <paramref name="url"/>.
/// </para>
/// <para>
/// So if <paramref name="url"/> is without a port, either port 80 or 443
/// is used on which to listen. It's determined by the scheme (ws or wss)
/// in <paramref name="url"/>. (port 80 if the scheme is ws.)
/// </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.
@ -161,14 +168,14 @@ namespace WebSocketSharp.Server
/// connection requests 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 to listen. /// An <see cref="int"/> that represents the port number on which to listen.
/// </param> /// </param>
/// <param name="secure"> /// <param name="secure">
/// A <see cref="bool"/> that indicates providing a secure connection or not. /// A <see cref="bool"/> that indicates providing a secure connection or not.
/// (<c>true</c> indicates providing a secure connection.) /// (<c>true</c> indicates providing a secure connection.)
/// </param> /// </param>
/// <exception cref="ArgumentOutOfRangeException"> /// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="port"/> is not between 1 and 65535. /// <paramref name="port"/> isn't between 1 and 65535.
/// </exception> /// </exception>
/// <exception cref="ArgumentException"> /// <exception cref="ArgumentException">
/// Pair of <paramref name="port"/> and <paramref name="secure"/> is invalid. /// Pair of <paramref name="port"/> and <paramref name="secure"/> is invalid.
@ -193,19 +200,20 @@ namespace WebSocketSharp.Server
/// </para> /// </para>
/// </remarks> /// </remarks>
/// <param name="address"> /// <param name="address">
/// A <see cref="System.Net.IPAddress"/> that represents the local IP address. /// A <see cref="System.Net.IPAddress"/> that represents the local IP address
/// of the server.
/// </param> /// </param>
/// <param name="port"> /// <param name="port">
/// An <see cref="int"/> that represents the port number to listen. /// An <see cref="int"/> that represents the port number on which to listen.
/// </param> /// </param>
/// <exception cref="ArgumentNullException"> /// <exception cref="ArgumentNullException">
/// <paramref name="address"/> is <see langword="null"/>. /// <paramref name="address"/> is <see langword="null"/>.
/// </exception> /// </exception>
/// <exception cref="ArgumentOutOfRangeException"> /// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="port"/> is not between 1 and 65535. /// <paramref name="port"/> isn't between 1 and 65535.
/// </exception> /// </exception>
/// <exception cref="ArgumentException"> /// <exception cref="ArgumentException">
/// <paramref name="address"/> is not the local IP address. /// <paramref name="address"/> isn't a local IP address.
/// </exception> /// </exception>
public WebSocketServer (System.Net.IPAddress address, int port) public WebSocketServer (System.Net.IPAddress address, int port)
: this (address, port, port == 443 ? true : false) : this (address, port, port == 443 ? true : false)
@ -222,10 +230,11 @@ namespace WebSocketSharp.Server
/// connection requests on <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. /// A <see cref="System.Net.IPAddress"/> that represents the local IP address
/// of the server.
/// </param> /// </param>
/// <param name="port"> /// <param name="port">
/// An <see cref="int"/> that represents the port number to listen. /// An <see cref="int"/> that represents the port number on which to listen.
/// </param> /// </param>
/// <param name="secure"> /// <param name="secure">
/// A <see cref="bool"/> that indicates providing a secure connection or not. /// A <see cref="bool"/> that indicates providing a secure connection or not.
@ -235,11 +244,11 @@ namespace WebSocketSharp.Server
/// <paramref name="address"/> is <see langword="null"/>. /// <paramref name="address"/> is <see langword="null"/>.
/// </exception> /// </exception>
/// <exception cref="ArgumentOutOfRangeException"> /// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="port"/> is not between 1 and 65535. /// <paramref name="port"/> isn't between 1 and 65535.
/// </exception> /// </exception>
/// <exception cref="ArgumentException"> /// <exception cref="ArgumentException">
/// <para> /// <para>
/// <paramref name="address"/> is not the local IP address. /// <paramref name="address"/> isn't a local IP address.
/// </para> /// </para>
/// <para> /// <para>
/// -or- /// -or-
@ -332,10 +341,10 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Gets a value indicating whether the server has been started. /// Gets a value indicating whether the server has started.
/// </summary> /// </summary>
/// <value> /// <value>
/// <c>true</c> if the server has been started; otherwise, <c>false</c>. /// <c>true</c> if the server has started; otherwise, <c>false</c>.
/// </value> /// </value>
public bool IsListening { public bool IsListening {
get { get {
@ -344,10 +353,10 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Gets a value indicating whether the server provides secure connection. /// Gets a value indicating whether the server provides a secure connection.
/// </summary> /// </summary>
/// <value> /// <value>
/// <c>true</c> if the server provides secure connection; otherwise, /// <c>true</c> if the server provides a secure connection; otherwise,
/// <c>false</c>. /// <c>false</c>.
/// </value> /// </value>
public bool IsSecure { public bool IsSecure {
@ -395,7 +404,7 @@ namespace WebSocketSharp.Server
/// Gets the port on which to listen for incoming connection requests. /// Gets the port on which to listen for incoming connection requests.
/// </summary> /// </summary>
/// <value> /// <value>
/// An <see cref="int"/> that represents the port number to listen. /// An <see cref="int"/> that represents the port number on which to listen.
/// </value> /// </value>
public int Port { public int Port {
get { get {
@ -622,10 +631,10 @@ namespace WebSocketSharp.Server
_receiveRequestThread.Start (); _receiveRequestThread.Start ();
} }
private void stopListener (int timeOut) private void stopListener (int millisecondsTimeout)
{ {
_listener.Stop (); _listener.Stop ();
_receiveRequestThread.Join (timeOut); _receiveRequestThread.Join (millisecondsTimeout);
} }
private static bool tryCreateUri (string uriString, out Uri result, out string message) private static bool tryCreateUri (string uriString, out Uri result, out string message)