Krut.Netcode 1.1.0

dotnet add package Krut.Netcode --version 1.1.0
                    
NuGet\Install-Package Krut.Netcode -Version 1.1.0
                    
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="Krut.Netcode" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Krut.Netcode" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Krut.Netcode" />
                    
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 Krut.Netcode --version 1.1.0
                    
#r "nuget: Krut.Netcode, 1.1.0"
                    
#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 Krut.Netcode@1.1.0
                    
#: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=Krut.Netcode&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Krut.Netcode&version=1.1.0
                    
Install as a Cake Tool

<div align="center">

<img src="assets/icon.svg" alt="Krut.Netcode" width="96" height="96" />

Krut.Netcode

Deterministic, zero-allocation, Native-AOT authoritative-server netcode for .NET 9 on LiteNetLib.

CI NuGet NuGet Downloads .NET 9 License: MIT

Quick Start • Architecture • Prediction & Interpolation • Link Simulator • Benchmarks • Documentation

</div>


Overview

Krut.Netcode is a lightweight, engine-agnostic multiplayer networking library built on top of LiteNetLib and System.Numerics. Extracted from the dedicated server and client engine of Krut.Racing, it provides the core primitives needed for fast-paced, authoritative-server multiplayer games without tying you to a specific game engine, scene graph, or reflection-heavy serializer.

Key Capabilities

Area What Krut.Netcode Provides
Session & Handshake NetworkServer and NetworkClient with application ID, protocol version, sanitized player names, custom ConnectionApproval, explicit rejection reasons, and library-managed auto-reconnect.
Zero-Allocation Messaging Static per-type pools (NetworkMessagePool<T>), single-pass Broadcast serialization, ArrayPool<byte> receive buffers, and explicit NetDataWriter/NetDataReader codecs (0 B allocated per packet in steady state).
Native-AOT Ready Zero runtime reflection, zero dynamic code generation, and zero attribute scanning. Verified in CI with PublishAot=true and trim/AOT warnings treated as errors.
Fixed-Tick Clock & Time Sync Frame-rate-independent NetworkClock with catch-up clamping, plus NTP-style NetworkTimeSync using minimum-RTT filtering and a rolling median offset.
Client Prediction & Replay NetworkReconciler<TState, TInput> with ring-buffered input history, authoritative rollback/replay, and exponential/ease-out visual error smoothing with teleport thresholds.
Snapshot Interpolation NetworkSnapshotInterpolator<TState> for remote entities with sub-tick rendering, bounded dead-reckoning extrapolation, and SIMD-accelerated component blending (NetworkStateBlend).
Deterministic Link Simulator Built-in NetworkSimulation (latency, jitter, Gilbert–Elliott burst loss, packet reorder, duplication, token-bucket bandwidth cap, and scheduled blackouts) at message or raw datagram level.
Hardened Wire Codec 16-bit quantized 3D positions and unit quaternions, non-finite (NaN/Infinity) sanitization, UTF-16 surrogate-safe truncation, control/Bidi-override stripping, and strict packet-length validation.
Authenticated Transport Encryption Opt-in two-tier AES-256-GCM encryption: 0-RTT pre-shared key / match-token datagram encryption (including handshake) and/or per-peer ephemeral ECDH P-256 session keys with optional ECDsa server identity pinning and 64-packet anti-replay windows.
NAT Punchthrough & Rendezvous Built-in NativeAOT-safe binary "KNAT" v1 hole punching (NetworkServer.StartWithNat, NetworkClient.ConnectViaNat) and a standalone bounded/rate-limited NetworkNatRendezvousServer (samples/NatRendezvousServer) with 128-bit nonces/HostSecret, optional Rendezvous AES-256-GCM PSK, and full Match PSK / ECDH compatibility.

Scope & Non-Goals

