|
|
@ -19,22 +19,22 @@ namespace STcpHelper.Packet |
|
|
|
public STcpTextPacket(byte[] dataBuffer) |
|
|
|
public STcpTextPacket(byte[] dataBuffer) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int cursor = 0; |
|
|
|
int cursor = 0; |
|
|
|
short textSize = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(dataBuffer, cursor)); |
|
|
|
short length = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(dataBuffer, cursor)); |
|
|
|
cursor += sizeof(short); |
|
|
|
cursor += sizeof(short); |
|
|
|
|
|
|
|
|
|
|
|
this.Text = Encoding.UTF8.GetString(dataBuffer, cursor, textSize); |
|
|
|
this.Text = SEncoding.GetString(dataBuffer, cursor, length); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public byte[] Serialize() |
|
|
|
public byte[] Serialize() |
|
|
|
{ |
|
|
|
{ |
|
|
|
byte[] text = Encoding.UTF8.GetBytes(Text); |
|
|
|
byte[] data = SEncoding.GetBytes(Text); |
|
|
|
byte[] textSize = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)text.Length)); |
|
|
|
byte[] dataLength = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)data.Length)); |
|
|
|
|
|
|
|
|
|
|
|
// 4byte header |
|
|
|
// 4bytes header |
|
|
|
STcpPacketHeader header = new STcpPacketHeader(PacketType.TEXT, text.Length + textSize.Length); |
|
|
|
STcpPacketHeader header = new STcpPacketHeader(PacketType.TEXT, dataLength.Length + data.Length); |
|
|
|
|
|
|
|
|
|
|
|
// [2bytes text size][n bytes text] |
|
|
|
// [header][2bytes text size][n bytes text] |
|
|
|
return SBufferHelper.GetBuffer(4 + textSize.Length + text.Length, header.Serialize(), textSize, text); |
|
|
|
return SBufferHelper.GetBuffer(4 + dataLength.Length + data.Length, header.Serialize(), dataLength, data); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|