Packet Metadata

Read time 2 minutes Package: Nalix.Abstractions
On this page

Namespace: Nalix.Abstractions.Networking.Packets
Assembly: Nalix.Abstractions

PacketMetadata is an immutable readonly struct that stores the compiled handler attributes for a [PacketOpcode] method. It is instantiated once at startup by PacketHandlerGenerator and exposed on IPacketContext<TPacket>.Attributes.

Source Mapping

  • src/Nalix.Abstractions/Networking/Packets/PacketMetadata.cs
  • analyzers/Nalix.Analyzers.Generators/PacketHandlerGenerator.cs

PacketMetadata Properties & Methods

MemberTypeDescription
PacketOpcodePacketOpcodeAttributeThe [PacketOpcode] attribute that binds the handler to its 16-bit wire opcode.
TimeoutPacketTimeoutAttribute?Optional [PacketTimeout] budget in milliseconds.
PermissionPacketPermissionAttribute?Optional [PacketPermission] level and evaluation mode.
EncryptionPacketEncryptionAttribute?Optional [PacketEncryption] requirement and cipher suite.
RateLimitPacketRateLimitAttribute?Optional [PacketRateLimit] token-bucket / policy settings.
TransportPacketTransportAttribute?Optional [PacketTransport] restriction (TCP, UDP, WEBSOCKET).
CustomAttributesIReadOnlyDictionary<Type, Attribute>Dictionary of additional custom attributes discovered on the handler.
GetCustomAttribute<TAttribute>()TAttribute?Retrieves a custom attribute of type TAttribute in $O(1)$ time without reflection.

Compile-Time Generation

PacketHandlerGenerator inspects all attributes declared on each [PacketOpcode] method at compile time and constructs the PacketMetadata instance directly in <Controller>_PacketController.g.cs:

// Generated by PacketHandlerGenerator:
PacketMetadata metadata = new(
    packetOpcode: new PacketOpcodeAttribute(0x3001),
    timeout: new PacketTimeoutAttribute(5000),
    permission: new PacketPermissionAttribute(PermissionLevel.USER, PermissionEvaluation.MinimumLevel),
    encryption: new PacketEncryptionAttribute(true, CipherSuiteType.Chacha20Poly1305),
    rateLimit: new PacketRateLimitAttribute(10, 3) { PolicyId = "chat-send" },
    transport: new PacketTransportAttribute(NetworkTransport.TCP),
    customAttributes: null);