diff --git a/Example/bin/Debug_Ubuntu/example.exe b/Example/bin/Debug_Ubuntu/example.exe index 3b547c25..35e5f224 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 38b6a4aa..4edc2629 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 43441283..342ac8b5 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 9e89f3a4..fa0d2aeb 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/Example1/AudioStreamer.cs b/Example1/AudioStreamer.cs index 636e0d1e..5b81cca9 100644 --- a/Example1/AudioStreamer.cs +++ b/Example1/AudioStreamer.cs @@ -160,15 +160,15 @@ namespace Example1 int ch_num = buffer_array.GetLength(0); int buffer_length = buffer_array.GetLength(1); - msg.AddRange(user_id.ToBytes(ByteOrder.BIG)); + msg.AddRange(user_id.ToByteArray(ByteOrder.BIG)); msg.Add((byte)ch_num); - msg.AddRange(((uint)buffer_length).ToBytes(ByteOrder.BIG)); + msg.AddRange(((uint)buffer_length).ToByteArray(ByteOrder.BIG)); ch_num.Times(i => { buffer_length.Times(j => { - msg.AddRange(buffer_array[i, j].ToBytes(ByteOrder.BIG)); + msg.AddRange(buffer_array[i, j].ToByteArray(ByteOrder.BIG)); }); }); diff --git a/Example1/Example1.pidb b/Example1/Example1.pidb index 308af6ce..4bb84f9d 100644 Binary files a/Example1/Example1.pidb and b/Example1/Example1.pidb differ diff --git a/Example1/bin/Debug_Ubuntu/example1.exe b/Example1/bin/Debug_Ubuntu/example1.exe index eadb0196..9d82bead 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 bcb1157b..0805fc90 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 43441283..342ac8b5 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 9e89f3a4..fa0d2aeb 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/Example2/bin/Debug_Ubuntu/example2.exe b/Example2/bin/Debug_Ubuntu/example2.exe index 266e8406..8367bae8 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 0f7a6653..8c0c53b2 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 43441283..342ac8b5 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 9e89f3a4..fa0d2aeb 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/Example3/bin/Debug_Ubuntu/Example3.exe b/Example3/bin/Debug_Ubuntu/Example3.exe index 0292bbb1..e90c9ae0 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 901e5095..f62ec323 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 43441283..342ac8b5 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 9e89f3a4..fa0d2aeb 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/websocket-sharp/Ext.cs b/websocket-sharp/Ext.cs index a231cd89..3a55b2a4 100644 --- a/websocket-sharp/Ext.cs +++ b/websocket-sharp/Ext.cs @@ -1,4 +1,4 @@ -#region MIT License +#region License /* * Ext.cs * IsPredefinedScheme and MaybeUri methods derived from System.Uri.cs @@ -55,7 +55,7 @@ namespace WebSocketSharp { /// public static class Ext { - #region Field + #region Private Const Fields private const string _tspecials = "()<>@,;:\\\"/[]?={} \t"; @@ -71,7 +71,7 @@ namespace WebSocketSharp { #endregion - #region Internal Method + #region Internal Methods internal static string GetNameInternal(this string nameAndValue, string separator) { @@ -1202,7 +1202,7 @@ namespace WebSocketSharp { /// /// The type of the . The T must be a value type. /// - public static byte[] ToBytes(this T value, ByteOrder order) + public static byte[] ToByteArray(this T value, ByteOrder order) where T : struct { var type = typeof(T); diff --git a/websocket-sharp/Opcode.cs b/websocket-sharp/Opcode.cs index 8964b4a1..335dd6dd 100644 --- a/websocket-sharp/Opcode.cs +++ b/websocket-sharp/Opcode.cs @@ -1,4 +1,4 @@ -#region MIT License +#region License /* * Opcode.cs * @@ -43,11 +43,11 @@ namespace WebSocketSharp { /// /// Equivalent to numeric value 0. Indicates a continuation frame. /// - CONT = 0x0, + CONT = 0x0, /// /// Equivalent to numeric value 1. Indicates a text frame. /// - TEXT = 0x1, + TEXT = 0x1, /// /// Equivalent to numeric value 2. Indicates a binary frame. /// @@ -55,14 +55,14 @@ namespace WebSocketSharp { /// /// Equivalent to numeric value 8. Indicates a connection close frame. /// - CLOSE = 0x8, + CLOSE = 0x8, /// /// Equivalent to numeric value 9. Indicates a ping frame. /// - PING = 0x9, + PING = 0x9, /// /// Equivalent to numeric value 10. Indicates a pong frame. /// - PONG = 0xa + PONG = 0xa } } diff --git a/websocket-sharp/PayloadData.cs b/websocket-sharp/PayloadData.cs index b83233f5..dd2d48c3 100644 --- a/websocket-sharp/PayloadData.cs +++ b/websocket-sharp/PayloadData.cs @@ -106,21 +106,21 @@ namespace WebSocketSharp { } } + internal bool IsMasked { get; private set; } + + internal ulong Length { + get { + return (ulong)(ExtensionData.LongLength + ApplicationData.LongLength); + } + } + #endregion #region Public Properties - public byte[] ApplicationData { get; private set; } - public byte[] ExtensionData { get; private set; } - public bool IsMasked { get; private set; } - - public ulong Length { - get { - return (ulong)(ExtensionData.LongLength + ApplicationData.LongLength); - } - } + public byte[] ApplicationData { get; private set; } #endregion @@ -164,7 +164,9 @@ namespace WebSocketSharp { public byte[] ToByteArray() { - return this.ToArray(); + return ExtensionData.LongLength > 0 + ? this.ToArray() + : ApplicationData; } public override string ToString() diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index a1584c24..01162a7f 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -408,7 +408,7 @@ namespace WebSocketSharp { private void close(ushort code, string reason) { - var data = new List(code.ToBytes(ByteOrder.BIG)); + var data = new List(code.ToByteArray(ByteOrder.BIG)); if (!reason.IsNullOrEmpty()) { var buffer = Encoding.UTF8.GetBytes(reason); diff --git a/websocket-sharp/WsFrame.cs b/websocket-sharp/WsFrame.cs index 17b5f5db..510ff41a 100644 --- a/websocket-sharp/WsFrame.cs +++ b/websocket-sharp/WsFrame.cs @@ -36,7 +36,7 @@ namespace WebSocketSharp { internal class WsFrame : IEnumerable { - #region Fields + #region Private Const Fields private const int _readBufferLen = 1024; @@ -46,11 +46,6 @@ namespace WebSocketSharp { private WsFrame() { - Rsv1 = Rsv.OFF; - Rsv2 = Rsv.OFF; - Rsv3 = Rsv.OFF; - ExtPayloadLen = new byte[]{}; - MaskingKey = new byte[]{}; } #endregion @@ -68,11 +63,20 @@ namespace WebSocketSharp { } public WsFrame(Fin fin, Opcode opcode, Mask mask, PayloadData payloadData) - : this() { - Fin = fin; - Opcode = opcode; - Masked = mask; + 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."); + + Fin = fin; + Opcode = opcode; + Mask = mask; PayloadData = payloadData; init(); @@ -80,57 +84,174 @@ namespace WebSocketSharp { #endregion - #region Properties + #region Internal Properties + + internal bool IsControl { + get { + return isControl(Opcode); + } + } - public Fin Fin { get; private set; } - public Rsv Rsv1 { get; private set; } - public Rsv Rsv2 { get; private set; } - public Rsv Rsv3 { get; private set; } - public Opcode Opcode { get; private set; } - public Mask Masked { get; private set; } - public byte PayloadLen { get; private set; } - public byte[] ExtPayloadLen { get; private set; } - public byte[] MaskingKey { get; private set; } - public PayloadData PayloadData { get; private set; } + internal bool IsData { + get { + return isData(Opcode); + } + } - public bool IsData { + internal bool IsFinal { get { - Opcode data = Opcode.TEXT | Opcode.BINARY; - return (data & Opcode) == Opcode; + return isFinal(Fin); } } - public ulong Length { + internal bool IsMasked { get { - return 2 + (ulong)(ExtPayloadLen.Length + MaskingKey.Length) + PayloadLength; + return isMasked(Mask); } } - public ulong PayloadLength { + internal ulong Length { get { - return PayloadData.Length; + return 2 + (ulong)(ExtPayloadLen.Length + MaskingKey.Length) + PayloadData.Length; } } #endregion + #region Public Properties + + public Fin Fin { get; private set; } + + public Rsv Rsv1 { get; private set; } + + public Rsv Rsv2 { get; private set; } + + public Rsv Rsv3 { get; private set; } + + public Opcode Opcode { get; private set; } + + public Mask Mask { get; private set; } + + public byte PayloadLen { get; private set; } + + public byte[] ExtPayloadLen { get; private set; } + + public byte[] MaskingKey { get; private set; } + + public PayloadData PayloadData { get; private set; } + + #endregion + #region Private Methods - IEnumerator IEnumerable.GetEnumerator() + private static void dump(WsFrame frame) { - return GetEnumerator(); + var len = frame.Length; + var count = (long)(len / 4); + var remainder = (int)(len % 4); + + int countDigit; + string countFmt; + if (count < 10000) + { + countDigit = 4; + countFmt = "{0,4}"; + } + else if (count < 0x010000) + { + countDigit = 4; + countFmt = "{0,4:X}"; + } + else if (count < 0x0100000000) + { + countDigit = 8; + countFmt = "{0,8:X}"; + } + else + { + countDigit = 16; + countFmt = "{0,16:X}"; + } + + var spFmt = String.Format("{{0,{0}}}", countDigit); + var headerFmt = String.Format(@" + {0} 01234567 89ABCDEF 01234567 89ABCDEF + {0}+--------+--------+--------+--------+", spFmt); + var footerFmt = String.Format(" {0}+--------+--------+--------+--------+", spFmt); + + Func> linePrinter = lineNumberFmt => + { + long lineCount = 0; + string lineFmt = String.Format(" {0}|{{1,8}} {{2,8}} {{3,8}} {{4,8}}|", lineNumberFmt); + return (arg1, arg2, arg3, arg4) => + { + Console.WriteLine(lineFmt, ++lineCount, arg1, arg2, arg3, arg4); + }; + }; + var printLine = linePrinter(countFmt); + + Console.WriteLine(headerFmt, String.Empty); + + var buffer = frame.ToByteArray(); + int i, j; + for (i = 0; i <= count; i++) + { + j = i * 4; + if (i < count) + printLine( + Convert.ToString(buffer[j], 2).PadLeft(8, '0'), + Convert.ToString(buffer[j + 1], 2).PadLeft(8, '0'), + Convert.ToString(buffer[j + 2], 2).PadLeft(8, '0'), + Convert.ToString(buffer[j + 3], 2).PadLeft(8, '0')); + else if (remainder > 0) + printLine( + Convert.ToString(buffer[j], 2).PadLeft(8, '0'), + remainder >= 2 ? Convert.ToString(buffer[j + 1], 2).PadLeft(8, '0') : String.Empty, + remainder == 3 ? Convert.ToString(buffer[j + 2], 2).PadLeft(8, '0') : String.Empty, + String.Empty); + } + + Console.WriteLine(footerFmt, String.Empty); } private void init() { - setPayloadLen(PayloadLength); - if (Masked == Mask.MASK) + Rsv1 = Rsv.OFF; + Rsv2 = Rsv.OFF; + Rsv3 = Rsv.OFF; + + setPayloadLen(PayloadData.Length); + if (IsMasked) maskPayloadData(); + else + MaskingKey = new byte[]{}; + } + + private static bool isControl(Opcode opcode) + { + Opcode control = Opcode.CLOSE | Opcode.PING | Opcode.PONG; + return (control & opcode) == opcode; + } + + private static bool isData(Opcode opcode) + { + Opcode data = Opcode.TEXT | Opcode.BINARY; + return (data & opcode) == opcode; + } + + private static bool isFinal(Fin fin) + { + return fin == Fin.FINAL; + } + + private static bool isMasked(Mask mask) + { + return mask == Mask.MASK; } private void maskPayloadData() { - var key = new byte[4]; + var key = new byte[4]; var rand = new Random(); rand.NextBytes(key); @@ -138,47 +259,75 @@ namespace WebSocketSharp { PayloadData.Mask(key); } - private static WsFrame parse(Stream stream, bool unmask) - { - return parse(stream.ReadBytes(2), stream, unmask); - } - private static WsFrame parse(byte[] header, Stream stream, bool unmask) { if (header.IsNull() || header.Length != 2) return null; - try - { - var frame = readHeader(header); - readExtPayloadLen(stream, frame); - readMaskingKey(stream, frame); - readPayloadData(stream, frame, unmask); + var frame = readHeader(header); + readExtPayloadLen(stream, frame); + readMaskingKey(stream, frame); + readPayloadData(stream, frame, unmask); - return frame; - } - catch - { - return null; - } + 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 || ((Opcode.CONT | Opcode.BINARY | Opcode.CLOSE) & opcode) == opcode + ? 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) { - var length = frame.PayloadLen <= 125 + int length = frame.PayloadLen <= 125 ? 0 : frame.PayloadLen == 126 ? 2 : 8; if (length == 0) + { + frame.ExtPayloadLen = new byte[]{}; return; + } - var extLen = stream.ReadBytes(length); - if (extLen.Length != length) + var extPayloadLen = stream.ReadBytes(length); + if (extPayloadLen.Length != length) throw new IOException(); - frame.ExtPayloadLen = extLen; + frame.ExtPayloadLen = extPayloadLen; } private static WsFrame readHeader(byte[] header) @@ -194,25 +343,28 @@ namespace WebSocketSharp { // Opcode Opcode opcode = (Opcode)(header[0] & 0x0f); // MASK - Mask masked = (header[1] & 0x80) == 0x80 ? Mask.MASK : Mask.UNMASK; + Mask mask = (header[1] & 0x80) == 0x80 ? Mask.MASK : Mask.UNMASK; // Payload len byte payloadLen = (byte)(header[1] & 0x7f); return new WsFrame { - Fin = fin, - Rsv1 = rsv1, - Rsv2 = rsv2, - Rsv3 = rsv3, - Opcode = opcode, - Masked = masked, + Fin = fin, + Rsv1 = rsv1, + Rsv2 = rsv2, + Rsv3 = rsv3, + Opcode = opcode, + Mask = mask, PayloadLen = payloadLen }; } private static void readMaskingKey(Stream stream, WsFrame frame) { - if (frame.Masked == Mask.UNMASK) + if (!isMasked(frame.Mask)) + { + frame.MaskingKey = new byte[]{}; return; + } var maskingKey = stream.ReadBytes(4); if (maskingKey.Length != 4) @@ -231,7 +383,7 @@ namespace WebSocketSharp { if (length == 0) { - frame.PayloadData = new PayloadData(new byte[]{}); + frame.PayloadData = new PayloadData(); return; } @@ -245,14 +397,15 @@ namespace WebSocketSharp { if (buffer.LongLength != (long)length) throw new IOException(); - var payloadData = frame.Masked == Mask.MASK + var masked = isMasked(frame.Mask); + var payloadData = masked ? new PayloadData(buffer, true) : new PayloadData(buffer); - if (frame.Masked == Mask.MASK && unmask) + if (masked && unmask) { payloadData.Mask(frame.MaskingKey); - frame.Masked = Mask.UNMASK; + frame.Mask = Mask.UNMASK; frame.MaskingKey = new byte[]{}; } @@ -264,18 +417,19 @@ namespace WebSocketSharp { if (length < 126) { PayloadLen = (byte)length; + ExtPayloadLen = new byte[]{}; return; } if (length < 0x010000) { - PayloadLen = (byte)126; - ExtPayloadLen = ((ushort)length).ToBytes(ByteOrder.BIG); + PayloadLen = (byte)126; + ExtPayloadLen = ((ushort)length).ToByteArray(ByteOrder.BIG); return; } - PayloadLen = (byte)127; - ExtPayloadLen = length.ToBytes(ByteOrder.BIG); + PayloadLen = (byte)127; + ExtPayloadLen = length.ToByteArray(ByteOrder.BIG); } #endregion @@ -284,7 +438,7 @@ namespace WebSocketSharp { public IEnumerator GetEnumerator() { - foreach (byte b in ToBytes()) + foreach (byte b in ToByteArray()) yield return b; } @@ -293,6 +447,11 @@ namespace WebSocketSharp { return Parse(src, true); } + public static WsFrame Parse(Stream stream) + { + return Parse(stream, true); + } + public static WsFrame Parse(byte[] src, bool unmask) { using (MemoryStream ms = new MemoryStream(src)) @@ -301,14 +460,26 @@ namespace WebSocketSharp { } } - public static WsFrame Parse(Stream stream) + public static WsFrame Parse(Stream stream, bool unmask) { - return Parse(stream, true); + return Parse(stream, unmask, null); } - public static WsFrame Parse(Stream stream, bool unmask) + public static WsFrame Parse(Stream stream, bool unmask, Action error) { - return parse(stream, unmask); + WsFrame frame = null; + try + { + var header = stream.ReadBytes(2); + frame = parse(header, stream, unmask); + } + catch (Exception ex) + { + if (!error.IsNull()) + error(ex); + } + + return frame; } public static void ParseAsync(Stream stream, Action completed) @@ -356,138 +527,31 @@ namespace WebSocketSharp { stream.BeginRead(header, 0, 2, callback, null); } - public void Print() + public void Print(bool dumped) { - byte[] buffer; - long count, i, j; - int countDigit, remainder; - string countFmt, extPayloadLen, headerFmt, topLineFmt, bottomLineFmt, payloadData, spFmt; - - switch (ExtPayloadLen.Length) - { - case 2: - extPayloadLen = ExtPayloadLen.To(ByteOrder.BIG).ToString(); - break; - case 8: - extPayloadLen = ExtPayloadLen.To(ByteOrder.BIG).ToString(); - break; - default: - extPayloadLen = String.Empty; - break; - } - - if (((Opcode.TEXT | Opcode.PING | Opcode.PONG) & Opcode) == Opcode && - Masked == Mask.UNMASK && - PayloadLength > 0) - { - payloadData = Encoding.UTF8.GetString(PayloadData.ToByteArray()); - } - else - { - payloadData = BitConverter.ToString(PayloadData.ToByteArray()); - } - - headerFmt = @" - WsFrame: - - 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}"; - - buffer = ToBytes(); - count = (long)(Length / 4); - remainder = (int)(Length % 4); - - if (count < 10000) - { - countDigit = 4; - countFmt = "{0,4}"; - } - else if (count < 0x010000) - { - countDigit = 4; - countFmt = "{0,4:X}"; - } - else if (count < 0x0100000000) - { - countDigit = 8; - countFmt = "{0,8:X}"; - } + if (dumped) + dump(this); else - { - countDigit = 16; - countFmt = "{0,16:X}"; - } - - spFmt = String.Format("{{0,{0}}}", countDigit); - - topLineFmt = String.Format(@" - {0} 01234567 89ABCDEF 01234567 89ABCDEF - {0}+--------+--------+--------+--------+", spFmt); - - Func> func = s => - { - long lineCount = 0; - string lineFmt = String.Format(" {0}|{{1,8}} {{2,8}} {{3,8}} {{4,8}}|", s); - return (arg1, arg2, arg3, arg4) => - { - Console.WriteLine(lineFmt, ++lineCount, arg1, arg2, arg3, arg4); - }; - }; - var printLine = func(countFmt); - - bottomLineFmt = String.Format(" {0}+--------+--------+--------+--------+", spFmt); - - Console.WriteLine(headerFmt, - Fin, Rsv1, Rsv2, Rsv3, Opcode, - Masked, PayloadLen, extPayloadLen, - BitConverter.ToString(MaskingKey), - payloadData); - - Console.WriteLine(topLineFmt, String.Empty); - - for (i = 0; i <= count; i++) - { - j = i * 4; - if (i < count) - { - printLine( - Convert.ToString(buffer[j], 2).PadLeft(8, '0'), - Convert.ToString(buffer[j + 1], 2).PadLeft(8, '0'), - Convert.ToString(buffer[j + 2], 2).PadLeft(8, '0'), - Convert.ToString(buffer[j + 3], 2).PadLeft(8, '0')); - } - else if (i == count && remainder > 0) - { - printLine( - Convert.ToString(buffer[j], 2).PadLeft(8, '0'), - remainder >= 2 ? Convert.ToString(buffer[j + 1], 2).PadLeft(8, '0') : String.Empty, - remainder == 3 ? Convert.ToString(buffer[j + 2], 2).PadLeft(8, '0') : String.Empty, - String.Empty); - } - } - - Console.WriteLine(bottomLineFmt, String.Empty); + print(this); } - public byte[] ToBytes() + public byte[] ToByteArray() { var buffer = new List(); - var header = (int)Fin; + 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)Masked; + header = (header << 1) + (int)Mask; header = (header << 7) + (int)PayloadLen; - buffer.AddRange(((ushort)header).ToBytes(ByteOrder.BIG)); + buffer.AddRange(((ushort)header).ToByteArray(ByteOrder.BIG)); if (PayloadLen >= 126) buffer.AddRange(ExtPayloadLen); - if (Masked == Mask.MASK) + if (IsMasked) buffer.AddRange(MaskingKey); if (PayloadLen > 0) @@ -498,7 +562,16 @@ namespace WebSocketSharp { public override string ToString() { - return BitConverter.ToString(ToBytes()); + return BitConverter.ToString(ToByteArray()); + } + + #endregion + + #region Explicitly Implemented Interface Members + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); } #endregion diff --git a/websocket-sharp/WsStream.cs b/websocket-sharp/WsStream.cs index 9111ec59..fb77b117 100644 --- a/websocket-sharp/WsStream.cs +++ b/websocket-sharp/WsStream.cs @@ -1,4 +1,4 @@ -#region MIT License +#region License /* * WsStream.cs * @@ -40,7 +40,7 @@ namespace WebSocketSharp { internal class WsStream : IDisposable { - #region Fields + #region Private Fields private Stream _innerStream; private bool _isSecure; @@ -49,7 +49,7 @@ namespace WebSocketSharp { #endregion - #region Private Constructor + #region Private Constructors private WsStream() { @@ -83,7 +83,7 @@ namespace WebSocketSharp { #endregion - #region Properties + #region Public Properties public bool DataAvailable { get { @@ -251,7 +251,7 @@ namespace WebSocketSharp { public bool Write(WsFrame frame) { - return write(frame.ToBytes()); + return write(frame.ToByteArray()); } public bool Write(Handshake handshake) diff --git a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll index 43441283..342ac8b5 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 9e89f3a4..fa0d2aeb 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_Ubuntu/websocket-sharp.dll b/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll index 996c3e84..b71749fa 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 ce684b8d..68134321 100644 --- a/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.xml +++ b/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.xml @@ -607,7 +607,7 @@ Is thrown when the parameter passed to a method is invalid because it is . - + Converts the specified data to an array of . diff --git a/websocket-sharp/doc/html/WebSocketSharp/Ext.html b/websocket-sharp/doc/html/WebSocketSharp/Ext.html index 1b7b0ab4..fef971b0 100644 --- a/websocket-sharp/doc/html/WebSocketSharp/Ext.html +++ b/websocket-sharp/doc/html/WebSocketSharp/Ext.html @@ -693,7 +693,7 @@ - ToBytes<T> + ToByteArray<T> (this T, ByteOrder) : byte[]
Converts the specified data to an array of byte.
@@ -2431,15 +2431,15 @@ Namespace: WebSocketSharp
Assembly: websocket-sharp (in websocket-sharp.dll)
-

