From 433a97fe5a973157f9cd9f62f0a62ad1a951b0c9 Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Sat, 5 Apr 2025 17:37:24 -0400 Subject: Move binary back to zaprus Also clean up the args for the aux functions by computing the type instead of passing it --- build.zig | 18 +++++++++--------- src/main.zig | 43 +++++++++++++++++++++++++++++++++++++++++++ src/saprus.zig | 4 ---- src/saprus_message.zig | 18 +++++++++--------- src/saprus_relay.zig | 43 ------------------------------------------- 5 files changed, 61 insertions(+), 65 deletions(-) create mode 100644 src/main.zig delete mode 100644 src/saprus_relay.zig diff --git a/build.zig b/build.zig index 1781842..328b822 100644 --- a/build.zig +++ b/build.zig @@ -16,34 +16,34 @@ pub fn build(b: *std.Build) void { const optimize = b.standardOptimizeOption(.{}); // We will also create a module for our other entry point, 'main.zig'. - const relay_exe_mod = b.createModule(.{ + const exe_mod = b.createModule(.{ // `root_source_file` is the Zig "entry point" of the module. If a module // only contains e.g. external object files, you can make this `null`. // In this case the main source file is merely a path, however, in more // complicated build scripts, this could be a generated file. - .root_source_file = b.path("src/saprus_relay.zig"), + .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, }); - relay_exe_mod.addImport("network", b.dependency("network", .{}).module("network")); + exe_mod.addImport("network", b.dependency("network", .{}).module("network")); // This creates another `std.Build.Step.Compile`, but this one builds an executable // rather than a static library. - const relay_exe = b.addExecutable(.{ - .name = "saprus_relay", - .root_module = relay_exe_mod, + const exe = b.addExecutable(.{ + .name = "zaprus", + .root_module = exe_mod, }); // This declares intent for the executable to be installed into the // standard location when the user invokes the "install" step (the default // step when running `zig build`). - b.installArtifact(relay_exe); + b.installArtifact(exe); // This *creates* a Run step in the build graph, to be executed when another // step is evaluated that depends on it. The next line below will establish // such a dependency. - const run_cmd = b.addRunArtifact(relay_exe); + const run_cmd = b.addRunArtifact(exe); // By making the run step depend on the install step, it will be run from the // installation directory rather than directly from within the cache directory. @@ -64,7 +64,7 @@ pub fn build(b: *std.Build) void { run_step.dependOn(&run_cmd.step); const exe_unit_tests = b.addTest(.{ - .root_module = relay_exe_mod, + .root_module = exe_mod, }); const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..f43f2bd --- /dev/null +++ b/src/main.zig @@ -0,0 +1,43 @@ +const is_debug = builtin.mode == .Debug; + +/// This creates a debug allocator that can only be referenced in debug mode. +/// You should check for is_debug around every reference to dba. +var dba: DebugAllocator = + if (is_debug) + DebugAllocator.init + else + @compileError("Should not use debug allocator in release mode"); + +pub fn main() !void { + defer if (is_debug) { + _ = dba.deinit(); + }; + + const gpa = if (is_debug) dba.allocator() else std.heap.smp_allocator; + + const args = try std.process.argsAlloc(gpa); + defer std.process.argsFree(gpa, args); + + var message = ArrayList(u8).init(gpa); + defer message.deinit(); + + for (args[1..], 0..) |arg, i| { + try message.appendSlice(arg); + if (i < args.len - 1) + try message.append(' '); + } + + try Saprus.init(); + defer Saprus.deinit(); + + try Saprus.sendInitialConnection(if (message.items.len > 0) message.items else "Hello darkness my old friend", 6868, gpa); + + // try Saprus.sendRelay(if (message.items.len > 0) message.items else "Hello darkness my old friend", gpa); +} + +const builtin = @import("builtin"); +const std = @import("std"); +const DebugAllocator = std.heap.DebugAllocator(.{}); +const ArrayList = std.ArrayList; + +const Saprus = @import("./saprus.zig"); diff --git a/src/saprus.zig b/src/saprus.zig index e7ea951..fce3dc7 100644 --- a/src/saprus.zig +++ b/src/saprus.zig @@ -51,10 +51,6 @@ pub fn sendInitialConnection(payload: []const u8, initial_port: u16, allocator: .seq_num = 1, .msg_id = 2, .reserved = 5, - // .options = .{ - // .opt2 = true, - // .opt8 = true, - // }, }, .payload = payload, }, diff --git a/src/saprus_message.zig b/src/saprus_message.zig index 495b741..1f7f819 100644 --- a/src/saprus_message.zig +++ b/src/saprus_message.zig @@ -64,12 +64,12 @@ pub const SaprusMessage = union(SaprusPacketType) { } fn toBytesAux( - Header: type, - header: Header, + header: anytype, payload: []const u8, buf: *std.ArrayList(u8), allocator: Allocator, ) !void { + const Header = @TypeOf(header); // Create a growable string to store the base64 bytes in. // Doing this first so I can use the length of the encoded bytes for the length field. var payload_list = std.ArrayList(u8).init(allocator); @@ -107,8 +107,8 @@ pub const SaprusMessage = union(SaprusPacketType) { // Write the proper header and payload for the given packet type. switch (self) { - .relay => |r| try toBytesAux(Relay.Header, r.header, r.payload, &buf, allocator), - .connection => |c| try toBytesAux(Connection.Header, c.header, c.payload, &buf, allocator), + .relay => |r| try toBytesAux(r.header, r.payload, &buf, allocator), + .connection => |c| try toBytesAux(c.header, c.payload, &buf, allocator), .file_transfer => return SaprusError.NotImplementedSaprusType, } @@ -118,13 +118,12 @@ pub const SaprusMessage = union(SaprusPacketType) { fn fromBytesAux( comptime packet: SaprusPacketType, - Header: type, r: std.io.FixedBufferStream([]const u8).Reader, allocator: Allocator, ) !SaprusMessage { - // Read the length of the base64 encoded payload. + 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]); @@ -152,11 +151,12 @@ pub const SaprusMessage = union(SaprusPacketType) { var s = std.io.fixedBufferStream(bytes); const r = s.reader(); + // Read packet type const packet_type = @as(SaprusPacketType, @enumFromInt(try r.readInt(u16, .big))); switch (packet_type) { - .relay => return fromBytesAux(.relay, Relay.Header, r, allocator), - .connection => return fromBytesAux(.connection, Connection.Header, r, allocator), + .relay => return fromBytesAux(.relay, r, allocator), + .connection => return fromBytesAux(.connection, r, allocator), .file_transfer => return SaprusError.NotImplementedSaprusType, else => return SaprusError.UnknownSaprusType, } diff --git a/src/saprus_relay.zig b/src/saprus_relay.zig deleted file mode 100644 index f43f2bd..0000000 --- a/src/saprus_relay.zig +++ /dev/null @@ -1,43 +0,0 @@ -const is_debug = builtin.mode == .Debug; - -/// This creates a debug allocator that can only be referenced in debug mode. -/// You should check for is_debug around every reference to dba. -var dba: DebugAllocator = - if (is_debug) - DebugAllocator.init - else - @compileError("Should not use debug allocator in release mode"); - -pub fn main() !void { - defer if (is_debug) { - _ = dba.deinit(); - }; - - const gpa = if (is_debug) dba.allocator() else std.heap.smp_allocator; - - const args = try std.process.argsAlloc(gpa); - defer std.process.argsFree(gpa, args); - - var message = ArrayList(u8).init(gpa); - defer message.deinit(); - - for (args[1..], 0..) |arg, i| { - try message.appendSlice(arg); - if (i < args.len - 1) - try message.append(' '); - } - - try Saprus.init(); - defer Saprus.deinit(); - - try Saprus.sendInitialConnection(if (message.items.len > 0) message.items else "Hello darkness my old friend", 6868, gpa); - - // try Saprus.sendRelay(if (message.items.len > 0) message.items else "Hello darkness my old friend", gpa); -} - -const builtin = @import("builtin"); -const std = @import("std"); -const DebugAllocator = std.heap.DebugAllocator(.{}); -const ArrayList = std.ArrayList; - -const Saprus = @import("./saprus.zig"); -- cgit