mirror of
https://git.robbyzambito.me/zaprus
synced 2025-12-20 16:24:50 +00:00
Add msgs with toBytes functions
This commit is contained in:
35
src/main.zig
35
src/main.zig
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user