Zig:数据包
函数
packet.intercept
| Zig |
|---|
| pub fn intercept(dirs: Directions, comptime handler: fn (*Packet) Verdict) core.Error!Hook
|
为所选方向上的每一个数据包注册一个拦截器,区块数据也在内,每个包都会调用一次。只关心某些 id 时 interceptIds 更省。
- 参数:
- dirs :
Directions
- handler :
comptime fn (*Packet) Verdict
- 返回值类型:
core.Error!Hook
- 对应槽位:
packet_hook_register
packet.interceptIds
| Zig |
|---|
| pub fn interceptIds(dirs: Directions, ids: []const i32, comptime handler: fn (*Packet) Verdict) core.Error!Hook
|
同 intercept,但处理函数只对列出的数据包 id 运行,范围 0 到 1023。
- 参数:
- dirs :
Directions
- ids :
[]const i32
- handler :
comptime fn (*Packet) Verdict
- 返回值类型:
core.Error!Hook
- 对应槽位:
packet_hook_register_ids
packet.onConnection
| Zig |
|---|
| pub fn onConnection(comptime handler: fn (conn_id: u64, address: []const u8, state: ConnectionState) void) core.Error!Hook
|
注册一个观察连接打开和关闭的回调。
- 参数:
- handler :
comptime fn (conn_id: u64, address: []const u8, state: ConnectionState) void
- 返回值类型:
core.Error!Hook
- 对应槽位:
packet_conn_hook_register
packet.send
| Zig |
|---|
| pub fn send(player: props.Player, packet_id: i32, body: []const u8) core.Error!void
|
向一名玩家发送一个数据包:id 和包体,不含包头。
- 参数:
- player :
props.Player
- packet_id :
i32
- body :
[]const u8
- 返回值类型:
core.Error!void
- 对应槽位:
send_packet
packet.Packet
| Zig |
|---|
| pub const Packet = struct {
event: *const c.PierPacketEvent,
edit: *c.PierPacketEdit,
replace_ctx: ?*anyopaque,
replace: c.PierBytesSink,
replaced: bool = false,
// ...
};
|
处理函数收到的数据包。只在调用期间有效。
packet.Packet.direction
| Zig |
|---|
| pub fn direction(self: *const Packet) Direction
|
packet.Packet.connId
| Zig |
|---|
| pub fn connId(self: *const Packet) u64
|
这个连接的 id。在多个数据包之间保持不变,可以用作按连接保存状态的键。
packet.Packet.address
| Zig |
|---|
| pub fn address(self: *const Packet) []const u8
|
对端地址,形如 ip:port。用于诊断;要当键用请用 connId。
packet.Packet.packetId
| Zig |
|---|
| pub fn packetId(self: *const Packet) i32
|
packet.Packet.senderSubId
| Zig |
|---|
| pub fn senderSubId(self: *const Packet) u8
|
packet.Packet.targetSubId
| Zig |
|---|
| pub fn targetSubId(self: *const Packet) u8
|
packet.Packet.body
| Zig |
|---|
| pub fn body(self: *const Packet) []const u8
|
包体,不含包头,因为数据包 id 和子 id 已经解码出来了。
packet.Packet.setBody
| Zig |
|---|
| pub fn setBody(self: *Packet, bytes: []const u8) void
|
替换包体。一次调用里可以调用多次,以最后一次为准。
packet.Packet.setPacketId
| Zig |
|---|
| pub fn setPacketId(self: *Packet, id: i32) void
|
改写数据包 id,用于映射到另一个版本的编号上。
packet.Packet.setSenderSubId
| Zig |
|---|
| pub fn setSenderSubId(self: *Packet, id: u8) void
|
packet.Packet.setTargetSubId
| Zig |
|---|
| pub fn setTargetSubId(self: *Packet, id: u8) void
|
packet.Hook
| Zig |
|---|
| pub const Hook = struct {
handle: c.PierPacketHookHandle,
connection: bool,
// ...
};
|
一个已注册的数据包拦截器或连接观察器。
packet.Hook.unregister
| Zig |
|---|
| pub fn unregister(self: *Hook) core.Error!void
|
注销它;调用两次也没有害处。在处理函数里面调用也是安全的。
packet.Direction
| Zig |
|---|
| pub const Direction = enum {
/// Client to server.
inbound,
/// Server to client.
outbound,
};
|
数据包的方向。
packet.Directions
| Zig |
|---|
| pub const Directions = enum {
inbound,
outbound,
both,
};
|
注册时选择哪些方向。
packet.Verdict
| Zig |
|---|
| pub const Verdict = enum {
/// Forward it. A prior `setBody` or `setPacketId` takes effect.
forward,
/// Consume the packet entirely.
drop,
};
|
处理函数对这个数据包做什么。
packet.ConnectionState
| Zig |
|---|
| pub const ConnectionState = enum {
opened, closed
};
|
一个连接的两种状态。关闭是清理 mod 按连接保存的状态时唯一可靠的信号:没有完成登录握手的连接不会变成玩家,任何玩家事件都覆盖不到它。