2013-04-08 14:11:57 +08:00
|
|
|
#region License
|
2013-01-11 19:32:38 +08:00
|
|
|
/*
|
2012-07-31 09:36:52 +08:00
|
|
|
* PayloadData.cs
|
|
|
|
*
|
|
|
|
* The MIT License
|
|
|
|
*
|
2015-05-22 12:47:19 +08:00
|
|
|
* Copyright (c) 2012-2015 sta.blockhead
|
2014-06-09 19:02:01 +08:00
|
|
|
*
|
2012-07-31 09:36:52 +08:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2014-06-09 19:02:01 +08:00
|
|
|
*
|
2012-07-31 09:36:52 +08:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2013-10-01 13:52:39 +08:00
|
|
|
namespace WebSocketSharp
|
|
|
|
{
|
2013-01-15 14:29:05 +08:00
|
|
|
internal class PayloadData : IEnumerable<byte>
|
2012-07-31 09:36:52 +08:00
|
|
|
{
|
2014-06-09 19:02:01 +08:00
|
|
|
#region Private Fields
|
|
|
|
|
2014-09-24 18:01:01 +08:00
|
|
|
private byte[] _data;
|
|
|
|
private long _extDataLength;
|
|
|
|
private long _length;
|
2014-06-09 19:02:01 +08:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2014-09-07 21:18:45 +08:00
|
|
|
#region Public Fields
|
2012-07-31 09:36:52 +08:00
|
|
|
|
2015-10-01 13:50:17 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Represents the empty payload data.
|
|
|
|
/// </summary>
|
2015-10-09 14:33:30 +08:00
|
|
|
public static readonly PayloadData Empty;
|
2015-10-01 13:50:17 +08:00
|
|
|
|
2015-09-28 14:27:29 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Represents the allowable max length.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// <para>
|
|
|
|
/// A <see cref="WebSocketException"/> will occur if the payload data length is
|
|
|
|
/// greater than this.
|
|
|
|
/// </para>
|
|
|
|
/// <para>
|
2015-09-28 15:00:51 +08:00
|
|
|
/// If you would like to change this value, you must set this to a value between
|
|
|
|
/// <c>WebSocket.FragmentLength</c> and <c>Int64.MaxValue</c> inclusive.
|
2015-09-28 14:27:29 +08:00
|
|
|
/// </para>
|
|
|
|
/// </remarks>
|
2015-10-09 14:33:30 +08:00
|
|
|
public static readonly ulong MaxLength;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Static Constructor
|
|
|
|
|
|
|
|
static PayloadData ()
|
|
|
|
{
|
|
|
|
Empty = new PayloadData ();
|
|
|
|
MaxLength = Int64.MaxValue;
|
|
|
|
}
|
2012-07-31 09:36:52 +08:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2014-09-15 13:39:14 +08:00
|
|
|
#region Internal Constructors
|
2012-07-31 09:36:52 +08:00
|
|
|
|
2014-09-15 13:39:14 +08:00
|
|
|
internal PayloadData ()
|
2013-04-08 14:11:57 +08:00
|
|
|
{
|
2015-08-24 15:03:50 +08:00
|
|
|
_data = WebSocket.EmptyBytes;
|
2013-04-08 14:11:57 +08:00
|
|
|
}
|
|
|
|
|
2014-09-15 13:39:14 +08:00
|
|
|
internal PayloadData (byte[] data)
|
2015-09-30 11:28:48 +08:00
|
|
|
: this (data, data.LongLength)
|
2012-07-31 09:36:52 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-09-30 11:28:48 +08:00
|
|
|
internal PayloadData (byte[] data, long length)
|
2012-07-31 09:36:52 +08:00
|
|
|
{
|
2014-09-15 13:39:14 +08:00
|
|
|
_data = data;
|
2015-09-30 11:28:48 +08:00
|
|
|
_length = length;
|
2012-07-31 09:36:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2013-04-08 14:11:57 +08:00
|
|
|
#region Internal Properties
|
2013-03-21 15:55:53 +08:00
|
|
|
|
2014-09-15 13:39:14 +08:00
|
|
|
internal long ExtensionDataLength {
|
|
|
|
get {
|
|
|
|
return _extDataLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
set {
|
|
|
|
_extDataLength = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-07 21:18:45 +08:00
|
|
|
internal bool IncludesReservedCloseStatusCode {
|
2013-03-21 15:55:53 +08:00
|
|
|
get {
|
2014-09-24 18:01:01 +08:00
|
|
|
return _length > 1 && _data.SubArray (0, 2).ToUInt16 (ByteOrder.Big).IsReserved ();
|
2013-03-21 15:55:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Public Properties
|
2012-12-05 21:10:15 +08:00
|
|
|
|
2014-09-07 21:18:45 +08:00
|
|
|
public byte[] ApplicationData {
|
2014-06-09 19:02:01 +08:00
|
|
|
get {
|
2014-09-15 13:39:14 +08:00
|
|
|
return _extDataLength > 0
|
|
|
|
? _data.SubArray (_extDataLength, _length - _extDataLength)
|
|
|
|
: _data;
|
2014-06-09 19:02:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-07 21:18:45 +08:00
|
|
|
public byte[] ExtensionData {
|
2014-06-09 19:02:01 +08:00
|
|
|
get {
|
2014-09-15 13:39:14 +08:00
|
|
|
return _extDataLength > 0
|
|
|
|
? _data.SubArray (0, _extDataLength)
|
2015-08-24 15:03:50 +08:00
|
|
|
: WebSocket.EmptyBytes;
|
2014-06-09 19:02:01 +08:00
|
|
|
}
|
2013-05-17 22:02:11 +08:00
|
|
|
}
|
2013-04-08 14:11:57 +08:00
|
|
|
|
2013-10-01 13:52:39 +08:00
|
|
|
public ulong Length {
|
|
|
|
get {
|
2014-09-15 13:39:14 +08:00
|
|
|
return (ulong) _length;
|
2013-10-01 13:52:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 21:10:15 +08:00
|
|
|
#endregion
|
|
|
|
|
2014-09-12 20:47:50 +08:00
|
|
|
#region Internal Methods
|
2012-07-31 09:36:52 +08:00
|
|
|
|
2014-09-12 20:47:50 +08:00
|
|
|
internal void Mask (byte[] key)
|
2012-07-31 09:36:52 +08:00
|
|
|
{
|
2014-09-15 13:39:14 +08:00
|
|
|
for (long i = 0; i < _length; i++)
|
|
|
|
_data[i] = (byte) (_data[i] ^ key[i % 4]);
|
2012-07-31 09:36:52 +08:00
|
|
|
}
|
|
|
|
|
2014-09-12 20:47:50 +08:00
|
|
|
#endregion
|
2012-08-01 10:38:40 +08:00
|
|
|
|
2014-09-12 20:47:50 +08:00
|
|
|
#region Public Methods
|
2012-07-31 09:36:52 +08:00
|
|
|
|
2014-09-12 20:47:50 +08:00
|
|
|
public IEnumerator<byte> GetEnumerator ()
|
|
|
|
{
|
2014-09-15 13:39:14 +08:00
|
|
|
foreach (var b in _data)
|
2014-09-12 20:47:50 +08:00
|
|
|
yield return b;
|
2012-07-31 09:36:52 +08:00
|
|
|
}
|
|
|
|
|
2015-09-29 14:20:19 +08:00
|
|
|
public byte[] ToArray ()
|
2012-07-31 09:36:52 +08:00
|
|
|
{
|
2014-09-15 13:39:14 +08:00
|
|
|
return _data;
|
2012-07-31 09:36:52 +08:00
|
|
|
}
|
|
|
|
|
2013-10-01 13:52:39 +08:00
|
|
|
public override string ToString ()
|
2012-07-31 09:36:52 +08:00
|
|
|
{
|
2014-09-15 13:39:14 +08:00
|
|
|
return BitConverter.ToString (_data);
|
2013-04-08 14:11:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2014-09-12 20:47:50 +08:00
|
|
|
#region Explicit Interface Implementations
|
2013-04-08 14:11:57 +08:00
|
|
|
|
2013-10-01 13:52:39 +08:00
|
|
|
IEnumerator IEnumerable.GetEnumerator ()
|
2013-04-08 14:11:57 +08:00
|
|
|
{
|
2013-10-01 13:52:39 +08:00
|
|
|
return GetEnumerator ();
|
2012-07-31 09:36:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|