Modified some classes in WebSocketSharp.Server namespace

This commit is contained in:
sta 2013-07-06 00:27:35 +09:00
parent b9c5c222ca
commit 5386431a2e
4 changed files with 113 additions and 96 deletions

View File

@ -62,12 +62,13 @@ namespace WebSocketSharp.Server {
#region Private Fields
private bool _isWindows;
private HttpListener _listener;
private bool _listening;
private int _port;
private Thread _receiveRequestThread;
private string _rootPath;
private ServiceHostManager _svcHosts;
private bool _windows;
#endregion
@ -99,6 +100,18 @@ namespace WebSocketSharp.Server {
#region Public Properties
/// <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 the port on which to listen for incoming requests.
/// </summary>
@ -125,7 +138,8 @@ namespace WebSocketSharp.Server {
}
set {
_rootPath = value;
if (!_listening)
_rootPath = value;
}
}
@ -220,18 +234,18 @@ namespace WebSocketSharp.Server {
private void init()
{
_listener = new HttpListener();
_listening = false;
_rootPath = getRootPath();
_svcHosts = new ServiceHostManager();
_isWindows = false;
_windows = false;
var os = Environment.OSVersion;
if (os.Platform != PlatformID.Unix && os.Platform != PlatformID.MacOSX)
_isWindows = true;
_windows = true;
var prefix = String.Format(
"http{0}://*:{1}/", _port == 443 ? "s" : String.Empty, _port);
_listener.Prefixes.Add(prefix);
_rootPath = getRootPath();
}
private static string getRootPath()
@ -252,7 +266,7 @@ namespace WebSocketSharp.Server {
{
#if DEBUG
var callerFrame = new StackFrame(1);
var caller = callerFrame.GetMethod();
var caller = callerFrame.GetMethod();
Console.WriteLine("HTTPSV: Error@{0}: {1}", caller.Name, message);
#endif
OnError.Emit(this, new ErrorEventArgs(message));
@ -382,9 +396,9 @@ namespace WebSocketSharp.Server {
private bool upgradeToWebSocket(HttpListenerContext context)
{
var res = context.Response;
var res = context.Response;
var wsContext = context.AcceptWebSocket();
var path = wsContext.Path.UrlDecode();
var path = wsContext.Path.UrlDecode();
IServiceHost svcHost;
if (!_svcHosts.TryGetServiceHost(path, out svcHost))
@ -441,7 +455,7 @@ namespace WebSocketSharp.Server {
public byte[] GetFile(string path)
{
var filePath = _rootPath + path;
if (_isWindows)
if (_windows)
filePath = filePath.Replace("/", "\\");
return File.Exists(filePath)
@ -450,22 +464,30 @@ namespace WebSocketSharp.Server {
}
/// <summary>
/// Starts the <see cref="HttpServer"/>.
/// Starts to receive the HTTP requests.
/// </summary>
public void Start()
{
if (_listening)
return;
_listener.Start();
startReceiveRequestThread();
_listening = true;
}
/// <summary>
/// Shuts down the <see cref="HttpServer"/>.
/// Stops receiving the HTTP requests.
/// </summary>
public void Stop()
{
if (!_listening)
return;
_listener.Close();
_receiveRequestThread.Join(5 * 1000);
_svcHosts.Stop();
_listening = false;
}
#endregion

View File

@ -88,7 +88,7 @@ namespace WebSocketSharp.Server {
throw new ArgumentException(msg, "url");
}
init();
_svcHosts = new ServiceHostManager();
}
/// <summary>
@ -137,7 +137,7 @@ namespace WebSocketSharp.Server {
public WebSocketServer(System.Net.IPAddress address, int port, bool secure)
: base(address, port, "/", secure)
{
init();
_svcHosts = new ServiceHostManager();
}
#endregion
@ -155,6 +155,7 @@ namespace WebSocketSharp.Server {
var url = BaseUri.IsAbsoluteUri
? BaseUri.ToString().TrimEnd('/')
: String.Empty;
foreach (var path in _svcHosts.Paths)
yield return url + path;
}
@ -180,15 +181,6 @@ namespace WebSocketSharp.Server {
#endregion
#region Private Methods
private void init()
{
_svcHosts = new ServiceHostManager();
}
#endregion
#region Protected Methods
/// <summary>
@ -199,18 +191,18 @@ namespace WebSocketSharp.Server {
/// </param>
protected override void AcceptWebSocket(TcpListenerWebSocketContext context)
{
var websocket = context.WebSocket;
var path = context.Path.UrlDecode();
var ws = context.WebSocket;
var path = context.Path.UrlDecode();
IServiceHost svcHost;
if (!_svcHosts.TryGetServiceHost(path, out svcHost))
{
websocket.Close(HttpStatusCode.NotImplemented);
ws.Close(HttpStatusCode.NotImplemented);
return;
}
if (BaseUri.IsAbsoluteUri)
websocket.Url = new Uri(BaseUri, path);
ws.Url = new Uri(BaseUri, path);
svcHost.BindWebSocket(context);
}
@ -242,6 +234,7 @@ namespace WebSocketSharp.Server {
svcHost.Uri = BaseUri.IsAbsoluteUri
? new Uri(BaseUri, absPath)
: absPath.ToUri();
if (!Sweeping)
svcHost.Sweeping = false;

View File

@ -45,11 +45,12 @@ namespace WebSocketSharp.Server {
#region Private Fields
private Thread _receiveRequestThread;
private IPAddress _address;
private bool _isSecure;
private bool _isSelfHost;
private bool _listening;
private int _port;
private Thread _receiveRequestThread;
private bool _secure;
private bool _selfHost;
private TcpListener _tcpListener;
private Uri _uri;
@ -62,7 +63,7 @@ namespace WebSocketSharp.Server {
/// </summary>
protected WebSocketServerBase()
{
_isSelfHost = false;
_selfHost = false;
}
/// <summary>
@ -133,7 +134,7 @@ namespace WebSocketSharp.Server {
if (!absPath.IsValidAbsolutePath(out msg))
throw new ArgumentException(msg, "absPath");
if ((port == 80 && secure) ||
if ((port == 80 && secure) ||
(port == 443 && !secure))
{
msg = String.Format(
@ -141,12 +142,10 @@ namespace WebSocketSharp.Server {
throw new ArgumentException(msg);
}
_address = address;
_port = port > 0
? port
: secure ? 443 : 80;
_uri = absPath.ToUri();
_isSecure = secure;
_address = address;
_port = port > 0 ? port : secure ? 443 : 80;
_uri = absPath.ToUri();
_secure = secure;
init();
}
@ -161,8 +160,7 @@ namespace WebSocketSharp.Server {
/// <value>
/// A <see cref="Uri"/> that contains a WebSocket URL.
/// </value>
protected Uri BaseUri
{
protected Uri BaseUri {
get {
return _uri;
}
@ -188,6 +186,18 @@ namespace WebSocketSharp.Server {
}
}
/// <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>
@ -196,7 +206,7 @@ namespace WebSocketSharp.Server {
/// </value>
public bool IsSecure {
get {
return _isSecure;
return _secure;
}
}
@ -208,7 +218,7 @@ namespace WebSocketSharp.Server {
/// </value>
public bool IsSelfHost {
get {
return _isSelfHost;
return _selfHost;
}
}
@ -237,7 +247,38 @@ namespace WebSocketSharp.Server {
#region Private Methods
private void acceptWebSocketAsync(TcpListenerWebSocketContext context)
private void init()
{
_listening = false;
_selfHost = true;
_tcpListener = 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];
_secure = scheme == "wss" ? true : false;
_port = port > 0 ? port : _secure ? 443 : 80;
init();
}
private void onError(string message)
{
#if DEBUG
var callerFrame = new StackFrame(1);
var caller = callerFrame.GetMethod();
Console.WriteLine("WSSV: Error@{0}: {1}", caller.Name, message);
#endif
OnError.Emit(this, new ErrorEventArgs(message));
}
private void processRequestAsync(TcpListenerWebSocketContext context)
{
WaitCallback callback = (state) =>
{
@ -254,47 +295,14 @@ namespace WebSocketSharp.Server {
ThreadPool.QueueUserWorkItem(callback);
}
private void init()
{
_tcpListener = new TcpListener(_address, _port);
_isSelfHost = true;
}
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];
_isSecure = scheme == "wss" ? true : false;
_port = port > 0
? port
: _isSecure ? 443 : 80;
init();
}
private void onError(string message)
{
#if DEBUG
var callerFrame = new StackFrame(1);
var caller = callerFrame.GetMethod();
Console.WriteLine("WSSV: Error@{0}: {1}", caller.Name, message);
#endif
OnError.Emit(this, new ErrorEventArgs(message));
}
private void receiveRequest()
{
while (true)
{
try
{
var context = _tcpListener.AcceptWebSocket(_isSecure);
acceptWebSocketAsync(context);
var context = _tcpListener.AcceptWebSocket(_secure);
processRequestAsync(context);
}
catch (SocketException)
{
@ -323,8 +331,9 @@ namespace WebSocketSharp.Server {
if (!result.Query.IsNullOrEmpty())
{
result = null;
result = null;
message = "Must not contain the query component: " + uriString;
return false;
}
@ -363,11 +372,12 @@ namespace WebSocketSharp.Server {
/// </summary>
public virtual void Start()
{
if (!_isSelfHost)
if (!_selfHost || _listening)
return;
_tcpListener.Start();
startReceiveRequestThread();
_listening = true;
}
/// <summary>
@ -375,11 +385,12 @@ namespace WebSocketSharp.Server {
/// </summary>
public virtual void Stop()
{
if (!_isSelfHost)
if (!_selfHost || !_listening)
return;
_tcpListener.Stop();
_receiveRequestThread.Join(5 * 1000);
_listening = false;
}
#endregion

View File

@ -58,7 +58,7 @@ namespace WebSocketSharp.Server {
internal WebSocketServiceHost()
{
init();
_sessions = new WebSocketServiceManager();
}
#endregion
@ -87,7 +87,7 @@ namespace WebSocketSharp.Server {
public WebSocketServiceHost(string url)
: base(url)
{
init();
_sessions = new WebSocketServiceManager();
}
/// <summary>
@ -175,7 +175,7 @@ namespace WebSocketSharp.Server {
public WebSocketServiceHost(System.Net.IPAddress address, int port, string absPath, bool secure)
: base(address, port, absPath, secure)
{
init();
_sessions = new WebSocketServiceManager();
}
#endregion
@ -218,15 +218,6 @@ namespace WebSocketSharp.Server {
#endregion
#region Private Methods
private void init()
{
_sessions = new WebSocketServiceManager();
}
#endregion
#region Protected Methods
/// <summary>
@ -237,16 +228,16 @@ namespace WebSocketSharp.Server {
/// </param>
protected override void AcceptWebSocket(TcpListenerWebSocketContext context)
{
var websocket = context.WebSocket;
var ws = context.WebSocket;
var path = context.Path.UrlDecode();
if (path != Uri.GetAbsolutePath().UrlDecode())
{
websocket.Close(HttpStatusCode.NotImplemented);
ws.Close(HttpStatusCode.NotImplemented);
return;
}
if (Uri.IsAbsoluteUri)
websocket.Url = Uri;
ws.Url = Uri;
((IServiceHost)this).BindWebSocket(context);
}