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.

42 lines
1.2 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace STcpHelper.Packet
{
/// <summary>
/// 4byte header
/// </summary>
public class STcpPacketHeader : ISTcpPacket
{
public PacketType Type { get; private set; }
public short DataLength { get; private set; }
public STcpPacketHeader(PacketType type, int dataLength)
{
Type = type;
DataLength = (short)dataLength;
}
public STcpPacketHeader(byte[] buffer)
{
int cursor = 0;
this.Type = (PacketType)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, cursor));
cursor += 2;
this.DataLength = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(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);
}
}
}