refactoring, file packet

This commit is contained in:
2023-10-12 16:21:04 +09:00
parent 08207c9eec
commit 044ace6552
13 changed files with 247 additions and 37 deletions

View File

@@ -12,31 +12,45 @@ namespace STcpHelper.Packet
/// </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 short DataLength { get; private set; }
public int DataLength { get; private set; }
public STcpPacketHeader(PacketType type, int dataLength)
{
Type = type;
DataLength = (short)dataLength;
DataLength = dataLength;
}
public STcpPacketHeader(byte[] buffer)
{
int cursor = 0;
this.Type = (PacketType)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, cursor));
cursor += 2;
this.Type = (PacketType)SBufferHelper.ConvertBufferToDataLength(buffer, cursor);
cursor += TYPE_LENGTH;
this.DataLength = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, cursor));
this.DataLength = SBufferHelper.ConvertBufferToDataLength(buffer, cursor);
}
// [2bytes Type][2bytes Size]
public byte[] Serialize()
{
byte[] type = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)this.Type));
byte[] size = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(this.DataLength));
return SBufferHelper.GetBuffer(type.Length + size.Length, type, size);
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;
}
}
}