diff --git a/Example/bin/Debug/example.exe b/Example/bin/Debug/example.exe index 12176b54..ced42141 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 9c709603..52ee71bf 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 9d53da1e..a083df16 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 00e14f4d..77daf56c 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 28ae917b..bfd397c1 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 7cb3c30c..8c36fe00 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 1156effe..0d0c39ec 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 322f211e..4677f303 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 971cc444..48310657 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 2e922cf2..977618e7 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 57ac63b9..cfdce71e 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 c4696f93..d9e22c98 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 366b2c8b..f937877f 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 aab332e1..ea59fe08 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 9d53da1e..a083df16 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 00e14f4d..77daf56c 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 e37424ef..adf248a5 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 cf26b5f9..a315e5e7 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 1156effe..0d0c39ec 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 322f211e..4677f303 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 027501ad..8ea1730f 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 2e922cf2..977618e7 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 855ed69b..6588febd 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 c4696f93..d9e22c98 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 ccf9d1bc..1aa73a61 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 533d5529..d19839b5 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 9d53da1e..a083df16 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 00e14f4d..77daf56c 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 f204475b..67286e95 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 6f6f706b..f7a1d218 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 1156effe..0d0c39ec 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 322f211e..4677f303 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 559a0caf..dd2a120b 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 2e922cf2..977618e7 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 e4cc328b..5053459c 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 c4696f93..d9e22c98 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 8a063dbf..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 dbea36a4..f83510b7 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -271,8 +271,8 @@ namespace WebSocketSharp } else if (_readyState == WsState.CONNECTING) { - ReadyState = WsState.CLOSED; OnClose.Emit(this, new CloseEventArgs(data)); + ReadyState = WsState.CLOSED; return; } @@ -280,7 +280,7 @@ namespace WebSocketSharp } OnClose.Emit(this, new CloseEventArgs(data)); - var frame = new WsFrame(Opcode.CLOSE, data); + var frame = createFrame(Fin.FINAL, Opcode.CLOSE, data); closeHandshake(frame); #if DEBUG Console.WriteLine("WS: Info@close: Exit close method."); @@ -289,18 +289,24 @@ namespace WebSocketSharp private void close(CloseStatusCode code, string reason) { - byte[] buffer; - List data; - - data = new List(((ushort)code).ToBytes(ByteOrder.BIG)); + var data = new List(((ushort)code).ToBytes(ByteOrder.BIG)); if (reason != String.Empty) { - buffer = Encoding.UTF8.GetBytes(reason); + var buffer = Encoding.UTF8.GetBytes(reason); data.AddRange(buffer); } - close(new PayloadData(data.ToArray())); + var payloadData = new PayloadData(data.ToArray()); + + if (payloadData.Length > 125) + { + var msg = "Close frame must have a payload length of 125 bytes or less."; + error(msg); + return; + } + + close(payloadData); } private void closeConnection() @@ -325,17 +331,7 @@ namespace WebSocketSharp private void closeHandshake(WsFrame frame) { - try - { - _wsStream.WriteFrame(frame); - } - catch (Exception ex) - { - _unTransmittedBuffer.Add(frame); - error(ex.Message); - } - - if (!Thread.CurrentThread.IsBackground) + if (send(frame) && !Thread.CurrentThread.IsBackground) { if (_isClient) { @@ -402,6 +398,18 @@ namespace WebSocketSharp return Convert.ToBase64String(keySrc); } + private WsFrame createFrame(Fin fin, Opcode opcode, PayloadData payloadData) + { + if (_isClient) + { + return new WsFrame(fin, opcode, payloadData); + } + else + { + return new WsFrame(fin, opcode, Mask.UNMASK, payloadData); + } + } + private string createOpeningHandshake() { byte[] keySrc; @@ -896,7 +904,7 @@ namespace WebSocketSharp private void pong(PayloadData data) { - var frame = new WsFrame(Opcode.PONG, data); + var frame = createFrame(Fin.FINAL, Opcode.PONG, data); send(frame); } @@ -928,11 +936,11 @@ namespace WebSocketSharp private bool send(WsFrame frame) { - if (_readyState != WsState.OPEN) + if (_readyState == WsState.CONNECTING || + _readyState == WsState.CLOSED) { var msg = "Connection isn't established or connection was closed."; error(msg); - return false; } @@ -945,13 +953,15 @@ namespace WebSocketSharp else { _unTransmittedBuffer.Add(frame); + var msg = "Current data can not be sent because there is untransmitted data."; + error(msg); + return false; } } catch (Exception ex) { _unTransmittedBuffer.Add(frame); error(ex.Message); - return false; } @@ -969,20 +979,21 @@ namespace WebSocketSharp private void send(Opcode opcode, TStream stream) where TStream : System.IO.Stream { - if (_unTransmittedBuffer.Count > 0) - { - var msg = "Current data can not be sent because there is untransmitted data."; - error(msg); - } - lock(_forSend) { + if (_readyState != WsState.OPEN) + { + var msg = "Connection isn't established or connection was closed."; + error(msg); + return; + } + var length = stream.Length; if (length <= _fragmentLen) { var buffer = new byte[length]; stream.Read(buffer, 0, (int)length); - var frame = new WsFrame(opcode, new PayloadData(buffer)); + var frame = createFrame(Fin.FINAL, opcode, new PayloadData(buffer)); send(frame); } else @@ -995,22 +1006,14 @@ namespace WebSocketSharp private ulong sendFragmented(Opcode opcode, TStream stream) where TStream : System.IO.Stream { - if (_readyState != WsState.OPEN) - { - var msg = "Connection isn't established or connection was closed."; - error(msg); - - return 0; - } - WsFrame frame; PayloadData payloadData; - var buffer1 = new byte[_fragmentLen]; - var buffer2 = new byte[_fragmentLen]; - ulong readLen = 0; - int tmpLen1 = 0; - int tmpLen2 = 0; + byte[] buffer1 = new byte[_fragmentLen]; + byte[] buffer2 = new byte[_fragmentLen]; + ulong readLen = 0; + int tmpLen1 = 0; + int tmpLen2 = 0; tmpLen1 = stream.Read(buffer1, 0, _fragmentLen); while (tmpLen1 > 0) @@ -1022,22 +1025,22 @@ namespace WebSocketSharp { if (readLen > 0) { - frame = new WsFrame(Fin.MORE, Opcode.CONT, payloadData); + frame = createFrame(Fin.MORE, Opcode.CONT, payloadData); } else { - frame = new WsFrame(Fin.MORE, opcode, payloadData); + frame = createFrame(Fin.MORE, opcode, payloadData); } } else { if (readLen > 0) { - frame = new WsFrame(Opcode.CONT, payloadData); + frame = createFrame(Fin.FINAL, Opcode.CONT, payloadData); } else { - frame = new WsFrame(opcode, payloadData); + frame = createFrame(Fin.FINAL, opcode, payloadData); } } @@ -1050,11 +1053,11 @@ namespace WebSocketSharp tmpLen1 = stream.Read(buffer1, 0, _fragmentLen); if (tmpLen1 > 0) { - frame = new WsFrame(Fin.MORE, Opcode.CONT, payloadData); + frame = createFrame(Fin.MORE, Opcode.CONT, payloadData); } else { - frame = new WsFrame(Opcode.CONT, payloadData); + frame = createFrame(Fin.FINAL, Opcode.CONT, payloadData); } readLen += (ulong)tmpLen2; @@ -1153,7 +1156,15 @@ namespace WebSocketSharp public void Ping(string data) { var payloadData = new PayloadData(data); - var frame = new WsFrame(Opcode.PING, payloadData); + + if (payloadData.Length > 125) + { + var msg = "Ping frame must have a payload length of 125 bytes or less."; + error(msg); + return; + } + + var frame = createFrame(Fin.FINAL, Opcode.PING, payloadData); send(frame); } diff --git a/websocket-sharp/bin/Debug/websocket-sharp.dll b/websocket-sharp/bin/Debug/websocket-sharp.dll index 9d53da1e..a083df16 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 00e14f4d..77daf56c 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 1156effe..0d0c39ec 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 322f211e..4677f303 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 2e922cf2..977618e7 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 c4696f93..d9e22c98 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 f01465ff..afbc47be 100644 Binary files a/websocket-sharp/websocket-sharp.pidb and b/websocket-sharp/websocket-sharp.pidb differ