Skip to content

Packet Context

PacketContext<TPacket> is the pooled per-request object used by context-aware handlers in Nalix.Network. It carries the packet, connection, resolved metadata, cancellation token, and a pooled sender that can emit outbound packets using the current handler rules.

Source mapping

  • src/Nalix.Network/Routing/PacketContext.cs

State carried by the context

Property Purpose
Packet Current deserialized packet instance.
Connection Current IConnection.
Attributes PacketMetadata resolved for the handler.
CancellationToken Request-scoped cancellation token.
SkipOutbound Internal flag that skips normal outbound middleware after the handler finishes.
Sender Pooled IPacketSender<TPacket> resolved during initialization.

Pooling behavior

  • implements IPoolable
  • uses ObjectPoolManager
  • preallocates and sets pool limits based on PoolingOptions
  • Initialize(...) moves the object into the in-use state and rents a PacketSender<TPacket>
  • Reset() returns the rented sender and clears packet, connection, and metadata state
  • Return() guards against double-return races

Handler guidance

Use a context-based handler when you need metadata or manual sending:

Example

[PacketOpcode(0x1002)]
public async ValueTask Handle(PacketContext<MyPacket> context, CancellationToken ct)
{
    await context.Sender.SendAsync(BuildReply(context.Packet), ct);
}

Returning a packet uses the normal return-type pipeline. Sending through context.Sender is the explicit path for immediate or multiple replies.