diff options
| author | Robby Zambito <contact@robbyzambito.me> | 2025-04-28 07:03:34 -0400 | 
|---|---|---|
| committer | Robby Zambito <contact@robbyzambito.me> | 2025-04-30 13:46:54 -0400 | 
| commit | 08954b9f3d2d0204469a3c8d2d69418d02e74215 (patch) | |
| tree | 3f9852c007232c22081055ee55e4b87a9734b744 /src | |
| parent | 8d404a7c8d2db3f132c716f645ada9ef09ca0e9a (diff) | |
Diffstat (limited to 'src')
| -rw-r--r-- | src/message.zig | 35 | 
1 files changed, 24 insertions, 11 deletions
diff --git a/src/message.zig b/src/message.zig index 6d95e4b..efeea29 100644 --- a/src/message.zig +++ b/src/message.zig @@ -33,7 +33,6 @@ pub const Error = error{  pub const ZeroCopyMessage = packed struct {      pub const Relay = packed struct {          dest: @Vector(4, u8), -        /// @as([*]u8, @ptrCast(&payload))          payload: void,      };      pub const Connection = packed struct { @@ -52,30 +51,44 @@ pub const ZeroCopyMessage = packed struct {      length: u16,      bytes: void = {}, -    fn getRelay(self: *align(1) Self) *align(1) const Relay { +    fn getRelay(self: *align(1) Self) *align(1) Relay {          return std.mem.bytesAsValue(Relay, &self.bytes);      } -    fn getConnection(self: *align(1) Self) *align(1) const Connection { +    fn getConnection(self: *align(1) Self) *align(1) Connection {          return std.mem.bytesAsValue(Connection, &self.bytes);      } + +    pub fn getSaprusTypePayload(self: *align(1) Self) Error!(union(PacketType) { +        relay: *align(1) Relay, +        file_transfer: void, +        connection: *align(1) Connection, +    }) { +        return switch (self.type) { +            .relay => .{ .relay = self.getRelay() }, +            .connection => .{ .connection = self.getConnection() }, +            .file_transfer => Error.NotImplementedSaprusType, +            else => Error.UnknownSaprusType, +        }; +    }  };  test "testing variable length zero copy struct" {      const gpa = std.testing.allocator;      const bytes = try gpa.alloc(u8, @sizeOf(ZeroCopyMessage) + 64);      defer gpa.free(bytes); -    // const zcm = std.mem.bytesAsValue(ZeroCopyMessage, bytes).*; +      const zcm: *align(1) ZeroCopyMessage = @ptrCast(bytes.ptr); -    // @compileLog(@typeInfo(@TypeOf(ZeroCopyMessage{ .type = .relay, .length = 0 }))); -    // @compileLog(@typeInfo(@TypeOf(zcm))); -    // _ = zcm; +      zcm.type = .relay;      zcm.length = 3;      std.debug.print("{any}\n", .{zcm}); -    std.debug.print("{any}\n", .{zcm.getRelay()}); -    std.debug.print("{any}\n", .{zcm.getConnection()}); -    std.debug.print("{x}\n", .{std.mem.asBytes(zcm.getConnection())}); -    std.debug.print("{x}\n", .{std.mem.asBytes(zcm.getRelay())}); +    std.debug.print("{any}\n", .{zcm.getSaprusTypePayload()}); +    if (false) { +        std.debug.print("{any}\n", .{zcm.getRelay()}); +        std.debug.print("{any}\n", .{zcm.getConnection()}); +        std.debug.print("{x}\n", .{std.mem.asBytes(zcm.getRelay())}); +        std.debug.print("{x}\n", .{std.mem.asBytes(zcm.getConnection())}); +    }      std.debug.print("{x}\n", .{bytes});      try std.testing.expect(true);  | 
