Mqtt-zig: MQTT packet codec for both client and server usage

A Simple Example:

// Encode a packet
const pkt = Packet{
    .connect = Connect{
        .protocol = .V311,
        .clean_session = true,
        .keep_alive = 120,
        .client_id = Utf8View.initUnchecked("sample"),
    },
};
const remaining_len = (try pkt.encode_len()).remaining_len;
var buf: [512]u8 = undefined;
var idx: usize = 0;
pkt.encode(remaining_len, write_buf[0..], &idx);

// Decode a packet
const allocator = std.testing.allocator;
const header, const header_len = (try Header.decode(buf[0..])).?;
const read_pkt = (try Packet.decode(buf[header_len..], header, allocator)).?;
defer read_pkt.deinit();
4 Likes

Make the library allocation free and simplify packet encode/decode API.

// Encode a packet
const pkt = Packet{ .connect = Connect{
    .protocol = .V311,
    .clean_session = true,
    .keep_alive = 120,
    .client_id = Utf8View.initUnchecked("sample"),
} };
var buf: [512]u8 = undefined;
var idx: usize = 0;
try pkt.encode(buf[0..], &idx);

// Decode a packet
const read_pkt = (try Packet.decode(buf[0..])).?;
1 Like