UDP Server
On this page
This guide shows how to host the datagram listener (DatagramListener, the Nalix Datagram Protocol over UDP) and what to configure first.
Use it when you have already decided UDP is the right transport, but want the safest minimal server shape before layering in more game or telemetry logic.
Source Mapping
src/Nalix.Network/Listeners/Datagram/DatagramListener.Core.cssrc/Nalix.Network/Listeners/Datagram/DatagramListener.Receive.cssrc/Nalix.Network/Listeners/Datagram/DatagramListener.PublicMethods.cssrc/Nalix.Network/Options/DatagramOptions.cssrc/Nalix.Environment/Options/DatagramEngineOptions.cssrc/Nalix.Hosting/NetworkApplicationBuilder.cssrc/Nalix.Runtime/Handlers/DatagramTicketHandlers.cs
When UDP makes sense
Choose UDP when you care more about:
- low latency
- tolerance for packet loss
- lightweight datagrams
- telemetry, state sync, discovery, or custom real-time protocols
Choose TCP when you need ordered, reliable byte streams by default. A datagram session can also send reliably: each message is sent with a DeliveryMethod (Unreliable, UnreliableSequenced, ReliableUnordered, ReliableOrdered, ReliableSequenced) on one of the session’s channels.
Minimal shape
await using NetworkApplication app = NetworkApplication.CreateBuilder()
.UseSecureConnections()
.MapHandlers(typeof(MyHandlers))
.MapTcp<DefaultProtocol>().OnPort(57206).Bind()
.MapDatagram<DefaultProtocol>().OnPort(57206).Bind()
.Build();
await app.RunAsync(cts.Token);
MapDatagram<TProtocol>() creates a DatagramListener and maps the ticket handlers (DatagramTicketHandlers) for you. It can be called once per application, because tickets are issued by a single listener; a second call throws InvalidOperationException. There is no subclass to write and no IsAuthenticated(...) hook: authentication is part of the protocol (see UDP Security).
A datagram session is always created from an established TCP or WebSocket session. The client asks for a ticket over that stream (the handshake must be complete), then opens a UDP socket and sends its first datagram within the ticket lifetime.
What you must decide
1. Session story
Use the stream connection to establish identity (handshake, login, permissions) and the datagram session to carry fast traffic. The datagram connection inherits the completed handshake of its stream connection.
2. Delivery method per handler
A handler can declare how its responses travel on a datagram session with PacketDeliveryAttribute (DeliveryMethod method, byte channel = 0). Without the attribute the session’s own transport is used as is.
3. Runtime tuning
DatagramOptions (listener, in Nalix.Network.Options):
| Property | Default | Meaning |
|---|---|---|
TicketLifetimeSeconds | 10 | Seconds within which the first datagram of a ticket must arrive (1-300) |
MaxSessions | 100000 | Largest number of sessions plus pending tickets |
ReceiveWorkers | 1 | Receive workers reading the UDP socket (1-64) |
SocketReceiveBufferSize | 4194304 | UDP socket receive buffer, bytes |
SocketSendBufferSize | 4194304 | UDP socket send buffer, bytes |
DatagramEngineOptions (per session, in Nalix.Environment.Options) includes ChannelCount (default 8, max 64), IdleTimeoutMs (15000), MaxAckDelayMs (25), UnreliableExpiryMs (100), MaxMessageSize (1048576), EnableMtuProbing (true) and the receive/send buffer budgets and key-update intervals. DatagramEngineOptions.Validate() rejects budgets that cannot hold one maximum-size message.
Per-IP admission is handled by the ConnectionGuard the host passes to the listener: it is consulted when a session is promoted, and a rejection is counted in TotalGuardRejections.
Recommended startup order
The diagram shows how MapDatagram plus the options, hub and guard produce one DatagramListener (NetworkApplicationBuilder.cs:268-290, 426-433).
flowchart TD
A[CreateBuilder] --> B[MapDatagram]
B --> C[Map DatagramTicketHandlers]
B --> D[OnPort and Bind]
C --> E[Build]
D --> E
O[DatagramOptions and engine options] --> F[DatagramListener]
G[ConnectionHub and ConnectionGuard] --> F
E --> F
F --> H[Register IDatagramTicketIssuer]
H --> I[RunAsync activates listener]
I --> J[Receive workers and ticket sweep]- Initialize core managers (
ObjectPoolManager,BufferPoolManager) - Validate options
- Register logger and build
PacketRegistry - Build the dispatch channel (
PacketDispatchChannel) - Build the protocol handler
- Build
DatagramListener(the hosting builder does this forMapDatagram) - Activate dispatch, then activate the listener
That keeps transport setup and application setup aligned.
Diagnostics you get for free
DatagramListener.Metrics (DMetrics) and GenerateReport() track:
- datagrams and bytes received (
DatagramsReceived,BytesReceived) - malformed drops (
DroppedMalformed) - unknown connection-id drops (
DroppedUnknown) - unauthenticated drops (
DroppedUnauthenticated) - tickets issued and expired (
TicketsIssued,TicketsExpired) - accepted sessions, guard rejections and errors (
TotalAccepted,TotalGuardRejections,TotalErrors)
SessionCount and PendingTicketCount show the live session and ticket tables.
Common pattern
receive datagram (at most DatagramEngine.MaxDatagramSize = 1472 bytes)
-> read the connection id from the cleartext datagram header
-> known session -> authenticate and decrypt it in the session's engine
-> pending ticket -> authenticate the first datagram, then promote it to a session
-> unknown id -> drop (DroppedUnknown)
-> failed check -> drop (DroppedUnauthenticated)
-> accepted session -> hand the message to the protocol and dispatch
What teams often miss
- UDP does not invent a second identity model: the ticket is issued over an authenticated stream session
- unauthenticated and unknown drops should be visible in diagnostics
- the largest datagram is
DatagramEngine.MaxDatagramSize(1472bytes); larger messages are fragmented by the engine up toMaxMessageSize - every datagram is sealed with ChaCha20-Poly1305, so frames are not encrypted a second time