Packet Contracts¶
Nalix.Abstractions.Networking.Packets defines the shared packet contracts used by both server and client packages.
Why These Contracts Exist¶
Nalix uses one packet model across runtime and SDK code. Shared contracts prevent divergence between transport, dispatch, and application handlers.
Source Mapping¶
src/Nalix.Abstractions/Networking/Packets/IPacket.cssrc/Nalix.Abstractions/Networking/Packets/IPacketContext.cssrc/Nalix.Abstractions/Networking/Packets/IPacketDeserializer.cssrc/Nalix.Abstractions/Networking/Packets/IPacketRegistry.cssrc/Nalix.Abstractions/Networking/Packets/IPacketSender.cssrc/Nalix.Abstractions/Networking/Packets/PacketDeserializer.cssrc/Nalix.Abstractions/Networking/Packets/IPacketTimestamped.cssrc/Nalix.Abstractions/Networking/Packets/IPacketReasoned.cs
Main Types¶
IPacket¶
IPacket is the wire contract. It includes:
- header metadata (
MagicNumber,OpCode,Flags,Priority,SequenceId) Length- serialization methods (
Serialize(),Serialize(Span<byte>))
IPacketRegistry¶
IPacketRegistry provides read-only deserializer lookup for dispatch and client receive paths:
DeserializerCountIsKnownMagic(uint)IsRegistered<TPacket>()Deserialize(ReadOnlySpan<byte>)TryDeserialize(ReadOnlySpan<byte>, out IPacket?)
IPacketContext<TPacket>¶
Handler context contract shared with runtime context implementations:
Packet,Connection,Attributes,Sender,CancellationTokenIsReliableindicates whether the transport protocol (TCP/UDP) is reliableSkipOutboundfor outbound middleware control
IPacketSender¶
Metadata-aware send contract:
Initialize<TPacket>(IPacketContext<TPacket> context)SendAsync(IPacket packet, CancellationToken ct = default)SendAsync(IPacket packet, bool forceEncrypt, CancellationToken ct = default)
Supporting Contracts¶
PacketDeserializer: delegate from raw bytes toIPacketPacketDeserializerInto<TPacket>: delegate that deserializes into an existing instance viarefIPacketDeserializer<TPacket>:Deserialize(ReadOnlySpan<byte> buffer)— returns a newTPacketinstanceIPacketTimestamped: packet contract with timestamp semanticsIPacketReasoned: packet contract exposingProtocolReason Reasonproperty
Responsibility Boundaries¶
Nalix.Abstractions: only contracts and shared primitives.Nalix.Framework: concrete packet model and registry implementations.Nalix.Runtime: dispatch/context/sender implementations.Nalix.SDK: client transport usage of the same contracts.
Best Practices¶
- Keep packet serialization deterministic for registry deserialization.
- Use
IPacketContext<TPacket>.Senderin handlers to preserve metadata-driven send behavior. - Use
TryDeserializein hot paths where exception-free failure handling is preferred.