[ci skip] Add integration tests
This commit is contained in:
parent
a5c329eefb
commit
869e18d615
|
@ -0,0 +1,21 @@
|
||||||
|
using TcpDotNet.Protocol;
|
||||||
|
|
||||||
|
namespace TcpDotNet.ClientIntegrationTest;
|
||||||
|
|
||||||
|
[Packet(0x02)]
|
||||||
|
internal sealed class GoodbyePacket : Packet
|
||||||
|
{
|
||||||
|
public string Message { get; set; }
|
||||||
|
|
||||||
|
protected override Task DeserializeAsync(ProtocolReader reader)
|
||||||
|
{
|
||||||
|
Message = reader.ReadString();
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override Task SerializeAsync(ProtocolWriter writer)
|
||||||
|
{
|
||||||
|
writer.Write(Message);
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
using TcpDotNet.Protocol;
|
||||||
|
|
||||||
|
namespace TcpDotNet.ClientIntegrationTest;
|
||||||
|
|
||||||
|
[Packet(0x01)]
|
||||||
|
internal sealed class HelloPacket : Packet
|
||||||
|
{
|
||||||
|
public string Message { get; set; }
|
||||||
|
|
||||||
|
protected override Task DeserializeAsync(ProtocolReader reader)
|
||||||
|
{
|
||||||
|
Message = reader.ReadString();
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override Task SerializeAsync(ProtocolWriter writer)
|
||||||
|
{
|
||||||
|
writer.Write(Message);
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
using TcpDotNet.Protocol;
|
||||||
|
|
||||||
|
namespace TcpDotNet.ClientIntegrationTest.PacketHandlers;
|
||||||
|
|
||||||
|
internal sealed class GoodbyePacketHandler : PacketHandler<GoodbyePacket>
|
||||||
|
{
|
||||||
|
public override Task HandleAsync(BaseClientNode recipient, GoodbyePacket packet, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Server sent {packet.Message}");
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,7 @@
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using TcpDotNet;
|
using TcpDotNet;
|
||||||
|
using TcpDotNet.ClientIntegrationTest;
|
||||||
|
using TcpDotNet.ClientIntegrationTest.PacketHandlers;
|
||||||
using TcpDotNet.Protocol;
|
using TcpDotNet.Protocol;
|
||||||
using TcpDotNet.Protocol.Packets.ClientBound;
|
using TcpDotNet.Protocol.Packets.ClientBound;
|
||||||
using TcpDotNet.Protocol.Packets.ServerBound;
|
using TcpDotNet.Protocol.Packets.ServerBound;
|
||||||
|
@ -7,29 +9,23 @@ using TcpDotNet.Protocol.Packets.ServerBound;
|
||||||
using var client = new ProtocolClient();
|
using var client = new ProtocolClient();
|
||||||
client.Disconnected += (_, e) => Console.WriteLine($"Disconnected: {e.DisconnectReason}");
|
client.Disconnected += (_, e) => Console.WriteLine($"Disconnected: {e.DisconnectReason}");
|
||||||
|
|
||||||
new Thread(() =>
|
|
||||||
{
|
|
||||||
ClientState oldState = client.State;
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
if (oldState != client.State)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"State changed to {client.State}");
|
|
||||||
oldState = client.State;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}).Start();
|
|
||||||
|
|
||||||
client.RegisterPacketHandler(PacketHandler<PongPacket>.Empty);
|
client.RegisterPacketHandler(PacketHandler<PongPacket>.Empty);
|
||||||
|
client.RegisterPacketHandler(new GoodbyePacketHandler());
|
||||||
await client.ConnectAsync(IPAddress.IPv6Loopback, 1234);
|
await client.ConnectAsync(IPAddress.IPv6Loopback, 1234);
|
||||||
|
|
||||||
Console.WriteLine($"Connected to {client.RemoteEndPoint}. My session is {client.SessionId}");
|
Console.WriteLine($"Connected to {client.RemoteEndPoint}. My session is {client.SessionId}");
|
||||||
var cancellationTokenSource = new CancellationTokenSource();
|
|
||||||
cancellationTokenSource.CancelAfter(5000); // if no pong is received in 5 seconds, cancel
|
|
||||||
|
|
||||||
var ping = new PingPacket();
|
var ping = new PingPacket();
|
||||||
Console.WriteLine($"Sending ping packet with payload: {BitConverter.ToString(ping.Payload)}");
|
Console.WriteLine($"Sending ping packet with payload: {BitConverter.ToString(ping.Payload)}");
|
||||||
var pong = await client.SendAndReceive<PingPacket, PongPacket>(ping, cancellationTokenSource.Token);
|
var pong = await client.SendAndReceive<PingPacket, PongPacket>(ping);
|
||||||
|
|
||||||
Console.WriteLine($"Received pong packet with payload: {BitConverter.ToString(pong.Payload)}");
|
Console.WriteLine($"Received pong packet with payload: {BitConverter.ToString(pong.Payload)}");
|
||||||
Console.WriteLine(pong.Payload.SequenceEqual(ping.Payload) ? "Payload matches!" : "Payload does not match!");
|
Console.WriteLine(pong.Payload.SequenceEqual(ping.Payload) ? "Payload matches!" : "Payload does not match!");
|
||||||
|
|
||||||
|
await client.SendPacketAsync(new HelloPacket {Message = "Hello, world!"});
|
||||||
|
|
||||||
|
while (client.IsConnected)
|
||||||
|
{
|
||||||
|
await client.ReadNextPacketAsync();
|
||||||
|
}
|
||||||
|
await Task.Delay(-1);
|
||||||
|
|
|
@ -11,8 +11,4 @@
|
||||||
<ProjectReference Include="..\TcpDotNet\TcpDotNet.csproj" />
|
<ProjectReference Include="..\TcpDotNet\TcpDotNet.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="PacketHandlers" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
using TcpDotNet.Protocol;
|
||||||
|
|
||||||
|
namespace TcpDotNet.ListenerIntegrationTest;
|
||||||
|
|
||||||
|
[Packet(0x02)]
|
||||||
|
internal sealed class GoodbyePacket : Packet
|
||||||
|
{
|
||||||
|
public string Message { get; set; }
|
||||||
|
|
||||||
|
protected override Task DeserializeAsync(ProtocolReader reader)
|
||||||
|
{
|
||||||
|
Message = reader.ReadString();
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override Task SerializeAsync(ProtocolWriter writer)
|
||||||
|
{
|
||||||
|
writer.Write(Message);
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
using TcpDotNet.Protocol;
|
||||||
|
|
||||||
|
namespace TcpDotNet.ListenerIntegrationTest;
|
||||||
|
|
||||||
|
[Packet(0x01)]
|
||||||
|
internal sealed class HelloPacket : Packet
|
||||||
|
{
|
||||||
|
public string Message { get; set; }
|
||||||
|
|
||||||
|
protected override Task DeserializeAsync(ProtocolReader reader)
|
||||||
|
{
|
||||||
|
Message = reader.ReadString();
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override Task SerializeAsync(ProtocolWriter writer)
|
||||||
|
{
|
||||||
|
writer.Write(Message);
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
using TcpDotNet.Protocol;
|
||||||
|
|
||||||
|
namespace TcpDotNet.ListenerIntegrationTest.PacketHandlers;
|
||||||
|
|
||||||
|
internal sealed class HelloPacketHandler : PacketHandler<HelloPacket>
|
||||||
|
{
|
||||||
|
public override Task HandleAsync(BaseClientNode recipient, HelloPacket packet, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Client sent {packet.Message}");
|
||||||
|
return recipient.SendPacketAsync(new GoodbyePacket {Message = "Nope"}, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
using TcpDotNet;
|
using TcpDotNet;
|
||||||
|
using TcpDotNet.ListenerIntegrationTest.PacketHandlers;
|
||||||
using TcpDotNet.Protocol;
|
using TcpDotNet.Protocol;
|
||||||
using TcpDotNet.Protocol.Packets.ClientBound;
|
using TcpDotNet.Protocol.Packets.ClientBound;
|
||||||
using TcpDotNet.Protocol.Packets.ServerBound;
|
using TcpDotNet.Protocol.Packets.ServerBound;
|
||||||
|
@ -7,6 +8,7 @@ var listener = new ProtocolListener();
|
||||||
listener.ClientConnected += (_, e) => Console.WriteLine($"Client connected from {e.Client.RemoteEndPoint} with session {e.Client.SessionId}");
|
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})");
|
listener.ClientDisconnected += (_, e) => Console.WriteLine($"Client {e.Client.SessionId} disconnected ({e.DisconnectReason})");
|
||||||
|
|
||||||
|
listener.RegisterPacketHandler(new HelloPacketHandler());
|
||||||
listener.RegisterPacketHandler(new PingPacketHandler());
|
listener.RegisterPacketHandler(new PingPacketHandler());
|
||||||
listener.Start(1234);
|
listener.Start(1234);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue