mirror of
https://git.robbyzambito.me/zaprus
synced 2025-12-20 08:14:50 +00:00
Initial awaiting for handshake response
seems like i might be getting my own initial connection? I get a response even without the sentinal running.
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user