Packet Dispatch¶
PacketDispatchChannel is the main asynchronous dispatch loop for raw network frames. It accepts IBufferLease payloads, queues them by connection and packet priority, runs buffer middleware, deserializes IPacket, and then invokes the compiled handler pipeline from PacketDispatcherBase<IPacket>.
Source mapping¶
src/Nalix.Network/Routing/PacketDispatchChannel.cssrc/Nalix.Network/Routing/PacketDispatcherBase.cssrc/Nalix.Network/Routing/Options/PacketDispatchOptions.cssrc/Nalix.Network/Routing/Options/PacketDispatchOptions.Execution.cssrc/Nalix.Network/Routing/Options/PacketDispatchOptions.PublicMethods.cs
Runtime model¶
_dispatchis a priority-awareDispatchChannel<IPacket>_semaphoresignals worker loops when leases are availableActivate(...)startsDispatchLoopCountworkers, or defaults toclamp(Environment.ProcessorCount / 2, 1, 12)Deactivate(...)cancels workers and releases the semaphore so blocked loops can exit
Input paths¶
HandlePacket(IBufferLease, IConnection):
- rejects empty leases
- pushes the lease into
_dispatch - releases the semaphore once
HandlePacket(IPacket, IConnection):
- bypasses the queue and directly executes the compiled handler pipeline
Worker loop¶
Each worker:
- waits on
_semaphore - pulls the next
(connection, lease)pair from_dispatch - runs
Options.NetworkPipeline.ExecuteAsync(...) - deserializes through
IPacketRegistry.TryDeserialize(...) - calls
ExecutePacketHandlerAsync(packet, connection) - disposes the lease
If middleware returns null, the packet is dropped before deserialization. If deserialization fails, the dispatcher logs the packet head in hex and drops the lease.
Diagnostics¶
GenerateReport() includes:
- running state
- dispatch loop count
- total pending packets
- total and ready connection counts
- pending ready connections per priority
- top connections by pending packet count
- semaphore count and cancellation status
- packet registry type
Basic usage¶
dispatch.Activate(ct);
await dispatch.HandlePacket(lease, connection);
string report = dispatch.GenerateReport();
Console.WriteLine(report);