Fix due to the modified WsFrame.cs
This commit is contained in:
parent
672e48796a
commit
815d12f6a0
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -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));
|
||||
});
|
||||
});
|
||||
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -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 {
|
||||
/// </summary>
|
||||
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 {
|
||||
/// <typeparam name="T">
|
||||
/// The type of the <paramref name="value"/>. The T must be a value type.
|
||||
/// </typeparam>
|
||||
public static byte[] ToBytes<T>(this T value, ByteOrder order)
|
||||
public static byte[] ToByteArray<T>(this T value, ByteOrder order)
|
||||
where T : struct
|
||||
{
|
||||
var type = typeof(T);
|
||||
|
@ -1,4 +1,4 @@
|
||||
#region MIT License
|
||||
#region License
|
||||
/*
|
||||
* Opcode.cs
|
||||
*
|
||||
@ -43,11 +43,11 @@ namespace WebSocketSharp {
|
||||
/// <summary>
|
||||
/// Equivalent to numeric value 0. Indicates a continuation frame.
|
||||
/// </summary>
|
||||
CONT = 0x0,
|
||||
CONT = 0x0,
|
||||
/// <summary>
|
||||
/// Equivalent to numeric value 1. Indicates a text frame.
|
||||
/// </summary>
|
||||
TEXT = 0x1,
|
||||
TEXT = 0x1,
|
||||
/// <summary>
|
||||
/// Equivalent to numeric value 2. Indicates a binary frame.
|
||||
/// </summary>
|
||||
@ -55,14 +55,14 @@ namespace WebSocketSharp {
|
||||
/// <summary>
|
||||
/// Equivalent to numeric value 8. Indicates a connection close frame.
|
||||
/// </summary>
|
||||
CLOSE = 0x8,
|
||||
CLOSE = 0x8,
|
||||
/// <summary>
|
||||
/// Equivalent to numeric value 9. Indicates a ping frame.
|
||||
/// </summary>
|
||||
PING = 0x9,
|
||||
PING = 0x9,
|
||||
/// <summary>
|
||||
/// Equivalent to numeric value 10. Indicates a pong frame.
|
||||
/// </summary>
|
||||
PONG = 0xa
|
||||
PONG = 0xa
|
||||
}
|
||||
}
|
||||
|
@ -106,17 +106,9 @@ namespace WebSocketSharp {
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
internal bool IsMasked { get; private set; }
|
||||
|
||||
#region Public Properties
|
||||
|
||||
public byte[] ApplicationData { get; private set; }
|
||||
|
||||
public byte[] ExtensionData { get; private set; }
|
||||
|
||||
public bool IsMasked { get; private set; }
|
||||
|
||||
public ulong Length {
|
||||
internal ulong Length {
|
||||
get {
|
||||
return (ulong)(ExtensionData.LongLength + ApplicationData.LongLength);
|
||||
}
|
||||
@ -124,6 +116,14 @@ namespace WebSocketSharp {
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Properties
|
||||
|
||||
public byte[] ExtensionData { get; private set; }
|
||||
|
||||
public byte[] ApplicationData { get; private set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private static void mask(byte[] src, byte[] key)
|
||||
@ -164,7 +164,9 @@ namespace WebSocketSharp {
|
||||
|
||||
public byte[] ToByteArray()
|
||||
{
|
||||
return this.ToArray();
|
||||
return ExtensionData.LongLength > 0
|
||||
? this.ToArray()
|
||||
: ApplicationData;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
|
@ -408,7 +408,7 @@ namespace WebSocketSharp {
|
||||
|
||||
private void close(ushort code, string reason)
|
||||
{
|
||||
var data = new List<byte>(code.ToBytes(ByteOrder.BIG));
|
||||
var data = new List<byte>(code.ToByteArray(ByteOrder.BIG));
|
||||
if (!reason.IsNullOrEmpty())
|
||||
{
|
||||
var buffer = Encoding.UTF8.GetBytes(reason);
|
||||
|
@ -36,7 +36,7 @@ namespace WebSocketSharp {
|
||||
|
||||
internal class WsFrame : IEnumerable<byte>
|
||||
{
|
||||
#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
|
||||
|
||||
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; }
|
||||
|
||||
public bool IsData {
|
||||
internal bool IsControl {
|
||||
get {
|
||||
Opcode data = Opcode.TEXT | Opcode.BINARY;
|
||||
return (data & Opcode) == Opcode;
|
||||
return isControl(Opcode);
|
||||
}
|
||||
}
|
||||
|
||||
public ulong Length {
|
||||
internal bool IsData {
|
||||
get {
|
||||
return 2 + (ulong)(ExtPayloadLen.Length + MaskingKey.Length) + PayloadLength;
|
||||
return isData(Opcode);
|
||||
}
|
||||
}
|
||||
|
||||
public ulong PayloadLength {
|
||||
internal bool IsFinal {
|
||||
get {
|
||||
return PayloadData.Length;
|
||||
return isFinal(Fin);
|
||||
}
|
||||
}
|
||||
|
||||
internal bool IsMasked {
|
||||
get {
|
||||
return isMasked(Mask);
|
||||
}
|
||||
}
|
||||
|
||||
internal ulong Length {
|
||||
get {
|
||||
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<string, Action<string, string, string, string>> 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<ushort>(ByteOrder.BIG).ToString()
|
||||
: len == 8
|
||||
? frame.ExtPayloadLen.To<ulong>(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<byte> 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)
|
||||
{
|
||||
return Parse(stream, true);
|
||||
}
|
||||
|
||||
public static WsFrame Parse(Stream stream, bool unmask)
|
||||
{
|
||||
return parse(stream, unmask);
|
||||
return Parse(stream, unmask, null);
|
||||
}
|
||||
|
||||
public static WsFrame Parse(Stream stream, bool unmask, Action<Exception> error)
|
||||
{
|
||||
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<WsFrame> 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<ushort>(ByteOrder.BIG).ToString();
|
||||
break;
|
||||
case 8:
|
||||
extPayloadLen = ExtPayloadLen.To<ulong>(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());
|
||||
}
|
||||
if (dumped)
|
||||
dump(this);
|
||||
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}";
|
||||
}
|
||||
else
|
||||
{
|
||||
countDigit = 16;
|
||||
countFmt = "{0,16:X}";
|
||||
}
|
||||
|
||||
spFmt = String.Format("{{0,{0}}}", countDigit);
|
||||
|
||||
topLineFmt = String.Format(@"
|
||||
{0} 01234567 89ABCDEF 01234567 89ABCDEF
|
||||
{0}+--------+--------+--------+--------+", spFmt);
|
||||
|
||||
Func<string, Action<string, string, string, string>> 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<byte>();
|
||||
|
||||
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
|
||||
|
@ -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)
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -607,7 +607,7 @@
|
||||
Is thrown when the <paramref name="src" /> parameter passed to a method is invalid because it is <see langword="null" />.
|
||||
</exception>
|
||||
</member>
|
||||
<member name="M:WebSocketSharp.Ext.ToBytes``1(``0,WebSocketSharp.ByteOrder)">
|
||||
<member name="M:WebSocketSharp.Ext.ToByteArray``1(``0,WebSocketSharp.ByteOrder)">
|
||||
<summary>
|
||||
Converts the specified data to an array of <see cref="T:System.Byte" />.
|
||||
</summary>
|
||||
|
@ -693,7 +693,7 @@
|
||||
</td>
|
||||
<td colspan="2">
|
||||
<b>
|
||||
<a href="#M:WebSocketSharp.Ext.ToBytes``1(``0,WebSocketSharp.ByteOrder)">ToBytes<T></a>
|
||||
<a href="#M:WebSocketSharp.Ext.ToByteArray``1(``0,WebSocketSharp.ByteOrder)">ToByteArray<T></a>
|
||||
</b>(<i>this</i> <i title="
 The type of the . The T must be a value type.
 ">T</i>, <a href="../WebSocketSharp/ByteOrder.html">ByteOrder</a>)<nobr> : <a href="http://www.go-mono.com/docs/monodoc.ashx?link=T:System.Byte">byte</a>[]</nobr><blockquote>
|
||||
Converts the specified data to an array of <a href="http://www.go-mono.com/docs/monodoc.ashx?link=T:System.Byte">byte</a>.
|
||||
</blockquote></td>
|
||||
@ -2431,15 +2431,15 @@
|
||||
<b>Namespace: </b>WebSocketSharp<br /><b>Assembly: </b>websocket-sharp (in websocket-sharp.dll)</div>
|
||||
<hr size="1" />
|
||||
</blockquote>
|
||||
<h3 id="M:WebSocketSharp.Ext.ToBytes``1(``0,WebSocketSharp.ByteOrder)">ToBytes<T> Generic Method</h3>
|
||||
<blockquote id="M:WebSocketSharp.Ext.ToBytes``1(``0,WebSocketSharp.ByteOrder):member">
|
||||
<h3 id="M:WebSocketSharp.Ext.ToByteArray``1(``0,WebSocketSharp.ByteOrder)">ToByteArray<T> Generic Method</h3>
|
||||
<blockquote id="M:WebSocketSharp.Ext.ToByteArray``1(``0,WebSocketSharp.ByteOrder):member">
|
||||
<p class="Summary">
|
||||
Converts the specified data to an array of <a href="http://www.go-mono.com/docs/monodoc.ashx?link=T:System.Byte">byte</a>.
|
||||
</p>
|
||||
<h2>Syntax</h2>
|
||||
<div class="Signature">public static <a href="http://www.go-mono.com/docs/monodoc.ashx?link=T:System.Byte">byte</a>[] <b>ToBytes<T></b> (<i>this</i> <i title="
 The type of the . The T must be a value type.
 ">T</i> value, <a href="../WebSocketSharp/ByteOrder.html">ByteOrder</a> order)<br /> where T : struct</div>
|
||||
<div class="Signature">public static <a href="http://www.go-mono.com/docs/monodoc.ashx?link=T:System.Byte">byte</a>[] <b>ToByteArray<T></b> (<i>this</i> <i title="
 The type of the . The T must be a value type.
 ">T</i> value, <a href="../WebSocketSharp/ByteOrder.html">ByteOrder</a> order)<br /> where T : struct</div>
|
||||
<h4 class="Subsection">Type Parameters</h4>
|
||||
<blockquote class="SubsectionBox" id="M:WebSocketSharp.Ext.ToBytes``1(``0,WebSocketSharp.ByteOrder):Type Parameters">
|
||||
<blockquote class="SubsectionBox" id="M:WebSocketSharp.Ext.ToByteArray``1(``0,WebSocketSharp.ByteOrder):Type Parameters">
|
||||
<dl>
|
||||
<dt>
|
||||
<i>T</i>
|
||||
@ -2450,7 +2450,7 @@
|
||||
</dl>
|
||||
</blockquote>
|
||||
<h4 class="Subsection">Parameters</h4>
|
||||
<blockquote class="SubsectionBox" id="M:WebSocketSharp.Ext.ToBytes``1(``0,WebSocketSharp.ByteOrder):Parameters">
|
||||
<blockquote class="SubsectionBox" id="M:WebSocketSharp.Ext.ToByteArray``1(``0,WebSocketSharp.ByteOrder):Parameters">
|
||||
<dl>
|
||||
<dt>
|
||||
<i>value</i>
|
||||
@ -2467,15 +2467,15 @@
|
||||
</dl>
|
||||
</blockquote>
|
||||
<h4 class="Subsection">Returns</h4>
|
||||
<blockquote class="SubsectionBox" id="M:WebSocketSharp.Ext.ToBytes``1(``0,WebSocketSharp.ByteOrder):Returns">
|
||||
<blockquote class="SubsectionBox" id="M:WebSocketSharp.Ext.ToByteArray``1(``0,WebSocketSharp.ByteOrder):Returns">
|
||||
An array of <a href="http://www.go-mono.com/docs/monodoc.ashx?link=T:System.Byte">byte</a> converted from the <i>value</i>.
|
||||
</blockquote>
|
||||
<h2 class="Section">Remarks</h2>
|
||||
<div class="SectionBox" id="M:WebSocketSharp.Ext.ToBytes``1(``0,WebSocketSharp.ByteOrder):Remarks">
|
||||
<div class="SectionBox" id="M:WebSocketSharp.Ext.ToByteArray``1(``0,WebSocketSharp.ByteOrder):Remarks">
|
||||
<span class="NotEntered">Documentation for this section has not yet been entered.</span>
|
||||
</div>
|
||||
<h2 class="Section">Requirements</h2>
|
||||
<div class="SectionBox" id="M:WebSocketSharp.Ext.ToBytes``1(``0,WebSocketSharp.ByteOrder):Version Information">
|
||||
<div class="SectionBox" id="M:WebSocketSharp.Ext.ToByteArray``1(``0,WebSocketSharp.ByteOrder):Version Information">
|
||||
<b>Namespace: </b>WebSocketSharp<br /><b>Assembly: </b>websocket-sharp (in websocket-sharp.dll)</div>
|
||||
<hr size="1" />
|
||||
</blockquote>
|
||||
|
@ -1181,9 +1181,9 @@
|
||||
</exception>
|
||||
</Docs>
|
||||
</Member>
|
||||
<Member MemberName="ToBytes<T>">
|
||||
<MemberSignature Language="C#" Value="public static byte[] ToBytes<T> (this T value, WebSocketSharp.ByteOrder order) where T : struct;" />
|
||||
<MemberSignature Language="ILAsm" Value=".method public static hidebysig unsigned int8[] ToBytes<struct .ctor (class System.ValueType) T>(!!T value, valuetype WebSocketSharp.ByteOrder order) cil managed" />
|
||||
<Member MemberName="ToByteArray<T>">
|
||||
<MemberSignature Language="C#" Value="public static byte[] ToByteArray<T> (this T value, WebSocketSharp.ByteOrder order) where T : struct;" />
|
||||
<MemberSignature Language="ILAsm" Value=".method public static hidebysig unsigned int8[] ToByteArray<struct .ctor (class System.ValueType) T>(!!T value, valuetype WebSocketSharp.ByteOrder order) cil managed" />
|
||||
<MemberType>Method</MemberType>
|
||||
<ReturnValue>
|
||||
<ReturnType>System.Byte[]</ReturnType>
|
||||
|
@ -1,6 +1,6 @@
|
||||
<Overview>
|
||||
<Assemblies>
|
||||
<Assembly Name="websocket-sharp" Version="1.0.2.28404">
|
||||
<Assembly Name="websocket-sharp" Version="1.0.2.26272">
|
||||
<AssemblyPublicKey>[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 ]</AssemblyPublicKey>
|
||||
<Attributes>
|
||||
<Attribute>
|
||||
@ -1324,9 +1324,9 @@
|
||||
<Targets>
|
||||
<Target Type="T:System.ValueType" />
|
||||
</Targets>
|
||||
<Member MemberName="ToBytes<T>">
|
||||
<MemberSignature Language="C#" Value="public static byte[] ToBytes<T> (this T value, WebSocketSharp.ByteOrder order) where T : struct;" />
|
||||
<MemberSignature Language="ILAsm" Value=".method public static hidebysig unsigned int8[] ToBytes<struct .ctor (class System.ValueType) T>(!!T value, valuetype WebSocketSharp.ByteOrder order) cil managed" />
|
||||
<Member MemberName="ToByteArray<T>">
|
||||
<MemberSignature Language="C#" Value="public static byte[] ToByteArray<T> (this T value, WebSocketSharp.ByteOrder order) where T : struct;" />
|
||||
<MemberSignature Language="ILAsm" Value=".method public static hidebysig unsigned int8[] ToByteArray<struct .ctor (class System.ValueType) T>(!!T value, valuetype WebSocketSharp.ByteOrder order) cil managed" />
|
||||
<MemberType>ExtensionMethod</MemberType>
|
||||
<ReturnValue>
|
||||
<ReturnType>System.Byte[]</ReturnType>
|
||||
@ -1358,7 +1358,7 @@
|
||||
Converts the specified data to an array of <see cref="T:System.Byte" />.
|
||||
</summary>
|
||||
</Docs>
|
||||
<Link Type="WebSocketSharp.Ext" Member="M:WebSocketSharp.Ext.ToBytes``1(``0,WebSocketSharp.ByteOrder)" />
|
||||
<Link Type="WebSocketSharp.Ext" Member="M:WebSocketSharp.Ext.ToByteArray``1(``0,WebSocketSharp.ByteOrder)" />
|
||||
</Member>
|
||||
</ExtensionMethod>
|
||||
<ExtensionMethod>
|
||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user