2 Commits

Author SHA1 Message Date
8779b29149 Do some things for invy 2025-04-13 16:41:20 -04:00
0f4a7c9bcd Use dynamic array instead of arraylist
we know the size (assuming the len is correct) so we can preallocate the whole array
2025-04-06 21:33:00 -04:00
3 changed files with 14 additions and 11 deletions

View File

@@ -50,7 +50,7 @@ pub fn main() !void {
if (res.args.relay) |r| {
try Saprus.sendRelay(if (r.len > 0) r else "Hello darkness my old friend", gpa);
std.debug.print("Sent: {s}\n", .{r});
// std.debug.print("Sent: {s}\n", .{r});
return;
} else if (res.args.connect) |c| {
const conn_res: ?SaprusMessage = Saprus.connect(if (c.len > 0) c else "Hello darkness my old friend", gpa) catch |err| switch (err) {

View File

@@ -42,7 +42,7 @@ fn broadcastSaprusMessage(msg: SaprusMessage, udp_port: u16, allocator: Allocato
pub fn sendRelay(payload: []const u8, allocator: Allocator) !void {
const msg = SaprusMessage{
.relay = .{
.header = .{ .dest = .{ 255, 255, 255, 255 } },
.header = .{ .dest = .{ 70, 70, 70, 70 } },
.payload = payload,
},
};

View File

@@ -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,
}