Krut.Netcode is a focused core networking foundation — not a full entity-replication framework or game engine runtime. While opt-in transport encryption (NetworkEncryptionConfig) and server identity pinning protect confidentiality, integrity, and replay resistance on the wire, the library intentionally leaves the following to your game or server layer:

  • Entity lifecycle & network IDs (spawning, despawning, object ownership mapping)
  • Snapshot serialization & delta compression (what fields exist per entity, baseline/delta encoding)
  • Interest management & prioritization (spatial/team visibility, relevancy filtering, send budgets)
  • Key distribution, account auth & relay fallback (matchmaking token distribution, player account authentication, and UDP/TURN relay fallback when Symmetric NAT blocks direct hole punching)
  • Authoritative gameplay validation & anti-cheat (verifying entity ownership, input rates, tick windows, movement/acceleration/speed bounds, cooldowns, and valid action states server-side)

Installation

Install from NuGet:

dotnet add package Krut.Netcode

Architecture

Everything runs on the thread that calls session.Update(deltaSeconds) — no background callbacks into game logic and no locks required in user code.

flowchart LR
    subgraph Client["Client (Predicts & Interpolates)"]
        direction TB
        CClock["NetworkTimeSync\nMin-RTT + Median Offset"]
        CReconcile["NetworkReconciler&lt;TState, TInput&gt;\nPredict, Rollback & Visual Smoothing"]
        CInterp["NetworkSnapshotInterpolator&lt;TState&gt;\nRender DelayTicks in Past"]
    end

    subgraph Link["UDP / LiteNetLib + NetworkSimulation"]
        direction TB
        Ch0["Channel 0: Input (Unreliable)"]
        Ch1["Channel 1: Snapshot (Unreliable)"]
        Ch2["Channel 2: Events (ReliableOrdered)"]
        Ch3["Channel 3: Control (Reliable)"]
    end

    subgraph Server["Server (Headless, Authoritative)"]
        direction TB
        STick["NetworkServer.OnTick\nFixed Tick Clock"]
        SSim["Authoritative Simulation\nApply Inputs & Broadcast State"]
        STick --> SSim
    end

    CReconcile -- "1. Tick Input" --> Ch0 --> SSim
    SSim -- "2. Authoritative Snapshot" --> Ch1 --> CReconcile & CInterp
    SSim -- "3. Discrete Game Events" --> Ch2 --> Client
    CClock <-- "4. Handshake & Clock Probes" --> Ch3 <-- "Welcome / Sync" --> Server

Quick Start

1. Define and Register a Game Message

Game messages claim a stable wire byte in 16..255 (1..15 are reserved for built-in protocol messages) and serialize fields explicitly in matching order:

using Krut.Netcode;
using LiteNetLib.Utils;

public sealed class CarInputMessage : INetworkMessage
{
    public const byte Wire = 16;

    public NetworkMessageType Type => NetworkMessageType.None;
    public byte WireType => Wire;
    public NetworkDelivery Delivery => NetworkDelivery.Unreliable;
    public byte Channel => NetworkChannels.Input;

    public int Tick;
    public float Throttle;
    public float Steer;

    public void Reset()
    {
        Tick = 0;
        Throttle = 0f;
        Steer = 0f;
    }

    public void Write(NetDataWriter writer)
    {
        writer.Put(Tick);
        writer.Put(Throttle);
        writer.Put(Steer);
    }

    public void Read(NetDataReader reader)
    {
        Tick = reader.GetInt();
        // Clamp untrusted floats on decode; server logic must still verify tick windows, rate, and physics limits.
        Throttle = NetworkCodec.ReadClampedFloat(reader, -1f, 1f);
        Steer = NetworkCodec.ReadClampedFloat(reader, -1f, 1f);
    }
}

// Register once at startup (shared across sessions in the same process):
NetworkMessages.RegisterGameMessage<CarInputMessage>(CarInputMessage.Wire, NetworkDelivery.Unreliable);

2. Run the Server and Client Loop

var config = new NetworkConfig
{
    ApplicationId = "my.game",
    ProtocolVersion = 1,
    Port = 25555,
    TickRate = 60, // Match your fixed physics timestep
    MaxClients = 16,
};