ToBytes<T> Generic Method

-
+

ToByteArray<T> Generic Method

+

Converts the specified data to an array of byte.

Syntax

-
public static byte[] ToBytes<T> (this T value, ByteOrder order)
where T : struct
+
public static byte[] ToByteArray<T> (this T value, ByteOrder order)
where T : struct

Type Parameters

-
+
T @@ -2450,7 +2450,7 @@

Parameters

-
+
value @@ -2467,15 +2467,15 @@

Returns

-
+
An array of byte converted from the value.

Remarks

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

Requirements

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

diff --git a/websocket-sharp/doc/mdoc/WebSocketSharp/Ext.xml b/websocket-sharp/doc/mdoc/WebSocketSharp/Ext.xml index 428bc9a6..f1abe33e 100644 --- a/websocket-sharp/doc/mdoc/WebSocketSharp/Ext.xml +++ b/websocket-sharp/doc/mdoc/WebSocketSharp/Ext.xml @@ -1181,9 +1181,9 @@ - - - + + + Method System.Byte[] diff --git a/websocket-sharp/doc/mdoc/index.xml b/websocket-sharp/doc/mdoc/index.xml index 0aa67e0c..2d5fc3a5 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 ] @@ -1324,9 +1324,9 @@ - - - + + + ExtensionMethod System.Byte[] @@ -1358,7 +1358,7 @@ Converts the specified data to an array of . - + diff --git a/websocket-sharp/websocket-sharp.pidb b/websocket-sharp/websocket-sharp.pidb index b8c17959..5c36af83 100644 Binary files a/websocket-sharp/websocket-sharp.pidb and b/websocket-sharp/websocket-sharp.pidb differ