mirror of
https://git.robbyzambito.me/zaprus
synced 2025-12-20 16:24:50 +00:00
Fix extra bytes in connection message.
This commit is contained in:
@@ -38,7 +38,7 @@ pub const Message = packed struct {
|
|||||||
|
|
||||||
pub fn getPayload(self: *align(1) Relay) []u8 {
|
pub fn getPayload(self: *align(1) Relay) []u8 {
|
||||||
const len: *u16 = @ptrFromInt(@intFromPtr(self) - @sizeOf(u16));
|
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 {
|
const Connection = packed struct {
|
||||||
@@ -52,7 +52,7 @@ pub const Message = packed struct {
|
|||||||
|
|
||||||
pub fn getPayload(self: *align(1) Connection) []u8 {
|
pub fn getPayload(self: *align(1) Connection) []u8 {
|
||||||
const len: *u16 = @ptrFromInt(@intFromPtr(self) - @sizeOf(u16));
|
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 {
|
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.
|
/// 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 {
|
pub fn calcSize(comptime @"type": PacketType, payload_len: usize) MessageTypeError!u16 {
|
||||||
std.debug.assert(payload_len < std.math.maxInt(u16));
|
const header_size = @bitSizeOf(switch (@"type") {
|
||||||
const header_size = @sizeOf(switch (@"type") {
|
|
||||||
.relay => Relay,
|
.relay => Relay,
|
||||||
.connection => Connection,
|
.connection => Connection,
|
||||||
.file_transfer => return MessageTypeError.NotImplementedSaprusType,
|
.file_transfer => return MessageTypeError.NotImplementedSaprusType,
|
||||||
else => return MessageTypeError.UnknownSaprusType,
|
else => return MessageTypeError.UnknownSaprusType,
|
||||||
});
|
}) / 8;
|
||||||
return @intCast(payload_len + @sizeOf(Self) + header_size);
|
return @intCast(payload_len + @sizeOf(Self) + header_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,42 +190,74 @@ pub const Message = packed struct {
|
|||||||
};
|
};
|
||||||
|
|
||||||
test "testing variable length zero copy 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;
|
|
||||||
|
|
||||||
// Create a view of the byte slice as a Message
|
|
||||||
const msg: *Message = .init(.relay, &msg_bytes);
|
|
||||||
|
|
||||||
{
|
{
|
||||||
// Set the message values
|
// 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);
|
||||||
|
|
||||||
{
|
{
|
||||||
// These are both set by the init call.
|
// Set the message values
|
||||||
// msg.type = .relay;
|
{
|
||||||
// msg.length = payload_len;
|
// 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);
|
||||||
}
|
}
|
||||||
const relay = (try msg.getSaprusTypePayload()).relay;
|
|
||||||
relay.dest = .{ 1, 2, 3, 4 };
|
{
|
||||||
@memcpy(relay.getPayload(), payload);
|
// 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});
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
// Connection test
|
||||||
|
const payload = "Hello darkness my old friend";
|
||||||
|
var msg_bytes: [try Message.calcSize(.connection, payload.len)]u8 align(@alignOf(Message)) = undefined;
|
||||||
|
|
||||||
// Print the message as hex using the network byte order
|
// Create a view of the byte slice as a Message
|
||||||
try msg.networkFromNativeEndian();
|
const msg: *Message = .init(.connection, &msg_bytes);
|
||||||
// We know the error from nativeFromNetworkEndian is unreachable because
|
|
||||||
// it would have returned an error from networkFromNativeEndian.
|
{
|
||||||
defer msg.nativeFromNetworkEndian() catch unreachable;
|
// Initializing connection header values
|
||||||
std.debug.print("network bytes: {x}\n", .{bytes});
|
const connection = (try msg.getSaprusTypePayload()).connection;
|
||||||
std.debug.print("bytes len: {d}\n", .{bytes.len});
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// 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});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (false) {
|
|
||||||
// Illegal behavior
|
|
||||||
std.debug.print("{any}\n", .{(try msg.getSaprusTypePayload()).connection});
|
|
||||||
}
|
|
||||||
|
|
||||||
try std.testing.expectEqualDeep(msg, try Message.bytesAsValue(msg.asBytes()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|||||||
Reference in New Issue
Block a user