|
|
|
@ -1,4 +1,4 @@
|
|
|
|
#region MIT License
|
|
|
|
#region License
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* PayloadData.cs
|
|
|
|
* PayloadData.cs
|
|
|
|
*
|
|
|
|
*
|
|
|
|
@ -36,16 +36,16 @@ namespace WebSocketSharp {
|
|
|
|
|
|
|
|
|
|
|
|
internal class PayloadData : IEnumerable<byte>
|
|
|
|
internal class PayloadData : IEnumerable<byte>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
#region Field
|
|
|
|
#region Fields
|
|
|
|
|
|
|
|
|
|
|
|
public const ulong MaxLength = long.MaxValue;
|
|
|
|
public const ulong MaxLength = long.MaxValue;
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Public Constructors
|
|
|
|
#region Constructors
|
|
|
|
|
|
|
|
|
|
|
|
public PayloadData(string appData)
|
|
|
|
public PayloadData()
|
|
|
|
: this(Encoding.UTF8.GetBytes(appData))
|
|
|
|
: this(new byte[]{})
|
|
|
|
{
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -54,6 +54,11 @@ namespace WebSocketSharp {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public PayloadData(string appData)
|
|
|
|
|
|
|
|
: this(Encoding.UTF8.GetBytes(appData))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public PayloadData(byte[] appData, bool masked)
|
|
|
|
public PayloadData(byte[] appData, bool masked)
|
|
|
|
: this(new byte[]{}, appData, masked)
|
|
|
|
: this(new byte[]{}, appData, masked)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
@ -73,22 +78,23 @@ namespace WebSocketSharp {
|
|
|
|
throw new ArgumentNullException("appData");
|
|
|
|
throw new ArgumentNullException("appData");
|
|
|
|
|
|
|
|
|
|
|
|
if ((ulong)extData.LongLength + (ulong)appData.LongLength > MaxLength)
|
|
|
|
if ((ulong)extData.LongLength + (ulong)appData.LongLength > MaxLength)
|
|
|
|
throw new ArgumentOutOfRangeException("Plus 'extData' length and 'appData' lenght must be less than MaxLength.");
|
|
|
|
throw new ArgumentOutOfRangeException(
|
|
|
|
|
|
|
|
"The length of 'extData' plus 'appData' must be less than MaxLength.");
|
|
|
|
|
|
|
|
|
|
|
|
ExtensionData = extData;
|
|
|
|
ExtensionData = extData;
|
|
|
|
ApplicationData = appData;
|
|
|
|
ApplicationData = appData;
|
|
|
|
IsMasked = masked;
|
|
|
|
IsMasked = masked;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Internal Property
|
|
|
|
#region Internal Properties
|
|
|
|
|
|
|
|
|
|
|
|
internal bool ContainsReservedCloseStatusCode {
|
|
|
|
internal bool ContainsReservedCloseStatusCode {
|
|
|
|
get {
|
|
|
|
get {
|
|
|
|
if (Length >= 2)
|
|
|
|
if (Length >= 2)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var code = ToBytes().SubArray(0, 2).To<ushort>(ByteOrder.BIG);
|
|
|
|
var code = ToByteArray().SubArray(0, 2).To<ushort>(ByteOrder.BIG);
|
|
|
|
if (code == (ushort)CloseStatusCode.UNDEFINED ||
|
|
|
|
if (code == (ushort)CloseStatusCode.UNDEFINED ||
|
|
|
|
code == (ushort)CloseStatusCode.NO_STATUS_CODE ||
|
|
|
|
code == (ushort)CloseStatusCode.NO_STATUS_CODE ||
|
|
|
|
code == (ushort)CloseStatusCode.ABNORMAL ||
|
|
|
|
code == (ushort)CloseStatusCode.ABNORMAL ||
|
|
|
|
@ -104,10 +110,10 @@ namespace WebSocketSharp {
|
|
|
|
|
|
|
|
|
|
|
|
#region Public Properties
|
|
|
|
#region Public Properties
|
|
|
|
|
|
|
|
|
|
|
|
public byte[] ExtensionData { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public byte[] ApplicationData { get; private set; }
|
|
|
|
public byte[] ApplicationData { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public byte[] ExtensionData { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsMasked { get; private set; }
|
|
|
|
public bool IsMasked { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
public ulong Length {
|
|
|
|
public ulong Length {
|
|
|
|
@ -120,12 +126,7 @@ namespace WebSocketSharp {
|
|
|
|
|
|
|
|
|
|
|
|
#region Private Methods
|
|
|
|
#region Private Methods
|
|
|
|
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
|
|
private static void mask(byte[] src, byte[] key)
|
|
|
|
{
|
|
|
|
|
|
|
|
return GetEnumerator();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void mask(byte[] src, byte[] key)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
for (long i = 0; i < src.LongLength; i++)
|
|
|
|
for (long i = 0; i < src.LongLength; i++)
|
|
|
|
src[i] = (byte)(src[i] ^ key[i % 4]);
|
|
|
|
src[i] = (byte)(src[i] ^ key[i % 4]);
|
|
|
|
@ -150,7 +151,7 @@ namespace WebSocketSharp {
|
|
|
|
throw new ArgumentNullException("maskingKey");
|
|
|
|
throw new ArgumentNullException("maskingKey");
|
|
|
|
|
|
|
|
|
|
|
|
if (maskingKey.Length != 4)
|
|
|
|
if (maskingKey.Length != 4)
|
|
|
|
throw new ArgumentOutOfRangeException("maskingKey", "'maskingKey' length must be 4.");
|
|
|
|
throw new ArgumentOutOfRangeException("maskingKey", "The length must be 4.");
|
|
|
|
|
|
|
|
|
|
|
|
if (ExtensionData.LongLength > 0)
|
|
|
|
if (ExtensionData.LongLength > 0)
|
|
|
|
mask(ExtensionData, maskingKey);
|
|
|
|
mask(ExtensionData, maskingKey);
|
|
|
|
@ -161,7 +162,7 @@ namespace WebSocketSharp {
|
|
|
|
IsMasked = !IsMasked;
|
|
|
|
IsMasked = !IsMasked;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public byte[] ToBytes()
|
|
|
|
public byte[] ToByteArray()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return ExtensionData.LongLength > 0
|
|
|
|
return ExtensionData.LongLength > 0
|
|
|
|
? ExtensionData.Concat(ApplicationData).ToArray()
|
|
|
|
? ExtensionData.Concat(ApplicationData).ToArray()
|
|
|
|
@ -170,7 +171,16 @@ namespace WebSocketSharp {
|
|
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return BitConverter.ToString(ToBytes());
|
|
|
|
return BitConverter.ToString(ToByteArray());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region Explicitly Implemented Interface Members
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return GetEnumerator();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
|