From 774d52ad59fbffb8cfad7db9cd46dd6a53ac3268 Mon Sep 17 00:00:00 2001
From: Robby Zambito <contact@robbyzambito.me>
Date: Sat, 5 Apr 2025 22:38:37 -0400
Subject: Initial awaiting for handshake response

seems like i might be getting my own initial connection?
I get a response even without the sentinal running.
---
 src/saprus.zig | 56 ++++++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 44 insertions(+), 12 deletions(-)

(limited to 'src')

diff --git a/src/saprus.zig b/src/saprus.zig
index b29a381..12e97f3 100644
--- a/src/saprus.zig
+++ b/src/saprus.zig
@@ -14,7 +14,7 @@ pub fn deinit() void {
     network.deinit();
 }
 
-inline fn broadcastSaprusMessage(msg: SaprusMessage, allocator: Allocator) !void {
+fn broadcastSaprusMessage(msg: SaprusMessage, allocator: Allocator) !void {
     const msg_bytes = try msg.toBytes(allocator);
     defer allocator.free(msg_bytes);
 
@@ -74,16 +74,31 @@ pub fn sendInitialConnection(payload: []const u8, initial_port: u16, allocator:
     return msg;
 }
 
-fn awaitSentinelConnectionResponse(res: *?SaprusMessage) !void {
-    res.* = SaprusMessage{
-        .relay = .{
-            .header = .{
-                .dest = .{ 255, 255, 255, 255 },
-            },
-            .payload = "",
-        },
+var setting_up_socket: std.Thread.Semaphore = std.Thread.Semaphore{};
+
+fn awaitSentinelConnectionResponse(
+    res: *?SaprusMessage,
+    allocator: Allocator,
+) !void {
+    var sock = try network.Socket.create(.ipv4, .udp);
+    defer sock.close();
+
+    // Bind to 255.255.255.255:8888
+    const bind_addr = network.EndPoint{
+        .address = network.Address{ .ipv4 = network.Address.IPv4.broadcast },
+        .port = 8888,
     };
-    std.Thread.sleep(3 * 1000 * 1000 * 1000);
+
+    try sock.bind(bind_addr);
+
+    // Signal that the socket is ready to receive data.
+    setting_up_socket.post();
+
+    var response_buf: [4096]u8 = undefined;
+
+    const len = try sock.receive(&response_buf);
+
+    res.* = try SaprusMessage.fromBytes(response_buf[0..len], allocator);
 }
 
 pub fn connect(payload: []const u8, allocator: Allocator) !void {
@@ -93,13 +108,30 @@ pub fn connect(payload: []const u8, allocator: Allocator) !void {
     } else unreachable;
 
     var initial_conn_res: ?SaprusMessage = null;
-    const response_thread = try std.Thread.spawn(.{}, awaitSentinelConnectionResponse, .{&initial_conn_res});
+    defer if (initial_conn_res) |c| c.deinit(allocator);
+
+    const response_thread = try std.Thread.spawn(
+        .{},
+        awaitSentinelConnectionResponse,
+        .{
+            &initial_conn_res,
+            allocator,
+        },
+    );
+
+    // Block until the socket is set up.
+    setting_up_socket.wait();
 
     const msg = try sendInitialConnection(payload, initial_port, allocator);
     std.debug.print("msg: {any}\n", .{msg});
 
     response_thread.join();
-    std.debug.print("initial_conn_res: {any}\n", .{initial_conn_res});
+
+    if (initial_conn_res) |c| {
+        std.debug.print("response: {any}\n", .{c});
+    } else {
+        std.debug.print("no data???", .{});
+    }
 }
 
 const SaprusMessage = @import("./saprus_message.zig").SaprusMessage;
-- 
cgit