summaryrefslogtreecommitdiff
path: root/src/Server/parse/Payload.zig
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2026-01-06 21:56:39 -0500
committerRobby Zambito <contact@robbyzambito.me>2026-01-06 21:59:41 -0500
commit48969283527e0db6b71893b2b3f3bbeb21e522db (patch)
treedee322b777c79ef1cf7133bb65f226488b2cdab7 /src/Server/parse/Payload.zig
parentcc036318387cc5c44f2a0a2a1e28d067f3e6bdf6 (diff)
Major restructuring
This makes things much easier to use as a library
Diffstat (limited to 'src/Server/parse/Payload.zig')
-rw-r--r--src/Server/parse/Payload.zig51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/Server/parse/Payload.zig b/src/Server/parse/Payload.zig
new file mode 100644
index 0000000..b512a81
--- /dev/null
+++ b/src/Server/parse/Payload.zig
@@ -0,0 +1,51 @@
+const std = @import("std");
+const Reader = std.Io.Reader;
+const Writer = std.Io.Writer;
+const Allocator = std.mem.Allocator;
+
+const Payload = @This();
+
+len: u32,
+short: [128]u8,
+long: ?[]u8,
+
+pub fn read(alloc: Allocator, in: *Reader, bytes: usize) !Payload {
+ var res: Payload = .{
+ .len = @intCast(bytes),
+ .short = undefined,
+ .long = null,
+ };
+
+ try in.readSliceAll(res.short[0..@min(bytes, res.short.len)]);
+ if (bytes > res.short.len) {
+ const long = try alloc.alloc(u8, bytes - res.short.len);
+ errdefer alloc.free(long);
+ try in.readSliceAll(long);
+ res.long = long;
+ }
+ return res;
+}
+
+pub fn write(self: Payload, out: *Writer) !void {
+ std.debug.assert(out.buffer.len >= self.short.len);
+ std.debug.assert(self.len <= self.short.len or self.long != null);
+ try out.writeAll(self.short[0..@min(self.len, self.short.len)]);
+ if (self.long) |l| {
+ try out.writeAll(l);
+ }
+}
+
+pub fn deinit(self: Payload, alloc: Allocator) void {
+ if (self.long) |l| {
+ alloc.free(l);
+ }
+}
+
+pub fn dupe(self: Payload, alloc: Allocator) !Payload {
+ var res = self;
+ if (self.long) |l| {
+ res.long = try alloc.dupe(u8, l);
+ }
+ errdefer if (res.long) |l| alloc.free(l);
+ return res;
+}