Files
dotNetStudyWithGPT/MySolution/STcpHelper/Packet/STcpPacketHeader.cs

58 lines
1.5 KiB
C#
Raw Normal View History

2023-10-10 17:55:14 +09:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
2023-10-13 14:15:26 +09:00
using STcpHelper.Utility;
2023-10-10 17:55:14 +09:00
namespace STcpHelper.Packet
{
/// <summary>
/// 4byte header
/// </summary>
public class STcpPacketHeader : ISTcpPacket
{
2023-10-12 16:21:04 +09:00
private readonly int TYPE_LENGTH = 4;
public static readonly int HEADER_LENGTH = 8;
2023-10-10 17:55:14 +09:00
public PacketType Type { get; private set; }
2023-10-12 16:21:04 +09:00
public int DataLength { get; private set; }
2023-10-10 17:55:14 +09:00
public STcpPacketHeader(PacketType type, int dataLength)
{
Type = type;
2023-10-12 16:21:04 +09:00
DataLength = dataLength;
2023-10-10 17:55:14 +09:00
}
public STcpPacketHeader(byte[] buffer)
{
int cursor = 0;
2023-10-12 16:21:04 +09:00
this.Type = (PacketType)SBufferHelper.ConvertBufferToDataLength(buffer, cursor);
cursor += TYPE_LENGTH;
2023-10-10 17:55:14 +09:00
2023-10-12 16:21:04 +09:00
this.DataLength = SBufferHelper.ConvertBufferToDataLength(buffer, cursor);
2023-10-10 17:55:14 +09:00
}
public byte[] Serialize()
{
2023-10-12 16:21:04 +09:00
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;
2023-10-10 17:55:14 +09:00
}
}
}