mirror of
https://git.robbyzambito.me/zaprus
synced 2025-12-20 16:24: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();
|
network.deinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fn broadcastSaprusMessage(msg: SaprusMessage, allocator: Allocator) !void {
|
fn broadcastSaprusMessage(msg: SaprusMessage, allocator: Allocator) !void {
|
||||||
const msg_bytes = try msg.toBytes(allocator);
|
const msg_bytes = try msg.toBytes(allocator);
|
||||||
defer allocator.free(msg_bytes);
|
defer allocator.free(msg_bytes);
|
||||||
|
|
||||||
@@ -74,16 +74,31 @@ pub fn sendInitialConnection(payload: []const u8, initial_port: u16, allocator:
|
|||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn awaitSentinelConnectionResponse(res: *?SaprusMessage) !void {
|
var setting_up_socket: std.Thread.Semaphore = std.Thread.Semaphore{};
|
||||||
res.* = SaprusMessage{
|
|
||||||
.relay = .{
|
fn awaitSentinelConnectionResponse(
|
||||||
.header = .{
|
res: *?SaprusMessage,
|
||||||
.dest = .{ 255, 255, 255, 255 },
|
allocator: Allocator,
|
||||||
},
|
) !void {
|
||||||
.payload = "",
|
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 {
|
pub fn connect(payload: []const u8, allocator: Allocator) !void {
|
||||||
@@ -93,13 +108,30 @@ pub fn connect(payload: []const u8, allocator: Allocator) !void {
|
|||||||
} else unreachable;
|
} else unreachable;
|
||||||
|
|
||||||
var initial_conn_res: ?SaprusMessage = null;
|
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);
|
const msg = try sendInitialConnection(payload, initial_port, allocator);
|
||||||
std.debug.print("msg: {any}\n", .{msg});
|
std.debug.print("msg: {any}\n", .{msg});
|
||||||
|
|
||||||
response_thread.join();
|
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;
|
const SaprusMessage = @import("./saprus_message.zig").SaprusMessage;
|
||||||
|
|||||||
Reference in New Issue
Block a user