diff options
author | Robby Zambito <contact@robbyzambito.me> | 2025-04-05 12:01:52 -0400 |
---|---|---|
committer | Robby Zambito <contact@robbyzambito.me> | 2025-04-06 13:08:09 -0400 |
commit | bc75e869043e84f11a3aae6d3d228beff9a34be6 (patch) | |
tree | 9936465047608ce25b6d7fd7b325bc7e869f9911 | |
parent | ac5511e9bdda73f42e58189e7e143e8ba78ceb26 (diff) |
Write the header as a packed int
this seems like the best way to do it.
-rw-r--r-- | src/saprus_message.zig | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/src/saprus_message.zig b/src/saprus_message.zig index 6c46a8f..1f1d37e 100644 --- a/src/saprus_message.zig +++ b/src/saprus_message.zig @@ -79,16 +79,21 @@ pub const SaprusMessage = union(SaprusPacketType) { // Write the payload bytes as base64 to the growable string. try base64Enc.encodeWriter(buf_w, payload); - // Write the packet body to the output writer + // At this point, payload_list contains the base64 encoded payload. + + // Write the packet body to the output buf. try buf.*.appendSlice(asBytes(&nativeToBig(u16, @intCast(payload_list.items.len)))); - var h = header; - inline for (@typeInfo(Header).@"struct".fields) |f| { - @field(h, f.name) = nativeToBig(@TypeOf(@field(h, f.name)), @field(h, f.name)); - } + // Write the header bytes to the output buf. + const HeaderInt = @typeInfo(Header).@"struct".backing_integer.?; + std.mem.writePackedInt( + HeaderInt, + try buf.*.addManyAsSlice(@bitSizeOf(Header) / 8), + 0, + @bitCast(header), + .little, + ); - const h_bytes = asBytes(&h); - try buf.*.appendSlice(h_bytes[0 .. h_bytes.len - 2]); try buf.*.appendSlice(payload_list.items); } |