diff --git a/Example/bin/Debug/example.exe b/Example/bin/Debug/example.exe index da3e1e6d..05f8f4e1 100755 Binary files a/Example/bin/Debug/example.exe and b/Example/bin/Debug/example.exe differ diff --git a/Example/bin/Debug/example.exe.mdb b/Example/bin/Debug/example.exe.mdb index 124bc8bb..0a4c0d51 100644 Binary files a/Example/bin/Debug/example.exe.mdb and b/Example/bin/Debug/example.exe.mdb differ diff --git a/Example/bin/Debug/websocket-sharp.dll b/Example/bin/Debug/websocket-sharp.dll index d5a82b46..19822fe9 100755 Binary files a/Example/bin/Debug/websocket-sharp.dll and b/Example/bin/Debug/websocket-sharp.dll differ diff --git a/Example/bin/Debug/websocket-sharp.dll.mdb b/Example/bin/Debug/websocket-sharp.dll.mdb index 6bad1f2f..2a23b72a 100644 Binary files a/Example/bin/Debug/websocket-sharp.dll.mdb and b/Example/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/Example/bin/Debug_Ubuntu/example.exe b/Example/bin/Debug_Ubuntu/example.exe index 4c0a0c1f..40c6d04d 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 738535dd..6c2d7a4f 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 fd81cba5..99ce4a29 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 71dd29e3..86ebda13 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/Example/bin/Release/example.exe b/Example/bin/Release/example.exe index 754ac798..3cfa1db8 100755 Binary files a/Example/bin/Release/example.exe and b/Example/bin/Release/example.exe differ diff --git a/Example/bin/Release/websocket-sharp.dll b/Example/bin/Release/websocket-sharp.dll index 2da2cf3c..fb7bb84f 100755 Binary files a/Example/bin/Release/websocket-sharp.dll and b/Example/bin/Release/websocket-sharp.dll differ diff --git a/Example/bin/Release_Ubuntu/example.exe b/Example/bin/Release_Ubuntu/example.exe index 19d7f2ad..34b4e978 100755 Binary files a/Example/bin/Release_Ubuntu/example.exe and b/Example/bin/Release_Ubuntu/example.exe differ diff --git a/Example/bin/Release_Ubuntu/websocket-sharp.dll b/Example/bin/Release_Ubuntu/websocket-sharp.dll index 387f9ac2..da3c5d44 100755 Binary files a/Example/bin/Release_Ubuntu/websocket-sharp.dll and b/Example/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/Example1/bin/Debug/example1.exe b/Example1/bin/Debug/example1.exe index fa67fb19..4b9b011a 100755 Binary files a/Example1/bin/Debug/example1.exe and b/Example1/bin/Debug/example1.exe differ diff --git a/Example1/bin/Debug/example1.exe.mdb b/Example1/bin/Debug/example1.exe.mdb index 2627c23d..a49c05bf 100644 Binary files a/Example1/bin/Debug/example1.exe.mdb and b/Example1/bin/Debug/example1.exe.mdb differ diff --git a/Example1/bin/Debug/websocket-sharp.dll b/Example1/bin/Debug/websocket-sharp.dll index d5a82b46..19822fe9 100755 Binary files a/Example1/bin/Debug/websocket-sharp.dll and b/Example1/bin/Debug/websocket-sharp.dll differ diff --git a/Example1/bin/Debug/websocket-sharp.dll.mdb b/Example1/bin/Debug/websocket-sharp.dll.mdb index 6bad1f2f..2a23b72a 100644 Binary files a/Example1/bin/Debug/websocket-sharp.dll.mdb and b/Example1/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/Example1/bin/Debug_Ubuntu/example1.exe b/Example1/bin/Debug_Ubuntu/example1.exe index 04c93060..81eddfec 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 9963f7ae..d77c8b83 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 fd81cba5..99ce4a29 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 71dd29e3..86ebda13 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/Example1/bin/Release/example1.exe b/Example1/bin/Release/example1.exe index ec9926d9..2dad60fa 100755 Binary files a/Example1/bin/Release/example1.exe and b/Example1/bin/Release/example1.exe differ diff --git a/Example1/bin/Release/websocket-sharp.dll b/Example1/bin/Release/websocket-sharp.dll index 2da2cf3c..fb7bb84f 100755 Binary files a/Example1/bin/Release/websocket-sharp.dll and b/Example1/bin/Release/websocket-sharp.dll differ diff --git a/Example1/bin/Release_Ubuntu/example1.exe b/Example1/bin/Release_Ubuntu/example1.exe index f0c2f790..b81d0289 100755 Binary files a/Example1/bin/Release_Ubuntu/example1.exe and b/Example1/bin/Release_Ubuntu/example1.exe differ diff --git a/Example1/bin/Release_Ubuntu/websocket-sharp.dll b/Example1/bin/Release_Ubuntu/websocket-sharp.dll index 387f9ac2..da3c5d44 100755 Binary files a/Example1/bin/Release_Ubuntu/websocket-sharp.dll and b/Example1/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/Example2/bin/Debug/example2.exe b/Example2/bin/Debug/example2.exe index 283f4c04..a7d35cac 100755 Binary files a/Example2/bin/Debug/example2.exe and b/Example2/bin/Debug/example2.exe differ diff --git a/Example2/bin/Debug/example2.exe.mdb b/Example2/bin/Debug/example2.exe.mdb index 083a4238..503a38e9 100644 Binary files a/Example2/bin/Debug/example2.exe.mdb and b/Example2/bin/Debug/example2.exe.mdb differ diff --git a/Example2/bin/Debug/websocket-sharp.dll b/Example2/bin/Debug/websocket-sharp.dll index d5a82b46..19822fe9 100755 Binary files a/Example2/bin/Debug/websocket-sharp.dll and b/Example2/bin/Debug/websocket-sharp.dll differ diff --git a/Example2/bin/Debug/websocket-sharp.dll.mdb b/Example2/bin/Debug/websocket-sharp.dll.mdb index 6bad1f2f..2a23b72a 100644 Binary files a/Example2/bin/Debug/websocket-sharp.dll.mdb and b/Example2/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/Example2/bin/Debug_Ubuntu/example2.exe b/Example2/bin/Debug_Ubuntu/example2.exe index b5160646..10735c2e 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 fc481c29..20057597 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 fd81cba5..99ce4a29 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 71dd29e3..86ebda13 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/Example2/bin/Release/example2.exe b/Example2/bin/Release/example2.exe index 4ea81903..f96b838e 100755 Binary files a/Example2/bin/Release/example2.exe and b/Example2/bin/Release/example2.exe differ diff --git a/Example2/bin/Release/websocket-sharp.dll b/Example2/bin/Release/websocket-sharp.dll index 2da2cf3c..fb7bb84f 100755 Binary files a/Example2/bin/Release/websocket-sharp.dll and b/Example2/bin/Release/websocket-sharp.dll differ diff --git a/Example2/bin/Release_Ubuntu/example2.exe b/Example2/bin/Release_Ubuntu/example2.exe index 6d1bd8a3..f13ad917 100755 Binary files a/Example2/bin/Release_Ubuntu/example2.exe and b/Example2/bin/Release_Ubuntu/example2.exe differ diff --git a/Example2/bin/Release_Ubuntu/websocket-sharp.dll b/Example2/bin/Release_Ubuntu/websocket-sharp.dll index 387f9ac2..da3c5d44 100755 Binary files a/Example2/bin/Release_Ubuntu/websocket-sharp.dll and b/Example2/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/websocket-sharp.userprefs b/websocket-sharp.userprefs index 5c09f1eb..8a063dbf 100644 --- a/websocket-sharp.userprefs +++ b/websocket-sharp.userprefs @@ -1,12 +1,6 @@  - - - - - - - - + + diff --git a/websocket-sharp/Server/IWebSocketServer.cs b/websocket-sharp/Server/IWebSocketServer.cs index 42eb5111..8a3150a5 100644 --- a/websocket-sharp/Server/IWebSocketServer.cs +++ b/websocket-sharp/Server/IWebSocketServer.cs @@ -33,6 +33,8 @@ namespace WebSocketSharp.Server { public interface IWebSocketServer { + WsServerState State { get; } + void AddService(WebSocketService service); void CloseServices(CloseStatusCode code, string reason); void Ping(string data); diff --git a/websocket-sharp/Server/WebSocketServer.cs b/websocket-sharp/Server/WebSocketServer.cs index 5e8989e4..ae8879bf 100644 --- a/websocket-sharp/Server/WebSocketServer.cs +++ b/websocket-sharp/Server/WebSocketServer.cs @@ -45,6 +45,7 @@ namespace WebSocketSharp.Server #region Private Fields private SynchronizedCollection _services; + private WsServerState _state; private TcpListener _tcpListener; private Uri _uri; @@ -67,6 +68,11 @@ namespace WebSocketSharp.Server get { return Endpoint.Port; } } + public WsServerState State + { + get { return _state; } + } + public string Url { get { return _uri.ToString(); } @@ -107,6 +113,7 @@ namespace WebSocketSharp.Server _tcpListener = new TcpListener(IPAddress.Any, port); _services = new SynchronizedCollection(); + _state = WsServerState.READY; } #endregion @@ -248,12 +255,17 @@ namespace WebSocketSharp.Server { _tcpListener.Start(); _tcpListener.BeginAcceptTcpClient(acceptClient, _tcpListener); + _state = WsServerState.START; } public void Stop() { + _state = WsServerState.SHUTDOWN; + _tcpListener.Stop(); CloseServices(); + + _state = WsServerState.STOP; } #endregion diff --git a/websocket-sharp/Server/WebSocketService.cs b/websocket-sharp/Server/WebSocketService.cs index 2db77d1f..475e9579 100644 --- a/websocket-sharp/Server/WebSocketService.cs +++ b/websocket-sharp/Server/WebSocketService.cs @@ -42,7 +42,7 @@ namespace WebSocketSharp.Server #region Property - public bool IsBinded { get; private set; } + public bool IsBound { get; private set; } #endregion @@ -50,7 +50,7 @@ namespace WebSocketSharp.Server public WebSocketService() { - IsBinded = false; + IsBound = false; } #endregion @@ -64,10 +64,13 @@ namespace WebSocketSharp.Server _server.AddService(this); }; -// _socket.OnClose += (sender, e) => -// { -// _server.RemoveService(this); -// }; + _socket.OnClose += (sender, e) => + { + if (_server.State == WsServerState.START) + { + _server.RemoveService(this); + } + }; } #endregion @@ -105,7 +108,7 @@ namespace WebSocketSharp.Server _socket.OnError += onError; _socket.OnClose += onClose; - IsBinded = true; + IsBound = true; } public void BPing() @@ -115,52 +118,52 @@ namespace WebSocketSharp.Server public void BPing(string data) { - if (IsBinded) _server.Ping(data); + if (IsBound) _server.Ping(data); } public void Close() { - if (IsBinded) _socket.Close(); + if (IsBound) _socket.Close(); } public void Close(CloseStatusCode code, string reason) { - if (IsBinded) _socket.Close(code, reason); + if (IsBound) _socket.Close(code, reason); } public void Open() { - if (IsBinded) _socket.Connect(); + if (IsBound) _socket.Connect(); } public void Ping() { - if (IsBinded) _socket.Ping(); + if (IsBound) _socket.Ping(); } public void Ping(string data) { - if (IsBinded) _socket.Ping(data); + if (IsBound) _socket.Ping(data); } public void Publish(byte[] data) { - if (IsBinded) _server.Publish(data); + if (IsBound) _server.Publish(data); } public void Publish(string data) { - if (IsBinded) _server.Publish(data); + if (IsBound) _server.Publish(data); } public void Send(byte[] data) { - if (IsBinded) _socket.Send(data); + if (IsBound) _socket.Send(data); } public void Send(string data) { - if (IsBinded) _socket.Send(data); + if (IsBound) _socket.Send(data); } #endregion diff --git a/websocket-sharp/Server/WsServerState.cs b/websocket-sharp/Server/WsServerState.cs new file mode 100644 index 00000000..9c764d6a --- /dev/null +++ b/websocket-sharp/Server/WsServerState.cs @@ -0,0 +1,40 @@ +#region MIT License +/** + * WsServerState.cs + * + * The MIT License + * + * Copyright (c) 2012 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; + +namespace WebSocketSharp.Server +{ + public enum WsServerState + { + READY, + START, + SHUTDOWN, + STOP + } +} diff --git a/websocket-sharp/Stream/WsStream.cs b/websocket-sharp/Stream/WsStream.cs index ab7efecd..936a5cf2 100644 --- a/websocket-sharp/Stream/WsStream.cs +++ b/websocket-sharp/Stream/WsStream.cs @@ -74,12 +74,24 @@ namespace WebSocketSharp.Stream public int Read(byte[] buffer, int offset, int size) { - return _innerStream.Read(buffer, offset, size); + lock (_forRead) + { + var readLen = _innerStream.Read(buffer, offset, size); + if (readLen < size) + { + var msg = String.Format("Data can not be read from {0}.", typeof(TStream).Name); + throw new IOException(msg); + } + return readLen; + } } public int ReadByte() { - return _innerStream.ReadByte(); + lock (_forRead) + { + return _innerStream.ReadByte(); + } } public WsFrame ReadFrame() @@ -92,12 +104,18 @@ namespace WebSocketSharp.Stream public void Write(byte[] buffer, int offset, int count) { - _innerStream.Write(buffer, offset, count); + lock (_forWrite) + { + _innerStream.Write(buffer, offset, count); + } } public void WriteByte(byte value) { - _innerStream.WriteByte(value); + lock (_forWrite) + { + _innerStream.WriteByte(value); + } } public void WriteFrame(WsFrame frame) diff --git a/websocket-sharp/bin/Debug/websocket-sharp.dll b/websocket-sharp/bin/Debug/websocket-sharp.dll index d5a82b46..19822fe9 100755 Binary files a/websocket-sharp/bin/Debug/websocket-sharp.dll and b/websocket-sharp/bin/Debug/websocket-sharp.dll differ diff --git a/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb b/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb index 6bad1f2f..2a23b72a 100644 Binary files a/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb and b/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll index fd81cba5..99ce4a29 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 71dd29e3..86ebda13 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/bin/Release/websocket-sharp.dll b/websocket-sharp/bin/Release/websocket-sharp.dll index 2da2cf3c..fb7bb84f 100755 Binary files a/websocket-sharp/bin/Release/websocket-sharp.dll and b/websocket-sharp/bin/Release/websocket-sharp.dll differ diff --git a/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll b/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll index 387f9ac2..da3c5d44 100755 Binary files a/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll and b/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/websocket-sharp/websocket-sharp.csproj b/websocket-sharp/websocket-sharp.csproj index 5b2a3be6..a805191e 100644 --- a/websocket-sharp/websocket-sharp.csproj +++ b/websocket-sharp/websocket-sharp.csproj @@ -77,6 +77,7 @@ + diff --git a/websocket-sharp/websocket-sharp.pidb b/websocket-sharp/websocket-sharp.pidb index 75030839..1df069ae 100644 Binary files a/websocket-sharp/websocket-sharp.pidb and b/websocket-sharp/websocket-sharp.pidb differ