diff --git a/Example/bin/Debug_Ubuntu/example.exe b/Example/bin/Debug_Ubuntu/example.exe index 1eb32d06..2390d1c0 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/websocket-sharp.dll b/Example/bin/Debug_Ubuntu/websocket-sharp.dll index 86fd3a42..ed4127e1 100755 Binary files a/Example/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example1/bin/Debug_Ubuntu/example1.exe b/Example1/bin/Debug_Ubuntu/example1.exe index aec52a24..60671926 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/websocket-sharp.dll b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll index 86fd3a42..ed4127e1 100755 Binary files a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example2/bin/Debug_Ubuntu/example2.exe b/Example2/bin/Debug_Ubuntu/example2.exe index e3513839..565a0f80 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/websocket-sharp.dll b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll index 86fd3a42..ed4127e1 100755 Binary files a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example3/bin/Debug_Ubuntu/Example3.exe b/Example3/bin/Debug_Ubuntu/Example3.exe index 532b8c2c..d898bf51 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/websocket-sharp.dll b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll index 86fd3a42..ed4127e1 100755 Binary files a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/websocket-sharp/Ext.cs b/websocket-sharp/Ext.cs index 1824ed69..da4a5708 100644 --- a/websocket-sharp/Ext.cs +++ b/websocket-sharp/Ext.cs @@ -146,6 +146,23 @@ namespace WebSocketSharp { #region Internal Methods + internal static byte[] Append(this ushort code, string reason) + { + using (var buffer = new MemoryStream()) + { + var tmp = code.ToByteArray(ByteOrder.BIG); + buffer.Write(tmp, 0, 2); + if (reason != null && reason.Length > 0) + { + tmp = Encoding.UTF8.GetBytes(reason); + buffer.Write(tmp, 0, tmp.Length); + } + + buffer.Close(); + return buffer.ToArray(); + } + } + internal static byte[] Compress(this byte[] value, CompressionMethod method) { return method == CompressionMethod.DEFLATE diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index 92dbbebe..ccb89a75 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -467,28 +467,14 @@ namespace WebSocketSharp { private void close(ushort code, string reason) { - using (var buffer = new MemoryStream()) + var data = code.Append(reason); + if (data.Length > 125) { - var tmp = code.ToByteArray(ByteOrder.BIG); - buffer.Write(tmp, 0, tmp.Length); - if (!reason.IsNullOrEmpty()) - { - tmp = Encoding.UTF8.GetBytes(reason); - buffer.Write(tmp, 0, tmp.Length); - } - - buffer.Close(); - var data = buffer.ToArray(); - if (data.Length > 125) - { - var msg = "The payload length of a Close frame must be 125 bytes or less."; - onError(msg); - - return; - } - - close(new PayloadData(data)); + onError("The payload length of a Close frame must be 125 bytes or less."); + return; } + + close(new PayloadData(data)); } private void closeHandshake(PayloadData data) diff --git a/websocket-sharp/WsFrame.cs b/websocket-sharp/WsFrame.cs index 6785070c..350fc631 100644 --- a/websocket-sharp/WsFrame.cs +++ b/websocket-sharp/WsFrame.cs @@ -226,21 +226,10 @@ namespace WebSocketSharp { #region Private Methods - private static WsFrame createCloseFrame(CloseStatusCode code, string message) + private static WsFrame createCloseFrame(CloseStatusCode code, string reason, Mask mask) { - using (var buffer = new MemoryStream()) - { - var tmp = ((ushort)code).ToByteArray(ByteOrder.BIG); - buffer.Write(tmp, 0, 2); - if (message.Length != 0) - { - tmp = Encoding.UTF8.GetBytes(message); - buffer.Write(tmp, 0, tmp.Length); - } - - buffer.Close(); - return new WsFrame(Fin.FINAL, Opcode.CLOSE, Mask.UNMASK, new PayloadData(buffer.ToArray())); - } + var data = ((ushort)code).Append(reason); + return new WsFrame(Fin.FINAL, Opcode.CLOSE, mask, new PayloadData(data)); } private static byte[] createMaskingKey() @@ -393,7 +382,8 @@ namespace WebSocketSharp { if (isControl(opcode) && payloadLen > 125) return createCloseFrame(CloseStatusCode.INCONSISTENT_DATA, - "The payload length of a control frame must be 125 bytes or less."); + "The payload length of a control frame must be 125 bytes or less.", + Mask.UNMASK); var frame = new WsFrame { Fin = fin, @@ -419,7 +409,8 @@ namespace WebSocketSharp { if (extLen > 0 && extPayloadLen.Length != extLen) return createCloseFrame(CloseStatusCode.ABNORMAL, - "'Extended Payload Length' of a frame cannot be read from the data stream."); + "'Extended Payload Length' of a frame cannot be read from the data stream.", + Mask.UNMASK); frame.ExtPayloadLen = extPayloadLen; @@ -432,7 +423,8 @@ namespace WebSocketSharp { if (masked && maskingKey.Length != 4) return createCloseFrame(CloseStatusCode.ABNORMAL, - "'Masking Key' of a frame cannot be read from the data stream."); + "'Masking Key' of a frame cannot be read from the data stream.", + Mask.UNMASK); frame.MaskingKey = maskingKey; @@ -450,7 +442,7 @@ namespace WebSocketSharp { if (payloadLen > 126 && dataLen > PayloadData.MaxLength) { var code = CloseStatusCode.TOO_BIG; - return createCloseFrame(code, code.GetMessage()); + return createCloseFrame(code, code.GetMessage(), Mask.UNMASK); } data = dataLen > 1024 @@ -459,7 +451,8 @@ namespace WebSocketSharp { if (data.LongLength != (long)dataLen) return createCloseFrame(CloseStatusCode.ABNORMAL, - "'Payload Data' of a frame cannot be read from the data stream."); + "'Payload Data' of a frame cannot be read from the data stream.", + Mask.UNMASK); } else { @@ -557,7 +550,8 @@ namespace WebSocketSharp { frame = header.Length == 2 ? parse(header, stream, unmask) : createCloseFrame(CloseStatusCode.ABNORMAL, - "'Header' of a frame cannot be read from the data stream."); + "'Header' of a frame cannot be read from the data stream.", + Mask.UNMASK); } catch (Exception ex) { @@ -601,7 +595,8 @@ namespace WebSocketSharp { frame = readLen == 2 ? parse(header, stream, unmask) : createCloseFrame(CloseStatusCode.ABNORMAL, - "'Header' of a frame cannot be read from the data stream."); + "'Header' of a frame cannot be read from the data stream.", + Mask.UNMASK); } catch (Exception ex) { diff --git a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll index 86fd3a42..ed4127e1 100755 Binary files a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll and b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll differ