|
|
|
@ -0,0 +1,51 @@ |
|
|
|
|
using STcpHelper.Packet; |
|
|
|
|
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 |
|
|
|
|
{ |
|
|
|
|
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; |
|
|
|
|
short length = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(dataBuffer, cursor)); |
|
|
|
|
cursor += sizeof(short); |
|
|
|
|
|
|
|
|
|
string jsonString = SEncoding.GetString(dataBuffer, cursor, length); |
|
|
|
|
List<string> clients = JsonSerializer.Deserialize<List<string>>(jsonString); |
|
|
|
|
|
|
|
|
|
this.Clients = clients; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public byte[] Serialize() |
|
|
|
|
{ |
|
|
|
|
string jsonString = JsonSerializer.Serialize(this.Clients); |
|
|
|
|
byte[] data = SEncoding.GetBytes(jsonString); |
|
|
|
|
byte[] dataLength = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)data.Length)); |
|
|
|
|
|
|
|
|
|
// 4bytes header |
|
|
|
|
STcpPacketHeader header = new STcpPacketHeader(PacketType.RES_CLIENT_LIST, dataLength.Length + data.Length); |
|
|
|
|
|
|
|
|
|
// [header][2bytes size][n bytes string] |
|
|
|
|
return SBufferHelper.GetBuffer(4 + dataLength.Length + data.Length, header.Serialize(), dataLength, data); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |