From 52cdfa90878291c8827042ddf8f4da6884512544 Mon Sep 17 00:00:00 2001 From: Marius Vikhammer Date: Mon, 30 Sep 2019 14:57:24 +0800 Subject: [PATCH] Fix early retransmit The time for retransmitting a message was set before transfer was done. This ment that if the transfer speed is slow or the message is big then it would be possible to trigger a retransmit too early. Closes https://github.com/espressif/esp-mqtt/issues/131 --- lib/include/mqtt_outbox.h | 1 + lib/mqtt_outbox.c | 10 ++++++++++ mqtt_client.c | 4 +++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/include/mqtt_outbox.h b/lib/include/mqtt_outbox.h index 5c84b66..8d5f342 100644 --- a/lib/include/mqtt_outbox.h +++ b/lib/include/mqtt_outbox.h @@ -45,6 +45,7 @@ esp_err_t outbox_delete_msgtype(outbox_handle_t outbox, int msg_type); int outbox_delete_expired(outbox_handle_t outbox, int current_tick, int timeout); esp_err_t outbox_set_pending(outbox_handle_t outbox, int msg_id, pending_state_t pending); +esp_err_t outbox_set_tick(outbox_handle_t outbox, int msg_id, int tick); int outbox_get_size(outbox_handle_t outbox); esp_err_t outbox_cleanup(outbox_handle_t outbox, int max_size); void outbox_destroy(outbox_handle_t outbox); diff --git a/lib/mqtt_outbox.c b/lib/mqtt_outbox.c index 8b1ff8d..5db1224 100644 --- a/lib/mqtt_outbox.c +++ b/lib/mqtt_outbox.c @@ -131,6 +131,16 @@ esp_err_t outbox_set_pending(outbox_handle_t outbox, int msg_id, pending_state_t return ESP_FAIL; } +esp_err_t outbox_set_tick(outbox_handle_t outbox, int msg_id, int tick) +{ + outbox_item_handle_t item = outbox_get(outbox, msg_id); + if (item) { + item->tick = tick; + return ESP_OK; + } + return ESP_FAIL; +} + esp_err_t outbox_delete_msgtype(outbox_handle_t outbox, int msg_type) { outbox_item_handle_t item, tmp; diff --git a/mqtt_client.c b/mqtt_client.c index 4d228ee..f359766 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -1355,7 +1355,9 @@ int esp_mqtt_client_publish(esp_mqtt_client_handle_t client, const char *topic, } } - if (qos > 0) { + if (qos > 0) { + //Tick is set after transmit to avoid retransmitting too early due slow network speed / big messages + outbox_set_tick(client->outbox, pending_msg_id, platform_tick_get_ms()); outbox_set_pending(client->outbox, pending_msg_id, TRANSMITTED); } MQTT_API_UNLOCK_FROM_OTHER_TASK(client);