Write the header as a packed int

this seems like the best way to do it.
This commit is contained in:
2025-04-05 12:01:52 -04:00
parent ac5511e9bd
commit bc75e86904

View File

@@ -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);
}