diff --git a/Example/Example.pidb b/Example/Example.pidb index 422aa8bd..9aa225a0 100644 Binary files a/Example/Example.pidb and b/Example/Example.pidb differ diff --git a/Example/bin/Debug/example.exe b/Example/bin/Debug/example.exe index ced42141..da3e1e6d 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 52ee71bf..124bc8bb 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 a083df16..d5a82b46 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 77daf56c..6bad1f2f 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 bfd397c1..4c0a0c1f 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 8c36fe00..738535dd 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 0d0c39ec..fd81cba5 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 4677f303..71dd29e3 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 48310657..754ac798 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 977618e7..2da2cf3c 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 cfdce71e..19d7f2ad 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 d9e22c98..387f9ac2 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/Example1.pidb b/Example1/Example1.pidb index 6a620199..8b9cc32a 100644 Binary files a/Example1/Example1.pidb and b/Example1/Example1.pidb differ diff --git a/Example1/bin/Debug/example1.exe b/Example1/bin/Debug/example1.exe index f937877f..fa67fb19 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 ea59fe08..2627c23d 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 a083df16..d5a82b46 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 77daf56c..6bad1f2f 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 adf248a5..04c93060 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 a315e5e7..9963f7ae 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 0d0c39ec..fd81cba5 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 4677f303..71dd29e3 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 8ea1730f..ec9926d9 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 977618e7..2da2cf3c 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 6588febd..f0c2f790 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 d9e22c98..387f9ac2 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/Example2.pidb b/Example2/Example2.pidb index 4c5aa484..3e10b523 100644 Binary files a/Example2/Example2.pidb and b/Example2/Example2.pidb differ diff --git a/Example2/bin/Debug/example2.exe b/Example2/bin/Debug/example2.exe index 1aa73a61..283f4c04 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 d19839b5..083a4238 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 a083df16..d5a82b46 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 77daf56c..6bad1f2f 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 67286e95..b5160646 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 f7a1d218..fc481c29 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 0d0c39ec..fd81cba5 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 4677f303..71dd29e3 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 dd2a120b..4ea81903 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 977618e7..2da2cf3c 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 5053459c..6d1bd8a3 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 d9e22c98..387f9ac2 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 4f01e372..5c09f1eb 100644 --- a/websocket-sharp.userprefs +++ b/websocket-sharp.userprefs @@ -1,6 +1,12 @@  - - + + + + + + + + diff --git a/websocket-sharp/Ext.cs b/websocket-sharp/Ext.cs index f3a7c9f8..3b82ca7b 100644 --- a/websocket-sharp/Ext.cs +++ b/websocket-sharp/Ext.cs @@ -112,24 +112,24 @@ namespace WebSocketSharp public static byte[] ReadBytes(this TStream stream, ulong length, int bufferLength) where TStream : System.IO.Stream { - List readData = new List(); - ulong count = length / (ulong)bufferLength; int remainder = (int)(length % (ulong)bufferLength); - byte[] buffer1 = new byte[bufferLength]; + List readData = new List(); + byte[] buffer1 = new byte[bufferLength]; + int readLen = 0; count.Times(() => { - stream.Read(buffer1, 0, bufferLength); - readData.AddRange(buffer1); + readLen = stream.Read(buffer1, 0, bufferLength); + if (readLen > 0) readData.AddRange(buffer1.SubArray(0, readLen)); }); if (remainder > 0) { byte[] buffer2 = new byte[remainder]; - stream.Read(buffer2, 0, remainder); - readData.AddRange(buffer2); + readLen = stream.Read(buffer2, 0, remainder); + if (readLen > 0) readData.AddRange(buffer2.SubArray(0, readLen)); } return readData.ToArray(); diff --git a/websocket-sharp/Frame/WsFrame.cs b/websocket-sharp/Frame/WsFrame.cs index 267802c5..ae3e37d2 100644 --- a/websocket-sharp/Frame/WsFrame.cs +++ b/websocket-sharp/Frame/WsFrame.cs @@ -179,9 +179,14 @@ namespace WebSocketSharp.Frame int buffer2Len = 0; ulong buffer3Len = 0; int maskingKeyLen = 4; + int readLen = 0; buffer1 = new byte[buffer1Len]; - stream.Read(buffer1, 0, buffer1Len); + readLen = stream.Read(buffer1, 0, buffer1Len); + if (readLen < buffer1Len) + { + return null; + } // FIN fin = (buffer1[0] & 0x80) == 0x80 @@ -224,7 +229,13 @@ namespace WebSocketSharp.Frame if (buffer2Len > 0) { buffer2 = new byte[buffer2Len]; - stream.Read(buffer2, 0, buffer2Len); + readLen = stream.Read(buffer2, 0, buffer2Len); + + if (readLen < buffer2Len) + { + return null; + } + extPayloadLen = buffer2; switch (buffer2Len) { @@ -245,17 +256,32 @@ namespace WebSocketSharp.Frame if (masked == Mask.MASK) { maskingKey = new byte[maskingKeyLen]; - stream.Read(maskingKey, 0, maskingKeyLen); + readLen = stream.Read(maskingKey, 0, maskingKeyLen); + + if (readLen < maskingKeyLen) + { + return null; + } } // Payload Data if (buffer3Len <= (ulong)_readBufferLen) { buffer3 = new byte[buffer3Len]; - stream.Read(buffer3, 0, (int)buffer3Len); + readLen = stream.Read(buffer3, 0, (int)buffer3Len); + + if (readLen < (int)buffer3Len) + { + return null; + } } else { buffer3 = stream.ReadBytes(buffer3Len, _readBufferLen); + + if ((ulong)buffer3.LongLength < buffer3Len) + { + return null; + } } if (masked == Mask.MASK) diff --git a/websocket-sharp/Server/WebSocketService.cs b/websocket-sharp/Server/WebSocketService.cs index 9385bd5a..2db77d1f 100644 --- a/websocket-sharp/Server/WebSocketService.cs +++ b/websocket-sharp/Server/WebSocketService.cs @@ -64,10 +64,10 @@ namespace WebSocketSharp.Server _server.AddService(this); }; - _socket.OnClose += (sender, e) => - { - _server.RemoveService(this); - }; +// _socket.OnClose += (sender, e) => +// { +// _server.RemoveService(this); +// }; } #endregion diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index f83510b7..56c59de6 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -752,6 +752,10 @@ namespace WebSocketSharp { close(CloseStatusCode.TOO_BIG, ex.Message); } + catch (Exception ex) + { + close(CloseStatusCode.ABNORMAL, ex.Message); + } } private void messageLoop() @@ -762,39 +766,16 @@ namespace WebSocketSharp } } - private void startMessageThread() + private void pong(PayloadData data) { - if (_isClient) - { - _msgThread = new Thread(new ThreadStart(messageLoop)); - _msgThread.IsBackground = true; - _msgThread.Start(); - } - else - { - _autoEvent = new AutoResetEvent(false); - Action act = () => - { - if (_readyState == WsState.OPEN) - { - message(); - } - }; - AsyncCallback callback = (ar) => - { - act.EndInvoke(ar); + var frame = createFrame(Fin.FINAL, Opcode.PONG, data); + send(frame); + } - if (_readyState == WsState.OPEN) - { - act.BeginInvoke(callback, null); - } - else - { - _autoEvent.Set(); - } - }; - act.BeginInvoke(callback, null); - } + private void pong(string data) + { + var payloadData = new PayloadData(data); + pong(payloadData); } private MessageEventArgs receive() @@ -806,7 +787,15 @@ namespace WebSocketSharp Opcode opcode; PayloadData payloadData; + Action act = () => + { + var msg = "WebSocket data frame can not be read from network stream."; + close(CloseStatusCode.ABNORMAL, msg); + }; + frame = _wsStream.ReadFrame(); + if (frame.IsNullDo(act)) return null; + if ((frame.Fin == Fin.FINAL && frame.Opcode == Opcode.CONT) || (frame.Fin == Fin.MORE && frame.Opcode == Opcode.CONT)) { @@ -825,6 +814,7 @@ namespace WebSocketSharp while (true) { frame = _wsStream.ReadFrame(); + if (frame.IsNullDo(act)) return null; if (frame.Fin == Fin.MORE) { @@ -856,8 +846,10 @@ namespace WebSocketSharp } else if (frame.Opcode == Opcode.PING) {// FINAL & PING + #if DEBUG + Console.WriteLine("WS: Info@receive: Return Pong."); + #endif pong(frame.PayloadData); - OnMessage.Emit(this, new MessageEventArgs(frame.Opcode, frame.PayloadData)); } else if (frame.Opcode == Opcode.PONG) {// FINAL & PONG @@ -889,8 +881,11 @@ namespace WebSocketSharp close(payloadData); break; case Opcode.PING: + #if DEBUG + Console.WriteLine("WS: Info@receive: Return Pong."); + #endif pong(payloadData); - goto default; + break; default: eventArgs = new MessageEventArgs(opcode, payloadData); break; @@ -902,18 +897,6 @@ namespace WebSocketSharp return eventArgs; } - private void pong(PayloadData data) - { - var frame = createFrame(Fin.FINAL, Opcode.PONG, data); - send(frame); - } - - private void pong(string data) - { - var payloadData = new PayloadData(data); - pong(payloadData); - } - private string[] receiveOpeningHandshake() { var readData = new List(); @@ -1096,10 +1079,60 @@ namespace WebSocketSharp _wsStream.Write(buffer, 0, buffer.Length); } + private void startMessageThread() + { + if (_isClient) + { + _msgThread = new Thread(new ThreadStart(messageLoop)); + _msgThread.IsBackground = true; + _msgThread.Start(); + } + else + { + _autoEvent = new AutoResetEvent(false); + Action act = () => + { + if (_readyState == WsState.OPEN) + { + message(); + } + }; + AsyncCallback callback = (ar) => + { + act.EndInvoke(ar); + + if (_readyState == WsState.OPEN) + { + act.BeginInvoke(callback, null); + } + else + { + _autoEvent.Set(); + } + }; + act.BeginInvoke(callback, null); + } + } + #endregion #region Public Methods + public void Close() + { + Close(CloseStatusCode.NORMAL); + } + + public void Close(CloseStatusCode code) + { + Close(code, String.Empty); + } + + public void Close(CloseStatusCode code, string reason) + { + close(code, reason); + } + public void Connect() { if (_readyState == WsState.OPEN) @@ -1128,21 +1161,6 @@ namespace WebSocketSharp } } - public void Close() - { - Close(CloseStatusCode.NORMAL); - } - - public void Close(CloseStatusCode code) - { - Close(code, String.Empty); - } - - public void Close(CloseStatusCode code, string reason) - { - close(code, reason); - } - public void Dispose() { Close(CloseStatusCode.AWAY); diff --git a/websocket-sharp/bin/Debug/websocket-sharp.dll b/websocket-sharp/bin/Debug/websocket-sharp.dll index a083df16..d5a82b46 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 77daf56c..6bad1f2f 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 0d0c39ec..fd81cba5 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 4677f303..71dd29e3 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 977618e7..2da2cf3c 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 d9e22c98..387f9ac2 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.pidb b/websocket-sharp/websocket-sharp.pidb index afbc47be..75030839 100644 Binary files a/websocket-sharp/websocket-sharp.pidb and b/websocket-sharp/websocket-sharp.pidb differ