diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.zig | 11 | ||||
| -rw-r--r-- | src/saprus.zig | 24 | 
2 files changed, 24 insertions, 11 deletions
diff --git a/src/main.zig b/src/main.zig index 3959f08..d1a0165 100644 --- a/src/main.zig +++ b/src/main.zig @@ -32,7 +32,16 @@ pub fn main() !void {      // _ = try Saprus.sendInitialConnection(if (message.items.len > 0) message.items else "Hello darkness my old friend", 6868, gpa); -    try Saprus.connect(if (message.items.len > 0) message.items else "Hello darkness my old friend", gpa); +    const res = Saprus.connect(if (message.items.len > 0) message.items else "Hello darkness my old friend", gpa) catch |err| { +        std.debug.print("Error: {s}", .{@errorName(err)}); +        return; +    }; +    defer if (res) |r| r.deinit(gpa); +    if (res) |r| { +        std.debug.print("{s}\n", .{r.connection.payload}); +    } else { +        std.debug.print("null\n", .{}); +    }      // try Saprus.sendRelay(if (message.items.len > 0) message.items else "Hello darkness my old friend", gpa);  } diff --git a/src/saprus.zig b/src/saprus.zig index 12e97f3..97748be 100644 --- a/src/saprus.zig +++ b/src/saprus.zig @@ -78,6 +78,7 @@ var setting_up_socket: std.Thread.Semaphore = std.Thread.Semaphore{};  fn awaitSentinelConnectionResponse(      res: *?SaprusMessage, +    err: *?anyerror,      allocator: Allocator,  ) !void {      var sock = try network.Socket.create(.ipv4, .udp); @@ -89,6 +90,7 @@ fn awaitSentinelConnectionResponse(          .port = 8888,      }; +    try sock.setReadTimeout(1000);      try sock.bind(bind_addr);      // Signal that the socket is ready to receive data. @@ -96,42 +98,44 @@ fn awaitSentinelConnectionResponse(      var response_buf: [4096]u8 = undefined; -    const len = try sock.receive(&response_buf); +    _ = try sock.receive(&response_buf); +    const len = sock.receive(&response_buf) catch |e| { +        err.* = e; +        return; +    };      res.* = try SaprusMessage.fromBytes(response_buf[0..len], allocator);  } -pub fn connect(payload: []const u8, allocator: Allocator) !void { +pub fn connect(payload: []const u8, allocator: Allocator) !?SaprusMessage {      var initial_port: u16 = 0;      if (rand) |r| {          initial_port = r.intRangeAtMost(u16, 1024, 65000);      } else unreachable; +    var err: ?anyerror = null;      var initial_conn_res: ?SaprusMessage = null; -    defer if (initial_conn_res) |c| c.deinit(allocator); +    errdefer if (initial_conn_res) |c| c.deinit(allocator);      const response_thread = try std.Thread.spawn(          .{},          awaitSentinelConnectionResponse,          .{              &initial_conn_res, +            &err,              allocator,          },      );      // Block until the socket is set up. -    setting_up_socket.wait(); +    try setting_up_socket.timedWait(500 * 1000 * 1000 * 1000);      const msg = try sendInitialConnection(payload, initial_port, allocator); -    std.debug.print("msg: {any}\n", .{msg}); +    _ = msg;      response_thread.join(); -    if (initial_conn_res) |c| { -        std.debug.print("response: {any}\n", .{c}); -    } else { -        std.debug.print("no data???", .{}); -    } +    return initial_conn_res;  }  const SaprusMessage = @import("./saprus_message.zig").SaprusMessage;  | 
