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;
using STcpHelper.Utility;
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;
int length = SBufferHelper.ConvertBufferToDataLength(dataBuffer, cursor);
cursor += sizeof(int);
this.Text = SEncoding.GetString(dataBuffer, cursor, length);
}
public byte[] Serialize()
{
byte[] data = SEncoding.GetBytes(Text);
byte[] dataLength = SBufferHelper.ConvertDataLengthToBuffer(data.Length);
int dataSize = STcpPacketHeader.GetDataSize(data, dataLength);
STcpPacketHeader header = new STcpPacketHeader(PacketType.TEXT, dataSize);
return SBufferHelper.GetBuffer(
STcpPacketHeader.HEADER_LENGTH + dataSize, header.Serialize(), dataLength, data);
}
}
}