From 7afb913ef7bc57633d1ce9b4c083e8a814de4554 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Thu, 26 Apr 2018 13:39:41 +0100 Subject: [PATCH] src/coap_io.c: Fix re-transmission delays There are 2 issues fixed here Issue 1: In coap_write() (circa line 1074) if (XXXX && (s->last_rx_tx + session_timeout <= now || YYYY)) { coap_session_free(s); } else { if (XXXX) { coap_tick_t s_timeout = now - (s->last_rx_tx + session_timeout); s_timeout will end up as a large number as 'now' is less than (s->last_rx_tx + session_timeout) Fix is to reverse the expression Issue 2: In coap_write() (two places circa lines 1122 and 1136) If coap_dtls_handle_timeout(s) causes the session to go away, tls_timeout is set to 0, and so timeout does not get updated (could be 0 and so coap_write() returns 0 causing a wait for the default time in the select() call. Fix is to set timeout to 1 to cause a minimal time waiting in the select() --- src/coap_io.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/coap_io.c b/src/coap_io.c index f202731e..af8df960 100644 --- a/src/coap_io.c +++ b/src/coap_io.c @@ -1071,7 +1071,7 @@ coap_write(coap_context_t *ctx, coap_session_free(s); } else { if (s->type == COAP_SESSION_TYPE_SERVER && s->ref == 0 && s->sendqueue == NULL) { - coap_tick_t s_timeout = now - (s->last_rx_tx + session_timeout); + coap_tick_t s_timeout = (s->last_rx_tx + session_timeout) - now; if (timeout == 0 || s_timeout < timeout) timeout = s_timeout; } @@ -1119,7 +1119,12 @@ coap_write(coap_context_t *ctx, while (tls_timeout > 0 && tls_timeout <= now) { coap_log(LOG_DEBUG, "** %s: DTLS retransmit timeout\n", coap_session_str(s)); coap_dtls_handle_timeout(s); - tls_timeout = s->tls ? coap_dtls_get_timeout(s) : 0; + if (s->tls) + tls_timeout = coap_dtls_get_timeout(s); + else { + tls_timeout = 0; + timeout = 1; + } } if (tls_timeout > 0 && (timeout == 0 || tls_timeout - now < timeout)) timeout = tls_timeout - now; @@ -1133,7 +1138,12 @@ coap_write(coap_context_t *ctx, while (tls_timeout > 0 && tls_timeout <= now) { coap_log(LOG_DEBUG, "** %s: DTLS retransmit timeout\n", coap_session_str(s)); coap_dtls_handle_timeout(s); - tls_timeout = s->tls ? coap_dtls_get_timeout(s) : 0; + if (s->tls) + tls_timeout = coap_dtls_get_timeout(s); + else { + tls_timeout = 0; + timeout = 1; + } } if (tls_timeout > 0 && (timeout == 0 || tls_timeout - now < timeout)) timeout = tls_timeout - now;