Refactored PayloadData.cs

This commit is contained in:
sta 2014-06-09 20:02:01 +09:00
parent 3372de5b1a
commit 254f6dd268

View File

@ -4,7 +4,7 @@
* *
* The MIT License * The MIT License
* *
* Copyright (c) 2012-2013 sta.blockhead * Copyright (c) 2012-2014 sta.blockhead
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -29,14 +29,20 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text; using System.Text;
namespace WebSocketSharp namespace WebSocketSharp
{ {
internal class PayloadData : IEnumerable<byte> internal class PayloadData : IEnumerable<byte>
{ {
#region Private Fields
private byte [] _applicationData;
private byte [] _extensionData;
private bool _masked;
#endregion
#region Public Const Fields #region Public Const Fields
public const ulong MaxLength = long.MaxValue; public const ulong MaxLength = long.MaxValue;
@ -46,39 +52,34 @@ namespace WebSocketSharp
#region Public Constructors #region Public Constructors
public PayloadData () public PayloadData ()
: this (new byte []{}) : this (new byte [0], new byte [0], false)
{ {
} }
public PayloadData (byte [] appData) public PayloadData (byte [] applicationData)
: this (new byte []{}, appData) : this (new byte [0], applicationData, false)
{ {
} }
public PayloadData (string appData) public PayloadData (string applicationData)
: this (Encoding.UTF8.GetBytes (appData)) : this (new byte [0], Encoding.UTF8.GetBytes (applicationData), false)
{ {
} }
public PayloadData (byte [] appData, bool masked) public PayloadData (byte [] applicationData, bool masked)
: this (new byte []{}, appData, masked) : this (new byte [0], applicationData, masked)
{ {
} }
public PayloadData (byte [] extData, byte [] appData) public PayloadData (byte [] extensionData, byte [] applicationData, bool masked)
: this (extData, appData, false)
{ {
} if ((ulong) extensionData.LongLength + (ulong) applicationData.LongLength > MaxLength)
public PayloadData (byte [] extData, byte [] appData, bool masked)
{
if ((ulong) extData.LongLength + (ulong) appData.LongLength > MaxLength)
throw new ArgumentOutOfRangeException ( throw new ArgumentOutOfRangeException (
"The length of 'extData' plus 'appData' must be less than MaxLength."); "The length of 'extensionData' plus 'applicationData' is greater than MaxLength.");
ExtensionData = extData; _extensionData = extensionData;
ApplicationData = appData; _applicationData = applicationData;
IsMasked = masked; _masked = masked;
} }
#endregion #endregion
@ -87,31 +88,36 @@ namespace WebSocketSharp
internal bool ContainsReservedCloseStatusCode { internal bool ContainsReservedCloseStatusCode {
get { get {
return ApplicationData.Length > 1 return _applicationData.Length > 1 &&
? ApplicationData.SubArray (0, 2).ToUInt16 (ByteOrder.Big).IsReserved () _applicationData.SubArray (0, 2).ToUInt16 (ByteOrder.Big).IsReserved ();
: false;
} }
} }
internal bool IsMasked {
get; private set;
}
#endregion #endregion
#region Public Properties #region Public Properties
public byte [] ExtensionData { public byte [] ApplicationData {
get; private set; get {
return _applicationData;
}
} }
public byte [] ApplicationData { public byte [] ExtensionData {
get; private set; get {
return _extensionData;
}
}
public bool IsMasked {
get {
return _masked;
}
} }
public ulong Length { public ulong Length {
get { get {
return (ulong) (ExtensionData.LongLength + ApplicationData.LongLength); return (ulong) (_extensionData.LongLength + _applicationData.LongLength);
} }
} }
@ -127,37 +133,33 @@ namespace WebSocketSharp
#endregion #endregion
#region Internal Methods
#endregion
#region Public Methods #region Public Methods
public IEnumerator<byte> GetEnumerator () public IEnumerator<byte> GetEnumerator ()
{ {
foreach (byte b in ExtensionData) foreach (byte b in _extensionData)
yield return b; yield return b;
foreach (byte b in ApplicationData) foreach (byte b in _applicationData)
yield return b; yield return b;
} }
public void Mask (byte [] maskingKey) public void Mask (byte [] maskingKey)
{ {
if (ExtensionData.LongLength > 0) if (_extensionData.LongLength > 0)
mask (ExtensionData, maskingKey); mask (_extensionData, maskingKey);
if (ApplicationData.LongLength > 0) if (_applicationData.LongLength > 0)
mask (ApplicationData, maskingKey); mask (_applicationData, maskingKey);
IsMasked = !IsMasked; _masked = !_masked;
} }
public byte [] ToByteArray () public byte [] ToByteArray ()
{ {
return ExtensionData.LongLength > 0 return _extensionData.LongLength > 0
? this.ToArray () ? new List<byte> (this).ToArray ()
: ApplicationData; : _applicationData;
} }
public override string ToString () public override string ToString ()