Nalix.Runtime
On this page
- Source Mapping#
- Where It Fits#
- Core Components#
- 1. Sharded Packet Dispatch (PacketDispatchChannel)#
- 2. Built-in Inbound Middleware (Nalix.Runtime.Middleware.Standard)#
- 3. Protection & Throttling Primitives#
- 4. Built-in Protocol Handlers (Nalix.Runtime.Handlers)#
- 5. Session Store & Connection Groups#
- Supported Handler Return Shapes#
- Related Packages#
- Key API Pages#
Nalix.Runtime is the Level 3 server-side packet orchestration engine. It powers multi-threaded sharded packet dispatch (PacketDispatchChannel), inbound/outbound middleware execution (MiddlewarePipeline<TPacket>), zero-reflection handler invocation, rate-limiting primitives, and session resumption (SessionService).
Source Mapping
src/Nalix.Runtime/Dispatching/PacketDispatchChannel.cssrc/Nalix.Runtime/Dispatching/PacketDispatcherBase.cssrc/Nalix.Runtime/Dispatching/PacketContext.cssrc/Nalix.Runtime/Dispatching/PacketSender.cssrc/Nalix.Runtime/Middleware/MiddlewarePipeline.cssrc/Nalix.Runtime/Middleware/Standard/PermissionMiddleware.cssrc/Nalix.Runtime/Middleware/Standard/RateLimitMiddleware.cssrc/Nalix.Runtime/Middleware/Standard/TimeoutMiddleware.cssrc/Nalix.Runtime/Handlers/HandshakeHandlers.cssrc/Nalix.Runtime/Handlers/ProofOfWorkHandlers.cssrc/Nalix.Runtime/Handlers/SessionHandlers.cssrc/Nalix.Runtime/Handlers/SystemControlHandlers.cssrc/Nalix.Runtime/Handlers/SystemTimeSyncHandlers.cssrc/Nalix.Runtime/Sessions/SessionService.cssrc/Nalix.Runtime/Groups/InMemoryGroupStore.cs
Where It Fits
flowchart TD
subgraph L3 ["Level 3 — Service Layer"]
Runtime["Nalix.Runtime (Dispatch, Middleware & Sessions)"]
Network["Nalix.Network (Listeners & Connections)"]
end
subgraph L2 ["Level 2 — Core Layer"]
Codec["Nalix.Codec (PacketRegistry & Serialization)"]
Framework["Nalix.Framework (InstanceManager & Pools)"]
end
subgraph L1_0 ["Levels 0 & 1 — Foundation"]
Env["Nalix.Environment (Memory & Configuration)"]
Abs["Nalix.Abstractions (Contracts & Attributes)"]
end
Runtime --> Codec
Runtime --> Framework
Network --> Framework
Codec --> Env
Framework --> Env
Env --> AbsCore Components
1. Sharded Packet Dispatch (PacketDispatchChannel)
PacketDispatchChannel is the high-throughput engine that processes incoming network frames:
- Fair per-connection scheduling & sharded worker loops — Worker loops scale to CPU core count (or
WithDispatchLoopCount(...)) and pull from priority-aware queues without head-of-line blocking across connections. - Priority queueing — Frames are scheduled according to
PacketPriority(URGENT,HIGH,MEDIUM,LOW,NONE) andDispatchOptionsdrop policies (DropNewest,DropOldest,Block,Coalesce). - Pre-handler encryption enforcement — Verifies
[PacketEncryption](context.IsEncrypted) inPacketHandler<TPacket>.CanExecutebefore invoking the handler. - Zero-reflection handler invocation — Resolves compiled handler delegates generated at build time by
PacketHandlerGenerator(supportingIPacketContext<TPacket>, trailing[FromScope]reference-type parameters resolved fromcontext.Scope, and[Inject]controller fields/properties).
using Nalix.Abstractions.Networking.Packets;
using Nalix.Runtime.Dispatching;
PacketDispatchChannel dispatch = new(options =>
{
options.WithErrorHandling((ex, opcode) =>
{
// Custom error hook for unhandled handler exceptions
})
.WithMiddleware(new MyAuditMiddleware())
.WithHandler(() => new AccountHandlers());
});
dispatch.Activate();
2. Built-in Inbound Middleware (Nalix.Runtime.Middleware.Standard)
Nalix.Runtime ships three attribute-driven inbound middleware components in Nalix.Runtime.Middleware.Standard:
| Middleware | Order | Stage | Behavior |
|---|---|---|---|
PermissionMiddleware | -50 | Inbound | Enforces [PacketPermission] (PermissionEvaluation.MinimumLevel or PermissionEvaluation.StrictMatch) against context.Connection.Level. |
RateLimitMiddleware | 50 | Inbound | Enforces per-opcode [PacketRateLimit] policies (PolicyRateLimiter) or falls back to global TokenBucketLimiter. |
TimeoutMiddleware | 75 | Inbound | Wraps handler execution in a linked CancellationTokenSource governed by [PacketTimeout]. |
3. Protection & Throttling Primitives
TokenBucketLimiter: Per-endpoint token bucket traffic shaper (TokenBucketOptions).PolicyRateLimiter: Enforces method-level[PacketRateLimit]quotas.DirectiveGuard: Prevents outboundDirectiveamplification under flood conditions (DirectiveGuardOptions).
4. Built-in Protocol Handlers (Nalix.Runtime.Handlers)
HandshakeHandlers&ProofOfWorkHandlers: Server-side X25519 key exchange, TOFU public key serving, certificate storage (FileCertificateStore), and Proof-of-Work verification (AdaptiveProofOfWorkPolicy).SessionHandlers: Stateless session resumption (SessionResume) withHmacKeccak256proof-of-possession and atomic token rotation viaISessionService.SystemControlHandlers&SystemTimeSyncHandlers: HandlesControl(PING/PONG,DISCONNECT,CIPHER_UPDATE,PUBLIC_KEY_REQUEST) andTimeSyncframes.
5. Session Store & Connection Groups
SessionService,SessionFactory,InMemorySessionStore,SessionPersistenceObserver: CapturesSessionSnapshotstate on disconnect and restores encryption keys, permission levels, and attributes on validSessionResumerequests.InMemoryGroupStore&GroupMembershipObserver: ImplementsIConnectionGroupRegistryfor room/channel broadcasting and automatically removes disconnected clients from all groups.
Supported Handler Return Shapes
| Return Type | Runtime Behavior |
|---|---|
void / Task / ValueTask | Executes side-effects; sends no automatic reply. |
TPacket / Task<TPacket> / ValueTask<TPacket> | Serializes and sends the returned packet back on the caller’s connection (ReplyAsync stamps the request’s SequenceId when response.Header.SequenceId == 0). |
IAsyncEnumerable<TPacket> | Server-streams each yielded packet via ReplyAsync with 1-item look-ahead and marks IPacketStreamable.IsEndOfStream = true on the final packet. |
byte[] / Memory<byte> / ReadOnlyMemory<byte> | Sends the raw buffer directly to the caller’s connection. |
ReturnValidation | Evaluates declarative validation result before continuing. |
Related Packages
- Nalix.Network — TCP, UDP, and WebSocket transport listeners
- Nalix.Hosting — Fluent server builder
- Nalix.Codec — Packet registry, serialization, and frame transforms
- Nalix.Framework — DI, workers, and object pools