Packet Sender
On this page
Namespace: Nalix.Runtime.Dispatching
Assembly: Nalix.Runtime
PacketSender (public sealed class PacketSender : IPacketSender) is the default runtime implementation of IPacketSender bound to each pooled PacketContext<TPacket> and exposed via context.Sender.
Source Mapping
src/Nalix.Abstractions/Networking/Packets/IPacketSender.cssrc/Nalix.Runtime/Dispatching/PacketSender.cssrc/Nalix.Runtime/Extensions/ConnectionExtensions.cs
How PacketSender Routes & Transforms Outbound Packets
When SendAsync or ReplyAsync is called:
- Transport Selection (
GetTransport):- If the handler has
[PacketTransport(NetworkTransport.UDP)], selectsconnection.UDP(throwingInvalidOperationExceptionif the UDP companion transport is not initialized). - If
[PacketTransport]specifiesTCPorWEBSOCKET, selectsconnection.TCP. - If
[PacketTransport]is absent, replies onconnection.UDPwhen the inbound request arrived over UDP (!context.IsReliable && connection.UDP is not null), orconnection.TCPotherwise.
- If the handler has
- Encryption Decision:
SendAsync(packet, ct)usesneedEncrypt = context.Attributes.Encryption?.IsEncrypted ?? false(from[PacketEncryption]).SendAsync(packet, forceEncrypt, ct)overrides the attribute policy withforceEncrypt.
- Serialization & Frame Transformation:
- Serializes
packetinto a pooledBufferLeaseviaPacketPipeline.Serialize(packet, zeroOnDispose: needEncrypt). - Calls
PacketPipeline.ProcessAndSendAsyncwithCompressionOptions.EnabledandCompressionOptions.MinSizeToCompress, applying LZ4 compression (if beneficial) and AEAD/symmetric encryption before sending on the selected transport. - Disposes the
BufferLeasedeterministically on both synchronous and asynchronous completion paths.
- Serializes
Public Methods (IPacketSender)
| Method | Signature | Description |
|---|---|---|
ReplyAsync | public ValueTask ReplyAsync(IPacket response, CancellationToken ct = default) | Copies the inbound request’s SequenceId onto response.Header.SequenceId (for client RequestAsync / StreamAsync correlation) and calls SendAsync(response, ct). |
SendAsync (default policy) | public ValueTask SendAsync(IPacket packet, CancellationToken ct = default) | Serializes and sends packet using the handler’s [PacketEncryption] and [PacketTransport] metadata without altering packet.Header.SequenceId. |
SendAsync (explicit encryption) | public ValueTask SendAsync(IPacket packet, bool forceEncrypt, CancellationToken ct = default) | Serializes and sends packet with an explicit forceEncrypt override. |
Initialize<TPacket> | public void Initialize<TPacket>(IPacketContext<TPacket> context) where TPacket : IPacket | Binds the sender to context (invoked automatically by PacketContext<TPacket>.Initialize). |
ResetForPool | public void ResetForPool() | Clears connection, metadata, token, and sequence state when PacketContext<TPacket> returns to the pool. |
Usage Example
using System.Threading.Tasks;
using Nalix.Abstractions.Networking.Packets;
using Nalix.Codec.Pooling;
[PacketOpcode(0x1010)]
[PacketEncryption(true)]
public async ValueTask HandleOrder(IPacketContext<OrderRequestPacket> context)
{
using PacketScope<OrderAckPacket> lease = PacketFactory<OrderAckPacket>.Acquire();
OrderAckPacket ack = lease.Value;
ack.Initialize(orderId: 9001);
// ReplyAsync automatically stamps ack.Header.SequenceId = context.Packet.Header.SequenceId
await context.Sender.ReplyAsync(ack, context.CancellationToken);
}