Receive and print the response from the sentinel

This commit is contained in:
2025-04-05 23:23:15 -04:00
parent 774d52ad59
commit a2072436aa
2 changed files with 24 additions and 11 deletions

View File

@@ -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.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); // try Saprus.sendRelay(if (message.items.len > 0) message.items else "Hello darkness my old friend", gpa);
} }

View File

@@ -78,6 +78,7 @@ var setting_up_socket: std.Thread.Semaphore = std.Thread.Semaphore{};
fn awaitSentinelConnectionResponse( fn awaitSentinelConnectionResponse(
res: *?SaprusMessage, res: *?SaprusMessage,
err: *?anyerror,
allocator: Allocator, allocator: Allocator,
) !void { ) !void {
var sock = try network.Socket.create(.ipv4, .udp); var sock = try network.Socket.create(.ipv4, .udp);
@@ -89,6 +90,7 @@ fn awaitSentinelConnectionResponse(
.port = 8888, .port = 8888,
}; };
try sock.setReadTimeout(1000);
try sock.bind(bind_addr); try sock.bind(bind_addr);
// Signal that the socket is ready to receive data. // Signal that the socket is ready to receive data.
@@ -96,42 +98,44 @@ fn awaitSentinelConnectionResponse(
var response_buf: [4096]u8 = undefined; 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); 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; var initial_port: u16 = 0;
if (rand) |r| { if (rand) |r| {
initial_port = r.intRangeAtMost(u16, 1024, 65000); initial_port = r.intRangeAtMost(u16, 1024, 65000);
} else unreachable; } else unreachable;
var err: ?anyerror = null;
var initial_conn_res: ?SaprusMessage = 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( const response_thread = try std.Thread.spawn(
.{}, .{},
awaitSentinelConnectionResponse, awaitSentinelConnectionResponse,
.{ .{
&initial_conn_res, &initial_conn_res,
&err,
allocator, allocator,
}, },
); );
// Block until the socket is set up. // 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); const msg = try sendInitialConnection(payload, initial_port, allocator);
std.debug.print("msg: {any}\n", .{msg}); _ = msg;
response_thread.join(); response_thread.join();
if (initial_conn_res) |c| { return initial_conn_res;
std.debug.print("response: {any}\n", .{c});
} else {
std.debug.print("no data???", .{});
}
} }
const SaprusMessage = @import("./saprus_message.zig").SaprusMessage; const SaprusMessage = @import("./saprus_message.zig").SaprusMessage;