diff options
author | Robby Zambito <contact@robbyzambito.me> | 2025-04-02 17:27:46 -0400 |
---|---|---|
committer | Robby Zambito <contact@robbyzambito.me> | 2025-04-02 18:09:24 -0400 |
commit | 7b7c46669054312b3473c834d04a07746fbfd331 (patch) | |
tree | c468c0e0fa0b4a172f64d2a14cbbdcb20cf2b789 /src | |
parent | 18763983e2eadfac417c57110c6c21c1b453b560 (diff) |
Add msgs with toBytes functions
Diffstat (limited to 'src')
-rw-r--r-- | src/main.zig | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/main.zig b/src/main.zig index e8db093..ebb7d33 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2,6 +2,40 @@ //! you are building an executable. If you are making a library, the convention //! 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 { // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`) std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); @@ -37,3 +71,4 @@ test "fuzz example" { } const std = @import("std"); +const Allocator = std.mem.Allocator; |