diff --git a/Example/Example.pidb b/Example/Example.pidb index 793717f6..89d1ab86 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 b45026f6..0a4857e7 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 de59009b..6d4135d4 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 ab0165ce..76fbec1b 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 b1200b4e..148adbe7 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 24eccc16..31f12d76 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 4e9d0910..fa49b141 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 e23668f4..313e4ff2 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 2e2e5b1e..8a6bef35 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 2a19e18d..ed53d590 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 1bc42dcb..7f34c1d4 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 a640a243..5f613f18 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 cd94097c..8bca3b44 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 a9a89d8e..a95841e5 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 a2f284af..8243dde3 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 ab0165ce..76fbec1b 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 b1200b4e..148adbe7 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 1ad7aaa3..1760ba13 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 1b225ce1..7b0a2ec1 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 e23668f4..313e4ff2 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 2e2e5b1e..8a6bef35 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 dd225143..0db38d88 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 1bc42dcb..7f34c1d4 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 f226f691..daaa6f52 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 cd94097c..8bca3b44 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 117cad4a..f89b5afb 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 825fbc3b..b89e835e 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 dfa654e2..cfa0bc8b 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 ab0165ce..76fbec1b 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 b1200b4e..148adbe7 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 05194684..cae434b2 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 4dd20a99..994e58ae 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 e23668f4..313e4ff2 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 2e2e5b1e..8a6bef35 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 bd5f6ed6..36393725 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 1bc42dcb..7f34c1d4 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 7e73f85a..4db66af8 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 cd94097c..8bca3b44 100755 Binary files a/Example2/bin/Release_Ubuntu/websocket-sharp.dll and b/Example2/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/Example3/bin/Debug/Example3.exe b/Example3/bin/Debug/Example3.exe index 6dd355a0..4456ec67 100755 Binary files a/Example3/bin/Debug/Example3.exe and b/Example3/bin/Debug/Example3.exe differ diff --git a/Example3/bin/Debug/Example3.exe.mdb b/Example3/bin/Debug/Example3.exe.mdb index 686d663d..40461197 100644 Binary files a/Example3/bin/Debug/Example3.exe.mdb and b/Example3/bin/Debug/Example3.exe.mdb differ diff --git a/Example3/bin/Debug/websocket-sharp.dll b/Example3/bin/Debug/websocket-sharp.dll index ab0165ce..76fbec1b 100755 Binary files a/Example3/bin/Debug/websocket-sharp.dll and b/Example3/bin/Debug/websocket-sharp.dll differ diff --git a/Example3/bin/Debug/websocket-sharp.dll.mdb b/Example3/bin/Debug/websocket-sharp.dll.mdb index b1200b4e..148adbe7 100644 Binary files a/Example3/bin/Debug/websocket-sharp.dll.mdb and b/Example3/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/Example3/bin/Debug_Ubuntu/Example3.exe b/Example3/bin/Debug_Ubuntu/Example3.exe index f3856bb1..a5082516 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 a90ec2f3..ceaf3b4a 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 e23668f4..313e4ff2 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 2e2e5b1e..8a6bef35 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/Example3/bin/Release/Example3.exe b/Example3/bin/Release/Example3.exe index f5100eff..fc4aa886 100755 Binary files a/Example3/bin/Release/Example3.exe and b/Example3/bin/Release/Example3.exe differ diff --git a/Example3/bin/Release/websocket-sharp.dll b/Example3/bin/Release/websocket-sharp.dll index 1bc42dcb..7f34c1d4 100755 Binary files a/Example3/bin/Release/websocket-sharp.dll and b/Example3/bin/Release/websocket-sharp.dll differ diff --git a/Example3/bin/Release_Ubuntu/Example3.exe b/Example3/bin/Release_Ubuntu/Example3.exe index 6aaf59c8..9ba18f18 100755 Binary files a/Example3/bin/Release_Ubuntu/Example3.exe and b/Example3/bin/Release_Ubuntu/Example3.exe differ diff --git a/Example3/bin/Release_Ubuntu/websocket-sharp.dll b/Example3/bin/Release_Ubuntu/websocket-sharp.dll index cd94097c..8bca3b44 100755 Binary files a/Example3/bin/Release_Ubuntu/websocket-sharp.dll and b/Example3/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/websocket-sharp.userprefs b/websocket-sharp.userprefs index 9b182064..4f01e372 100644 --- a/websocket-sharp.userprefs +++ b/websocket-sharp.userprefs @@ -1,5 +1,5 @@  - + diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index f3b19642..559dc82a 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -643,6 +643,18 @@ namespace WebSocketSharp pong(payloadData); } + private WsFrame readFrame() + { + var frame = _wsStream.ReadFrame(); + if (frame == null) + { + var msg = "WebSocket data frame can not be read from network stream."; + close(CloseStatusCode.ABNORMAL, msg); + } + + return frame; + } + private string[] readHandshake() { return _wsStream.ReadHandshake(); @@ -650,124 +662,115 @@ namespace WebSocketSharp private MessageEventArgs receive() { - List dataBuffer; - MessageEventArgs eventArgs; - Fin fin; - WsFrame frame; - 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; + var frame = readFrame(); + if (frame == null) + return null; if ((frame.Fin == Fin.FINAL && frame.Opcode == Opcode.CONT) || (frame.Fin == Fin.MORE && frame.Opcode == Opcode.CONT)) - { + return null; + + if (frame.Fin == Fin.MORE) + {// MORE + var merged = receiveFragmented(frame); + if (merged == null) + return null; + + return new MessageEventArgs(frame.Opcode, new PayloadData(merged)); + } + + if (frame.Opcode == Opcode.CLOSE) + {// FINAL & CLOSE + #if DEBUG + Console.WriteLine("WS: Info@receive: Start closing handshake."); + #endif + close(frame.PayloadData); return null; } - fin = frame.Fin; - opcode = frame.Opcode; - payloadData = frame.PayloadData; - eventArgs = null; - - switch (fin) - { - case Fin.MORE: - dataBuffer = new List(payloadData.ToBytes()); - while (true) - { - frame = _wsStream.ReadFrame(); - if (frame.IsNullDo(act)) return null; - - if (frame.Fin == Fin.MORE) - { - if (frame.Opcode == Opcode.CONT) - {// MORE & CONT - dataBuffer.AddRange(frame.PayloadData.ToBytes()); - } - else - { - #if DEBUG - Console.WriteLine("WS: Info@receive: Start closing handshake."); - #endif - close(CloseStatusCode.INCORRECT_DATA, String.Empty); - return null; - } - } - else if (frame.Opcode == Opcode.CONT) - {// FINAL & CONT - dataBuffer.AddRange(frame.PayloadData.ToBytes()); - break; - } - else if (frame.Opcode == Opcode.CLOSE) - {// FINAL & CLOSE - #if DEBUG - Console.WriteLine("WS: Info@receive: Start closing handshake."); - #endif - close(frame.PayloadData); - return null; - } - else if (frame.Opcode == Opcode.PING) - {// FINAL & PING - #if DEBUG - Console.WriteLine("WS: Info@receive: Return Pong."); - #endif - pong(frame.PayloadData); - } - else if (frame.Opcode == Opcode.PONG) - {// FINAL & PONG - _receivedPong.Set(); - OnMessage.Emit(this, new MessageEventArgs(frame.Opcode, frame.PayloadData)); - } - else - {// FINAL & (TEXT | BINARY) - #if DEBUG - Console.WriteLine("WS: Info@receive: Start closing handshake."); - #endif - close(CloseStatusCode.INCORRECT_DATA, String.Empty); - return null; - } - } - - eventArgs = new MessageEventArgs(opcode, new PayloadData(dataBuffer.ToArray())); - break; - case Fin.FINAL: - switch (opcode) - { - case Opcode.TEXT: - case Opcode.BINARY: - goto default; - case Opcode.PONG: - _receivedPong.Set(); - goto default; - case Opcode.CLOSE: - #if DEBUG - Console.WriteLine("WS: Info@receive: Start closing handshake."); - #endif - close(payloadData); - break; - case Opcode.PING: - #if DEBUG - Console.WriteLine("WS: Info@receive: Return Pong."); - #endif - pong(payloadData); - break; - default: - eventArgs = new MessageEventArgs(opcode, payloadData); - break; - } - - break; + if (frame.Opcode == Opcode.PING) + {// FINAL & PING + #if DEBUG + Console.WriteLine("WS: Info@receive: Return Pong."); + #endif + pong(frame.PayloadData); + return null; } - return eventArgs; + if (frame.Opcode == Opcode.PONG) + {// FINAL & PONG + _receivedPong.Set(); + } + + // FINAL & (TEXT | BINARY | PONG) + return new MessageEventArgs(frame.Opcode, frame.PayloadData); + } + + private byte[] receiveFragmented(WsFrame firstFrame) + { + var buffer = new List(firstFrame.PayloadData.ToBytes()); + + while (true) + { + var frame = readFrame(); + if (frame == null) + return null; + + if (frame.Fin == Fin.MORE) + { + if (frame.Opcode == Opcode.CONT) + {// MORE & CONT + buffer.AddRange(frame.PayloadData.ToBytes()); + continue; + } + + #if DEBUG + Console.WriteLine("WS: Info@receiveFragmented: Start closing handshake."); + #endif + close(CloseStatusCode.INCORRECT_DATA, String.Empty); + return null; + } + + if (frame.Opcode == Opcode.CONT) + {// FINAL & CONT + buffer.AddRange(frame.PayloadData.ToBytes()); + break; + } + + if (frame.Opcode == Opcode.CLOSE) + {// FINAL & CLOSE + #if DEBUG + Console.WriteLine("WS: Info@receiveFragmented: Start closing handshake."); + #endif + close(frame.PayloadData); + return null; + } + + if (frame.Opcode == Opcode.PING) + {// FINAL & PING + #if DEBUG + Console.WriteLine("WS: Info@receiveFragmented: Return Pong."); + #endif + pong(frame.PayloadData); + continue; + } + + if (frame.Opcode == Opcode.PONG) + {// FINAL & PONG + _receivedPong.Set(); + OnMessage.Emit(this, new MessageEventArgs(frame.Opcode, frame.PayloadData)); + continue; + } + + // FINAL & (TEXT | BINARY) + #if DEBUG + Console.WriteLine("WS: Info@receiveFragmented: Start closing handshake."); + #endif + close(CloseStatusCode.INCORRECT_DATA, String.Empty); + return null; + } + + return buffer.ToArray(); } private RequestHandshake receiveOpeningHandshake() diff --git a/websocket-sharp/bin/Debug/websocket-sharp.dll b/websocket-sharp/bin/Debug/websocket-sharp.dll index ab0165ce..76fbec1b 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 b1200b4e..148adbe7 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 e23668f4..313e4ff2 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 2e2e5b1e..8a6bef35 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 1bc42dcb..7f34c1d4 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 cd94097c..8bca3b44 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 d743fc85..d9dfd344 100644 Binary files a/websocket-sharp/websocket-sharp.pidb and b/websocket-sharp/websocket-sharp.pidb differ