diff options
author | Robby Zambito <contact@robbyzambito.me> | 2025-04-03 16:21:11 -0400 |
---|---|---|
committer | Robby Zambito <contact@robbyzambito.me> | 2025-04-03 16:21:33 -0400 |
commit | 797624377a465766b396fb9a3d37d0a4e379e238 (patch) | |
tree | 83271c51fb69f0f030379c30da99035c2ba3c05d | |
parent | 3d8fa5e33c96455c6136c7217d9324e67e2b2e27 (diff) |
move aux funcs back into the union
also move the body of the base64 handling back to the only place it is used now
-rw-r--r-- | src/main.zig | 88 |
1 files changed, 39 insertions, 49 deletions
diff --git a/src/main.zig b/src/main.zig index d590fa3..e0674d4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -57,6 +57,23 @@ const SaprusMessage = union(SaprusPacketType) { } } + inline fn toBytesAux( + Header: type, + header: Header, + payload: []const u8, + w: std.ArrayList(u8).Writer, + allocator: Allocator, + ) !void { + var payload_list = std.ArrayList(u8).init(allocator); + defer payload_list.deinit(); + const buf_w = payload_list.writer(); + try base64Enc.encodeWriter(buf_w, payload); + + try w.writeStructEndian(header, .big); + try w.writeInt(u16, @intCast(payload_list.items.len), .big); + try w.writeAll(payload_list.items); + } + fn toBytes(self: SaprusMessage, allocator: Allocator) ![]u8 { var buf = std.ArrayList(u8).init(allocator); const w = buf.writer(); @@ -71,6 +88,28 @@ const SaprusMessage = union(SaprusPacketType) { return buf.toOwnedSlice(); } + inline fn fromBytesAux( + packet: SaprusPacketType, + Header: type, + r: std.io.FixedBufferStream([]const u8).Reader, + allocator: Allocator, + ) !SaprusMessage { + const header = try r.readStructEndian(Header, .big); + const len = try r.readInt(u16, .big); + + var payload_buf = std.ArrayList(u8).init(allocator); + defer payload_buf.deinit(); + try r.readAllArrayList(&payload_buf, len); + + const payload = try allocator.alloc(u8, try base64Dec.calcSizeForSlice(payload_buf.items)); + try base64Dec.decode(payload, payload_buf.items); + + return @unionInit(SaprusMessage, @tagName(packet), .{ + .header = header, + .payload = payload, + }); + } + fn fromBytes(bytes: []const u8, allocator: Allocator) !SaprusMessage { var s = std.io.fixedBufferStream(bytes); const r = s.reader(); @@ -84,37 +123,6 @@ const SaprusMessage = union(SaprusPacketType) { } }; -inline fn toBytesAux( - Header: type, - header: Header, - payload: []const u8, - w: std.ArrayList(u8).Writer, - allocator: Allocator, -) !void { - const payload_list = try encodeToList(payload, allocator); - defer payload_list.deinit(); - - try w.writeStructEndian(header, .big); - try w.writeInt(u16, @intCast(payload_list.items.len), .big); - try w.writeAll(payload_list.items); -} - -inline fn fromBytesAux( - packet: SaprusPacketType, - Header: type, - r: StringReader, - allocator: Allocator, -) !SaprusMessage { - const header = try r.readStructEndian(Header, .big); - const len = try r.readInt(u16, .big); - const payload = try decodeFromReader(r, len, allocator); - - return @unionInit(SaprusMessage, @tagName(packet), .{ - .header = header, - .payload = payload, - }); -} - pub fn main() !void { var dba: ?DebugAllocator = if (comptime is_debug) DebugAllocator.init else null; defer if (dba) |*d| { @@ -157,28 +165,10 @@ pub fn main() !void { _ = try sock.sendTo(dest_addr, msg_bytes); } -fn encodeToList(buf: []const u8, allocator: Allocator) !std.ArrayList(u8) { - var b64_buf = std.ArrayList(u8).init(allocator); - const buf_w = b64_buf.writer(); - try base64Enc.encodeWriter(buf_w, buf); - return b64_buf; -} - -fn decodeFromReader(reader: StringReader, len: u16, allocator: Allocator) ![]const u8 { - var b64_buf = std.ArrayList(u8).init(allocator); - defer b64_buf.deinit(); - try reader.readAllArrayList(&b64_buf, len); - - const payload = try allocator.alloc(u8, try base64Dec.calcSizeForSlice(b64_buf.items)); - try base64Dec.decode(payload, b64_buf.items); - return payload; -} - const builtin = @import("builtin"); const std = @import("std"); const Allocator = std.mem.Allocator; const DebugAllocator = std.heap.DebugAllocator(.{}); -const StringReader = std.io.FixedBufferStream([]const u8).Reader; const network = @import("network"); |