summaryrefslogtreecommitdiff
path: root/src/saprus_message.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/saprus_message.zig')
-rw-r--r--src/saprus_message.zig19
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);
}