packet structure

This commit is contained in:
2023-10-10 17:55:14 +09:00
parent ac15e63a0a
commit bf645548c5
9 changed files with 210 additions and 20 deletions

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STcpHelper.Packet
{
public interface ISTcpPacket
{
byte[] Serialize();
}
}

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STcpHelper.Packet
{
public enum PacketType
{
TEXT = 0,
}
}

View File

@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STcpHelper.Packet
{
static class SBufferHelper
{
public static byte[] GetBuffer(int size, params byte[][] args)
{
byte[] bytes = new byte[size];
int cursor = 0;
for (int i = 0; i < args.Length; i++)
{
byte[] data = args[i];
Array.Copy(data, 0, bytes, cursor, data.Length);
cursor += data.Length;
}
return bytes;
}
}
}

View File

@@ -0,0 +1,42 @@
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);
}
}
}

View File

@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace STcpHelper.Packet
{
public class STcpTextPacket : ISTcpPacket
{
public string Text { get; private set; }
public STcpTextPacket(string text)
{
this.Text = text;
}
public STcpTextPacket(byte[] dataBuffer)
{
int cursor = 0;
short textSize = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(dataBuffer, cursor));
cursor += sizeof(short);
this.Text = Encoding.UTF8.GetString(dataBuffer, cursor, textSize);
}
public byte[] Serialize()
{
byte[] text = Encoding.UTF8.GetBytes(Text);
byte[] textSize = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)text.Length));
// 4byte header
STcpPacketHeader header = new STcpPacketHeader(PacketType.TEXT, text.Length + textSize.Length);
// [2bytes text size][n bytes text]
return SBufferHelper.GetBuffer(4 + textSize.Length + text.Length, header.Serialize(), textSize, text);
}
}
}