Beskar.CodeGeneration.PacketGenerator 1.2.1

dotnet add package Beskar.CodeGeneration.PacketGenerator --version 1.2.1
                    
NuGet\Install-Package Beskar.CodeGeneration.PacketGenerator -Version 1.2.1
                    
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="Beskar.CodeGeneration.PacketGenerator" Version="1.2.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Beskar.CodeGeneration.PacketGenerator" Version="1.2.1" />
                    
Directory.Packages.props
<PackageReference Include="Beskar.CodeGeneration.PacketGenerator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Beskar.CodeGeneration.PacketGenerator --version 1.2.1
                    
#r "nuget: Beskar.CodeGeneration.PacketGenerator, 1.2.1"
                    
#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.
#:package Beskar.CodeGeneration.PacketGenerator@1.2.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Beskar.CodeGeneration.PacketGenerator&version=1.2.1
                    
Install as a Cake Addin
#tool nuget:?package=Beskar.CodeGeneration.PacketGenerator&version=1.2.1
                    
Install as a Cake Tool

Beskar.CodeGeneration.PacketGenerator

The Beskar Packet Generator is a high-performance, source-generator-based framework for handling binary or JSON packet serialization and automated routing in C#. It removes boilerplate by automatically generating the necessary logic to identify, deserialize, and dispatch packets to their respective handlers.

Core Concepts

1. The Packet

A packet is any class or struct that implements the IPacket marker interface. You mark it with the [Packet] attribute to associate it with one or more registries.

[Packet(typeof(GamePacketRegistry))]
public sealed class PlayerMovePacket : IPacket
{
   public required float X { get; init; }
   public required float Y { get; init; }
}

2. The Registry

The Registry is the central hub. By inheriting from BasePacketRegistry (for custom binary logic) or BaseJsonPacketRegistry (for UTF8 JSON), the source generator injects the complex RoutePacket logic.

Important: The registry and all its packets must be in the same assembly.

// JSON based (not recommended)
[PacketRegistry]
public sealed partial class GamePacketRegistry : BaseJsonPacketRegistry;

// Your binary packet registry
[PacketRegistry] 
public sealed partial class GamePacketRegistry : BasePacketRegistry 
{
    // override Serialize and TryDeserialize
}

Features

  • Zero Reflection: All routing and type identification are resolved at compile-time for maximum performance.
  • Flexible Serialization: Use the built-in JSON registry or implement your own binary format by overriding Serialize and TryDeserialize.
  • Memory Efficient: Utilizes ReadOnlySequence<byte> and BufferWriter<byte> to minimize allocations and support modern memory primitives.
  • Multi-Registry Support: A single packet can be registered across multiple different registries (e.g., a PingPacket used in both AuthRegistry and GameRegistry).

Usage Example

Serialization with Headers

The registry can automatically prepend a unique identifier to your payload, allowing for easy identification on the receiving end.

var registry = new GamePacketRegistry();
var packet = new PlayerMovePacket { X = 10, Y = 20 };

// Serializes the packet ID + JSON data into a byte array
// Also supports using a BufferWriter<byte> instead of byte[]
byte[] data = registry.SerializeWithHeader(packet);

Automated Routing

When you receive a byte sequence, simply pass it to the registry. The generator-produced code will identify the packet type and invoke the corresponding logic.

// Automatically identifies 'PlayerMovePacket' and routes it
await registry.RoutePacket(receivedData, cancellationToken);

Register handlers

registry.RegisterHandler<PlayerMovePacket>((ref packet, ct) =>
{
    // if async behavior is required, you need a second method called here,
    // since the packet is by reference (for better large structs)
    Console.WriteLine("Test");
    return ValueTask.CompletedTask;
});
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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.2.1 107 4/29/2026
1.2.0 113 4/26/2026
1.1.9 100 4/25/2026
1.1.8 106 4/8/2026
1.1.7 105 4/7/2026
1.1.6 117 4/5/2026
1.1.5 107 4/5/2026