You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.5 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using STcpHelper.Utility;
namespace STcpHelper.Packet
{
/// <summary>
/// 4byte header
/// </summary>
public class STcpPacketHeader : ISTcpPacket
{
private readonly int TYPE_LENGTH = 4;
public static readonly int HEADER_LENGTH = 8;
public PacketType Type { get; private set; }
public int DataLength { get; private set; }
public STcpPacketHeader(PacketType type, int dataLength)
{
Type = type;
DataLength = dataLength;
}
public STcpPacketHeader(byte[] buffer)
{
int cursor = 0;
this.Type = (PacketType)SBufferHelper.ConvertBufferToDataLength(buffer, cursor);
cursor += TYPE_LENGTH;
this.DataLength = SBufferHelper.ConvertBufferToDataLength(buffer, cursor);
}
public byte[] Serialize()
{
byte[] type = SBufferHelper.ConvertPacketTypeToBuffer(this.Type);
byte[] dataLength = SBufferHelper.ConvertDataLengthToBuffer(this.DataLength);
return SBufferHelper.GetBuffer(HEADER_LENGTH, type, dataLength);
}
public static int GetDataSize(params byte[][] data)
{
int size = 0;
for (int i = 0; i < data.Length; i++)
{
size += data[i].Length;
}
return size;
}
}
}