2022-05-18 16:39:48 +01:00
|
|
|
|
using TcpDotNet;
|
2022-07-05 14:55:59 +01:00
|
|
|
|
using TcpDotNet.ListenerIntegrationTest.PacketHandlers;
|
2022-05-18 16:39:48 +01:00
|
|
|
|
using TcpDotNet.Protocol;
|
|
|
|
|
using TcpDotNet.Protocol.Packets.ClientBound;
|
|
|
|
|
using TcpDotNet.Protocol.Packets.ServerBound;
|
|
|
|
|
|
|
|
|
|
var listener = new ProtocolListener();
|
2022-05-19 10:37:37 +01:00
|
|
|
|
listener.ClientConnected += (_, e) => Console.WriteLine($"Client connected from {e.Client.RemoteEndPoint} with session {e.Client.SessionId}");
|
|
|
|
|
listener.ClientDisconnected += (_, e) => Console.WriteLine($"Client {e.Client.SessionId} disconnected ({e.DisconnectReason})");
|
2022-05-18 16:39:48 +01:00
|
|
|
|
|
2022-07-05 14:55:59 +01:00
|
|
|
|
listener.RegisterPacketHandler(new HelloPacketHandler());
|
2022-05-18 16:39:48 +01:00
|
|
|
|
listener.RegisterPacketHandler(new PingPacketHandler());
|
|
|
|
|
listener.Start(1234);
|
|
|
|
|
|
|
|
|
|
await Task.Delay(-1);
|
|
|
|
|
|
|
|
|
|
internal sealed class PingPacketHandler : PacketHandler<PingPacket>
|
|
|
|
|
{
|
2022-05-18 17:18:33 +01:00
|
|
|
|
public override async Task HandleAsync(BaseClientNode recipient, PingPacket packet, CancellationToken cancellationToken = default)
|
2022-05-18 16:39:48 +01:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Client {recipient.SessionId} sent ping with payload {BitConverter.ToString(packet.Payload)}");
|
2022-07-07 13:17:13 +01:00
|
|
|
|
var pong = new PongPacket(packet.CallbackId, packet.Payload);
|
2022-05-18 17:18:33 +01:00
|
|
|
|
await recipient.SendPacketAsync(pong, cancellationToken);
|
2022-05-18 16:39:48 +01:00
|
|
|
|
}
|
|
|
|
|
}
|