Modified WebSocketServer.cs and removed WebSocketServerBase.cs
This commit is contained in:
parent
3488308b29
commit
cbf73e9865
@ -17,9 +17,9 @@ namespace Example2
|
|||||||
#if DEBUG
|
#if DEBUG
|
||||||
wssv.Log.Level = LogLevel.TRACE;
|
wssv.Log.Level = LogLevel.TRACE;
|
||||||
#endif
|
#endif
|
||||||
//var file = ConfigurationManager.AppSettings ["ServerCertFile"];
|
//var cert = ConfigurationManager.AppSettings ["ServerCertFile"];
|
||||||
//var password = ConfigurationManager.AppSettings ["CertFilePassword"];
|
//var password = ConfigurationManager.AppSettings ["CertFilePassword"];
|
||||||
//wssv.Certificate = new X509Certificate2 (file, password);
|
//wssv.Certificate = new X509Certificate2 (cert, password);
|
||||||
//wssv.KeepClean = false;
|
//wssv.KeepClean = false;
|
||||||
wssv.AddWebSocketService<Echo> ("/Echo");
|
wssv.AddWebSocketService<Echo> ("/Echo");
|
||||||
wssv.AddWebSocketService<Chat> ("/Chat");
|
wssv.AddWebSocketService<Chat> ("/Chat");
|
||||||
@ -28,10 +28,14 @@ namespace Example2
|
|||||||
//wssv.AddWebSocketService<Chat> ("/チャット");
|
//wssv.AddWebSocketService<Chat> ("/チャット");
|
||||||
|
|
||||||
wssv.Start ();
|
wssv.Start ();
|
||||||
Console.WriteLine (
|
if (wssv.IsListening)
|
||||||
"A WebSocket Server listening on port: {0} service path:", wssv.Port);
|
{
|
||||||
foreach (var path in wssv.WebSocketServices.ServicePaths)
|
Console.WriteLine (
|
||||||
Console.WriteLine (" {0}", path);
|
"A WebSocket Server listening on port: {0} service paths:", wssv.Port);
|
||||||
|
|
||||||
|
foreach (var path in wssv.WebSocketServices.ServicePaths)
|
||||||
|
Console.WriteLine (" {0}", path);
|
||||||
|
}
|
||||||
|
|
||||||
Console.WriteLine ("\nPress Enter key to stop server...");
|
Console.WriteLine ("\nPress Enter key to stop server...");
|
||||||
Console.ReadLine ();
|
Console.ReadLine ();
|
||||||
|
@ -19,9 +19,9 @@ namespace Example3
|
|||||||
_httpsv.Log.Level = LogLevel.TRACE;
|
_httpsv.Log.Level = LogLevel.TRACE;
|
||||||
#endif
|
#endif
|
||||||
_httpsv.RootPath = ConfigurationManager.AppSettings ["RootPath"];
|
_httpsv.RootPath = ConfigurationManager.AppSettings ["RootPath"];
|
||||||
//var certFile = ConfigurationManager.AppSettings ["ServerCertFile"];
|
//var cert = ConfigurationManager.AppSettings ["ServerCertFile"];
|
||||||
//var password = ConfigurationManager.AppSettings ["CertFilePassword"];
|
//var password = ConfigurationManager.AppSettings ["CertFilePassword"];
|
||||||
//_httpsv.Certificate = new X509Certificate2 (certFile, password);
|
//_httpsv.Certificate = new X509Certificate2 (cert, password);
|
||||||
//_httpsv.KeepClean = false;
|
//_httpsv.KeepClean = false;
|
||||||
_httpsv.AddWebSocketService<Echo> ("/Echo");
|
_httpsv.AddWebSocketService<Echo> ("/Echo");
|
||||||
_httpsv.AddWebSocketService<Chat> ("/Chat");
|
_httpsv.AddWebSocketService<Chat> ("/Chat");
|
||||||
@ -35,14 +35,14 @@ namespace Example3
|
|||||||
_httpsv.Start ();
|
_httpsv.Start ();
|
||||||
if (_httpsv.IsListening)
|
if (_httpsv.IsListening)
|
||||||
{
|
{
|
||||||
Console.WriteLine ("An HTTP Server listening on port: {0} service path:", _httpsv.Port);
|
Console.WriteLine (
|
||||||
|
"An HTTP Server listening on port: {0} WebSocket service paths:", _httpsv.Port);
|
||||||
|
|
||||||
foreach (var path in _httpsv.WebSocketServices.ServicePaths)
|
foreach (var path in _httpsv.WebSocketServices.ServicePaths)
|
||||||
Console.WriteLine (" {0}", path);
|
Console.WriteLine (" {0}", path);
|
||||||
|
|
||||||
Console.WriteLine ();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine ("Press Enter key to stop the server...");
|
Console.WriteLine ("\nPress Enter key to stop the server...");
|
||||||
Console.ReadLine ();
|
Console.ReadLine ();
|
||||||
|
|
||||||
_httpsv.Stop ();
|
_httpsv.Stop ();
|
||||||
|
@ -657,6 +657,17 @@ namespace WebSocketSharp
|
|||||||
return CompressionMethod.NONE;
|
return CompressionMethod.NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal static System.Net.IPAddress ToIPAddress (this string hostNameOrAddress)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
var addrs = System.Net.Dns.GetHostAddresses (hostNameOrAddress);
|
||||||
|
return addrs [0];
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
internal static ushort ToUInt16 (this byte [] src, ByteOrder srcOrder)
|
internal static ushort ToUInt16 (this byte [] src, ByteOrder srcOrder)
|
||||||
{
|
{
|
||||||
return BitConverter.ToUInt16 (src.ToHostOrder (srcOrder), 0);
|
return BitConverter.ToUInt16 (src.ToHostOrder (srcOrder), 0);
|
||||||
@ -675,6 +686,81 @@ namespace WebSocketSharp
|
|||||||
: "/";
|
: "/";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to create a <see cref="Uri"/> for WebSocket with the specified <paramref name="uriString"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// <c>true</c> if the <see cref="Uri"/> is successfully created; otherwise, <c>false</c>.
|
||||||
|
/// </returns>
|
||||||
|
/// <param name="uriString">
|
||||||
|
/// A <see cref="string"/> that contains a WebSocket URL to try.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="result">
|
||||||
|
/// When this method returns, a <see cref="Uri"/> that represents the WebSocket URL
|
||||||
|
/// if <paramref name="uriString"/> is valid; otherwise, <see langword="null"/>.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="message">
|
||||||
|
/// When this method returns, a <see cref="string"/> that contains an error message
|
||||||
|
/// if <paramref name="uriString"/> is invalid; otherwise, <c>String.Empty</c>.
|
||||||
|
/// </param>
|
||||||
|
internal static bool TryCreateWebSocketUri (this string uriString, out Uri result, out string message)
|
||||||
|
{
|
||||||
|
result = null;
|
||||||
|
if (uriString.Length == 0)
|
||||||
|
{
|
||||||
|
message = "Must not be empty.";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var uri = uriString.ToUri ();
|
||||||
|
if (!uri.IsAbsoluteUri)
|
||||||
|
{
|
||||||
|
message = "Must be the absolute URI: " + uriString;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var scheme = uri.Scheme;
|
||||||
|
if (scheme != "ws" && scheme != "wss")
|
||||||
|
{
|
||||||
|
message = "The scheme part must be 'ws' or 'wss': " + scheme;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var fragment = uri.Fragment;
|
||||||
|
if (fragment.Length != 0)
|
||||||
|
{
|
||||||
|
message = "Must not contain the fragment component: " + uriString;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var port = uri.Port;
|
||||||
|
if (port > 0)
|
||||||
|
{
|
||||||
|
if (port > 65535)
|
||||||
|
{
|
||||||
|
message = "The port part must be between 1 and 65535: " + port;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((scheme == "ws" && port == 443) || (scheme == "wss" && port == 80))
|
||||||
|
{
|
||||||
|
message = String.Format ("Invalid pair of scheme and port: {0}, {1}", scheme, port);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
port = scheme == "ws" ? 80 : 443;
|
||||||
|
var url = String.Format ("{0}://{1}:{2}{3}", scheme, uri.Host, port, uri.PathAndQuery);
|
||||||
|
uri = url.ToUri ();
|
||||||
|
}
|
||||||
|
|
||||||
|
result = uri;
|
||||||
|
message = String.Empty;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
internal static string Unquote (this string value)
|
internal static string Unquote (this string value)
|
||||||
{
|
{
|
||||||
var start = value.IndexOf ('\"');
|
var start = value.IndexOf ('\"');
|
||||||
@ -1048,10 +1134,12 @@ namespace WebSocketSharp
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Determines whether the specified <see cref="System.Net.IPAddress"/> represents a local IP address.
|
/// Determines whether the specified <see cref="System.Net.IPAddress"/> represents
|
||||||
|
/// the local IP address.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// <c>true</c> if <paramref name="address"/> represents a local IP address; otherwise, <c>false</c>.
|
/// <c>true</c> if <paramref name="address"/> represents the local IP address;
|
||||||
|
/// otherwise, <c>false</c>.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
/// <param name="address">
|
/// <param name="address">
|
||||||
/// A <see cref="System.Net.IPAddress"/> to test.
|
/// A <see cref="System.Net.IPAddress"/> to test.
|
||||||
@ -1064,7 +1152,8 @@ namespace WebSocketSharp
|
|||||||
if (address == null)
|
if (address == null)
|
||||||
throw new ArgumentNullException ("address");
|
throw new ArgumentNullException ("address");
|
||||||
|
|
||||||
if (System.Net.IPAddress.IsLoopback (address))
|
if (address.Equals (System.Net.IPAddress.Any) ||
|
||||||
|
System.Net.IPAddress.IsLoopback (address))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
var host = System.Net.Dns.GetHostName ();
|
var host = System.Net.Dns.GetHostName ();
|
||||||
@ -1549,87 +1638,6 @@ namespace WebSocketSharp
|
|||||||
: new Uri (uriString, UriKind.Relative);
|
: new Uri (uriString, UriKind.Relative);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Tries to create a new WebSocket <see cref="Uri"/> with the specified <paramref name="uriString"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>
|
|
||||||
/// <c>true</c> if a WebSocket <see cref="Uri"/> is successfully created; otherwise, <c>false</c>.
|
|
||||||
/// </returns>
|
|
||||||
/// <param name="uriString">
|
|
||||||
/// A <see cref="string"/> that contains a WebSocket URI.
|
|
||||||
/// </param>
|
|
||||||
/// <param name="result">
|
|
||||||
/// When this method returns, contains a created WebSocket <see cref="Uri"/>
|
|
||||||
/// if <paramref name="uriString"/> is valid WebSocket URI; otherwise, <see langword="null"/>.
|
|
||||||
/// </param>
|
|
||||||
/// <param name="message">
|
|
||||||
/// When this method returns, contains a error message <see cref="string"/>
|
|
||||||
/// if <paramref name="uriString"/> is invalid WebSocket URI; otherwise, <c>String.Empty</c>.
|
|
||||||
/// </param>
|
|
||||||
/// <exception cref="ArgumentNullException">
|
|
||||||
/// <paramref name="uriString"/> is <see langword="null"/>.
|
|
||||||
/// </exception>
|
|
||||||
public static bool TryCreateWebSocketUri (this string uriString, out Uri result, out string message)
|
|
||||||
{
|
|
||||||
if (uriString == null)
|
|
||||||
throw new ArgumentNullException ("uriString");
|
|
||||||
|
|
||||||
result = null;
|
|
||||||
if (uriString.Length == 0)
|
|
||||||
{
|
|
||||||
message = "Must not be empty.";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
var uri = uriString.ToUri ();
|
|
||||||
if (!uri.IsAbsoluteUri)
|
|
||||||
{
|
|
||||||
message = "Not absolute URI: " + uriString;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
var scheme = uri.Scheme;
|
|
||||||
if (scheme != "ws" && scheme != "wss")
|
|
||||||
{
|
|
||||||
message = "Unsupported scheme: " + scheme;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
var fragment = uri.Fragment;
|
|
||||||
if (fragment != null && fragment.Length != 0)
|
|
||||||
{
|
|
||||||
message = "Must not contain the fragment component: " + uriString;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
var port = uri.Port;
|
|
||||||
if (port > 0)
|
|
||||||
{
|
|
||||||
if (port > 65535)
|
|
||||||
{
|
|
||||||
message = "Invalid port number: " + port;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((scheme == "ws" && port == 443) || (scheme == "wss" && port == 80))
|
|
||||||
{
|
|
||||||
message = String.Format ("Invalid pair of scheme and port: {0}, {1}", scheme, port);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
port = scheme == "ws" ? 80 : 443;
|
|
||||||
var url = String.Format ("{0}://{1}:{2}{3}", scheme, uri.Host, port, uri.PathAndQuery);
|
|
||||||
uri = url.ToUri ();
|
|
||||||
}
|
|
||||||
|
|
||||||
result = uri;
|
|
||||||
message = String.Empty;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// URL-decodes the specified <see cref="string"/>.
|
/// URL-decodes the specified <see cref="string"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -56,7 +56,6 @@ namespace WebSocketSharp.Server
|
|||||||
#region Private Fields
|
#region Private Fields
|
||||||
|
|
||||||
private HttpListener _listener;
|
private HttpListener _listener;
|
||||||
private volatile bool _listening;
|
|
||||||
private Logger _logger;
|
private Logger _logger;
|
||||||
private int _port;
|
private int _port;
|
||||||
private Thread _receiveRequestThread;
|
private Thread _receiveRequestThread;
|
||||||
@ -72,8 +71,8 @@ namespace WebSocketSharp.Server
|
|||||||
#region Public Constructors
|
#region Public Constructors
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="HttpServer"/> class that listens for incoming requests
|
/// Initializes a new instance of the <see cref="HttpServer"/> class that listens for
|
||||||
/// on port 80.
|
/// incoming requests on port 80.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public HttpServer ()
|
public HttpServer ()
|
||||||
: this (80)
|
: this (80)
|
||||||
@ -81,14 +80,14 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="HttpServer"/> class that listens for incoming requests
|
/// Initializes a new instance of the <see cref="HttpServer"/> class that listens for
|
||||||
/// on the specified <paramref name="port"/>.
|
/// incoming requests on the specified <paramref name="port"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="port">
|
/// <param name="port">
|
||||||
/// An <see cref="int"/> that contains a port number.
|
/// An <see cref="int"/> that contains a port number.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <exception cref="ArgumentOutOfRangeException">
|
/// <exception cref="ArgumentOutOfRangeException">
|
||||||
/// <paramref name="port"/> is 0 or less, or 65536 or greater.
|
/// <paramref name="port"/> is not 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)
|
||||||
@ -96,8 +95,8 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="HttpServer"/> class that listens for incoming requests
|
/// Initializes a new instance of the <see cref="HttpServer"/> class that listens for
|
||||||
/// on the specified <paramref name="port"/> and <paramref name="secure"/>.
|
/// incoming requests on the specified <paramref name="port"/> and <paramref name="secure"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="port">
|
/// <param name="port">
|
||||||
/// An <see cref="int"/> that contains a port number.
|
/// An <see cref="int"/> that contains a port number.
|
||||||
@ -107,7 +106,7 @@ namespace WebSocketSharp.Server
|
|||||||
/// (<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 0 or less, or 65536 or greater.
|
/// <paramref name="port"/> is not 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.
|
||||||
@ -115,7 +114,7 @@ namespace WebSocketSharp.Server
|
|||||||
public HttpServer (int port, bool secure)
|
public HttpServer (int port, bool secure)
|
||||||
{
|
{
|
||||||
if (!port.IsPortNumber ())
|
if (!port.IsPortNumber ())
|
||||||
throw new ArgumentOutOfRangeException ("port", "Invalid port number: " + port);
|
throw new ArgumentOutOfRangeException ("port", "Must be between 1 and 65535: " + port);
|
||||||
|
|
||||||
if ((port == 80 && secure) || (port == 443 && !secure))
|
if ((port == 80 && secure) || (port == 443 && !secure))
|
||||||
throw new ArgumentException (String.Format (
|
throw new ArgumentException (String.Format (
|
||||||
@ -143,15 +142,16 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
set {
|
set {
|
||||||
if (_listening)
|
if (_state == ServerState.START || _state == ServerState.SHUTDOWN)
|
||||||
{
|
{
|
||||||
_logger.Error (
|
_logger.Error (
|
||||||
"The value of Certificate property cannot be changed because the server has already been started.");
|
"The value of Certificate property cannot be changed because the server has already been started.");
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (EndPointListener.CertificateExists (_port, _listener.CertificateFolderPath))
|
if (EndPointListener.CertificateExists (_port, _listener.CertificateFolderPath))
|
||||||
_logger.Warn ("Server certificate associated with the port number already exists.");
|
_logger.Warn ("The server certificate associated with the port number already exists.");
|
||||||
|
|
||||||
_listener.DefaultCertificate = value;
|
_listener.DefaultCertificate = value;
|
||||||
}
|
}
|
||||||
@ -165,7 +165,7 @@ namespace WebSocketSharp.Server
|
|||||||
/// </value>
|
/// </value>
|
||||||
public bool IsListening {
|
public bool IsListening {
|
||||||
get {
|
get {
|
||||||
return _listening;
|
return _state == ServerState.START;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,10 +243,11 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
set {
|
set {
|
||||||
if (_listening)
|
if (_state == ServerState.START || _state == ServerState.SHUTDOWN)
|
||||||
{
|
{
|
||||||
_logger.Error (
|
_logger.Error (
|
||||||
"The value of RootPath property cannot be changed because the server has already been started.");
|
"The value of RootPath property cannot be changed because the server has already been started.");
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -323,13 +324,12 @@ namespace WebSocketSharp.Server
|
|||||||
{
|
{
|
||||||
lock (_sync)
|
lock (_sync)
|
||||||
{
|
{
|
||||||
if (_state != ServerState.START)
|
if (!IsListening)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_state = ServerState.SHUTDOWN;
|
_state = ServerState.SHUTDOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
_listening = false;
|
|
||||||
_serviceHosts.Stop (
|
_serviceHosts.Stop (
|
||||||
((ushort) CloseStatusCode.SERVER_ERROR).ToByteArrayInternally (ByteOrder.BIG), true);
|
((ushort) CloseStatusCode.SERVER_ERROR).ToByteArrayInternally (ByteOrder.BIG), true);
|
||||||
_listener.Abort ();
|
_listener.Abort ();
|
||||||
@ -349,7 +349,6 @@ namespace WebSocketSharp.Server
|
|||||||
private void init ()
|
private void init ()
|
||||||
{
|
{
|
||||||
_listener = new HttpListener ();
|
_listener = new HttpListener ();
|
||||||
_listening = false;
|
|
||||||
_logger = new Logger ();
|
_logger = new Logger ();
|
||||||
_serviceHosts = new WebSocketServiceHostManager (_logger);
|
_serviceHosts = new WebSocketServiceHostManager (_logger);
|
||||||
_state = ServerState.READY;
|
_state = ServerState.READY;
|
||||||
@ -366,59 +365,59 @@ namespace WebSocketSharp.Server
|
|||||||
|
|
||||||
private void processHttpRequest (HttpListenerContext context)
|
private void processHttpRequest (HttpListenerContext context)
|
||||||
{
|
{
|
||||||
var eventArgs = 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" && OnGet != null)
|
||||||
{
|
{
|
||||||
OnGet (this, eventArgs);
|
OnGet (this, args);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (method == "HEAD" && OnHead != null)
|
if (method == "HEAD" && OnHead != null)
|
||||||
{
|
{
|
||||||
OnHead (this, eventArgs);
|
OnHead (this, args);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (method == "POST" && OnPost != null)
|
if (method == "POST" && OnPost != null)
|
||||||
{
|
{
|
||||||
OnPost (this, eventArgs);
|
OnPost (this, args);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (method == "PUT" && OnPut != null)
|
if (method == "PUT" && OnPut != null)
|
||||||
{
|
{
|
||||||
OnPut (this, eventArgs);
|
OnPut (this, args);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (method == "DELETE" && OnDelete != null)
|
if (method == "DELETE" && OnDelete != null)
|
||||||
{
|
{
|
||||||
OnDelete (this, eventArgs);
|
OnDelete (this, args);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (method == "OPTIONS" && OnOptions != null)
|
if (method == "OPTIONS" && OnOptions != null)
|
||||||
{
|
{
|
||||||
OnOptions (this, eventArgs);
|
OnOptions (this, args);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (method == "TRACE" && OnTrace != null)
|
if (method == "TRACE" && OnTrace != null)
|
||||||
{
|
{
|
||||||
OnTrace (this, eventArgs);
|
OnTrace (this, args);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (method == "CONNECT" && OnConnect != null)
|
if (method == "CONNECT" && OnConnect != null)
|
||||||
{
|
{
|
||||||
OnConnect (this, eventArgs);
|
OnConnect (this, args);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (method == "PATCH" && OnPatch != null)
|
if (method == "PATCH" && OnPatch != null)
|
||||||
{
|
{
|
||||||
OnPatch (this, eventArgs);
|
OnPatch (this, args);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -486,7 +485,7 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_listening)
|
if (IsListening)
|
||||||
abort ();
|
abort ();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -497,14 +496,10 @@ namespace WebSocketSharp.Server
|
|||||||
_receiveRequestThread.Start ();
|
_receiveRequestThread.Start ();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void stopListener ()
|
private void stopListener (int timeOut)
|
||||||
{
|
{
|
||||||
if (!_listening)
|
|
||||||
return;
|
|
||||||
|
|
||||||
_listening = false;
|
|
||||||
_listener.Close ();
|
_listener.Close ();
|
||||||
_receiveRequestThread.Join (5 * 1000);
|
_receiveRequestThread.Join (timeOut);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -637,7 +632,6 @@ namespace WebSocketSharp.Server
|
|||||||
_serviceHosts.Start ();
|
_serviceHosts.Start ();
|
||||||
_listener.Start ();
|
_listener.Start ();
|
||||||
startReceiveRequestThread ();
|
startReceiveRequestThread ();
|
||||||
_listening = true;
|
|
||||||
|
|
||||||
_state = ServerState.START;
|
_state = ServerState.START;
|
||||||
}
|
}
|
||||||
@ -661,7 +655,7 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
_serviceHosts.Stop (new byte []{}, true);
|
_serviceHosts.Stop (new byte []{}, true);
|
||||||
stopListener ();
|
stopListener (5000);
|
||||||
|
|
||||||
_state = ServerState.STOP;
|
_state = ServerState.STOP;
|
||||||
}
|
}
|
||||||
@ -687,7 +681,9 @@ namespace WebSocketSharp.Server
|
|||||||
|
|
||||||
if (msg != null)
|
if (msg != null)
|
||||||
{
|
{
|
||||||
_logger.Error (String.Format ("{0}\nstate: {1}\ncode: {2}\nreason: {3}", msg, _state, code, reason));
|
_logger.Error (String.Format (
|
||||||
|
"{0}\nstate: {1}\ncode: {2}\nreason: {3}", msg, _state, code, reason));
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -695,7 +691,7 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
_serviceHosts.Stop (data, !code.IsReserved ());
|
_serviceHosts.Stop (data, !code.IsReserved ());
|
||||||
stopListener ();
|
stopListener (5000);
|
||||||
|
|
||||||
_state = ServerState.STOP;
|
_state = ServerState.STOP;
|
||||||
}
|
}
|
||||||
@ -729,7 +725,7 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
_serviceHosts.Stop (data, !code.IsReserved ());
|
_serviceHosts.Stop (data, !code.IsReserved ());
|
||||||
stopListener ();
|
stopListener (5000);
|
||||||
|
|
||||||
_state = ServerState.STOP;
|
_state = ServerState.STOP;
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,9 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
|
using System.Security.Cryptography.X509Certificates;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
using WebSocketSharp.Net;
|
using WebSocketSharp.Net;
|
||||||
using WebSocketSharp.Net.WebSockets;
|
using WebSocketSharp.Net.WebSockets;
|
||||||
|
|
||||||
@ -50,20 +52,29 @@ namespace WebSocketSharp.Server
|
|||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// The WebSocketServer class provides the multi WebSocket service.
|
/// The WebSocketServer class provides the multi WebSocket service.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public class WebSocketServer : WebSocketServerBase
|
public class WebSocketServer
|
||||||
{
|
{
|
||||||
#region Private Fields
|
#region Private Fields
|
||||||
|
|
||||||
|
private System.Net.IPAddress _address;
|
||||||
|
private X509Certificate2 _cert;
|
||||||
|
private TcpListener _listener;
|
||||||
|
private Logger _logger;
|
||||||
|
private int _port;
|
||||||
|
private Thread _receiveRequestThread;
|
||||||
|
private bool _secure;
|
||||||
private WebSocketServiceHostManager _serviceHosts;
|
private WebSocketServiceHostManager _serviceHosts;
|
||||||
private volatile ServerState _state;
|
private volatile ServerState _state;
|
||||||
private object _sync;
|
private object _sync;
|
||||||
|
private Uri _uri;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Public Constructors
|
#region Public Constructors
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="WebSocketServer"/> class.
|
/// Initializes a new instance of the <see cref="WebSocketServer"/> class that listens for
|
||||||
|
/// incoming requests on port 80.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public WebSocketServer ()
|
public WebSocketServer ()
|
||||||
: this (80)
|
: this (80)
|
||||||
@ -78,7 +89,7 @@ namespace WebSocketSharp.Server
|
|||||||
/// An <see cref="int"/> that contains a port number.
|
/// An <see cref="int"/> that contains a port number.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <exception cref="ArgumentOutOfRangeException">
|
/// <exception cref="ArgumentOutOfRangeException">
|
||||||
/// <paramref name="port"/> is 0 or less, or 65536 or greater.
|
/// <paramref name="port"/> is not 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)
|
||||||
@ -99,14 +110,24 @@ namespace WebSocketSharp.Server
|
|||||||
/// <paramref name="url"/> is invalid.
|
/// <paramref name="url"/> is invalid.
|
||||||
/// </exception>
|
/// </exception>
|
||||||
public WebSocketServer (string url)
|
public WebSocketServer (string url)
|
||||||
: base (url)
|
|
||||||
{
|
{
|
||||||
if (BaseUri.AbsolutePath != "/")
|
if (url == null)
|
||||||
throw new ArgumentException ("Must not contain the path component: " + url, "url");
|
throw new ArgumentNullException ("url");
|
||||||
|
|
||||||
_serviceHosts = new WebSocketServiceHostManager (Log);
|
string msg;
|
||||||
_state = ServerState.READY;
|
if (!tryCreateUri (url, out _uri, out msg))
|
||||||
_sync = new object ();
|
throw new ArgumentException (msg, "url");
|
||||||
|
|
||||||
|
var host = _uri.DnsSafeHost;
|
||||||
|
_address = host.ToIPAddress ();
|
||||||
|
if (_address == null || !_address.IsLocal ())
|
||||||
|
throw new ArgumentException (String.Format (
|
||||||
|
"The host part must be the local host name: {0}", host), "url");
|
||||||
|
|
||||||
|
_port = _uri.Port;
|
||||||
|
_secure = _uri.Scheme == "wss" ? true : false;
|
||||||
|
|
||||||
|
init ();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -121,7 +142,7 @@ namespace WebSocketSharp.Server
|
|||||||
/// (<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 0 or less, or 65536 or greater.
|
/// <paramref name="port"/> is not 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.
|
||||||
@ -136,7 +157,7 @@ namespace WebSocketSharp.Server
|
|||||||
/// incoming connection attempts on the specified <paramref name="address"/> and <paramref name="port"/>.
|
/// incoming connection attempts on the specified <paramref name="address"/> and <paramref name="port"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="address">
|
/// <param name="address">
|
||||||
/// A <see cref="System.Net.IPAddress"/> that contains a local IP address.
|
/// A <see cref="System.Net.IPAddress"/> that represents the local IP address.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="port">
|
/// <param name="port">
|
||||||
/// An <see cref="int"/> that contains a port number.
|
/// An <see cref="int"/> that contains a port number.
|
||||||
@ -145,7 +166,10 @@ 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 0 or less, or 65536 or greater.
|
/// <paramref name="port"/> is not between 1 and 65535.
|
||||||
|
/// </exception>
|
||||||
|
/// <exception cref="ArgumentException">
|
||||||
|
/// <paramref name="address"/> is not the 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)
|
||||||
@ -158,7 +182,7 @@ namespace WebSocketSharp.Server
|
|||||||
/// and <paramref name="secure"/>.
|
/// and <paramref name="secure"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="address">
|
/// <param name="address">
|
||||||
/// A <see cref="System.Net.IPAddress"/> that contains a local IP address.
|
/// A <see cref="System.Net.IPAddress"/> that represents the local IP address.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="port">
|
/// <param name="port">
|
||||||
/// An <see cref="int"/> that contains a port number.
|
/// An <see cref="int"/> that contains a port number.
|
||||||
@ -171,23 +195,104 @@ 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 0 or less, or 65536 or greater.
|
/// <paramref name="port"/> is not between 1 and 65535.
|
||||||
/// </exception>
|
/// </exception>
|
||||||
/// <exception cref="ArgumentException">
|
/// <exception cref="ArgumentException">
|
||||||
/// Pair of <paramref name="port"/> and <paramref name="secure"/> is invalid.
|
/// <para>
|
||||||
|
/// <paramref name="address"/> is not the local IP address.
|
||||||
|
/// </para>
|
||||||
|
/// <para>
|
||||||
|
/// -or-
|
||||||
|
/// </para>
|
||||||
|
/// <para>
|
||||||
|
/// Pair of <paramref name="port"/> and <paramref name="secure"/> is invalid.
|
||||||
|
/// </para>
|
||||||
/// </exception>
|
/// </exception>
|
||||||
public WebSocketServer (System.Net.IPAddress address, int port, bool secure)
|
public WebSocketServer (System.Net.IPAddress address, int port, bool secure)
|
||||||
: base (address, port, "/", secure)
|
|
||||||
{
|
{
|
||||||
_serviceHosts = new WebSocketServiceHostManager (Log);
|
if (!address.IsLocal ())
|
||||||
_state = ServerState.READY;
|
throw new ArgumentException (String.Format (
|
||||||
_sync = new object ();
|
"Must be the local IP address: {0}", address), "address");
|
||||||
|
|
||||||
|
if (!port.IsPortNumber ())
|
||||||
|
throw new ArgumentOutOfRangeException ("port", "Must be between 1 and 65535: " + port);
|
||||||
|
|
||||||
|
if ((port == 80 && secure) || (port == 443 && !secure))
|
||||||
|
throw new ArgumentException (String.Format (
|
||||||
|
"Invalid pair of 'port' and 'secure': {0}, {1}", port, secure));
|
||||||
|
|
||||||
|
_address = address;
|
||||||
|
_port = port;
|
||||||
|
_secure = secure;
|
||||||
|
_uri = "/".ToUri ();
|
||||||
|
|
||||||
|
init ();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Public Properties
|
#region Public Properties
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the local IP address on which to listen for incoming connection attempts.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// A <see cref="System.Net.IPAddress"/> that represents the local IP address.
|
||||||
|
/// </value>
|
||||||
|
public System.Net.IPAddress Address {
|
||||||
|
get {
|
||||||
|
return _address;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the certificate used to authenticate the server on the secure connection.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// A <see cref="X509Certificate2"/> used to authenticate the server.
|
||||||
|
/// </value>
|
||||||
|
public X509Certificate2 Certificate {
|
||||||
|
get {
|
||||||
|
return _cert;
|
||||||
|
}
|
||||||
|
|
||||||
|
set {
|
||||||
|
if (_state == ServerState.START || _state == ServerState.SHUTDOWN)
|
||||||
|
{
|
||||||
|
_logger.Error (
|
||||||
|
"The value of Certificate property cannot be changed because the server has already been started.");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_cert = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a value indicating whether the server has been started.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// <c>true</c> if the server has been started; otherwise, <c>false</c>.
|
||||||
|
/// </value>
|
||||||
|
public bool IsListening {
|
||||||
|
get {
|
||||||
|
return _state == ServerState.START;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a value indicating whether the server provides secure connection.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// <c>true</c> if the server provides secure connection; otherwise, <c>false</c>.
|
||||||
|
/// </value>
|
||||||
|
public bool IsSecure {
|
||||||
|
get {
|
||||||
|
return _secure;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a value indicating whether the server cleans up the inactive sessions periodically.
|
/// Gets or sets a value indicating whether the server cleans up the inactive sessions periodically.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -206,7 +311,36 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the functions for the WebSocket services that the server provides.
|
/// Gets the logging functions.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// The default logging level is the <see cref="LogLevel.ERROR"/>.
|
||||||
|
/// If you want to change the current logging level, you set the <c>Log.Level</c> property
|
||||||
|
/// to one of the <see cref="LogLevel"/> values which you want.
|
||||||
|
/// </remarks>
|
||||||
|
/// <value>
|
||||||
|
/// A <see cref="Logger"/> that provides the logging functions.
|
||||||
|
/// </value>
|
||||||
|
public Logger Log {
|
||||||
|
get {
|
||||||
|
return _logger;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the port on which to listen for incoming connection attempts.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// An <see cref="int"/> that contains a port number.
|
||||||
|
/// </value>
|
||||||
|
public int Port {
|
||||||
|
get {
|
||||||
|
return _port;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the functions for the WebSocket services provided by the server.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>
|
/// <value>
|
||||||
/// A <see cref="WebSocketServiceHostManager"/> that manages the WebSocket services.
|
/// A <see cref="WebSocketServiceHostManager"/> that manages the WebSocket services.
|
||||||
@ -219,41 +353,29 @@ namespace WebSocketSharp.Server
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Protected Methods
|
#region Private Methods
|
||||||
|
|
||||||
/// <summary>
|
private void abort ()
|
||||||
/// Aborts receiving the WebSocket connection requests.
|
|
||||||
/// </summary>
|
|
||||||
/// <remarks>
|
|
||||||
/// This method is called when an exception occurs while receiving the WebSocket connection requests.
|
|
||||||
/// </remarks>
|
|
||||||
protected override void Abort ()
|
|
||||||
{
|
{
|
||||||
lock (_sync)
|
lock (_sync)
|
||||||
{
|
{
|
||||||
if (_state != ServerState.START)
|
if (!IsListening)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_state = ServerState.SHUTDOWN;
|
_state = ServerState.SHUTDOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
StopListener ();
|
_listener.Stop ();
|
||||||
_serviceHosts.Stop (
|
_serviceHosts.Stop (
|
||||||
((ushort) CloseStatusCode.SERVER_ERROR).ToByteArrayInternally (ByteOrder.BIG), true);
|
((ushort) CloseStatusCode.SERVER_ERROR).ToByteArrayInternally (ByteOrder.BIG), true);
|
||||||
|
|
||||||
_state = ServerState.STOP;
|
_state = ServerState.STOP;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
private void acceptWebSocket (TcpListenerWebSocketContext context)
|
||||||
/// Accepts a WebSocket connection request.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="context">
|
|
||||||
/// A <see cref="TcpListenerWebSocketContext"/> that contains the WebSocket connection request objects.
|
|
||||||
/// </param>
|
|
||||||
protected override void AcceptWebSocket (TcpListenerWebSocketContext context)
|
|
||||||
{
|
{
|
||||||
var websocket = context.WebSocket;
|
var websocket = context.WebSocket;
|
||||||
websocket.Log = Log;
|
websocket.Log = _logger;
|
||||||
|
|
||||||
var path = context.Path;
|
var path = context.Path;
|
||||||
WebSocketServiceHost host;
|
WebSocketServiceHost host;
|
||||||
@ -263,12 +385,95 @@ namespace WebSocketSharp.Server
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (BaseUri.IsAbsoluteUri)
|
if (_uri.IsAbsoluteUri)
|
||||||
websocket.Url = new Uri (BaseUri, path);
|
websocket.Url = new Uri (_uri, path);
|
||||||
|
|
||||||
host.StartSession (context);
|
host.StartSession (context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string checkIfCertExists ()
|
||||||
|
{
|
||||||
|
return _secure && _cert == null
|
||||||
|
? "The secure connection requires a server certificate."
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void init ()
|
||||||
|
{
|
||||||
|
_listener = new TcpListener (_address, _port);
|
||||||
|
_logger = new Logger ();
|
||||||
|
_serviceHosts = new WebSocketServiceHostManager (_logger);
|
||||||
|
_state = ServerState.READY;
|
||||||
|
_sync = new object ();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processRequestAsync (TcpClient client)
|
||||||
|
{
|
||||||
|
WaitCallback callback = state =>
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
acceptWebSocket (client.GetWebSocketContext (_secure, _cert));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.Fatal (ex.ToString ());
|
||||||
|
client.Close ();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ThreadPool.QueueUserWorkItem (callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void receiveRequest ()
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
processRequestAsync (_listener.AcceptTcpClient ());
|
||||||
|
}
|
||||||
|
catch (SocketException ex) {
|
||||||
|
_logger.Warn (String.Format ("Receiving has been stopped.\nreason: {0}.", ex.Message));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
_logger.Fatal (ex.ToString ());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsListening)
|
||||||
|
abort ();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startReceiveRequestThread ()
|
||||||
|
{
|
||||||
|
_receiveRequestThread = new Thread (new ThreadStart (receiveRequest));
|
||||||
|
_receiveRequestThread.IsBackground = true;
|
||||||
|
_receiveRequestThread.Start ();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void stopListener (int timeOut)
|
||||||
|
{
|
||||||
|
_listener.Stop ();
|
||||||
|
_receiveRequestThread.Join (timeOut);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool tryCreateUri (string uriString, out Uri result, out string message)
|
||||||
|
{
|
||||||
|
if (!uriString.TryCreateWebSocketUri (out result, out message))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (result.PathAndQuery != "/")
|
||||||
|
{
|
||||||
|
result = null;
|
||||||
|
message = "Must not contain the path or query component: " + uriString;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Public Methods
|
#region Public Methods
|
||||||
@ -325,11 +530,11 @@ namespace WebSocketSharp.Server
|
|||||||
|
|
||||||
if (msg != null)
|
if (msg != null)
|
||||||
{
|
{
|
||||||
Log.Error (String.Format ("{0}\nservice path: {1}", msg, servicePath ?? ""));
|
_logger.Error (String.Format ("{0}\nservice path: {1}", msg, servicePath ?? ""));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var host = new WebSocketServiceHost<T> (servicePath, serviceConstructor, Log);
|
var host = new WebSocketServiceHost<T> (servicePath, serviceConstructor, _logger);
|
||||||
if (!KeepClean)
|
if (!KeepClean)
|
||||||
host.KeepClean = false;
|
host.KeepClean = false;
|
||||||
|
|
||||||
@ -354,7 +559,7 @@ namespace WebSocketSharp.Server
|
|||||||
var msg = servicePath.CheckIfValidServicePath ();
|
var msg = servicePath.CheckIfValidServicePath ();
|
||||||
if (msg != null)
|
if (msg != null)
|
||||||
{
|
{
|
||||||
Log.Error (String.Format ("{0}\nservice path: {1}", msg, servicePath ?? ""));
|
_logger.Error (String.Format ("{0}\nservice path: {1}", msg, servicePath ?? ""));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -364,25 +569,20 @@ namespace WebSocketSharp.Server
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Starts to receive the WebSocket connection requests.
|
/// Starts to receive the WebSocket connection requests.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public override void Start ()
|
public void Start ()
|
||||||
{
|
{
|
||||||
lock (_sync)
|
lock (_sync)
|
||||||
{
|
{
|
||||||
var msg = _state.CheckIfStopped ();
|
var msg = _state.CheckIfStopped () ?? checkIfCertExists ();
|
||||||
if (msg != null)
|
if (msg != null)
|
||||||
{
|
{
|
||||||
Log.Error (String.Format ("{0}\nstate: {1}", msg, _state));
|
_logger.Error (String.Format ("{0}\nstate: {1}\nsecure: {2}", msg, _state, _secure));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_serviceHosts.Start ();
|
_serviceHosts.Start ();
|
||||||
|
_listener.Start ();
|
||||||
base.Start ();
|
startReceiveRequestThread ();
|
||||||
if (!IsListening)
|
|
||||||
{
|
|
||||||
_serviceHosts.Stop (new byte []{}, false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_state = ServerState.START;
|
_state = ServerState.START;
|
||||||
}
|
}
|
||||||
@ -391,21 +591,21 @@ namespace WebSocketSharp.Server
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Stops receiving the WebSocket connection requests.
|
/// Stops receiving the WebSocket connection requests.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public override void Stop ()
|
public void Stop ()
|
||||||
{
|
{
|
||||||
lock (_sync)
|
lock (_sync)
|
||||||
{
|
{
|
||||||
var msg = _state.CheckIfStarted ();
|
var msg = _state.CheckIfStarted ();
|
||||||
if (msg != null)
|
if (msg != null)
|
||||||
{
|
{
|
||||||
Log.Error (String.Format ("{0}\nstate: {1}", msg, _state));
|
_logger.Error (String.Format ("{0}\nstate: {1}", msg, _state));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_state = ServerState.SHUTDOWN;
|
_state = ServerState.SHUTDOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
base.Stop ();
|
stopListener (5000);
|
||||||
_serviceHosts.Stop (new byte []{}, true);
|
_serviceHosts.Stop (new byte []{}, true);
|
||||||
|
|
||||||
_state = ServerState.STOP;
|
_state = ServerState.STOP;
|
||||||
@ -432,14 +632,16 @@ namespace WebSocketSharp.Server
|
|||||||
|
|
||||||
if (msg != null)
|
if (msg != null)
|
||||||
{
|
{
|
||||||
Log.Error (String.Format ("{0}\nstate: {1}\ncode: {2}\nreason: {3}", msg, _state, code, reason));
|
_logger.Error (String.Format (
|
||||||
|
"{0}\nstate: {1}\ncode: {2}\nreason: {3}", msg, _state, code, reason));
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_state = ServerState.SHUTDOWN;
|
_state = ServerState.SHUTDOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
base.Stop ();
|
stopListener (5000);
|
||||||
_serviceHosts.Stop (data, !code.IsReserved ());
|
_serviceHosts.Stop (data, !code.IsReserved ());
|
||||||
|
|
||||||
_state = ServerState.STOP;
|
_state = ServerState.STOP;
|
||||||
@ -466,14 +668,14 @@ namespace WebSocketSharp.Server
|
|||||||
|
|
||||||
if (msg != null)
|
if (msg != null)
|
||||||
{
|
{
|
||||||
Log.Error (String.Format ("{0}\nstate: {1}\nreason: {2}", msg, _state, reason));
|
_logger.Error (String.Format ("{0}\nstate: {1}\nreason: {2}", msg, _state, reason));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_state = ServerState.SHUTDOWN;
|
_state = ServerState.SHUTDOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
base.Stop ();
|
stopListener (5000);
|
||||||
_serviceHosts.Stop (data, !code.IsReserved ());
|
_serviceHosts.Stop (data, !code.IsReserved ());
|
||||||
|
|
||||||
_state = ServerState.STOP;
|
_state = ServerState.STOP;
|
||||||
|
@ -1,464 +0,0 @@
|
|||||||
#region License
|
|
||||||
/*
|
|
||||||
* WebSocketServerBase.cs
|
|
||||||
*
|
|
||||||
* The MIT License
|
|
||||||
*
|
|
||||||
* Copyright (c) 2012-2013 sta.blockhead
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Net;
|
|
||||||
using System.Net.Sockets;
|
|
||||||
using System.Security.Cryptography.X509Certificates;
|
|
||||||
using System.Threading;
|
|
||||||
using WebSocketSharp.Net.WebSockets;
|
|
||||||
|
|
||||||
namespace WebSocketSharp.Server
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Provides the basic functions of the server that receives the WebSocket connection requests.
|
|
||||||
/// </summary>
|
|
||||||
/// <remarks>
|
|
||||||
/// The WebSocketServerBase class is an abstract class.
|
|
||||||
/// </remarks>
|
|
||||||
public abstract class WebSocketServerBase
|
|
||||||
{
|
|
||||||
#region Private Fields
|
|
||||||
|
|
||||||
private IPAddress _address;
|
|
||||||
private X509Certificate2 _cert;
|
|
||||||
private volatile bool _listening;
|
|
||||||
private Logger _logger;
|
|
||||||
private int _port;
|
|
||||||
private Thread _receiveRequestThread;
|
|
||||||
private bool _secure;
|
|
||||||
private bool _selfHost;
|
|
||||||
private TcpListener _listener;
|
|
||||||
private Uri _uri;
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Protected Constructors
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="WebSocketServerBase"/> class.
|
|
||||||
/// </summary>
|
|
||||||
/// <remarks>
|
|
||||||
/// This constructor initializes a new instance of this class as non self hosted server.
|
|
||||||
/// </remarks>
|
|
||||||
protected WebSocketServerBase ()
|
|
||||||
: this (new Logger ())
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="WebSocketServerBase"/> class
|
|
||||||
/// with the specified <paramref name="logger"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <remarks>
|
|
||||||
/// This constructor initializes a new instance of this class as non self hosted server.
|
|
||||||
/// </remarks>
|
|
||||||
/// <param name="logger">
|
|
||||||
/// A <see cref="Logger"/> that provides the logging functions.
|
|
||||||
/// </param>
|
|
||||||
protected WebSocketServerBase (Logger logger)
|
|
||||||
{
|
|
||||||
_logger = logger;
|
|
||||||
_selfHost = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="WebSocketServerBase"/> class
|
|
||||||
/// that listens for incoming connection attempts on the specified WebSocket URL.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="url">
|
|
||||||
/// A <see cref="string"/> that contains a WebSocket URL.
|
|
||||||
/// </param>
|
|
||||||
/// <exception cref="ArgumentNullException">
|
|
||||||
/// <paramref name="url"/> is <see langword="null"/>.
|
|
||||||
/// </exception>
|
|
||||||
/// <exception cref="ArgumentException">
|
|
||||||
/// <paramref name="url"/> is invalid.
|
|
||||||
/// </exception>
|
|
||||||
protected WebSocketServerBase (string url)
|
|
||||||
{
|
|
||||||
if (url == null)
|
|
||||||
throw new ArgumentNullException ("url");
|
|
||||||
|
|
||||||
Uri uri;
|
|
||||||
string msg;
|
|
||||||
if (!tryCreateUri (url, out uri, out msg))
|
|
||||||
throw new ArgumentException (msg, "url");
|
|
||||||
|
|
||||||
init (uri);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="WebSocketServerBase"/> class
|
|
||||||
/// that listens for incoming connection attempts on the specified <paramref name="address"/>,
|
|
||||||
/// <paramref name="port"/>, <paramref name="servicePath"/> and <paramref name="secure"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="address">
|
|
||||||
/// A <see cref="IPAddress"/> that contains a local IP address.
|
|
||||||
/// </param>
|
|
||||||
/// <param name="port">
|
|
||||||
/// An <see cref="int"/> that contains a port number.
|
|
||||||
/// </param>
|
|
||||||
/// <param name="servicePath">
|
|
||||||
/// A <see cref="string"/> that contains an absolute path.
|
|
||||||
/// </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="ArgumentNullException">
|
|
||||||
/// Either <paramref name="address"/> or <paramref name="servicePath"/> is <see langword="null"/>.
|
|
||||||
/// </exception>
|
|
||||||
/// <exception cref="ArgumentOutOfRangeException">
|
|
||||||
/// <paramref name="port"/> is 0 or less, or 65536 or greater.
|
|
||||||
/// </exception>
|
|
||||||
/// <exception cref="ArgumentException">
|
|
||||||
/// <para>
|
|
||||||
/// <paramref name="servicePath"/> is invalid.
|
|
||||||
/// </para>
|
|
||||||
/// <para>
|
|
||||||
/// -or-
|
|
||||||
/// </para>
|
|
||||||
/// <para>
|
|
||||||
/// Pair of <paramref name="port"/> and <paramref name="secure"/> is invalid.
|
|
||||||
/// </para>
|
|
||||||
/// </exception>
|
|
||||||
protected WebSocketServerBase (IPAddress address, int port, string servicePath, bool secure)
|
|
||||||
{
|
|
||||||
if (address == null)
|
|
||||||
throw new ArgumentNullException ("address");
|
|
||||||
|
|
||||||
if (servicePath == null)
|
|
||||||
throw new ArgumentNullException ("servicePath");
|
|
||||||
|
|
||||||
if (!port.IsPortNumber ())
|
|
||||||
throw new ArgumentOutOfRangeException ("port", "Invalid port number: " + port);
|
|
||||||
|
|
||||||
var msg = servicePath.CheckIfValidServicePath ();
|
|
||||||
if (msg != null)
|
|
||||||
throw new ArgumentException (msg, "servicePath");
|
|
||||||
|
|
||||||
if ((port == 80 && secure) || (port == 443 && !secure))
|
|
||||||
throw new ArgumentException (String.Format (
|
|
||||||
"Invalid pair of 'port' and 'secure': {0}, {1}", port, secure));
|
|
||||||
|
|
||||||
_address = address;
|
|
||||||
_port = port;
|
|
||||||
_uri = servicePath.ToUri ();
|
|
||||||
_secure = secure;
|
|
||||||
|
|
||||||
init ();
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Protected Properties
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the WebSocket URL on which to listen for incoming connection attempts.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>
|
|
||||||
/// A <see cref="Uri"/> that contains a WebSocket URL.
|
|
||||||
/// </value>
|
|
||||||
protected Uri BaseUri {
|
|
||||||
get {
|
|
||||||
return _uri;
|
|
||||||
}
|
|
||||||
|
|
||||||
set {
|
|
||||||
_uri = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Public Properties
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the local IP address on which to listen for incoming connection attempts.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>
|
|
||||||
/// A <see cref="IPAddress"/> that contains a local IP address.
|
|
||||||
/// </value>
|
|
||||||
public IPAddress Address {
|
|
||||||
get {
|
|
||||||
return _address;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the certificate used to authenticate the server on the secure connection.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>
|
|
||||||
/// A <see cref="X509Certificate2"/> used to authenticate the server.
|
|
||||||
/// </value>
|
|
||||||
public X509Certificate2 Certificate {
|
|
||||||
get {
|
|
||||||
return _cert;
|
|
||||||
}
|
|
||||||
|
|
||||||
set {
|
|
||||||
if (_listening)
|
|
||||||
return;
|
|
||||||
|
|
||||||
_cert = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a value indicating whether the server has been started.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>
|
|
||||||
/// <c>true</c> if the server has been started; otherwise, <c>false</c>.
|
|
||||||
/// </value>
|
|
||||||
public bool IsListening {
|
|
||||||
get {
|
|
||||||
return _listening;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a value indicating whether the server provides secure connection.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>
|
|
||||||
/// <c>true</c> if the server provides secure connection; otherwise, <c>false</c>.
|
|
||||||
/// </value>
|
|
||||||
public bool IsSecure {
|
|
||||||
get {
|
|
||||||
return _secure;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a value indicating whether the server is self host.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>
|
|
||||||
/// <c>true</c> if the server is self host; otherwise, <c>false</c>.
|
|
||||||
/// </value>
|
|
||||||
public bool IsSelfHost {
|
|
||||||
get {
|
|
||||||
return _selfHost;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the logging functions.
|
|
||||||
/// </summary>
|
|
||||||
/// <remarks>
|
|
||||||
/// The default logging level is the <see cref="LogLevel.ERROR"/>.
|
|
||||||
/// If you want to change the current logging level, you set the <c>Log.Level</c> property
|
|
||||||
/// to one of the <see cref="LogLevel"/> values which you want.
|
|
||||||
/// </remarks>
|
|
||||||
/// <value>
|
|
||||||
/// A <see cref="Logger"/> that provides the logging functions.
|
|
||||||
/// </value>
|
|
||||||
public Logger Log {
|
|
||||||
get {
|
|
||||||
return _logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal set {
|
|
||||||
if (value == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
_logger = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the port on which to listen for incoming connection attempts.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>
|
|
||||||
/// An <see cref="int"/> that contains a port number.
|
|
||||||
/// </value>
|
|
||||||
public int Port {
|
|
||||||
get {
|
|
||||||
return _port;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Private Methods
|
|
||||||
|
|
||||||
private void init ()
|
|
||||||
{
|
|
||||||
_listening = false;
|
|
||||||
_logger = new Logger ();
|
|
||||||
_selfHost = true;
|
|
||||||
_listener = new TcpListener (_address, _port);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void init (Uri uri)
|
|
||||||
{
|
|
||||||
var scheme = uri.Scheme;
|
|
||||||
var host = uri.DnsSafeHost;
|
|
||||||
var port = uri.Port;
|
|
||||||
var addrs = Dns.GetHostAddresses (host);
|
|
||||||
|
|
||||||
_uri = uri;
|
|
||||||
_address = addrs [0];
|
|
||||||
_port = port;
|
|
||||||
_secure = scheme == "wss" ? true : false;
|
|
||||||
|
|
||||||
init ();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void processRequestAsync (TcpClient client)
|
|
||||||
{
|
|
||||||
WaitCallback callback = state =>
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
AcceptWebSocket (client.GetWebSocketContext (_secure, _cert));
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.Fatal (ex.ToString ());
|
|
||||||
client.Close ();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
ThreadPool.QueueUserWorkItem (callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void receiveRequest ()
|
|
||||||
{
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
processRequestAsync (_listener.AcceptTcpClient ());
|
|
||||||
}
|
|
||||||
catch (SocketException ex) {
|
|
||||||
_logger.Warn (String.Format ("Receiving has been stopped.\nreason: {0}.", ex.Message));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
catch (Exception ex) {
|
|
||||||
_logger.Fatal (ex.ToString ());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_listening)
|
|
||||||
Abort ();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void startReceiveRequestThread ()
|
|
||||||
{
|
|
||||||
_receiveRequestThread = new Thread (new ThreadStart (receiveRequest));
|
|
||||||
_receiveRequestThread.IsBackground = true;
|
|
||||||
_receiveRequestThread.Start ();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool tryCreateUri (string uriString, out Uri result, out string message)
|
|
||||||
{
|
|
||||||
if (!uriString.TryCreateWebSocketUri (out result, out message))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (!result.Query.IsNullOrEmpty ())
|
|
||||||
{
|
|
||||||
result = null;
|
|
||||||
message = "Must not contain the query component: " + uriString;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Protected Methods
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Aborts receiving the WebSocket connection requests.
|
|
||||||
/// </summary>
|
|
||||||
/// <remarks>
|
|
||||||
/// This method is called when an exception occurs while receiving the WebSocket connection requests.
|
|
||||||
/// </remarks>
|
|
||||||
protected abstract void Abort ();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Accepts a WebSocket connection request.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="context">
|
|
||||||
/// A <see cref="TcpListenerWebSocketContext"/> that contains the WebSocket connection request objects.
|
|
||||||
/// </param>
|
|
||||||
protected abstract void AcceptWebSocket (TcpListenerWebSocketContext context);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Stops the inner <see cref="TcpListener"/> used to receive the WebSocket connection requests.
|
|
||||||
/// </summary>
|
|
||||||
protected void StopListener ()
|
|
||||||
{
|
|
||||||
if (!_listening)
|
|
||||||
return;
|
|
||||||
|
|
||||||
_listening = false;
|
|
||||||
_listener.Stop ();
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Public Methods
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Starts to receive the WebSocket connection requests.
|
|
||||||
/// </summary>
|
|
||||||
public virtual void Start ()
|
|
||||||
{
|
|
||||||
if (!_selfHost || _listening)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (_secure && _cert == null)
|
|
||||||
{
|
|
||||||
_logger.Error ("The secure connection requires a server certificate.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_listener.Start ();
|
|
||||||
startReceiveRequestThread ();
|
|
||||||
_listening = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Stops receiving the WebSocket connection requests.
|
|
||||||
/// </summary>
|
|
||||||
public virtual void Stop ()
|
|
||||||
{
|
|
||||||
if (!_selfHost || !_listening)
|
|
||||||
return;
|
|
||||||
|
|
||||||
_listening = false;
|
|
||||||
_listener.Stop ();
|
|
||||||
_receiveRequestThread.Join (5 * 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
|
||||||
}
|
|
@ -109,7 +109,6 @@ namespace WebSocketSharp
|
|||||||
_extensions = String.Empty;
|
_extensions = String.Empty;
|
||||||
_forClose = new object ();
|
_forClose = new object ();
|
||||||
_forSend = new object ();
|
_forSend = new object ();
|
||||||
_logger = new Logger ();
|
|
||||||
_origin = String.Empty;
|
_origin = String.Empty;
|
||||||
_preAuth = false;
|
_preAuth = false;
|
||||||
_protocol = String.Empty;
|
_protocol = String.Empty;
|
||||||
@ -154,7 +153,7 @@ namespace WebSocketSharp
|
|||||||
/// <paramref name="url"/> is <see langword="null"/>.
|
/// <paramref name="url"/> is <see langword="null"/>.
|
||||||
/// </exception>
|
/// </exception>
|
||||||
/// <exception cref="ArgumentException">
|
/// <exception cref="ArgumentException">
|
||||||
/// <paramref name="url"/> is not valid WebSocket URL.
|
/// <paramref name="url"/> is invalid.
|
||||||
/// </exception>
|
/// </exception>
|
||||||
public WebSocket (string url, params string[] protocols)
|
public WebSocket (string url, params string[] protocols)
|
||||||
: this ()
|
: this ()
|
||||||
@ -162,16 +161,15 @@ namespace WebSocketSharp
|
|||||||
if (url == null)
|
if (url == null)
|
||||||
throw new ArgumentNullException ("url");
|
throw new ArgumentNullException ("url");
|
||||||
|
|
||||||
Uri uri;
|
|
||||||
string msg;
|
string msg;
|
||||||
if (!url.TryCreateWebSocketUri (out uri, out msg))
|
if (!url.TryCreateWebSocketUri (out _uri, out msg))
|
||||||
throw new ArgumentException (msg, "url");
|
throw new ArgumentException (msg, "url");
|
||||||
|
|
||||||
_uri = uri;
|
|
||||||
_protocols = protocols.ToString (", ");
|
_protocols = protocols.ToString (", ");
|
||||||
|
_secure = _uri.Scheme == "wss" ? true : false;
|
||||||
_client = true;
|
_client = true;
|
||||||
_secure = uri.Scheme == "wss" ? true : false;
|
|
||||||
_base64key = createBase64Key ();
|
_base64key = createBase64Key ();
|
||||||
|
_logger = new Logger ();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -97,7 +97,6 @@
|
|||||||
<Compile Include="Server\HttpServer.cs" />
|
<Compile Include="Server\HttpServer.cs" />
|
||||||
<Compile Include="Net\HttpVersion.cs" />
|
<Compile Include="Net\HttpVersion.cs" />
|
||||||
<Compile Include="Net\HttpStatusCode.cs" />
|
<Compile Include="Net\HttpStatusCode.cs" />
|
||||||
<Compile Include="Server\WebSocketServerBase.cs" />
|
|
||||||
<Compile Include="Net\Security\SslStream.cs" />
|
<Compile Include="Net\Security\SslStream.cs" />
|
||||||
<Compile Include="Server\WebSocketServiceHost.cs" />
|
<Compile Include="Server\WebSocketServiceHost.cs" />
|
||||||
<Compile Include="CloseStatusCode.cs" />
|
<Compile Include="CloseStatusCode.cs" />
|
||||||
|
Loading…
Reference in New Issue
Block a user