EonaCat.Network/EonaCat.Network/System/Sockets/Web/EventArgs/MessageEventArgs.cs

72 lines
1.7 KiB
C#

// This file is part of the EonaCat project(s) which is released under the Apache License.
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
using System;
namespace EonaCat.Network
{
public class MessageEventArgs : EventArgs
{
private readonly byte[] _rawData;
private string _data;
private bool _dataSet;
internal MessageEventArgs(WSFrame frame)
{
Opcode = frame.Opcode;
_rawData = frame.Payload.ApplicationData;
}
internal MessageEventArgs(OperationCode opcode, byte[] rawData)
{
if ((ulong)rawData.LongLength > Payload.MaxLength)
{
throw new WSException(CloseStatusCode.TooBig);
}
Opcode = opcode;
_rawData = rawData;
}
public string Data
{
get
{
setData();
return _data;
}
}
public bool IsBinary => Opcode == OperationCode.Binary;
public bool IsPing => Opcode == OperationCode.Ping;
public bool IsText => Opcode == OperationCode.Text;
public byte[] RawData
{
get
{
setData();
return _rawData;
}
}
internal OperationCode Opcode { get; }
private void setData()
{
if (_dataSet)
{
return;
}
if (Opcode == OperationCode.Binary)
{
_dataSet = true;
return;
}
_data = _rawData.UTF8Decode();
_dataSet = true;
}
}
}