diff --git a/src/main.c b/src/main.c index a8149ba..c6f975f 100644 --- a/src/main.c +++ b/src/main.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include "log/log.h" @@ -53,7 +54,10 @@ static void load_default_ca_cert(struct ssl_context *ctx) static void signal_cb(struct ev_loop *loop, ev_signal *w, int revents) { + struct rtty *rtty = ev_userdata(loop); + if (w->signum == SIGINT) { + rtty->reconnect = false; ev_break(loop, EVBREAK_ALL); log_info("Normal quit\n"); } @@ -138,6 +142,8 @@ int main(int argc, char **argv) return -1; #endif + ev_set_userdata(loop, &rtty); + while (true) { c = getopt_long(argc, argv, shortopts, long_options, &option_index); if (c == -1) @@ -272,6 +278,8 @@ int main(int argc, char **argv) load_default_ca_cert(rtty.ssl_ctx); #endif + srand(time(NULL)); + if (rtty_start(&rtty) < 0) return -1; diff --git a/src/rtty.c b/src/rtty.c index 4ae3be9..592b45d 100644 --- a/src/rtty.c +++ b/src/rtty.c @@ -324,8 +324,10 @@ done: if (!rtty->reconnect) { ev_break(rtty->loop, EVBREAK_ALL); } else { - ev_timer_set(&rtty->tmr, 5.0, 0); + int delay = rand() % 10 + 5; + ev_timer_set(&rtty->tmr, delay, 0); ev_timer_start(rtty->loop, &rtty->tmr); + log_err("reconnect in %d seconds\n", delay); } } @@ -737,7 +739,6 @@ static void rtty_timer_cb(struct ev_loop *loop, struct ev_timer *w, int revents) struct rtty *rtty = container_of(w, struct rtty, tmr); if (rtty->sock < 0) { - log_err("rtty reconnecting...\n"); tcp_connect(rtty->loop, rtty->host, rtty->port, on_net_connected, rtty); return; }