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

View File

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