Set timeouts instead of sleeping

This commit is contained in:
2026-01-24 20:56:37 -05:00
parent 09152377ed
commit 3c5f34d5c2
3 changed files with 10 additions and 12 deletions

View File

@@ -113,10 +113,9 @@ pub fn connect(self: Client, io: Io, payload: []const u8) !SaprusConnection {
try self.socket.send(full_msg);
var res_buf: [4096]u8 = undefined;
// Ignore response from sentinel, just accept that we got one.
log.debug("Awaiting handshake response", .{});
// Ignore response from sentinel, just accept that we got one.
_ = try self.socket.receive(&res_buf);
try io.sleep(.fromMilliseconds(40), .real);
headers.udp.dst_port = udp_dest_port;
headers.ip.id = rand.int(u16);

View File

@@ -72,10 +72,6 @@ pub fn init() !RawSocket {
const bind_ret = std.os.linux.bind(socket, @ptrCast(&sockaddr_ll), @sizeOf(@TypeOf(sockaddr_ll)));
if (bind_ret != 0) return error.BindError;
const timeout: std.os.linux.timeval = .{ .sec = 60 * if (is_debug) 1 else 10, .usec = 0 };
const timeout_ret = std.os.linux.setsockopt(socket, std.os.linux.SOL.SOCKET, std.os.linux.SO.RCVTIMEO, @ptrCast(&timeout), @sizeOf(@TypeOf(timeout)));
if (timeout_ret != 0) return error.SetTimeoutError;
return .{
.fd = socket,
.sockaddr_ll = sockaddr_ll,
@@ -83,6 +79,12 @@ pub fn init() !RawSocket {
};
}
pub fn setTimeout(self: *RawSocket, sec: isize, usec: i64) !void {
const timeout: std.os.linux.timeval = .{ .sec = sec, .usec = usec };
const timeout_ret = std.os.linux.setsockopt(self.fd, std.os.linux.SOL.SOCKET, std.os.linux.SO.RCVTIMEO, @ptrCast(&timeout), @sizeOf(@TypeOf(timeout)));
if (timeout_ret != 0) return error.SetTimeoutError;
}
pub fn deinit(self: *RawSocket) void {
_ = std.os.linux.close(self.fd);
self.* = undefined;

View File

@@ -127,8 +127,6 @@ pub fn main(init: std.process.Init) !void {
return;
}
var retry_seconds: u16 = 12 * if (is_debug) 1 else 10;
var init_con_buf: [SaprusClient.max_payload_len]u8 = undefined;
var w: Writer = .fixed(&init_con_buf);
try w.print("{b64}", .{flags.connect.?});
@@ -139,19 +137,18 @@ pub fn main(init: std.process.Init) !void {
defer client.deinit();
log.debug("Starting connection", .{});
try client.socket.setTimeout(if (is_debug) 3 else 25, 0);
var connection = client.connect(init.io, w.buffered()) catch {
try init.io.sleep(.fromSeconds(retry_seconds), .boot);
log.debug("Connection timed out", .{});
continue;
};
retry_seconds = 60 * if (is_debug) 1 else 10;
log.debug("Connection started", .{});
next_message: while (true) {
var res_buf: [2048]u8 = undefined;
try client.socket.setTimeout(if (is_debug) 60 else 600, 0);
const next = connection.next(init.io, &res_buf) catch {
try init.io.sleep(.fromSeconds(retry_seconds), .boot);
continue :reconnect;
};