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.

46 lines
1.4 KiB

2 years ago
using SObject;
using STcpHelper.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STcpHelper.Packet
{
public class STcpZooResPacket : ISTcpPacket
{
private ISSerializer _serializer = new SJsonSerializerHelper();
public Zoo Zoo { get; private set; }
public STcpZooResPacket(Zoo zoo)
{
this.Zoo = zoo;
}
public STcpZooResPacket(byte[] dataBuffer)
{
int cursor = 0;
int length = SBufferHelper.ConvertBufferToDataLength(dataBuffer, cursor);
cursor += sizeof(int);
string serialized = SEncoding.GetString(dataBuffer, cursor, length);
this.Zoo = _serializer.Deserialize<Zoo>(serialized);
}
public byte[] Serialize()
{
string serialized = _serializer.Serialize(this.Zoo);
byte[] data = SEncoding.GetBytes(serialized);
byte[] dataLength = SBufferHelper.ConvertDataLengthToBuffer(data.Length);
int dataSize = STcpPacketHeader.GetDataSize(data, dataLength);
STcpPacketHeader header = new STcpPacketHeader(PacketType.RES_ZOO, dataSize);
return SBufferHelper.GetBuffer(STcpPacketHeader.HEADER_LENGTH + dataSize, header.Serialize(), dataLength, data);
}
}
}