using var server = new NetworkServer(config);
server.MessageReceived += envelope =>
{
    if (envelope.Is<CarInputMessage>(out var input))
    {
        // Apply input for envelope.PeerId; envelope.Message is recycled when this handler returns.
    }
};
server.Start();

using var client = new NetworkClient(config);
client.SessionReady += () => Console.WriteLine($"Ready as peer #{client.LocalPeerId} @ {client.Config.TickRate} Hz");
client.Closed += (reason, text) => Console.WriteLine($"Disconnected ({reason}): {text}");
client.Connect("127.0.0.1", config.Port, playerName: "Alice");

// Main loop: call Update(dt) once per frame
while (running)
{
    server.Update(dt);
    client.Update(dt);

    if (client.IsSessionReady)
    {
        var msg = NetworkSession.Rent<CarInputMessage>();
        msg.Tick = client.CurrentTick;
        msg.Throttle = 1.0f;
        msg.Steer = -0.25f;
        client.Send(client.ServerPeerId, msg); // Automatically recycled after Send
    }
}

Client Prediction & Interpolation

The lag-compensation primitives in Prediction/ and Time/ are completely decoupled from sockets and sessions. They operate over any unmanaged state/input structs with zero per-frame allocations:

// 1. Local entity: predict immediately, rewind & replay unacknowledged inputs on server snapshot
var reconciler = new NetworkReconciler<UnitState, UnitInput>(VisualSmoothingSettings.Default)
{
    Step = UnitSim.Step,          // Pure function: (state, tick, input) -> nextState
    DistanceMetric = UnitSim.Distance,
};

// Each client tick:
UnitState predicted = reconciler.Predict(client.CurrentTick, localInput);

// When an authoritative snapshot arrives from the server:
if (reconciler.SetAuthoritativeState(snapshot.ServerTick, snapshot.OwnState))
{
    reconciler.Reconcile(snapshot.ServerTick, snapshot.LastProcessedInputTick,
        static (authState, ackTick, inputs, count) =>
        {
            var s = authState;
            for (int i = 0; i < count; i++)
                s = UnitSim.Step(s, ackTick + 1 + i, inputs[i]);
            return s;
        });
}

// Each render frame: advance visual error decay and render SmoothedState
reconciler.UpdateRenderState(frameDeltaSeconds);
RenderLocalUnit(reconciler.SmoothedState);

// 2. Remote entities: buffer snapshots and sample DelayTicks in the past
var interpolator = new NetworkSnapshotInterpolator<UnitState>(delayTicks: 3);
interpolator.Push(snapshot.ServerTick, snapshot.RemoteState);

double renderTick = client.CurrentTick + client.TickProgress - interpolator.DelayTicks;
if (interpolator.TrySampleTime(renderTick, out UnitState remoteState, out NetworkSampleMode mode))
{
    RenderRemoteUnit(remoteState);
}

Because LiteNetLib strips its internal debug simulator from Release NuGet builds, Krut.Netcode ships a deterministic, seeded link simulator that works in all build configurations. Configure it in code or via environment variables:

# Apply a named profile ("clean", "lan", "gate", "stress", "bad", "blackout", "thin")
export KRUT_NET_PROFILE=gate
export KRUT_NET_SEED=42
Environment Variable Description
KRUT_NET_PROFILE Named preset in round-trip terms: clean (0 ms), lan (2 ms), gate (100 ms, 2% loss), stress (180 ms, 2% loss), bad (250 ms, 5% burst loss, reorder & dup), blackout (scheduled outages), thin (64 kbit/s downlink cap).
KRUT_NET_LAG_MS Round-trip latency in ms (split equally per direction when resolved by side).
KRUT_NET_JITTER_MS Round-trip latency variation ($\pm$ ms).
KRUT_NET_LOSS Packet drop probability (0.0 to 1.0).
KRUT_NET_BURST Mean burst loss length in messages (Gilbert–Elliott 2-state Markov model).
KRUT_NET_REORDER / KRUT_NET_DUP Message reorder probability and duplication probability (0.0 to 1.0).
KRUT_NET_BW_KBIT Token-bucket server downlink bandwidth cap in kbit/s with tail-drop queue.
KRUT_NET_OUTAGES Comma-separated start:duration blackout windows in seconds (e.g. 15:1,30:5).
KRUT_NET_DATAGRAM 1 or true moves loss, bursts, and outages to the raw LiteNetLib UDP datagram layer (NetworkDatagramSimLayer).

