diff options
-rw-r--r-- | build.zig | 4 | ||||
-rw-r--r-- | src/saprus_message.zig | 21 |
2 files changed, 13 insertions, 12 deletions
@@ -27,6 +27,7 @@ pub fn build(b: *std.Build) void { }); exe_mod.addImport("network", b.dependency("network", .{}).module("network")); + exe_mod.addImport("clap", b.dependency("clap", .{}).module("clap")); // This creates another `std.Build.Step.Compile`, but this one builds an executable // rather than a static library. @@ -35,9 +36,6 @@ pub fn build(b: *std.Build) void { .root_module = exe_mod, }); - const clap = b.dependency("clap", .{}); - exe.root_module.addImport("clap", clap.module("clap")); - // This declares intent for the executable to be installed into the // standard location when the user invokes the "install" step (the default // step when running `zig build`). diff --git a/src/saprus_message.zig b/src/saprus_message.zig index 19ed58d..3d47f66 100644 --- a/src/saprus_message.zig +++ b/src/saprus_message.zig @@ -117,12 +117,12 @@ pub const SaprusMessage = union(SaprusPacketType) { fn fromBytesAux( comptime packet: SaprusPacketType, + len: u16, r: std.io.FixedBufferStream([]const u8).Reader, allocator: Allocator, ) !SaprusMessage { 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]); @@ -130,13 +130,13 @@ pub const SaprusMessage = union(SaprusPacketType) { const header = try header_stream.reader().readStructEndian(Header, .big); // Read the base64 bytes into a list to be able to call the decoder on it. - var payload_buf = std.ArrayList(u8).init(allocator); - defer payload_buf.deinit(); - try r.readAllArrayList(&payload_buf, len); + const payload_buf = try allocator.alloc(u8, len - @bitSizeOf(Header) / 8); + defer allocator.free(payload_buf); + _ = try r.readAll(payload_buf); // Create a buffer to store the payload in, and decode the base64 bytes into the payload field. - const payload = try allocator.alloc(u8, try base64Dec.calcSizeForSlice(payload_buf.items)); - try base64Dec.decode(payload, payload_buf.items); + const payload = try allocator.alloc(u8, try base64Dec.calcSizeForSlice(payload_buf)); + try base64Dec.decode(payload, payload_buf); // Return the type of SaprusMessage specified by the `packet` argument. return @unionInit(SaprusMessage, @tagName(packet), .{ @@ -153,9 +153,12 @@ pub const SaprusMessage = union(SaprusPacketType) { // Read packet type const packet_type = @as(SaprusPacketType, @enumFromInt(try r.readInt(u16, .big))); + // Read the length of the header + base64 encoded payload. + const len = try r.readInt(u16, .big); + switch (packet_type) { - .relay => return fromBytesAux(.relay, r, allocator), - .connection => return fromBytesAux(.connection, r, allocator), + .relay => return fromBytesAux(.relay, len, r, allocator), + .connection => return fromBytesAux(.connection, len, r, allocator), .file_transfer => return SaprusError.NotImplementedSaprusType, else => return SaprusError.UnknownSaprusType, } |