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 { /// /// 4byte header /// 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; } } }