diff options
Diffstat (limited to 'src/message.zig')
-rw-r--r-- | src/message.zig | 95 |
1 files changed, 63 insertions, 32 deletions
diff --git a/src/message.zig b/src/message.zig index 3def0aa..1308d54 100644 --- a/src/message.zig +++ b/src/message.zig @@ -38,7 +38,7 @@ pub const Message = packed struct { pub fn getPayload(self: *align(1) Relay) []u8 { const len: *u16 = @ptrFromInt(@intFromPtr(self) - @sizeOf(u16)); - return @as([*]u8, @ptrCast(&self.payload))[0 .. len.* - @sizeOf(Relay)]; + return @as([*]u8, @ptrCast(&self.payload))[0 .. len.* - @bitSizeOf(Relay) / 8]; } }; const Connection = packed struct { @@ -52,7 +52,7 @@ pub const Message = packed struct { pub fn getPayload(self: *align(1) Connection) []u8 { const len: *u16 = @ptrFromInt(@intFromPtr(self) - @sizeOf(u16)); - return @as([*]u8, @ptrCast(&self.payload))[0 .. len.* - @sizeOf(Connection)]; + return @as([*]u8, @ptrCast(&self.payload))[0 .. len.* - @bitSizeOf(Connection) / 8]; } fn nativeFromNetworkEndian(self: *align(1) Connection) void { @@ -91,13 +91,12 @@ pub const Message = packed struct { /// Compute the number of bytes required to store a given payload size for a given message type. pub fn calcSize(comptime @"type": PacketType, payload_len: usize) MessageTypeError!u16 { - std.debug.assert(payload_len < std.math.maxInt(u16)); - const header_size = @sizeOf(switch (@"type") { + const header_size = @bitSizeOf(switch (@"type") { .relay => Relay, .connection => Connection, .file_transfer => return MessageTypeError.NotImplementedSaprusType, else => return MessageTypeError.UnknownSaprusType, - }); + }) / 8; return @intCast(payload_len + @sizeOf(Self) + header_size); } @@ -191,42 +190,74 @@ pub const Message = packed struct { }; test "testing variable length zero copy struct" { - const payload = "Hello darkness my old friend"; - var msg_bytes: [try Message.calcSize(.relay, payload.len)]u8 align(@alignOf(Message)) = undefined; + { + // Relay test + const payload = "Hello darkness my old friend"; + var msg_bytes: [try Message.calcSize(.relay, payload.len)]u8 align(@alignOf(Message)) = undefined; - // Create a view of the byte slice as a Message - const msg: *Message = .init(.relay, &msg_bytes); + // Create a view of the byte slice as a Message + const msg: *Message = .init(.relay, &msg_bytes); + + { + // Set the message values + { + // These are both set by the init call. + // msg.type = .relay; + // msg.length = payload_len; + } + const relay = (try msg.getSaprusTypePayload()).relay; + relay.dest = .{ 1, 2, 3, 4 }; + @memcpy(relay.getPayload(), payload); + } - { - // Set the message values { - // These are both set by the init call. - // msg.type = .relay; - // msg.length = payload_len; + // Print the message as hex using the network byte order + try msg.networkFromNativeEndian(); + // We know the error from nativeFromNetworkEndian is unreachable because + // it would have returned an error from networkFromNativeEndian. + defer msg.nativeFromNetworkEndian() catch unreachable; + std.debug.print("relay network bytes: {x}\n", .{msg_bytes}); + std.debug.print("bytes len: {d}\n", .{msg_bytes.len}); } - const relay = (try msg.getSaprusTypePayload()).relay; - relay.dest = .{ 1, 2, 3, 4 }; - @memcpy(relay.getPayload(), payload); + + if (false) { + // Illegal behavior + std.debug.print("{any}\n", .{(try msg.getSaprusTypePayload()).connection}); + } + + try std.testing.expectEqualDeep(msg, try Message.bytesAsValue(msg.asBytes())); } { - const bytes = msg.asBytes(); - - // Print the message as hex using the network byte order - try msg.networkFromNativeEndian(); - // We know the error from nativeFromNetworkEndian is unreachable because - // it would have returned an error from networkFromNativeEndian. - defer msg.nativeFromNetworkEndian() catch unreachable; - std.debug.print("network bytes: {x}\n", .{bytes}); - std.debug.print("bytes len: {d}\n", .{bytes.len}); - } + // Connection test + const payload = "Hello darkness my old friend"; + var msg_bytes: [try Message.calcSize(.connection, payload.len)]u8 align(@alignOf(Message)) = undefined; - if (false) { - // Illegal behavior - std.debug.print("{any}\n", .{(try msg.getSaprusTypePayload()).connection}); - } + // Create a view of the byte slice as a Message + const msg: *Message = .init(.connection, &msg_bytes); + + { + // Initializing connection header values + const connection = (try msg.getSaprusTypePayload()).connection; + connection.src_port = 1; + connection.dest_port = 2; + connection.seq_num = 3; + connection.msg_id = 4; + connection.reserved = 5; + connection.options = @bitCast(@as(u8, 6)); + @memcpy(connection.getPayload(), payload); + } - try std.testing.expectEqualDeep(msg, try Message.bytesAsValue(msg.asBytes())); + { + // Print the message as hex using the network byte order + try msg.networkFromNativeEndian(); + // We know the error from nativeFromNetworkEndian is unreachable because + // it would have returned an error from networkFromNativeEndian. + defer msg.nativeFromNetworkEndian() catch unreachable; + std.debug.print("connection network bytes: {x}\n", .{msg_bytes}); + std.debug.print("bytes len: {d}\n", .{msg_bytes.len}); + } + } } const std = @import("std"); |