packet structure
This commit is contained in:
13
MySolution/STcpHelper/Packet/ISTcpPacket.cs
Normal file
13
MySolution/STcpHelper/Packet/ISTcpPacket.cs
Normal 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();
|
||||
}
|
||||
}
|
14
MySolution/STcpHelper/Packet/PacketType.cs
Normal file
14
MySolution/STcpHelper/Packet/PacketType.cs
Normal 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,
|
||||
|
||||
}
|
||||
}
|
27
MySolution/STcpHelper/Packet/SBufferHelper.cs
Normal file
27
MySolution/STcpHelper/Packet/SBufferHelper.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
42
MySolution/STcpHelper/Packet/STcpPacketHeader.cs
Normal file
42
MySolution/STcpHelper/Packet/STcpPacketHeader.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
40
MySolution/STcpHelper/Packet/STcpTextPacket.cs
Normal file
40
MySolution/STcpHelper/Packet/STcpTextPacket.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user