NuGet package: ProtankiNetworking on NuGet
A C# library for ProTanki game communication. This project is based on code from the ProboTanki-Lib Python library, but it is not exact port.
The library provides three main components for TCP networking:
TankiTcpListener: Base class for TCP server implementation that accepts client connectionsTankiTcpClientHandler: Base class for handling individual client connections to TankiTcpListenerTankiTcpClient: Base class for TCP client implementation
public class MyTankiServer : TankiTcpListener
{
public MyTankiServer(IPEndPoint localEndPoint, Protection protection)
: base(localEndPoint, protection)
{
}
protected override TankiTcpClientHandler CreateClientHandler(
TcpClient client,
CancellationToken cancellationToken)
{
return new MyClientHandler(client, new Protection(), cancellationToken);
}
protected override async Task OnErrorAsync(Exception exception, string context)
{
Console.WriteLine($"Server error in {context}: {exception.Message}");
}
}
// Usage:
var endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080);
var server = new MyTankiServer(endPoint);
server.Start();public class MyClientHandler : TankiTcpClientHandler
{
public MyClientHandler(
TcpClient client,
Protection protection,
CancellationToken cancellationToken)
: base(client, protection, cancellationToken)
{
}
protected override async Task OnRawPacketReceivedAsync(byte[] rawPacket)
{
Console.WriteLine($"Received raw packet of length {rawPacket.Length}");
}
protected override async Task OnPacketReceivedAsync(Packet packet)
{
Console.WriteLine($"Received packet of type {packet.GetType().Name}");
}
protected override async Task OnErrorAsync(Exception exception, string context)
{
Console.WriteLine($"Handler error in {context}: {exception.Message}");
}
}public class MyTankiClient : TankiTcpClient
{
public MyTankiClient(IPEndPoint serverEndPoint, Protection protection)
: base(serverEndPoint, protection)
{
}
protected override async Task OnRawPacketReceivedAsync(byte[] rawPacket)
{
Console.WriteLine($"Received raw packet of length {rawPacket.Length}");
}
protected override async Task OnPacketReceivedAsync(Packet packet)
{
Console.WriteLine($"Received packet of type {packet.GetType().Name}");
}
protected override async Task OnErrorAsync(Exception exception, string context)
{
Console.WriteLine($"Client error in {context}: {exception.Message}");
}
}
// Usage:
var endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080);
var protection = new Protection(); // Configure protection as needed
var client = new MyTankiClient(endPoint, protection);
await client.ConnectAsync();Codec/- Data encoding/decoding systemNetworking/- Network communication utilitiesPackets/- Game packet definitionsSecurity/- Security and protection mechanisms
This library is for educational purposes only. Use of this library should comply with ProTanki's terms of service. (so you can't use it at all :D)