NetTCP.Server 1.0.0-beta5

This is a prerelease version of NetTCP.Server.
dotnet add package NetTCP.Server --version 1.0.0-beta5
NuGet\Install-Package NetTCP.Server -Version 1.0.0-beta5
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NetTCP.Server" Version="1.0.0-beta5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NetTCP.Server --version 1.0.0-beta5
#r "nuget: NetTCP.Server, 1.0.0-beta5"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install NetTCP.Server as a Cake Addin
#addin nuget:?package=NetTCP.Server&version=1.0.0-beta5&prerelease

// Install NetTCP.Server as a Cake Tool
#tool nuget:?package=NetTCP.Server&version=1.0.0-beta5&prerelease

NetTCP

Simple TCPListener and TCPClient wrapper for .NET 6+. Provides easy access to events and messages/op codes and handlers

Simple use and support for Autofac DI container and scopes for each message handler instance

Warning

This is a work in progress and not ready for production use. Use at your own risk

If you want to fork and improve or implement missing features, feel free to do so

Installation

You can install the package via Nuget:

Install-Package NetTCP
Install-Package NetTCP.Client
Install-Package NetTCP.Server

Features

  • TCPListener wrapper as NetTcpServer with NetTcpConnection for each client
  • TCPClient wrapper as NetTcpClient
  • Packet handler with opcodes
  • Autofac DI container for each message handler
  • Packet pool for each server and client
  • Packet registration from assembly
  • Fully implmented events for server

Todo

  • Fully implment events for client
  • DI Scope for each connection aside from message handler instance scope
  • Better logging (Serilog maybe)
  • Better error handling
  • Improve performance and cleanup
  • Allowing contructor injection for message handlers
  • Better documentation
  • Better abstraction for server and client to be overriden for custom implementations

Usage

  • Install NuGet package
  • Create 3 projects (Shared, Client, Server)
  • Add reference to Shared project in Client and Server
  • Define opcodes and packets in Shared project
  • Register packets in Client and Server project from Shared project assembly
  • Define packet handlers in Client and Server project
  • Build and run

Things to note

  • Each Packet class must implement IPacket interface as well ass [PacketAttribute] otherwise it will not register
  • Each Packet Handler method must have 3 parameters
    • First param must be NetTcpConnection for server or NetTcpClient for Client
    • Second param must be a class inherits from IPacket and has PacketAttribute
    • Third param must be ILifeTimeScope (even if you are not gonna use it you have to add it to method param)
  • Defining 2 Message Handlers for same op code will throw an error (Dictionary key error)
  • Defining 2 Packets with same op code will throw an error (Dictionary key error)
  • Assembly types will be merged meaning when you use RegisterPacketsFromAssembly you can use as many assembly as you want

Examples

For more details see the example project

Shared (Client and Server)

Define enum for opcodes

public enum OpCodes
{
  CMPing = 1000,
  SMPong
}

Define a client packet

[Packet(OpCodes.CMPing)]
public sealed class CmPing : IPacket
{
  public long Timestamp { get; set; } = 0;
}

Define a server packet

[Packet(OpCodes.SMPong)]
public sealed class SmPong : IPacket
{
  public long Timestamp { get; set; } = 0;
}
Client

Program.cs

// Create a new client builder
var builder = NetTcpClientBuilder.Create();

//Register packet from another assembly
builder.RegisterPacketsFromAssembly(typeof(OpCodes).Assembly);

//Build NetTcpClient
var client = builder.Build("127.0.0.1", 8080);

//Register events here (not implemented yet)
 
//Connect to server
client.Connect();

//Block thread until client is disconnected and send ping every 5 seconds
while (client.CanProcess) {
  Thread.Sleep(5000);
  client.EnqueuePacketSend(new CmPing {
    Timestamp = 123123
  });
}

Console.WriteLine("Client disconnected");

Client Packet Handler

public static class PongHandler
{
  public static void HandlePing(NetTcpClient client, SmPong request, ILifetimeScope scope) {
    Console.WriteLine($"[NetTCP - Client] Pong received from server with timestamp {request.Timestamp}.");
    Thread.Sleep(1000); //dont do this
    client.EnqueuePacketSend(new CmPing {
      Timestamp = request.Timestamp + 1
    });
  }
}
Server

Program.cs

// Create a new server builder
var builder = NetTcpServerBuilder.Create();

//Register packet from another assembly
//Entry assembly is always added to the packet pool
builder.RegisterPacketsFromAssembly(typeof(OpCodes).Assembly);

//Register a service to DI container
builder.RegisterSingleton<IServerInfoMgr, ServerInfoMgr>();

//Register a handler for a packet and builds DI container
var server = builder.Build("127.0.0.1", 8080);

//Register events
server.ServerStarted += (sender, args) => { Console.WriteLine("Server started on " + server.IpAddress + ":" + server.Port); };

//Start listening also blocks the thread
server.StartServerAsync().GetAwaiter().GetResult();

Server Packet Handler

public static class PingHandler
{
  public static void HandlePing(NetTcpConnection connection, CmPing request, ILifetimeScope scope) {
    var serverInfoMgr = scope.Resolve<IServerInfoMgr>();
    Console.WriteLine($"[NetTCP - Server - {serverInfoMgr.Name}] Ping received from {connection.RemoteIpAddress} with timestamp {request.Timestamp}.");
    Thread.Sleep(1000); //dont do this
    connection.EnqueuePacketSend(new SmPong {
      Timestamp = request.Timestamp + 1
    });
  }
}
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0-beta5 78 1/23/2024
1.0.0-beta4 47 1/23/2024
1.0.0-beta3 46 1/23/2024
1.0.0-beta2 48 1/22/2024
1.0.0-beta1 48 1/22/2024