Metadata gói tin
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.csanalyzers/Nalix.Analyzers.Generators/PacketHandlerGenerator.cs
Thuộc tính & phương thức của PacketMetadata
| Thành phần | Kiểu | Mô tả |
|---|---|---|
PacketOpcode | PacketOpcodeAttribute | Attribute [PacketOpcode] gắn handler với opcode 16-bit trên dây. |
Timeout | PacketTimeoutAttribute? | Ngân sách [PacketTimeout] tùy chọn, tính bằng mili giây. |
Permission | PacketPermissionAttribute? | Mức và chế độ đánh giá [PacketPermission] tùy chọn. |
Encryption | PacketEncryptionAttribute? | Yêu cầu và bộ mật mã [PacketEncryption] tùy chọn. |
RateLimit | PacketRateLimitAttribute? | Cài đặt token-bucket / policy [PacketRateLimit] tùy chọn. |
Transport | PacketTransportAttribute? | Giới hạn [PacketTransport] tùy chọn (TCP, UDP, WEBSOCKET). |
CustomAttributes | IReadOnlyDictionary<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);