Refactored packet.zig
This commit is contained in:
parent
b67ae9ec59
commit
cb72a9cdc9
1 changed files with 36 additions and 32 deletions
|
|
@ -1,3 +1,15 @@
|
|||
///////////////// Module
|
||||
//
|
||||
|
||||
const Self = @This();
|
||||
|
||||
header: PacketHeader,
|
||||
address1: [ADDRESS_SIZE]u8,
|
||||
address2: [ADDRESS_SIZE]u8,
|
||||
context: PacketContext,
|
||||
data: []u8,
|
||||
|
||||
|
||||
///////////////// Constants
|
||||
//
|
||||
|
||||
|
|
@ -76,14 +88,6 @@ pub const PacketHeader = packed struct {
|
|||
hops: u8,
|
||||
};
|
||||
|
||||
pub const Packet = struct {
|
||||
header: PacketHeader,
|
||||
address1: [ADDRESS_SIZE]u8,
|
||||
address2: [ADDRESS_SIZE]u8,
|
||||
context: PacketContext,
|
||||
data: []u8,
|
||||
};
|
||||
|
||||
///////////////// Functions
|
||||
//
|
||||
|
||||
|
|
@ -115,7 +119,7 @@ fn getPacketSize(data_size: usize, has_two_addresses: bool) usize {
|
|||
//////// Public functions
|
||||
//
|
||||
|
||||
pub fn serializePacket(packet: Packet, output_buffer: []u8) !usize {
|
||||
pub fn serialize(packet: *const Self, output_buffer: []u8) !usize {
|
||||
const has_two_addresses = packet.header.header == HeaderType.type2;
|
||||
|
||||
const target_size = getPacketSize(packet.data.len, has_two_addresses);
|
||||
|
|
@ -151,17 +155,17 @@ pub fn serializePacket(packet: Packet, output_buffer: []u8) !usize {
|
|||
}
|
||||
|
||||
// Reads the message buffer and builds the corresponding packet struct inside dest_packet
|
||||
pub fn deserializePacket(message_buffer: []u8, dest_packet: *Packet) !void {
|
||||
pub fn deserialize(packet: *Self, message_buffer: []u8) !void {
|
||||
var offset: usize = 0;
|
||||
|
||||
offset = try copyToPacket(&dest_packet.header, message_buffer, offset, @sizeOf(PacketHeader));
|
||||
offset = try copyToPacket(&dest_packet.address1, message_buffer, offset, ADDRESS_SIZE);
|
||||
const has_two_addresses = dest_packet.header.header == HeaderType.type2;
|
||||
offset = try copyToPacket(&packet.header, message_buffer, offset, @sizeOf(PacketHeader));
|
||||
offset = try copyToPacket(&packet.address1, message_buffer, offset, ADDRESS_SIZE);
|
||||
const has_two_addresses = packet.header.header == HeaderType.type2;
|
||||
if (has_two_addresses) {
|
||||
offset = try copyToPacket(&dest_packet.address2, message_buffer, offset, ADDRESS_SIZE);
|
||||
offset = try copyToPacket(&packet.address2, message_buffer, offset, ADDRESS_SIZE);
|
||||
}
|
||||
offset = try copyToPacket(&dest_packet.context, message_buffer, offset, @sizeOf(PacketContext));
|
||||
offset = try copyToPacket(dest_packet.data, message_buffer, offset, message_buffer.len - offset);
|
||||
offset = try copyToPacket(&packet.context, message_buffer, offset, @sizeOf(PacketContext));
|
||||
offset = try copyToPacket(packet.data, message_buffer, offset, message_buffer.len - offset);
|
||||
}
|
||||
|
||||
///////////////// Tests
|
||||
|
|
@ -177,10 +181,10 @@ const asBytes = std.mem.asBytes;
|
|||
//
|
||||
|
||||
// Serializes the given packet and makes sure the output is correct
|
||||
fn testPacketSerialization(packet: Packet) !void {
|
||||
const buf_size = comptime @sizeOf(Packet) + MAX_DATA_SIZE;
|
||||
fn testPacketSerialization(packet: *const Self) !void {
|
||||
const buf_size = comptime @sizeOf(Self) + MAX_DATA_SIZE;
|
||||
var buf: [buf_size]u8 = undefined;
|
||||
const res: usize = try serializePacket(packet, &buf);
|
||||
const res: usize = try packet.serialize(&buf);
|
||||
const has_second_address = packet.header.header == HeaderType.type2;
|
||||
const expected_res: usize = getPacketSize(packet.data.len, has_second_address);
|
||||
|
||||
|
|
@ -216,7 +220,7 @@ fn headersEql(h1: *const PacketHeader, h2: *const PacketHeader) bool {
|
|||
and h1.hops == h2.hops;
|
||||
}
|
||||
|
||||
fn packetsEql(p1: *const Packet, p2: *const Packet) bool {
|
||||
fn packetsEql(p1: *const Self, p2: *const Self) bool {
|
||||
return headersEql(&p1.header, &p2.header)
|
||||
and memeql(u8, &p1.address1, &p2.address1)
|
||||
and memeql(u8, &p1.address2, &p2.address2)
|
||||
|
|
@ -246,7 +250,7 @@ test "Basic serialization: header type 1, max data size" {
|
|||
const data_size = MAX_DATA_SIZE;
|
||||
var data: [data_size]u8 = undefined;
|
||||
|
||||
const packet: Packet = .{
|
||||
const packet: Self = .{
|
||||
.header = header,
|
||||
.address1 = undefined,
|
||||
.address2 = undefined,
|
||||
|
|
@ -254,7 +258,7 @@ test "Basic serialization: header type 1, max data size" {
|
|||
.data = &data,
|
||||
};
|
||||
|
||||
try testPacketSerialization(packet);
|
||||
try packet.testPacketSerialization();
|
||||
}
|
||||
|
||||
test "Basic serialization: header type 2, max data size" {
|
||||
|
|
@ -271,7 +275,7 @@ test "Basic serialization: header type 2, max data size" {
|
|||
const data_size = MAX_DATA_SIZE;
|
||||
var data: [data_size]u8 = undefined;
|
||||
|
||||
const packet: Packet = .{
|
||||
const packet: Self = .{
|
||||
.header = header,
|
||||
.address1 = undefined,
|
||||
.address2 = undefined,
|
||||
|
|
@ -279,7 +283,7 @@ test "Basic serialization: header type 2, max data size" {
|
|||
.data = &data,
|
||||
};
|
||||
|
||||
try testPacketSerialization(packet);
|
||||
try packet.testPacketSerialization();
|
||||
}
|
||||
|
||||
test "Basic serialization: header type 1, medium data size" {
|
||||
|
|
@ -296,7 +300,7 @@ test "Basic serialization: header type 1, medium data size" {
|
|||
const data_size = MAX_DATA_SIZE / 2 + 3;
|
||||
var data: [data_size]u8 = undefined;
|
||||
|
||||
const packet: Packet = .{
|
||||
const packet: Self = .{
|
||||
.header = header,
|
||||
.address1 = undefined,
|
||||
.address2 = undefined,
|
||||
|
|
@ -304,7 +308,7 @@ test "Basic serialization: header type 1, medium data size" {
|
|||
.data = &data,
|
||||
};
|
||||
|
||||
try testPacketSerialization(packet);
|
||||
try packet.testPacketSerialization();
|
||||
}
|
||||
|
||||
test "Basic serialization: header type 2, medium data size" {
|
||||
|
|
@ -321,7 +325,7 @@ test "Basic serialization: header type 2, medium data size" {
|
|||
const data_size = MAX_DATA_SIZE / 2 + 3;
|
||||
var data: [data_size]u8 = undefined;
|
||||
|
||||
const packet: Packet = .{
|
||||
const packet: Self = .{
|
||||
.header = header,
|
||||
.address1 = undefined,
|
||||
.address2 = undefined,
|
||||
|
|
@ -329,7 +333,7 @@ test "Basic serialization: header type 2, medium data size" {
|
|||
.data = &data,
|
||||
};
|
||||
|
||||
try testPacketSerialization(packet);
|
||||
try packet.testPacketSerialization();
|
||||
}
|
||||
|
||||
test "Serialize / Deserialize Packet: Header type2, Medium data size" {
|
||||
|
|
@ -346,7 +350,7 @@ test "Serialize / Deserialize Packet: Header type2, Medium data size" {
|
|||
const data_size = MAX_DATA_SIZE / 2 + 3;
|
||||
var data: [data_size]u8 = undefined;
|
||||
|
||||
const packet: Packet = .{
|
||||
const packet: Self = .{
|
||||
.header = header,
|
||||
.address1 = undefined,
|
||||
.address2 = undefined,
|
||||
|
|
@ -356,11 +360,11 @@ test "Serialize / Deserialize Packet: Header type2, Medium data size" {
|
|||
|
||||
const buf_size = comptime @sizeOf(PacketHeader) + 2*ADDRESS_SIZE + @sizeOf(PacketContext) + data_size;
|
||||
var buf: [buf_size]u8 = undefined;
|
||||
var res_packet: Packet = undefined;
|
||||
var res_packet: Self = undefined;
|
||||
var res_data: [data_size]u8 = undefined;
|
||||
res_packet.data = &res_data;
|
||||
_ = try serializePacket(packet, &buf);
|
||||
try deserializePacket(&buf, &res_packet);
|
||||
_ = try packet.serialize(&buf);
|
||||
try res_packet.deserialize(&buf);
|
||||
|
||||
try expect(packetsEql(&res_packet, &packet));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue