|
|
|
|
using STcpHelper.Utility;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace STcpHelper.Packet
|
|
|
|
|
{
|
|
|
|
|
public class STcpClientListResPacket : ISTcpPacket
|
|
|
|
|
{
|
|
|
|
|
private ISSerializer _serializer = new SJsonSerializerHelper();
|
|
|
|
|
|
|
|
|
|
public List<string> Clients { get; private set; }
|
|
|
|
|
|
|
|
|
|
public STcpClientListResPacket(List<TcpClient> clinets)
|
|
|
|
|
{
|
|
|
|
|
this.Clients = new List<string>();
|
|
|
|
|
foreach (var client in clinets)
|
|
|
|
|
{
|
|
|
|
|
this.Clients.Add(client.Client.RemoteEndPoint.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public STcpClientListResPacket(byte[] dataBuffer)
|
|
|
|
|
{
|
|
|
|
|
int cursor = 0;
|
|
|
|
|
int length = SBufferHelper.ConvertBufferToDataLength(dataBuffer, cursor);
|
|
|
|
|
cursor += sizeof(int);
|
|
|
|
|
|
|
|
|
|
string serialized = SEncoding.GetString(dataBuffer, cursor, length);
|
|
|
|
|
|
|
|
|
|
this.Clients = _serializer.Deserialize<List<string>>(serialized);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte[] Serialize()
|
|
|
|
|
{
|
|
|
|
|
string serialized = _serializer.Serialize(this.Clients);
|
|
|
|
|
byte[] data = SEncoding.GetBytes(serialized);
|
|
|
|
|
byte[] dataLength = SBufferHelper.ConvertDataLengthToBuffer(data.Length);
|
|
|
|
|
|
|
|
|
|
int dataSize = STcpPacketHeader.GetDataSize(data, dataLength);
|
|
|
|
|
|
|
|
|
|
STcpPacketHeader header = new STcpPacketHeader(PacketType.RES_CLIENT_LIST, dataSize);
|
|
|
|
|
|
|
|
|
|
return SBufferHelper.GetBuffer(STcpPacketHeader.HEADER_LENGTH + dataSize, header.Serialize(), dataLength, data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|