diff --git a/Example/bin/Debug_Ubuntu/example.exe b/Example/bin/Debug_Ubuntu/example.exe index c9304141..a4b861b5 100755 Binary files a/Example/bin/Debug_Ubuntu/example.exe and b/Example/bin/Debug_Ubuntu/example.exe differ diff --git a/Example/bin/Debug_Ubuntu/example.exe.mdb b/Example/bin/Debug_Ubuntu/example.exe.mdb index 2c3e0f2a..e4828cce 100644 Binary files a/Example/bin/Debug_Ubuntu/example.exe.mdb and b/Example/bin/Debug_Ubuntu/example.exe.mdb differ diff --git a/Example/bin/Debug_Ubuntu/websocket-sharp.dll b/Example/bin/Debug_Ubuntu/websocket-sharp.dll index 1b134f08..3c55813a 100755 Binary files a/Example/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index 5bef4736..368e8638 100644 Binary files a/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/Example1/bin/Debug_Ubuntu/example1.exe b/Example1/bin/Debug_Ubuntu/example1.exe index 63a1ba7d..d97c6971 100755 Binary files a/Example1/bin/Debug_Ubuntu/example1.exe and b/Example1/bin/Debug_Ubuntu/example1.exe differ diff --git a/Example1/bin/Debug_Ubuntu/example1.exe.mdb b/Example1/bin/Debug_Ubuntu/example1.exe.mdb index 26ddec8c..b13ec8fe 100644 Binary files a/Example1/bin/Debug_Ubuntu/example1.exe.mdb and b/Example1/bin/Debug_Ubuntu/example1.exe.mdb differ diff --git a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll index 1b134f08..3c55813a 100755 Binary files a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index 5bef4736..368e8638 100644 Binary files a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/Example2/Example2.pidb b/Example2/Example2.pidb index 46bf5e0b..4358674e 100644 Binary files a/Example2/Example2.pidb and b/Example2/Example2.pidb differ diff --git a/Example2/Program.cs b/Example2/Program.cs index 388a1431..9d8a50d7 100644 --- a/Example2/Program.cs +++ b/Example2/Program.cs @@ -14,7 +14,7 @@ namespace Example2 wssv.Start(); Console.WriteLine( "WebSocket Server (url: {0})\n listening on address: {1} port: {2}\n", - wssv.Url, wssv.Address, wssv.Port); + wssv.Uri, wssv.Address, wssv.Port); Console.WriteLine("Press any key to stop server..."); Console.ReadLine(); diff --git a/Example2/bin/Debug_Ubuntu/example2.exe b/Example2/bin/Debug_Ubuntu/example2.exe index a8cc68b4..be3bcb8e 100755 Binary files a/Example2/bin/Debug_Ubuntu/example2.exe and b/Example2/bin/Debug_Ubuntu/example2.exe differ diff --git a/Example2/bin/Debug_Ubuntu/example2.exe.mdb b/Example2/bin/Debug_Ubuntu/example2.exe.mdb index fa230fd8..29dca71e 100644 Binary files a/Example2/bin/Debug_Ubuntu/example2.exe.mdb and b/Example2/bin/Debug_Ubuntu/example2.exe.mdb differ diff --git a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll index 1b134f08..3c55813a 100755 Binary files a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index 5bef4736..368e8638 100644 Binary files a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/Example3/bin/Debug_Ubuntu/Example3.exe b/Example3/bin/Debug_Ubuntu/Example3.exe index 7513327c..7f6680e3 100755 Binary files a/Example3/bin/Debug_Ubuntu/Example3.exe and b/Example3/bin/Debug_Ubuntu/Example3.exe differ diff --git a/Example3/bin/Debug_Ubuntu/Example3.exe.mdb b/Example3/bin/Debug_Ubuntu/Example3.exe.mdb index 56877941..c3398270 100644 Binary files a/Example3/bin/Debug_Ubuntu/Example3.exe.mdb and b/Example3/bin/Debug_Ubuntu/Example3.exe.mdb differ diff --git a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll index 1b134f08..3c55813a 100755 Binary files a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index 5bef4736..368e8638 100644 Binary files a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/websocket-sharp/Server/WebSocketServer.cs b/websocket-sharp/Server/WebSocketServer.cs index a3b9e7b8..879d3037 100644 --- a/websocket-sharp/Server/WebSocketServer.cs +++ b/websocket-sharp/Server/WebSocketServer.cs @@ -45,7 +45,9 @@ namespace WebSocketSharp.Server { #region Fields private Thread _acceptClientThread; + private IPAddress _address; private bool _isSelfHost; + private int _port; private Dictionary _services; private TcpListener _tcpListener; private Uri _uri; @@ -65,33 +67,15 @@ namespace WebSocketSharp.Server { #region Public Constructors public WebSocketServer(string url) + : this() { - _uri = new Uri(url); - if (!isValidScheme(_uri)) - { - var msg = "Unsupported WebSocket URI scheme: " + _uri.Scheme; - throw new ArgumentException(msg); - } + var uri = new Uri(url); - var host = _uri.DnsSafeHost; - var ips = Dns.GetHostAddresses(host); - if (ips.Length == 0) - { - var msg = "Invalid WebSocket URI host: " + host; - throw new ArgumentException(msg); - } - - var scheme = _uri.Scheme; - var port = _uri.Port; - if (port <= 0) - { - port = 80; - if (scheme == "wss") - port = 443; - } + string msg; + if (!isValidUri(uri, out msg)) + throw new ArgumentException(msg, "url"); - _tcpListener = new TcpListener(ips[0], port); - _services = new Dictionary(); + _tcpListener = new TcpListener(_address, _port); _isSelfHost = true; } @@ -100,15 +84,21 @@ namespace WebSocketSharp.Server { { } - public WebSocketServer(int port, string absPath) + public WebSocketServer(int port, string path) + : this() { - _uri = new Uri(absPath, UriKind.Relative); + var uri = path.ToUri(); + if (uri.IsAbsoluteUri) + { + var msg = "Not absolute path: " + path; + throw new ArgumentException(msg, "path"); + } - if (port <= 0) - port = 80; + _uri = uri; + _address = IPAddress.Any; + _port = port <= 0 ? 80 : port; - _tcpListener = new TcpListener(IPAddress.Any, port); - _services = new Dictionary(); + _tcpListener = new TcpListener(_address, _port); _isSelfHost = true; } @@ -118,12 +108,7 @@ namespace WebSocketSharp.Server { public IPAddress Address { - get { return Endpoint.Address; } - } - - public IPEndPoint Endpoint - { - get { return (IPEndPoint)_tcpListener.LocalEndpoint; } + get { return _address; } } public bool IsSelfHost { @@ -132,12 +117,12 @@ namespace WebSocketSharp.Server { public int Port { - get { return Endpoint.Port; } + get { return _port; } } - public string Url + public Uri Uri { - get { return _uri.ToString(); } + get { return _uri; } } #endregion @@ -181,13 +166,42 @@ namespace WebSocketSharp.Server { OnError.Emit(this, new ErrorEventArgs(message)); } - private bool isValidScheme(Uri uri) + private bool isValidUri(Uri uri, out string message) { var scheme = uri.Scheme; - if (scheme == "ws" || scheme == "wss") - return true; + var port = uri.Port; + var host = uri.DnsSafeHost; + var ips = Dns.GetHostAddresses(host); + + if (scheme != "ws" && scheme != "wss") + { + message = "Unsupported WebSocket URI scheme: " + scheme; + return false; + } + + if ((scheme == "wss" && port != 443) || + (scheme != "wss" && port == 443)) + { + message = String.Format( + "Invalid pair of WebSocket URI scheme and port: {0}, {1}", scheme, port); + return false; + } + + if (ips.Length == 0) + { + message = "Invalid WebSocket URI host: " + host; + return false; + } + + if (port <= 0) + port = scheme == "ws" ? 80 : 443; + + _uri = uri; + _address = ips[0]; + _port = port; - return false; + message = String.Empty; + return true; } private void startAcceptClientThread() @@ -199,7 +213,7 @@ namespace WebSocketSharp.Server { private void startService(TcpClient client) { - WaitCallback startSv = (state) => + WaitCallback startServiceCb = (state) => { try { var socket = new WebSocket(_uri, client); @@ -210,7 +224,7 @@ namespace WebSocketSharp.Server { error(ex.Message); } }; - ThreadPool.QueueUserWorkItem(startSv); + ThreadPool.QueueUserWorkItem(startServiceCb); } #endregion diff --git a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll index 1b134f08..3c55813a 100755 Binary files a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll and b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index 5bef4736..368e8638 100644 Binary files a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/websocket-sharp/websocket-sharp.pidb b/websocket-sharp/websocket-sharp.pidb index 73dffb7b..52eec10f 100644 Binary files a/websocket-sharp/websocket-sharp.pidb and b/websocket-sharp/websocket-sharp.pidb differ