Headless Prediction Demo

Run the self-contained headless verification harness (samples/PredictionDemo) simulating a server and two clients over localhost UDP with a degraded link:

dotnet run --project samples/PredictionDemo -c Release -- --profile gate --seconds 10
Link: gate, 100 ms RTT +/-10, 2 % loss per direction
Running 10 s of play ...

                                                client 0    client 1
measured RTT, min / max (ms)                     124/156     124/156
clock lead (ticks)                                     8           8
packets dropped by link, up / down (%)           1.4/1.5     2.2/1.5
inputs repeated / late at server                    10/0        11/0

error vs server, no reconcile: mean (mm)            16.4        11.1
error vs server, no reconcile: max (mm)             30.4        18.0
error vs server, reconciled: mean (mm)               0.1         0.1
error vs server, reconciled: max (mm)                1.4         2.3

snapshots reconciled (rollbacks)                     299         290
inputs replayed, total / per rollback             2808/9     3048/10
rollback misses / ignored snapshots                  0/0         0/0

PASS: prediction stays close to the server, replay corrects the rest.

Benchmarks

Measured with BenchmarkDotNet on .NET 9 (benchmarks/Krut.Netcode.Benchmarks):

dotnet run -c Release --project benchmarks/Krut.Netcode.Benchmarks -- --filter "*"
Benchmark Mean Time Allocated
CodecBenchmarks.WritePositionAndRotation ~29 ns 0 B
CodecBenchmarks.ReadPositionAndRotation ~23 ns 0 B
MessagePoolBenchmarks.RentWriteReadRecycle ~38 ns 0 B
InterpolatorBenchmarks.SampleOnly ~28 ns 0 B
ReconcilerBenchmarks.PredictAndReconcile (6 ticks / 100 ms RTT) ~277 ns 0 B
ReconcilerBenchmarks.PredictAndReconcile (18 ticks / 300 ms RTT) ~866 ns 0 B

Repository Layout & Documentation

Krut.Netcode/
├── src/Krut.Netcode/                  # Core library (net9.0, IsAotCompatible)
│   ├── Session/                       # NetworkSession, NetworkServer, NetworkClient, NetworkConfig
│   ├── Messages/                      # INetworkMessage, NetworkMessages, NetworkCodec, NetworkProtocol
│   ├── Time/                          # NetworkClock, NetworkTimeSync
│   ├── Prediction/                    # NetworkReconciler, NetworkSnapshotInterpolator, NetworkStateRing
│   ├── Security/                      # NetworkEncryptionConfig, NetworkEncryptionLayer, PeerSessionCrypto
│   ├── Nat/                           # NatPunchProtocol, NatPunchConfig, NetworkNatRendezvousServer
│   ├── Simulation/                    # NetworkSimulation, NetworkLinkModel, NetworkDelayQueue, Datagram layers
│   └── Diagnostics/                   # NetworkStatistics, NetDatagramProbe
├── tests/Krut.Netcode.Tests/          # xUnit test suite (handshake, codec, zero-alloc, security, NAT, simulator)
├── benchmarks/Krut.Netcode.Benchmarks/# BenchmarkDotNet throughput & memory benchmarks
├── samples/PredictionDemo/            # Headless server + 2 clients verification harness
├── samples/NatRendezvousServer/       # Deployable CLI NAT rendezvous server (--port, --psk, --generate-key)
└── docs/                              # In-depth user guide, maintainer architecture & Mermaid diagrams

License

Licensed under the MIT License. Copyright © 2026 William Eklund.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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.1.0 61 11/20/2026
1.0.2 64 11/20/2026
1.0.1 66 11/20/2026
1.0.0 52 11/20/2026
0.1.0 104 10/26/2026