Add msgs with toBytes functions

This commit is contained in:
2025-04-02 17:27:46 -04:00
parent a0bea9311f
commit 3b5c8e397e

View File

@@ -2,6 +2,40 @@
//! you are building an executable. If you are making a library, the convention //! you are building an executable. If you are making a library, the convention
//! is to delete this file and start with root.zig instead. //! is to delete this file and start with root.zig instead.
const SaprusPacketType = enum(u16) {
relay = 0x003C,
file_transfer = 0x8888,
};
const SaprusHeaderFrame = struct {
msg_type: SaprusPacketType,
payload: []u8,
const Self = @This();
fn toBytes(s: Self, allocator: Allocator) ![]u8 {
const buf = allocator.alloc(u8, 32 + s.payload.len);
std.mem.writeInt(u16, buf[0..2], s.msg_type, .big);
std.mem.writeInt(u16, buf[2..4], s.payload.len);
std.mem.copyForwards(u8, buf[4..], s.payload);
return buf;
}
};
const SaprusRelayMessage = struct {
dest: [4]u8,
payload: []u8,
const Self = @This();
fn toBytes(s: Self, allocator: Allocator) ![]u8 {
const buf = allocator.alloc(u8, 4 + s.payload.len);
std.mem.copyForwards(u8, buf[0..4], s.dest);
std.mem.copyForwards(u8, buf[4..], s.payload);
return buf;
}
};
pub fn main() !void { pub fn main() !void {
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`) // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
@@ -37,3 +71,4 @@ test "fuzz example" {
} }
const std = @import("std"); const std = @import("std");
const Allocator = std.mem.Allocator;