websocket-sharp/websocket-sharp/PayloadData.cs

209 lines
4.7 KiB
C#
Raw Normal View History

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
*
2019-07-31 20:27:44 +08:00
* Copyright (c) 2012-2019 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
/// <summary>
/// Represents the empty payload data.
/// </summary>
2015-10-09 14:33:30 +08:00
public static readonly PayloadData Empty;
/// <summary>
2019-07-30 20:36:41 +08:00
/// Represents the allowable max length of payload data.
/// </summary>
/// <remarks>
/// <para>
2019-07-29 20:41:13 +08:00
/// A <see cref="WebSocketException"/> will occur when the length of
/// incoming payload data is greater than the value of this field.
/// </para>
/// <para>
2019-07-30 20:36:41 +08:00
/// If you would like to change the value of this field, it must be
/// a number between <see cref="WebSocket.FragmentLength"/> and
/// <see cref="Int64.MaxValue"/> inclusive.
/// </para>
/// </remarks>
2015-10-09 14:33:30 +08:00
public static readonly ulong MaxLength;
#endregion
#region Static Constructor
static PayloadData ()
{
2019-07-20 21:30:17 +08:00
Empty = new PayloadData (WebSocket.EmptyBytes, 0);
2015-10-09 14:33:30 +08:00
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 (byte[] data)
: this (data, data.LongLength)
2012-07-31 09:36:52 +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;
_length = length;
2012-07-31 09:36:52 +08:00
}
internal PayloadData (ushort code, string reason)
{
_data = code.Append (reason);
_length = _data.LongLength;
}
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
internal ushort Code {
get {
2019-07-22 20:25:33 +08:00
return _length >= 2
? _data.SubArray (0, 2).ToUInt16 (ByteOrder.Big)
: (ushort) 1005;
}
}
2014-09-15 13:39:14 +08:00
internal long ExtensionDataLength {
get {
return _extDataLength;
}
set {
_extDataLength = value;
}
}
2016-09-20 14:24:18 +08:00
internal bool HasReservedCode {
2013-03-21 15:55:53 +08:00
get {
2019-07-19 18:42:16 +08:00
return _length >= 2 && Code.IsReserved ();
2013-03-21 15:55:53 +08:00
}
}
internal string Reason {
get {
2019-07-23 20:54:56 +08:00
if (_length <= 2)
return String.Empty;
2019-07-23 20:54:56 +08:00
var raw = _data.SubArray (2, _length - 2);
2019-07-23 20:54:56 +08:00
string reason;
return raw.TryGetUTF8DecodedString (out reason)
? reason
: String.Empty;
}
}
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)
: WebSocket.EmptyBytes;
2014-06-09 19:02:01 +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
}
}