Tài liệu

Metadata gói tin

Thời gian đọc 2 phút Gói: Nalix.Abstractions
Trên trang này

Không gian tên: Nalix.Abstractions.Networking.Packets
Assembly: Nalix.Abstractions

PacketMetadata là một readonly struct bất biến, lưu các attribute handler đã biên dịch cho một method [PacketOpcode]. Nó được PacketHandlerGenerator khởi tạo một lần lúc khởi động và được expose qua IPacketContext<TPacket>.Attributes.

Ánh xạ mã nguồn

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

Thuộc tính & phương thức của PacketMetadata

Thành phầnKiểuMô tả
PacketOpcodePacketOpcodeAttributeAttribute [PacketOpcode] gắn handler với opcode 16-bit trên dây.
TimeoutPacketTimeoutAttribute?Ngân sách [PacketTimeout] tùy chọn, tính bằng mili giây.
PermissionPacketPermissionAttribute?Mức và chế độ đánh giá [PacketPermission] tùy chọn.
EncryptionPacketEncryptionAttribute?Yêu cầu và bộ mật mã [PacketEncryption] tùy chọn.
RateLimitPacketRateLimitAttribute?Cài đặt token-bucket / policy [PacketRateLimit] tùy chọn.
TransportPacketTransportAttribute?Giới hạn [PacketTransport] tùy chọn (TCP, UDP, WEBSOCKET).
CustomAttributesIReadOnlyDictionary<Type, Attribute>Dictionary các attribute tùy chỉnh bổ sung được phát hiện trên handler.
GetCustomAttribute<TAttribute>()TAttribute?Lấy attribute tùy chỉnh kiểu TAttribute trong thời gian $O(1)$ mà không dùng reflection.

Sinh mã lúc biên dịch

PacketHandlerGenerator kiểm tra mọi attribute khai báo trên từng method [PacketOpcode] lúc biên dịch, rồi tạo trực tiếp instance PacketMetadata trong <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);

API liên quan