From 116c8f7c3a092d192b44ba6eea53566eceb656b9 Mon Sep 17 00:00:00 2001 From: defanor Date: Wed, 9 Aug 2023 09:39:13 +0300 Subject: Fix pinging It was broken since commit 938963c1d1c6a9b929d28a90030332d4f397ca61, since seconds from monotonic clocks were compared to calendar ones. Now last_network_activity is a timespec, using monotonic clocks as well. --- src/rexmpp.c | 14 ++++++++------ src/rexmpp.h | 2 +- src/rexmpp.rs | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/rexmpp.c b/src/rexmpp.c index a119d1f..a647245 100644 --- a/src/rexmpp.c +++ b/src/rexmpp.c @@ -562,7 +562,8 @@ rexmpp_err_t rexmpp_init (rexmpp_t *s, s->console_print_cb = NULL; s->ping_delay = 600; s->ping_requested = 0; - s->last_network_activity = 0; + s->last_network_activity.tv_sec = 0; + s->last_network_activity.tv_nsec = 0; s->disco_info = NULL; /* The default description. Since the players and streamers are @@ -933,7 +934,7 @@ rexmpp_err_t rexmpp_send_continue (rexmpp_t *s) 0); } if (ret > 0) { - s->last_network_activity = time(NULL); + clock_gettime(CLOCK_MONOTONIC, &(s->last_network_activity)); s->send_buffer_sent += ret; if (s->send_buffer_sent == s->send_buffer_len) { free(s->send_buffer); @@ -1221,7 +1222,7 @@ rexmpp_err_t rexmpp_recv (rexmpp_t *s) { chunk_raw_len = recv(s->server_socket, chunk_raw, 4096, 0); } if (chunk_raw_len > 0) { - s->last_network_activity = time(NULL); + clock_gettime(CLOCK_MONOTONIC, &(s->last_network_activity)); if (s->sasl_state == REXMPP_SASL_ACTIVE) { sasl_err = rexmpp_sasl_decode(s, chunk_raw, chunk_raw_len, &chunk, &chunk_len); @@ -2493,7 +2494,7 @@ rexmpp_err_t rexmpp_run (rexmpp_t *s, fd_set *read_fds, fd_set *write_fds) { /* Pinging the server. */ if (s->tcp_state == REXMPP_TCP_CONNECTED && - s->last_network_activity + s->ping_delay <= time(NULL)) { + s->last_network_activity.tv_sec + s->ping_delay <= now.tv_sec) { if (s->ping_requested == 0) { s->ping_requested = 1; rexmpp_xml_t *ping_cmd = @@ -2653,8 +2654,9 @@ struct timespec *rexmpp_timeout (rexmpp_t *s, } if (s->tcp_state == REXMPP_TCP_CONNECTED && - s->last_network_activity + s->ping_delay > now.tv_sec) { - time_t next_ping = s->last_network_activity + s->ping_delay - now.tv_sec; + s->last_network_activity.tv_sec + s->ping_delay > now.tv_sec) { + time_t next_ping = + s->last_network_activity.tv_sec + s->ping_delay - now.tv_sec; if (ret == NULL || next_ping < ret->tv_sec) { tv->tv_sec = next_ping; tv->tv_nsec = 0; diff --git a/src/rexmpp.h b/src/rexmpp.h index dec03dc..8211886 100644 --- a/src/rexmpp.h +++ b/src/rexmpp.h @@ -333,7 +333,7 @@ struct rexmpp /* Server ping configuration and state. */ int ping_delay; int ping_requested; - time_t last_network_activity; + struct timespec last_network_activity; /* DNS-related structures. */ rexmpp_dns_ctx_t resolver; diff --git a/src/rexmpp.rs b/src/rexmpp.rs index bf09183..92b6fa0 100644 --- a/src/rexmpp.rs +++ b/src/rexmpp.rs @@ -200,7 +200,7 @@ pub struct Rexmpp { // Server ping configuration and state pub ping_delay: c_int, pub ping_requested: c_int, - pub last_network_activity: time_t, + pub last_network_activity: timespec, // DNS-related structures pub resolver: *mut c_void, -- cgit v1.2.3