Refactored HttpServer.cs, WebSocketServer.cs

This commit is contained in:
sta 2014-02-03 16:56:37 +09:00
parent 31c3ece37f
commit 2fe44b9279
3 changed files with 128 additions and 96 deletions

View File

@ -234,7 +234,7 @@ namespace WebSocketSharp
: null;
}
internal static string CheckIfStopped (this ServerState state)
internal static string CheckIfStartable (this ServerState state)
{
return state == ServerState.START
? "The server has already started."

View File

@ -74,20 +74,33 @@ namespace WebSocketSharp.Server
#region Public Constructors
/// <summary>
/// Initializes a new instance of the <see cref="HttpServer"/> class that
/// listens for incoming requests on port 80.
/// Initializes a new instance of the <see cref="HttpServer"/> class.
/// </summary>
/// <remarks>
/// An instance initialized by this constructor listens for the incoming
/// requests on port 80.
/// </remarks>
public HttpServer ()
: this (80)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpServer"/> class that
/// listens for incoming requests on the specified <paramref name="port"/>.
/// Initializes a new instance of the <see cref="HttpServer"/> class with
/// the specified <paramref name="port"/>.
/// </summary>
/// <remarks>
/// <para>
/// An instance initialized by this constructor listens for the incoming
/// requests on <paramref name="port"/>.
/// </para>
/// <para>
/// And if <paramref name="port"/> is 443, that instance provides a secure
/// connection.
/// </para>
/// </remarks>
/// <param name="port">
/// An <see cref="int"/> that contains a port number.
/// An <see cref="int"/> that represents the port number to listen.
/// </param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="port"/> is not between 1 and 65535.
@ -98,12 +111,15 @@ namespace WebSocketSharp.Server
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpServer"/> class that
/// listens for incoming requests on the specified <paramref name="port"/>
/// and <paramref name="secure"/>.
/// Initializes a new instance of the <see cref="HttpServer"/> class with
/// the specified <paramref name="port"/> and <paramref name="secure"/>.
/// </summary>
/// <remarks>
/// An instance initialized by this constructor listens for the incoming
/// requests on <paramref name="port"/>.
/// </remarks>
/// <param name="port">
/// An <see cref="int"/> that contains a port number.
/// An <see cref="int"/> that represents the port number to listen.
/// </param>
/// <param name="secure">
/// A <see cref="bool"/> that indicates providing a secure connection or not.
@ -150,8 +166,8 @@ namespace WebSocketSharp.Server
/// Gets or sets the scheme used to authenticate the clients.
/// </summary>
/// <value>
/// One of the <see cref="WebSocketSharp.Net.AuthenticationSchemes"/> values
/// that indicates the scheme used to authenticate the clients. The default
/// One of the <see cref="WebSocketSharp.Net.AuthenticationSchemes"/> enum
/// values, indicates the scheme used to authenticate the clients. The default
/// value is <see cref="WebSocketSharp.Net.AuthenticationSchemes.Anonymous"/>.
/// </value>
public AuthenticationSchemes AuthenticationSchemes {
@ -238,9 +254,9 @@ namespace WebSocketSharp.Server
/// Gets the logging functions.
/// </summary>
/// <remarks>
/// The default logging level is the <see cref="LogLevel.ERROR"/>. If you
/// change the current logging level, you set the <c>Log.Level</c> property
/// to any of the <see cref="LogLevel"/> values.
/// The default logging level is <see cref="LogLevel.ERROR"/>. If you would
/// like to change it, you should set the <c>Log.Level</c> property to any of
/// the <see cref="LogLevel"/> enum values.
/// </remarks>
/// <value>
/// A <see cref="Logger"/> that provides the logging functions.
@ -255,7 +271,7 @@ namespace WebSocketSharp.Server
/// Gets the port on which to listen for incoming requests.
/// </summary>
/// <value>
/// An <see cref="int"/> that contains a port number.
/// An <see cref="int"/> that represents the port number to listen.
/// </value>
public int Port {
get {
@ -268,7 +284,7 @@ namespace WebSocketSharp.Server
/// <see cref="HttpServer"/>.
/// </summary>
/// <value>
/// A <see cref="string"/> that contains the name of the realm.
/// A <see cref="string"/> that represents the name of the realm.
/// The default value is <c>SECRET AREA</c>.
/// </value>
public string Realm {
@ -288,7 +304,7 @@ namespace WebSocketSharp.Server
/// Gets or sets the document root path of server.
/// </summary>
/// <value>
/// A <see cref="string"/> that contains the document root path of server.
/// A <see cref="string"/> that represents the document root path of server.
/// The default value is <c>./Public</c>.
/// </value>
public string RootPath {
@ -312,8 +328,8 @@ namespace WebSocketSharp.Server
/// </summary>
/// <value>
/// A Func&lt;<see cref="IIdentity"/>, <see cref="NetworkCredential"/>&gt;
/// delegate that references the method(s) used to find the credentials. The
/// default value is a function that only returns <see langword="null"/>.
/// delegate that references the method(s) used to find the credentials.
/// The default value is a function that only returns <see langword="null"/>.
/// </value>
public Func<IIdentity, NetworkCredential> UserCredentialsFinder {
get {
@ -329,7 +345,7 @@ namespace WebSocketSharp.Server
}
/// <summary>
/// Gets the functions for the WebSocket services provided by the
/// Gets the access to the WebSocket services provided by the
/// <see cref="HttpServer"/>.
/// </summary>
/// <value>
@ -535,11 +551,6 @@ namespace WebSocketSharp.Server
return true;
}
private string checkIfCanStop (Func<string> checkParams)
{
return _state.CheckIfStart () ?? checkParams ();
}
private string checkIfCertExists ()
{
return _secure &&
@ -598,7 +609,7 @@ namespace WebSocketSharp.Server
/// and removes <c>'/'</c> from tail end of <paramref name="servicePath"/>.
/// </remarks>
/// <param name="servicePath">
/// A <see cref="string"/> that contains an absolute path to the WebSocket
/// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service.
/// </param>
/// <typeparam name="TWithNew">
@ -628,7 +639,7 @@ namespace WebSocketSharp.Server
/// </para>
/// </remarks>
/// <param name="servicePath">
/// A <see cref="string"/> that contains an absolute path to the WebSocket
/// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service.
/// </param>
/// <param name="serviceConstructor">
@ -639,7 +650,8 @@ namespace WebSocketSharp.Server
/// The type of the WebSocket service. The T must inherit the
/// <see cref="WebSocketService"/> class.
/// </typeparam>
public void AddWebSocketService<T> (string servicePath, Func<T> serviceConstructor)
public void AddWebSocketService<T> (
string servicePath, Func<T> serviceConstructor)
where T : WebSocketService
{
var msg = servicePath.CheckIfValidServicePath () ??
@ -667,13 +679,14 @@ namespace WebSocketSharp.Server
/// Gets the contents of the file with the specified <paramref name="path"/>.
/// </summary>
/// <returns>
/// An array of <see cref="byte"/> that contains the contents of the file if
/// An array of <see cref="byte"/> that receives the contents of the file if
/// it exists; otherwise, <see langword="null"/>.
/// </returns>
/// <param name="path">
/// A <see cref="string"/> that contains a virtual path to the file to get.
/// A <see cref="string"/> that represents the virtual path to the file
/// to get.
/// </param>
public byte[] GetFile (string path)
public byte [] GetFile (string path)
{
var filePath = RootPath + path;
if (_windows)
@ -685,8 +698,7 @@ namespace WebSocketSharp.Server
}
/// <summary>
/// Removes the WebSocket service with the specified
/// <paramref name="servicePath"/>.
/// Removes the WebSocket service with the specified <paramref name="servicePath"/>.
/// </summary>
/// <remarks>
/// This method converts <paramref name="servicePath"/> to URL-decoded string
@ -697,7 +709,7 @@ namespace WebSocketSharp.Server
/// otherwise, <c>false</c>.
/// </returns>
/// <param name="servicePath">
/// A <see cref="string"/> that contains an absolute path to the WebSocket
/// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param>
public bool RemoveWebSocketService (string servicePath)
@ -719,7 +731,7 @@ namespace WebSocketSharp.Server
public void Start ()
{
lock (_sync) {
var msg = _state.CheckIfStopped () ?? checkIfCertExists ();
var msg = _state.CheckIfStartable () ?? checkIfCertExists ();
if (msg != null) {
_logger.Error (
String.Format (
@ -772,9 +784,9 @@ namespace WebSocketSharp.Server
{
byte [] data = null;
lock (_sync) {
var msg = checkIfCanStop (
() => code.CheckIfValidCloseStatusCode () ??
(data = code.Append (reason)).CheckIfValidControlData ("reason"));
var msg = _state.CheckIfStart () ??
code.CheckIfValidCloseStatusCode () ??
(data = code.Append (reason)).CheckIfValidControlData ("reason");
if (msg != null) {
_logger.Error (
@ -808,9 +820,8 @@ namespace WebSocketSharp.Server
{
byte [] data = null;
lock (_sync) {
var msg = checkIfCanStop (
() => (data = ((ushort) code).Append (reason))
.CheckIfValidControlData ("reason"));
var msg = _state.CheckIfStart () ??
(data = ((ushort) code).Append (reason)).CheckIfValidControlData ("reason");
if (msg != null) {
_logger.Error (

View File

@ -48,8 +48,7 @@ using WebSocketSharp.Net.WebSockets;
namespace WebSocketSharp.Server
{
/// <summary>
/// Provides the functions of the server that receives the WebSocket connection
/// requests.
/// Provides a WebSocket protocol server.
/// </summary>
/// <remarks>
/// The WebSocketServer class provides the multi WebSocket service.
@ -78,9 +77,12 @@ namespace WebSocketSharp.Server
#region Public Constructors
/// <summary>
/// Initializes a new instance of the <see cref="WebSocketServer"/> class
/// that listens for incoming requests on port 80.
/// Initializes a new instance of the <see cref="WebSocketServer"/> class.
/// </summary>
/// <remarks>
/// An instance initialized by this constructor listens for the incoming
/// connection requests on port 80.
/// </remarks>
public WebSocketServer ()
: this (80)
{
@ -88,11 +90,20 @@ namespace WebSocketSharp.Server
/// <summary>
/// Initializes a new instance of the <see cref="WebSocketServer"/> class
/// that listens for incoming connection attempts on the specified
/// <paramref name="port"/>.
/// with the specified <paramref name="port"/>.
/// </summary>
/// <remarks>
/// <para>
/// An instance initialized by this constructor listens for the incoming
/// connection requests on <paramref name="port"/>.
/// </para>
/// <para>
/// And if <paramref name="port"/> is 443, that instance provides a secure
/// connection.
/// </para>
/// </remarks>
/// <param name="port">
/// An <see cref="int"/> that contains a port number.
/// An <see cref="int"/> that represents the port number to listen.
/// </param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="port"/> is not between 1 and 65535.
@ -104,11 +115,14 @@ namespace WebSocketSharp.Server
/// <summary>
/// Initializes a new instance of the <see cref="WebSocketServer"/> class
/// that listens for incoming connection attempts on the specified WebSocket
/// URL.
/// with the specified WebSocket URL.
/// </summary>
/// <remarks>
/// An instance initialized by this constructor listens for the incoming
/// connection requests on the port number of <paramref name="url"/>.
/// </remarks>
/// <param name="url">
/// A <see cref="string"/> that contains a WebSocket URL.
/// A <see cref="string"/> that represents the WebSocket URL of the server.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="url"/> is <see langword="null"/>.
@ -140,11 +154,14 @@ namespace WebSocketSharp.Server
/// <summary>
/// Initializes a new instance of the <see cref="WebSocketServer"/> class
/// that listens for incoming connection attempts on the specified
/// <paramref name="port"/> and <paramref name="secure"/>.
/// with the specified <paramref name="port"/> and <paramref name="secure"/>.
/// </summary>
/// <remarks>
/// An instance initialized by this constructor listens for the incoming
/// connection requests on <paramref name="port"/>.
/// </remarks>
/// <param name="port">
/// An <see cref="int"/> that contains a port number.
/// An <see cref="int"/> that represents the port number to listen.
/// </param>
/// <param name="secure">
/// A <see cref="bool"/> that indicates providing a secure connection or not.
@ -163,15 +180,23 @@ namespace WebSocketSharp.Server
/// <summary>
/// Initializes a new instance of the <see cref="WebSocketServer"/> class
/// that listens for incoming connection attempts on the specified
/// <paramref name="address"/> and <paramref name="port"/>.
/// with the specified <paramref name="address"/> and <paramref name="port"/>.
/// </summary>
/// <remarks>
/// <para>
/// An instance initialized by this constructor listens for the incoming
/// connection requests on <paramref name="port"/>.
/// </para>
/// <para>
/// And if <paramref name="port"/> is 443, that instance provides a secure
/// connection.
/// </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.
/// </param>
/// <param name="port">
/// An <see cref="int"/> that contains a port number.
/// An <see cref="int"/> that represents the port number to listen.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="address"/> is <see langword="null"/>.
@ -189,16 +214,18 @@ namespace WebSocketSharp.Server
/// <summary>
/// Initializes a new instance of the <see cref="WebSocketServer"/> class
/// that listens for incoming connection attempts on the specified
/// <paramref name="address"/>, <paramref name="port"/> and
/// <paramref name="secure"/>.
/// with the specified <paramref name="address"/>, <paramref name="port"/>,
/// and <paramref name="secure"/>.
/// </summary>
/// <remarks>
/// An instance initialized by this constructor listens for the incoming
/// 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.
/// </param>
/// <param name="port">
/// An <see cref="int"/> that contains a port number.
/// An <see cref="int"/> that represents the port number to listen.
/// </param>
/// <param name="secure">
/// A <see cref="bool"/> that indicates providing a secure connection or not.
@ -251,12 +278,11 @@ namespace WebSocketSharp.Server
#region Public Properties
/// <summary>
/// Gets the local IP address on which to listen for incoming connection
/// attempts.
/// Gets the local IP address of the server.
/// </summary>
/// <value>
/// A <see cref="System.Net.IPAddress"/> that represents the local IP
/// address.
/// address of the server.
/// </value>
public System.Net.IPAddress Address {
get {
@ -268,8 +294,8 @@ namespace WebSocketSharp.Server
/// Gets or sets the scheme used to authenticate the clients.
/// </summary>
/// <value>
/// One of the <see cref="WebSocketSharp.Net.AuthenticationSchemes"/> values
/// that indicates the scheme used to authenticate the clients. The default
/// One of the <see cref="WebSocketSharp.Net.AuthenticationSchemes"/> enum
/// values, indicates the scheme used to authenticate the clients. The default
/// value is <see cref="WebSocketSharp.Net.AuthenticationSchemes.Anonymous"/>.
/// </value>
public AuthenticationSchemes AuthenticationSchemes {
@ -352,9 +378,9 @@ namespace WebSocketSharp.Server
/// Gets the logging functions.
/// </summary>
/// <remarks>
/// The default logging level is the <see cref="LogLevel.ERROR"/>. If you
/// change the current logging level, you set the <c>Log.Level</c> property
/// to any of the <see cref="LogLevel"/> values.
/// The default logging level is <see cref="LogLevel.ERROR"/>. If you would
/// like to change it, you should set the <c>Log.Level</c> property to any of
/// the <see cref="LogLevel"/> enum values.
/// </remarks>
/// <value>
/// A <see cref="Logger"/> that provides the logging functions.
@ -366,10 +392,10 @@ namespace WebSocketSharp.Server
}
/// <summary>
/// Gets the port on which to listen for incoming connection attempts.
/// Gets the port on which to listen for incoming connection requests.
/// </summary>
/// <value>
/// An <see cref="int"/> that contains a port number.
/// An <see cref="int"/> that represents the port number to listen.
/// </value>
public int Port {
get {
@ -382,7 +408,7 @@ namespace WebSocketSharp.Server
/// <see cref="WebSocketServer"/>.
/// </summary>
/// <value>
/// A <see cref="string"/> that contains the name of the realm.
/// A <see cref="string"/> that represents the name of the realm.
/// The default value is <c>SECRET AREA</c>.
/// </value>
public string Realm {
@ -404,8 +430,8 @@ namespace WebSocketSharp.Server
/// </summary>
/// <value>
/// A Func&lt;<see cref="IIdentity"/>, <see cref="NetworkCredential"/>&gt;
/// delegate that references the method(s) used to find the credentials. The
/// default value is a function that only returns <see langword="null"/>.
/// delegate that references the method(s) used to find the credentials.
/// The default value is a function that only returns <see langword="null"/>.
/// </value>
public Func<IIdentity, NetworkCredential> UserCredentialsFinder {
get {
@ -421,7 +447,7 @@ namespace WebSocketSharp.Server
}
/// <summary>
/// Gets the functions for the WebSocket services provided by the
/// Gets the access to the WebSocket services provided by the
/// <see cref="WebSocketServer"/>.
/// </summary>
/// <value>
@ -549,11 +575,6 @@ namespace WebSocketSharp.Server
return true;
}
private string checkIfCanStop (Func<string> checkParams)
{
return _state.CheckIfStart () ?? checkParams ();
}
private string checkIfCertExists ()
{
return _secure && _cert == null
@ -635,7 +656,7 @@ namespace WebSocketSharp.Server
/// and removes <c>'/'</c> from tail end of <paramref name="servicePath"/>.
/// </remarks>
/// <param name="servicePath">
/// A <see cref="string"/> that contains an absolute path to the WebSocket
/// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service.
/// </param>
/// <typeparam name="TWithNew">
@ -665,7 +686,7 @@ namespace WebSocketSharp.Server
/// </para>
/// </remarks>
/// <param name="servicePath">
/// A <see cref="string"/> that contains an absolute path to the WebSocket
/// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service.
/// </param>
/// <param name="serviceConstructor">
@ -676,7 +697,8 @@ namespace WebSocketSharp.Server
/// The type of the WebSocket service. The T must inherit the
/// <see cref="WebSocketService"/> class.
/// </typeparam>
public void AddWebSocketService<T> (string servicePath, Func<T> serviceConstructor)
public void AddWebSocketService<T> (
string servicePath, Func<T> serviceConstructor)
where T : WebSocketService
{
var msg = servicePath.CheckIfValidServicePath () ??
@ -713,7 +735,7 @@ namespace WebSocketSharp.Server
/// otherwise, <c>false</c>.
/// </returns>
/// <param name="servicePath">
/// A <see cref="string"/> that contains an absolute path to the WebSocket
/// A <see cref="string"/> that represents the absolute path to the WebSocket
/// service to find.
/// </param>
public bool RemoveWebSocketService (string servicePath)
@ -735,7 +757,7 @@ namespace WebSocketSharp.Server
public void Start ()
{
lock (_sync) {
var msg = _state.CheckIfStopped () ?? checkIfCertExists ();
var msg = _state.CheckIfStartable () ?? checkIfCertExists ();
if (msg != null) {
_logger.Error (
String.Format (
@ -788,9 +810,9 @@ namespace WebSocketSharp.Server
{
byte [] data = null;
lock (_sync) {
var msg = checkIfCanStop (
() => code.CheckIfValidCloseStatusCode () ??
(data = code.Append (reason)).CheckIfValidControlData ("reason"));
var msg = _state.CheckIfStart () ??
code.CheckIfValidCloseStatusCode () ??
(data = code.Append (reason)).CheckIfValidControlData ("reason");
if (msg != null) {
_logger.Error (
@ -824,9 +846,8 @@ namespace WebSocketSharp.Server
{
byte [] data = null;
lock (_sync) {
var msg = checkIfCanStop (
() => (data = ((ushort) code).Append (reason))
.CheckIfValidControlData ("reason"));
var msg = _state.CheckIfStart () ??
(data = ((ushort) code).Append (reason)).CheckIfValidControlData ("reason");
if (msg != null) {
_logger.Error (