summaryrefslogtreecommitdiff
path: root/src/saprus_message.zig
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2025-04-05 17:37:24 -0400
committerRobby Zambito <contact@robbyzambito.me>2025-04-06 13:08:09 -0400
commit433a97fe5a973157f9cd9f62f0a62ad1a951b0c9 (patch)
treefe86004f150bbb3bb4b067e96d25d6c5af6f0cc1 /src/saprus_message.zig
parent3c935698aac68b081aaa62ef5473a194477e2578 (diff)
Move binary back to zaprus
Also clean up the args for the aux functions by computing the type instead of passing it
Diffstat (limited to 'src/saprus_message.zig')
-rw-r--r--src/saprus_message.zig18
1 files changed, 9 insertions, 9 deletions
diff --git a/src/saprus_message.zig b/src/saprus_message.zig
index 495b741..1f7f819 100644
--- a/src/saprus_message.zig
+++ b/src/saprus_message.zig
@@ -64,12 +64,12 @@ pub const SaprusMessage = union(SaprusPacketType) {
}
fn toBytesAux(
- Header: type,
- header: Header,
+ header: anytype,
payload: []const u8,
buf: *std.ArrayList(u8),
allocator: Allocator,
) !void {
+ const Header = @TypeOf(header);
// Create a growable string to store the base64 bytes in.
// Doing this first so I can use the length of the encoded bytes for the length field.
var payload_list = std.ArrayList(u8).init(allocator);
@@ -107,8 +107,8 @@ pub const SaprusMessage = union(SaprusPacketType) {
// Write the proper header and payload for the given packet type.
switch (self) {
- .relay => |r| try toBytesAux(Relay.Header, r.header, r.payload, &buf, allocator),
- .connection => |c| try toBytesAux(Connection.Header, c.header, c.payload, &buf, allocator),
+ .relay => |r| try toBytesAux(r.header, r.payload, &buf, allocator),
+ .connection => |c| try toBytesAux(c.header, c.payload, &buf, allocator),
.file_transfer => return SaprusError.NotImplementedSaprusType,
}
@@ -118,13 +118,12 @@ pub const SaprusMessage = union(SaprusPacketType) {
fn fromBytesAux(
comptime packet: SaprusPacketType,
- Header: type,
r: std.io.FixedBufferStream([]const u8).Reader,
allocator: Allocator,
) !SaprusMessage {
- // Read the length of the base64 encoded payload.
+ const Header = @field(@FieldType(SaprusMessage, @tagName(packet)), "Header");
+ // Read the length of the header + base64 encoded payload.
const len = try r.readInt(u16, .big);
-
// Read the header for the current message type.
var header_bytes: [@sizeOf(Header)]u8 = undefined;
_ = try r.read(header_bytes[0 .. @bitSizeOf(Header) / 8]);
@@ -152,11 +151,12 @@ pub const SaprusMessage = union(SaprusPacketType) {
var s = std.io.fixedBufferStream(bytes);
const r = s.reader();
+ // Read packet type
const packet_type = @as(SaprusPacketType, @enumFromInt(try r.readInt(u16, .big)));
switch (packet_type) {
- .relay => return fromBytesAux(.relay, Relay.Header, r, allocator),
- .connection => return fromBytesAux(.connection, Connection.Header, r, allocator),
+ .relay => return fromBytesAux(.relay, r, allocator),
+ .connection => return fromBytesAux(.connection, r, allocator),
.file_transfer => return SaprusError.NotImplementedSaprusType,
else => return SaprusError.UnknownSaprusType,
}