mirror of
https://git.robbyzambito.me/zaprus/
synced 2026-02-04 11:44:49 +00:00
Fix reconnection
Was failing to reconnect due to trying to reuse the same socket that already had a BPF filter on it.
This commit is contained in:
21
src/main.zig
21
src/main.zig
@@ -1,3 +1,5 @@
|
||||
const is_debug = builtin.mode == .Debug;
|
||||
|
||||
const help =
|
||||
\\-h, --help Display this help and exit.
|
||||
\\-r, --relay <str> A relay message to send.
|
||||
@@ -82,10 +84,11 @@ pub fn main(init: std.process.Init) !void {
|
||||
return error.InvalidArguments;
|
||||
}
|
||||
|
||||
var client = try SaprusClient.init();
|
||||
defer client.deinit();
|
||||
var client: SaprusClient = undefined;
|
||||
|
||||
if (flags.relay != null) {
|
||||
client = try .init();
|
||||
defer client.deinit();
|
||||
var chunk_writer_buf: [2048]u8 = undefined;
|
||||
var chunk_writer: Writer = .fixed(&chunk_writer_buf);
|
||||
if (flags.relay.?.len > 0) {
|
||||
@@ -124,22 +127,24 @@ pub fn main(init: std.process.Init) !void {
|
||||
return;
|
||||
}
|
||||
|
||||
var retry_seconds: u16 = 120;
|
||||
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.?});
|
||||
|
||||
if (flags.connect != null) {
|
||||
reconnect: while (true) {
|
||||
client = try .init();
|
||||
defer client.deinit();
|
||||
log.debug("Starting connection", .{});
|
||||
|
||||
var init_con_buf: [SaprusClient.max_payload_len]u8 = undefined;
|
||||
var w: Writer = .fixed(&init_con_buf);
|
||||
try w.print("{b64}", .{flags.connect.?});
|
||||
var connection = client.connect(init.io, w.buffered()) catch {
|
||||
try init.io.sleep(.fromSeconds(retry_seconds), .boot);
|
||||
continue;
|
||||
};
|
||||
defer connection.deinit();
|
||||
|
||||
retry_seconds = 600;
|
||||
retry_seconds = 60 * if (is_debug) 1 else 10;
|
||||
|
||||
log.debug("Connection started", .{});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user