Skip to content

Zig: Packets

Functions

packet.intercept

Zig
pub fn intercept(dirs: Directions, comptime handler: fn (*Packet) Verdict) core.Error!Hook

Registers an interceptor for every packet in the chosen directions, chunk data included, which costs one call per packet. interceptIds costs less when only some ids matter.

  • Parameters:
    • dirs : Directions
    • handler : comptime fn (*Packet) Verdict
  • Return type: core.Error!Hook
  • Slots: packet_hook_register

packet.interceptIds

Zig
pub fn interceptIds(dirs: Directions, ids: []const i32, comptime handler: fn (*Packet) Verdict) core.Error!Hook

As intercept, but the handler runs only for the listed packet ids, 0 to 1023.

  • Parameters:
    • dirs : Directions
    • ids : []const i32
    • handler : comptime fn (*Packet) Verdict
  • Return type: core.Error!Hook
  • Slots: 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

Registers an observer of connection opens and closes.

  • Parameters:
    • handler : comptime fn (conn_id: u64, address: []const u8, state: ConnectionState) void
  • Return type: core.Error!Hook
  • Slots: packet_conn_hook_register

packet.send

Zig
pub fn send(player: props.Player, packet_id: i32, body: []const u8) core.Error!void

Sends one packet to a player: the id and the body, without the header.

  • Parameters:
    • player : props.Player
    • packet_id : i32
    • body : []const u8
  • Return type: core.Error!void
  • Slots: send_packet

packet.Packet

Zig
1
2
3
4
5
6
7
8
pub const Packet = struct {
    event: *const c.PierPacketEvent,
    edit: *c.PierPacketEdit,
    replace_ctx: ?*anyopaque,
    replace: c.PierBytesSink,
    replaced: bool = false,
    // ...
};

The packet a handler receives. Valid only during the call.

packet.Packet.direction

Zig
pub fn direction(self: *const Packet) Direction
  • Return type: Direction

packet.Packet.connId

Zig
pub fn connId(self: *const Packet) u64

The id of this connection. Stable across packets and usable as the key of per-connection state.

  • Return type: u64

packet.Packet.address

Zig
pub fn address(self: *const Packet) []const u8

The peer address as ip:port. For diagnostics; use connId as a key.

  • Return type: []const u8

packet.Packet.packetId

Zig
pub fn packetId(self: *const Packet) i32
  • Return type: i32

packet.Packet.senderSubId

Zig
pub fn senderSubId(self: *const Packet) u8
  • Return type: u8

packet.Packet.targetSubId

Zig
pub fn targetSubId(self: *const Packet) u8
  • Return type: u8

packet.Packet.body

Zig
pub fn body(self: *const Packet) []const u8

The body, without the header, since the packet id and sub ids are already decoded.

  • Return type: []const u8

packet.Packet.setBody

Zig
pub fn setBody(self: *Packet, bytes: []const u8) void

Replaces the body. It may be called several times in one call and the last one wins.

  • Parameters:
    • bytes : []const u8
  • Return type: void

packet.Packet.setPacketId

Zig
pub fn setPacketId(self: *Packet, id: i32) void

Rewrites the packet id, for remapping onto the numbering of another version.

  • Parameters:
    • id : i32
  • Return type: void

packet.Packet.setSenderSubId

Zig
pub fn setSenderSubId(self: *Packet, id: u8) void
  • Parameters:
    • id : u8
  • Return type: void

packet.Packet.setTargetSubId

Zig
pub fn setTargetSubId(self: *Packet, id: u8) void
  • Parameters:
    • id : u8
  • Return type: void

packet.Hook

Zig
1
2
3
4
5
pub const Hook = struct {
    handle: c.PierPacketHookHandle,
    connection: bool,
    // ...
};

A registered packet interceptor or connection observer.

packet.Hook.unregister

Zig
pub fn unregister(self: *Hook) core.Error!void

Deregisters it; calling it twice is harmless. Safe to call from inside the handler.

packet.Direction

Zig
1
2
3
4
5
6
pub const Direction = enum {
    /// Client to server.
    inbound,
    /// Server to client.
    outbound,
};

The direction of a packet.

packet.Directions

Zig
1
2
3
4
5
pub const Directions = enum {
    inbound,
    outbound,
    both,
};

Which directions to select at registration.

packet.Verdict

Zig
1
2
3
4
5
6
pub const Verdict = enum {
    /// Forward it. A prior `setBody` or `setPacketId` takes effect.
    forward,
    /// Consume the packet entirely.
    drop,
};

What the handler does with this packet.

packet.ConnectionState

Zig
1
2
3
pub const ConnectionState = enum {
    opened, closed
};

The two states of a connection. A close is the only reliable signal to clear what a mod keeps per connection: one that never completed the login handshake never becomes a player, and no player event covers it.