diff --git a/Example/bin/Debug/example.exe b/Example/bin/Debug/example.exe index 6d0a9213..f58903fc 100755 Binary files a/Example/bin/Debug/example.exe and b/Example/bin/Debug/example.exe differ diff --git a/Example/bin/Debug/websocket-sharp.dll b/Example/bin/Debug/websocket-sharp.dll index 2d16b542..e6348c11 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_Ubuntu/example.exe b/Example/bin/Debug_Ubuntu/example.exe index 058adcd5..c5c47fa6 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 9175caf0..64c64cd5 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/Release/example.exe b/Example/bin/Release/example.exe index 4b24b22c..706edd70 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 a138726c..d0a30ed2 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 d07774a2..793a568f 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 73b88546..52ee834c 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 194c0626..414e11cb 100755 Binary files a/Example1/bin/Debug/example1.exe and b/Example1/bin/Debug/example1.exe differ diff --git a/Example1/bin/Debug/websocket-sharp.dll b/Example1/bin/Debug/websocket-sharp.dll index 2d16b542..e6348c11 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_Ubuntu/example1.exe b/Example1/bin/Debug_Ubuntu/example1.exe index 22f56a81..f721a810 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 9175caf0..64c64cd5 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/Release/example1.exe b/Example1/bin/Release/example1.exe index de169a3d..23182988 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 a138726c..d0a30ed2 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 f577383e..182ead32 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 73b88546..52ee834c 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 621200fe..b4fab08c 100755 Binary files a/Example2/bin/Debug/example2.exe and b/Example2/bin/Debug/example2.exe differ diff --git a/Example2/bin/Debug/websocket-sharp.dll b/Example2/bin/Debug/websocket-sharp.dll index 2d16b542..e6348c11 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_Ubuntu/example2.exe b/Example2/bin/Debug_Ubuntu/example2.exe index f487748f..510f3ba7 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 9175caf0..64c64cd5 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/Release/example2.exe b/Example2/bin/Release/example2.exe index 470423be..d8ba8c0e 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 a138726c..d0a30ed2 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 b7b6d78c..79a28de8 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 73b88546..52ee834c 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 78f8ff71..eeea3382 100755 Binary files a/Example3/bin/Debug/Example3.exe and b/Example3/bin/Debug/Example3.exe differ diff --git a/Example3/bin/Debug/websocket-sharp.dll b/Example3/bin/Debug/websocket-sharp.dll index 2d16b542..e6348c11 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_Ubuntu/Example3.exe b/Example3/bin/Debug_Ubuntu/Example3.exe index 2ea2a5ec..d10208ca 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 9175caf0..64c64cd5 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/Release/Example3.exe b/Example3/bin/Release/Example3.exe index 7dc4b60f..08efcafc 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 a138726c..d0a30ed2 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 e5fe2269..e89f3ba6 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 73b88546..52ee834c 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/Ext.cs b/websocket-sharp/Ext.cs index c6c8a035..1824ed69 100644 --- a/websocket-sharp/Ext.cs +++ b/websocket-sharp/Ext.cs @@ -174,8 +174,8 @@ namespace WebSocketSharp { internal static void CopyTo(this Stream src, Stream dest, bool setDefaultPosition) { - int readLen; - int bufferLen = 256; + var readLen = 0; + var bufferLen = 256; var buffer = new byte[bufferLen]; while ((readLen = src.Read(buffer, 0, bufferLen)) > 0) { @@ -321,6 +321,76 @@ namespace WebSocketSharp { : String.Format("\"{0}\"", value.Replace("\"", "\\\"")); } + internal static byte[] ReadBytesInternal(this Stream stream, int length) + { + var buffer = new byte[length]; + var readLen = stream.Read(buffer, 0, length); + if (readLen <= 0) + return new byte[]{}; + + var tmpLen = 0; + while (readLen < length) + { + tmpLen = stream.Read(buffer, readLen, length - readLen); + if (tmpLen <= 0) + break; + + readLen += tmpLen; + } + + return readLen == length + ? buffer + : buffer.SubArray(0, readLen); + } + + internal static byte[] ReadBytesInternal(this Stream stream, long length, int bufferLength) + { + var count = length / bufferLength; + var rem = length % bufferLength; + using (var readData = new MemoryStream()) + { + var readLen = 0; + var bufferLen = 0; + var tmpLen = 0; + Func read = buffer => + { + bufferLen = buffer.Length; + readLen = stream.Read(buffer, 0, bufferLen); + if (readLen <= 0) + return false; + + while (readLen < bufferLen) + { + tmpLen = stream.Read(buffer, readLen, bufferLen - readLen); + if (tmpLen <= 0) + break; + + readLen += tmpLen; + } + + readData.Write(buffer, 0, readLen); + return readLen == bufferLen; + }; + + var readBuffer = new byte[bufferLength]; + var readEnd = false; + for (long i = 0; i < count; i++) + { + if (!read(readBuffer)) + { + readEnd = true; + break; + } + } + + if (!readEnd && rem > 0) + read(new byte[rem]); + + readData.Close(); + return readData.ToArray(); + } + } + internal static string RemovePrefix(this string value, params string[] prefixes) { int i = 0; @@ -1073,28 +1143,6 @@ namespace WebSocketSharp { return uriString.Substring(0, p).IsPredefinedScheme(); } - /// - /// Determines whether two specified objects don't have the same value. - /// - /// - /// true if the value of parameter isn't the same as the value of parameter; otherwise, false. - /// - /// - /// The first to compare. - /// - /// - /// The second to compare. - /// - /// - /// A that indicates a case-sensitive or insensitive comparison. (true indicates a case-insensitive comparison.) - /// - public static bool NotEqual(this string expected, string actual, bool ignoreCase) - { - return String.Compare(expected, actual, ignoreCase) != 0 - ? true - : false; - } - /// /// Reads a block of bytes from the specified /// and returns the read data in an array of . @@ -1110,46 +1158,9 @@ namespace WebSocketSharp { /// public static byte[] ReadBytes(this Stream stream, int length) { - if (stream == null || length <= 0) - return new byte[]{}; - - var buffer = new byte[length]; - var readLen = stream.Read(buffer, 0, length); - if (readLen <= 0) - return new byte[]{}; - - var tmpLen = 0; - while (readLen < length) - { - tmpLen = stream.Read(buffer, readLen, length - readLen); - if (tmpLen <= 0) - break; - - readLen += tmpLen; - } - - return readLen == length - ? buffer - : readLen > 0 - ? buffer.SubArray(0, readLen) - : new byte[]{}; - } - - /// - /// Reads a block of bytes from the specified stream and returns the read data in an array of . - /// - /// - /// An array of that receives the read data. - /// - /// - /// A that contains the data to read. - /// - /// - /// A that contains the number of bytes to read. - /// - public static byte[] ReadBytes(this Stream stream, long length) - { - return stream.ReadBytes(length, 1024); + return stream == null || length < 1 + ? new byte[]{} + : stream.ReadBytesInternal(length); } /// @@ -1165,64 +1176,13 @@ namespace WebSocketSharp { /// /// A that contains the number of bytes to read. /// - /// - /// An that contains the buffer size in bytes of each internal read. - /// - public static byte[] ReadBytes(this Stream stream, long length, int bufferLength) + public static byte[] ReadBytes(this Stream stream, long length) { - if (stream == null || length <= 0) - return new byte[]{}; - - if (bufferLength <= 0) - bufferLength = 1024; - - var count = length / bufferLength; - var rem = length % bufferLength; - if (count == 0) - return stream.ReadBytes((int)rem); - - using (var readData = new MemoryStream()) - { - var readLen = 0; - var bufferLen = 0; - var tmpLen = 0; - Func read = buffer => - { - bufferLen = buffer.Length; - readLen = stream.Read(buffer, 0, bufferLen); - if (readLen <= 0) - return false; - - while (readLen < bufferLen) - { - tmpLen = stream.Read(buffer, readLen, bufferLen - readLen); - if (tmpLen <= 0) - break; - - readLen += tmpLen; - } - - readData.Write(buffer, 0, readLen); - return readLen == bufferLen; - }; - - var readBuffer = new byte[bufferLength]; - var cont = true; - for (long i = 0; i < count; i++) - { - if (!read(readBuffer)) - { - cont = false; - break; - } - } - - if (cont && rem > 0) - read(new byte[rem]); - - readData.Close(); - return readData.ToArray(); - } + return stream == null || length < 1 + ? new byte[]{} + : length > 1024 + ? stream.ReadBytesInternal(length, 1024) + : stream.ReadBytesInternal((int)length); } /// diff --git a/websocket-sharp/PayloadData.cs b/websocket-sharp/PayloadData.cs index 69bca92f..6be0cbac 100644 --- a/websocket-sharp/PayloadData.cs +++ b/websocket-sharp/PayloadData.cs @@ -72,12 +72,6 @@ namespace WebSocketSharp { public PayloadData(byte[] extData, byte[] appData, bool masked) { - if (extData.IsNull()) - throw new ArgumentNullException("extData"); - - if (appData.IsNull()) - throw new ArgumentNullException("appData"); - if ((ulong)extData.LongLength + (ulong)appData.LongLength > MaxLength) throw new ArgumentOutOfRangeException( "The length of 'extData' plus 'appData' must be less than MaxLength."); @@ -107,7 +101,9 @@ namespace WebSocketSharp { } } - internal bool IsMasked { get; private set; } + internal bool IsMasked { + get; private set; + } internal ulong Length { get { @@ -119,9 +115,13 @@ namespace WebSocketSharp { #region Public Properties - public byte[] ExtensionData { get; private set; } + public byte[] ExtensionData { + get; private set; + } - public byte[] ApplicationData { get; private set; } + public byte[] ApplicationData { + get; private set; + } #endregion @@ -152,12 +152,6 @@ namespace WebSocketSharp { public void Mask(byte[] maskingKey) { - if (maskingKey.IsNull()) - throw new ArgumentNullException("maskingKey"); - - if (maskingKey.Length != 4) - throw new ArgumentOutOfRangeException("maskingKey", "The length must be 4."); - if (ExtensionData.LongLength > 0) mask(ExtensionData, maskingKey); diff --git a/websocket-sharp/WsFrame.cs b/websocket-sharp/WsFrame.cs index 9d0d878f..5e0d231f 100644 --- a/websocket-sharp/WsFrame.cs +++ b/websocket-sharp/WsFrame.cs @@ -36,12 +36,6 @@ namespace WebSocketSharp { internal class WsFrame : IEnumerable { - #region Private Const Fields - - private const int _readBufferLen = 1024; - - #endregion - #region Private Constructors private WsFrame() @@ -70,26 +64,47 @@ namespace WebSocketSharp { public WsFrame( Fin fin, Opcode opcode, Mask mask, PayloadData payloadData, bool compressed) { - if (payloadData.IsNull()) - throw new ArgumentNullException("payloadData"); - - if (isControl(opcode) && payloadData.Length > 125) - throw new ArgumentOutOfRangeException("payloadData", - "The control frame must have a payload length of 125 bytes or less."); - - if (!isFinal(fin) && isControl(opcode)) - throw new ArgumentException("The control frame must not be fragmented."); - - if (isControl(opcode) && compressed) - throw new ArgumentException("The control frame must not be compressed."); - Fin = fin; Rsv1 = isData(opcode) && compressed ? Rsv.ON : Rsv.OFF; + Rsv2 = Rsv.OFF; + Rsv3 = Rsv.OFF; Opcode = opcode; Mask = mask; - PayloadData = payloadData; - init(); + /* PayloadLen */ + + ulong dataLen = payloadData.Length; + var payloadLen = dataLen < 126 + ? (byte)dataLen + : dataLen < 0x010000 + ? (byte)126 + : (byte)127; + + PayloadLen = payloadLen; + + /* ExtPayloadLen */ + + ExtPayloadLen = payloadLen < 126 + ? new byte[]{} + : payloadLen == 126 + ? ((ushort)dataLen).ToByteArray(ByteOrder.BIG) + : dataLen.ToByteArray(ByteOrder.BIG); + + /* MaskingKey */ + + var masking = mask == Mask.MASK; + var maskingKey = masking + ? createMaskingKey() + : new byte[]{}; + + MaskingKey = maskingKey; + + /* PayloadData */ + + if (masking) + payloadData.Mask(maskingKey); + + PayloadData = payloadData; } #endregion @@ -215,6 +230,36 @@ namespace WebSocketSharp { #region Private Methods + private static WsFrame createCloseFrame(WebSocketException exception) + { + using (var buffer = new MemoryStream()) + { + var code = (ushort)exception.Code; + var msg = exception.Message; + buffer.Write(code.ToByteArray(ByteOrder.BIG), 0, 2); + if (msg.Length != 0) + { + var tmp = Encoding.UTF8.GetBytes(msg); + buffer.Write(tmp, 0, tmp.Length); + } + + buffer.Close(); + var payload = new PayloadData(buffer.ToArray()); + var frame = new WsFrame(Fin.FINAL, Opcode.CLOSE, Mask.UNMASK, payload); + + return frame; + } + } + + private static byte[] createMaskingKey() + { + var key = new byte[4]; + var rand = new Random(); + rand.NextBytes(key); + + return key; + } + private static void dump(WsFrame frame) { var len = frame.Length; @@ -285,17 +330,6 @@ namespace WebSocketSharp { Console.WriteLine(footerFmt, String.Empty); } - private void init() - { - Rsv2 = Rsv.OFF; - Rsv3 = Rsv.OFF; - setPayloadLen(PayloadData.Length); - if (IsMasked) - maskPayloadData(); - else - MaskingKey = new byte[]{}; - } - private static bool isBinary(Opcode opcode) { return opcode == Opcode.BINARY; @@ -313,12 +347,12 @@ namespace WebSocketSharp { private static bool isControl(Opcode opcode) { - return isClose(opcode) || isPing(opcode) || isPong(opcode); + return opcode == Opcode.CLOSE || opcode == Opcode.PING || opcode == Opcode.PONG; } private static bool isData(Opcode opcode) { - return isText(opcode) || isBinary(opcode); + return opcode == Opcode.TEXT || opcode == Opcode.BINARY; } private static bool isFinal(Fin fin) @@ -346,105 +380,33 @@ namespace WebSocketSharp { return opcode == Opcode.TEXT; } - private void maskPayloadData() - { - var key = new byte[4]; - var rand = new Random(); - rand.NextBytes(key); - - MaskingKey = key; - PayloadData.Mask(key); - } - private static WsFrame parse(byte[] header, Stream stream, bool unmask) { - if (header.IsNull() || header.Length != 2) + if (header == null || header.Length != 2) return null; - var frame = readHeader(header); - readExtPayloadLen(stream, frame); - readMaskingKey(stream, frame); - readPayloadData(stream, frame, unmask); - - return frame; - } - - private static void print(WsFrame frame) - { - var len = frame.ExtPayloadLen.Length; - var extPayloadLen = len == 2 - ? frame.ExtPayloadLen.To(ByteOrder.BIG).ToString() - : len == 8 - ? frame.ExtPayloadLen.To(ByteOrder.BIG).ToString() - : String.Empty; - - var masked = frame.IsMasked; - var maskingKey = masked - ? BitConverter.ToString(frame.MaskingKey) - : String.Empty; - - var opcode = frame.Opcode; - var payloadData = frame.PayloadData.Length == 0 - ? String.Empty - : masked || frame.IsFragmented || frame.IsBinary || frame.IsClose - ? BitConverter.ToString(frame.PayloadData.ToByteArray()) - : Encoding.UTF8.GetString(frame.PayloadData.ToByteArray()); - - var format = @" - FIN: {0} - RSV1: {1} - RSV2: {2} - RSV3: {3} - Opcode: {4} - MASK: {5} - Payload Len: {6} - Extended Payload Len: {7} - Masking Key: {8} - Payload Data: {9}"; - - Console.WriteLine( - format, frame.Fin, frame.Rsv1, frame.Rsv2, frame.Rsv3, opcode, frame.Mask, frame.PayloadLen, extPayloadLen, maskingKey, payloadData); - } - - private static void readExtPayloadLen(Stream stream, WsFrame frame) - { - int length = frame.PayloadLen <= 125 - ? 0 - : frame.PayloadLen == 126 - ? 2 - : 8; - - if (length == 0) - { - frame.ExtPayloadLen = new byte[]{}; - return; - } - - var extPayloadLen = stream.ReadBytes(length); - if (extPayloadLen.Length != length) - throw new IOException(); - - frame.ExtPayloadLen = extPayloadLen; - } + /* Header */ - private static WsFrame readHeader(byte[] header) - { // FIN - Fin fin = (header[0] & 0x80) == 0x80 ? Fin.FINAL : Fin.MORE; + var fin = (header[0] & 0x80) == 0x80 ? Fin.FINAL : Fin.MORE; // RSV1 - Rsv rsv1 = (header[0] & 0x40) == 0x40 ? Rsv.ON : Rsv.OFF; + var rsv1 = (header[0] & 0x40) == 0x40 ? Rsv.ON : Rsv.OFF; // RSV2 - Rsv rsv2 = (header[0] & 0x20) == 0x20 ? Rsv.ON : Rsv.OFF; + var rsv2 = (header[0] & 0x20) == 0x20 ? Rsv.ON : Rsv.OFF; // RSV3 - Rsv rsv3 = (header[0] & 0x10) == 0x10 ? Rsv.ON : Rsv.OFF; + var rsv3 = (header[0] & 0x10) == 0x10 ? Rsv.ON : Rsv.OFF; // Opcode - Opcode opcode = (Opcode)(header[0] & 0x0f); + var opcode = (Opcode)(header[0] & 0x0f); // MASK - Mask mask = (header[1] & 0x80) == 0x80 ? Mask.MASK : Mask.UNMASK; + var mask = (header[1] & 0x80) == 0x80 ? Mask.MASK : Mask.UNMASK; // Payload len - byte payloadLen = (byte)(header[1] & 0x7f); + var payloadLen = (byte)(header[1] & 0x7f); - return new WsFrame { + if (isControl(opcode) && payloadLen > 125) + throw new WebSocketException(CloseStatusCode.INCONSISTENT_DATA, + "The payload length of a control frame must be 125 bytes or less."); + + var frame = new WsFrame { Fin = fin, Rsv1 = rsv1, Rsv2 = rsv2, @@ -453,87 +415,109 @@ namespace WebSocketSharp { Mask = mask, PayloadLen = payloadLen }; - } - private static void readMaskingKey(Stream stream, WsFrame frame) - { - if (!isMasked(frame.Mask)) - { - frame.MaskingKey = new byte[]{}; - return; - } + /* Extended Payload Length */ - var maskingKey = stream.ReadBytes(4); - if (maskingKey.Length != 4) - throw new IOException(); + var extLen = payloadLen < 126 + ? 0 + : payloadLen == 126 + ? 2 + : 8; - frame.MaskingKey = maskingKey; - } + var extPayloadLen = extLen > 0 + ? stream.ReadBytesInternal(extLen) + : new byte[]{}; - private static void readPayloadData(Stream stream, WsFrame frame, bool unmask) - { - ulong length = frame.PayloadLen <= 125 - ? frame.PayloadLen - : frame.PayloadLen == 126 - ? frame.ExtPayloadLen.To(ByteOrder.BIG) - : frame.ExtPayloadLen.To(ByteOrder.BIG); + if (extLen > 0 && extPayloadLen.Length != extLen) + throw new IOException(); - if (length == 0) - { - frame.PayloadData = new PayloadData(); - return; - } + frame.ExtPayloadLen = extPayloadLen; - if (frame.PayloadLen > 126 && length > PayloadData.MaxLength) - throw new WebSocketException(CloseStatusCode.TOO_BIG); + /* Masking Key */ - var buffer = length <= (ulong)_readBufferLen - ? stream.ReadBytes((int)length) - : stream.ReadBytes((long)length, _readBufferLen); + var masked = mask == Mask.MASK ? true : false; + var maskingKey = masked + ? stream.ReadBytesInternal(4) + : new byte[]{}; - if (buffer.LongLength != (long)length) + if (masked && maskingKey.Length != 4) throw new IOException(); - var masked = isMasked(frame.Mask); - var payloadData = masked - ? new PayloadData(buffer, true) - : new PayloadData(buffer); + frame.MaskingKey = maskingKey; + + /* Payload Data */ + + ulong dataLen = payloadLen < 126 + ? payloadLen + : payloadLen == 126 + ? extPayloadLen.To(ByteOrder.BIG) + : extPayloadLen.To(ByteOrder.BIG); + + byte[] data = null; + if (dataLen > 0) + { + if (payloadLen > 126 && dataLen > PayloadData.MaxLength) + throw new WebSocketException(CloseStatusCode.TOO_BIG); + + data = dataLen > 1024 + ? stream.ReadBytesInternal((long)dataLen, 1024) + : stream.ReadBytesInternal((int)dataLen); + if (data.LongLength != (long)dataLen) + throw new IOException(); + } + else + { + data = new byte[]{}; + } + + var payloadData = new PayloadData(data, masked); if (masked && unmask) { - payloadData.Mask(frame.MaskingKey); + payloadData.Mask(maskingKey); frame.Mask = Mask.UNMASK; frame.MaskingKey = new byte[]{}; } frame.PayloadData = payloadData; + return frame; } - private void setPayloadLen(ulong length) + private static void print(WsFrame frame) { - if (length < 126) - { - PayloadLen = (byte)length; - ExtPayloadLen = new byte[]{}; - return; - } + var len = frame.ExtPayloadLen.Length; + var extPayloadLen = len == 2 + ? frame.ExtPayloadLen.To(ByteOrder.BIG).ToString() + : len == 8 + ? frame.ExtPayloadLen.To(ByteOrder.BIG).ToString() + : String.Empty; - if (length < 0x010000) - { - PayloadLen = (byte)126; - ExtPayloadLen = ((ushort)length).ToByteArray(ByteOrder.BIG); - return; - } + var masked = frame.IsMasked; + var maskingKey = masked + ? BitConverter.ToString(frame.MaskingKey) + : String.Empty; - PayloadLen = (byte)127; - ExtPayloadLen = length.ToByteArray(ByteOrder.BIG); - } + var opcode = frame.Opcode; + var payloadData = frame.PayloadData.Length == 0 + ? String.Empty + : masked || frame.IsFragmented || frame.IsBinary || frame.IsClose + ? BitConverter.ToString(frame.PayloadData.ToByteArray()) + : Encoding.UTF8.GetString(frame.PayloadData.ToByteArray()); - private void unmaskPayloadData() - { - PayloadData.Mask(MaskingKey); - Mask = Mask.UNMASK; - MaskingKey = new byte[]{}; + var format = @" + FIN: {0} + RSV1: {1} + RSV2: {2} + RSV3: {3} + Opcode: {4} + MASK: {5} + Payload Len: {6} + Extended Payload Len: {7} + Masking Key: {8} + Payload Data: {9}"; + + Console.WriteLine( + format, frame.Fin, frame.Rsv1, frame.Rsv2, frame.Rsv3, opcode, frame.Mask, frame.PayloadLen, extPayloadLen, maskingKey, payloadData); } #endregion @@ -574,12 +558,16 @@ namespace WebSocketSharp { WsFrame frame = null; try { - var header = stream.ReadBytes(2); + var header = stream.ReadBytesInternal(2); frame = parse(header, stream, unmask); } + catch (WebSocketException ex) + { + frame = createCloseFrame(ex); + } catch (Exception ex) { - if (!error.IsNull()) + if (error != null) error(ex); } @@ -605,25 +593,27 @@ namespace WebSocketSharp { WsFrame frame = null; try { - int readLen = stream.EndRead(ar); - if (readLen != 2) + var readLen = stream.EndRead(ar); + if (readLen > 0) { if (readLen == 1) header[1] = (byte)stream.ReadByte(); - else - header = null; - } - frame = parse(header, stream, unmask); + frame = parse(header, stream, unmask); + } + } + catch (WebSocketException ex) + { + frame = createCloseFrame(ex); } catch (Exception ex) { - if (!error.IsNull()) + if (error != null) error(ex); } finally { - if (!completed.IsNull()) + if (completed != null) completed(frame); } }; @@ -641,27 +631,35 @@ namespace WebSocketSharp { public byte[] ToByteArray() { - var buffer = new List(); - - int header = (int)Fin; - header = (header << 1) + (int)Rsv1; - header = (header << 1) + (int)Rsv2; - header = (header << 1) + (int)Rsv3; - header = (header << 4) + (int)Opcode; - header = (header << 1) + (int)Mask; - header = (header << 7) + (int)PayloadLen; - buffer.AddRange(((ushort)header).ToByteArray(ByteOrder.BIG)); - - if (PayloadLen >= 126) - buffer.AddRange(ExtPayloadLen); - - if (IsMasked) - buffer.AddRange(MaskingKey); - - if (PayloadLen > 0) - buffer.AddRange(PayloadData.ToByteArray()); + using (var buffer = new MemoryStream()) + { + int header = (int)Fin; + header = (header << 1) + (int)Rsv1; + header = (header << 1) + (int)Rsv2; + header = (header << 1) + (int)Rsv3; + header = (header << 4) + (int)Opcode; + header = (header << 1) + (int)Mask; + header = (header << 7) + (int)PayloadLen; + buffer.Write(((ushort)header).ToByteArray(ByteOrder.BIG), 0, 2); + + if (PayloadLen > 125) + buffer.Write(ExtPayloadLen, 0, ExtPayloadLen.Length); + + if (Mask == Mask.MASK) + buffer.Write(MaskingKey, 0, MaskingKey.Length); + + if (PayloadLen > 0) + { + var payload = PayloadData.ToByteArray(); + if (PayloadLen < 127) + buffer.Write(payload, 0, payload.Length); + else + buffer.WriteBytes(payload); + } - return buffer.ToArray(); + buffer.Close(); + return buffer.ToArray(); + } } public override string ToString() diff --git a/websocket-sharp/bin/Debug/websocket-sharp.dll b/websocket-sharp/bin/Debug/websocket-sharp.dll index 2d16b542..e6348c11 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_Ubuntu/websocket-sharp.dll b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll index 9175caf0..64c64cd5 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/Release/websocket-sharp.dll b/websocket-sharp/bin/Release/websocket-sharp.dll index a138726c..d0a30ed2 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 73b88546..52ee834c 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/bin/Release_Ubuntu/websocket-sharp.xml b/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.xml index af000dc0..a3b68197 100644 --- a/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.xml +++ b/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.xml @@ -383,23 +383,6 @@ A to test. - - - Determines whether two specified objects don't have the same value. - - - true if the value of parameter isn't the same as the value of parameter; otherwise, false. - - - The first to compare. - - - The second to compare. - - - A that indicates a case-sensitive or insensitive comparison. (true indicates a case-insensitive comparison.) - - Reads a block of bytes from the specified @@ -416,20 +399,6 @@ - - Reads a block of bytes from the specified stream and returns the read data in an array of . - - - An array of that receives the read data. - - - A that contains the data to read. - - - A that contains the number of bytes to read. - - - Reads a block of bytes from the specified and returns the read data in an array of . @@ -443,9 +412,6 @@ A that contains the number of bytes to read. - - An that contains the buffer size in bytes of each internal read. - diff --git a/websocket-sharp/doc/html/WebSocketSharp/Ext.html b/websocket-sharp/doc/html/WebSocketSharp/Ext.html index 1c09233f..516e2311 100644 --- a/websocket-sharp/doc/html/WebSocketSharp/Ext.html +++ b/websocket-sharp/doc/html/WebSocketSharp/Ext.html @@ -512,17 +512,6 @@ MaybeUri (this string) : bool
Determines whether the specified string is a URI string. -
- - - -
static
- - - - NotEqual - (this string, string, bool) : bool
- Determines whether two specified string objects don't have the same value.
@@ -545,17 +534,6 @@ ReadBytes (this System.IO.Stream, long) : byte[]
- Reads a block of bytes from the specified stream and returns the read data in an array of byte. -
- - - -
static
- - - - ReadBytes - (this System.IO.Stream, long, int) : byte[]
Reads a block of bytes from the specified System.IO.Stream and returns the read data in an array of byte.
@@ -1778,49 +1756,6 @@ Namespace: WebSocketSharp
Assembly: websocket-sharp (in websocket-sharp.dll)
-

NotEqual Method

-
-

- Determines whether two specified string objects don't have the same value. -

-

Syntax

-
public static bool NotEqual (this string expected, string actual, bool ignoreCase)
-

Parameters

-
-
-
- expected -
-
- The first string to compare. -
-
- actual -
-
- The second string to compare. -
-
- ignoreCase -
-
- A bool that indicates a case-sensitive or insensitive comparison. (true indicates a case-insensitive comparison.) -
-
-
-

Returns

-
- true if the value of expected parameter isn't the same as the value of actual parameter; otherwise, false. -
-

Remarks

-
- Documentation for this section has not yet been entered. -
-

Requirements

-
- Namespace: WebSocketSharp
Assembly: websocket-sharp (in websocket-sharp.dll)
-
-

ReadBytes Method

@@ -1862,7 +1797,8 @@

ReadBytes Method

- Reads a block of bytes from the specified stream and returns the read data in an array of byte. + Reads a block of bytes from the specified System.IO.Stream + and returns the read data in an array of byte.

Syntax

public static byte[] ReadBytes (this System.IO.Stream stream, long length)
@@ -1896,50 +1832,6 @@ Namespace: WebSocketSharp
Assembly: websocket-sharp (in websocket-sharp.dll)
-

ReadBytes Method

-
-

- Reads a block of bytes from the specified System.IO.Stream - and returns the read data in an array of byte. -

-

Syntax

-
public static byte[] ReadBytes (this System.IO.Stream stream, long length, int bufferLength)
-

Parameters

-
-
-
- stream -
-
- A System.IO.Stream that contains the data to read. -
-
- length -
-
- A long that contains the number of bytes to read. -
-
- bufferLength -
-
- An int that contains the buffer size in bytes of each internal read. -
-
-
-

Returns

-
- An array of byte that receives the read data. -
-

Remarks

-
- Documentation for this section has not yet been entered. -
-

Requirements

-
- Namespace: WebSocketSharp
Assembly: websocket-sharp (in websocket-sharp.dll)
-
-

SubArray<T> Generic Method

diff --git a/websocket-sharp/doc/mdoc/WebSocketSharp/Ext.xml b/websocket-sharp/doc/mdoc/WebSocketSharp/Ext.xml index 1f46727d..95f968ba 100644 --- a/websocket-sharp/doc/mdoc/WebSocketSharp/Ext.xml +++ b/websocket-sharp/doc/mdoc/WebSocketSharp/Ext.xml @@ -721,37 +721,6 @@ To be added. - - - - Method - - System.Boolean - - - - - - - - - The first to compare. - - - The second to compare. - - - A that indicates a case-sensitive or insensitive comparison. (true indicates a case-insensitive comparison.) - -

- Determines whether two specified objects don't have the same value. - - - true if the value of parameter isn't the same as the value of parameter; otherwise, false. - - To be added. - - @@ -798,37 +767,6 @@ A that contains the number of bytes to read. - - Reads a block of bytes from the specified stream and returns the read data in an array of . - - - An array of that receives the read data. - - To be added. - - - - - - Method - - System.Byte[] - - - - - - - - - A that contains the data to read. - - - A that contains the number of bytes to read. - - - An that contains the buffer size in bytes of each internal read. - Reads a block of bytes from the specified and returns the read data in an array of . diff --git a/websocket-sharp/doc/mdoc/index.xml b/websocket-sharp/doc/mdoc/index.xml index d40954e5..8de50d22 100644 --- a/websocket-sharp/doc/mdoc/index.xml +++ b/websocket-sharp/doc/mdoc/index.xml @@ -1,6 +1,6 @@ - + [00 24 00 00 04 80 00 00 94 00 00 00 06 02 00 00 00 24 00 00 52 53 41 31 00 04 00 00 11 00 00 00 29 17 fb 89 fe c3 91 f7 2b cb 8b e2 61 d2 3f 05 93 6d 65 a8 9e 63 72 a6 f5 d5 2c f2 9d 20 fa 0b c0 70 6a f6 88 7e 8b 90 3f 39 f5 76 c8 48 e0 bb 7b b2 7b ed d3 10 a7 1a 0f 70 98 0f 7f f4 4b 53 09 d2 a5 ef 36 c3 56 b4 aa f0 91 72 63 25 07 89 e0 93 3e 3f 2e f2 b9 73 0e 12 15 5d 43 56 c3 f4 70 a5 89 fe f7 f6 ac 3e 77 c2 d8 d0 84 91 f4 0c d1 f3 8e dc c3 c3 b8 38 3d 0c bf 17 de 20 78 c1 ] @@ -810,39 +810,6 @@ - - - - - - - - ExtensionMethod - - System.Boolean - - - - - - - - - The first to compare. - - - The second to compare. - - - A that indicates a case-sensitive or insensitive comparison. (true indicates a case-insensitive comparison.) - - - Determines whether two specified objects don't have the same value. - - - - - @@ -895,45 +862,12 @@ A that contains the number of bytes to read. - - Reads a block of bytes from the specified stream and returns the read data in an array of . - - - - - - - - - - - - - ExtensionMethod - - System.Byte[] - - - - - - - - - A that contains the data to read. - - - A that contains the number of bytes to read. - - - An that contains the buffer size in bytes of each internal read. - Reads a block of bytes from the specified and returns the read data in an array of . - +