From 0ccc3a6aa1d305b85aaf8ca4f33ee14bef04271f Mon Sep 17 00:00:00 2001 From: Erki Aring Date: Wed, 3 Jul 2024 11:02:57 +0300 Subject: [PATCH] feat(websocket): interruptable wait timeout --- .../esp_websocket_client/esp_websocket_client.c | 17 ++++++++++++++++- .../include/esp_websocket_client.h | 7 +++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/components/esp_websocket_client/esp_websocket_client.c b/components/esp_websocket_client/esp_websocket_client.c index 6b7e897309..d3d80b5caf 100644 --- a/components/esp_websocket_client/esp_websocket_client.c +++ b/components/esp_websocket_client/esp_websocket_client.c @@ -1063,8 +1063,13 @@ static void esp_websocket_client_task(void *pv) esp_websocket_client_abort_connection(client, WEBSOCKET_ERROR_TYPE_TCP_TRANSPORT); } } else if (WEBSOCKET_STATE_WAIT_TIMEOUT == client->state) { + // clear any pending notifications + ulTaskNotifyTake(pdTRUE, 0); // waiting for reconnecting... - vTaskDelay(client->wait_timeout_ms / 2 / portTICK_PERIOD_MS); + int delay = client->wait_timeout_ms - (_tick_get_ms() - client->reconnect_tick_ms); + if (client->run && delay >= 0) { + ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(delay) + 1); + } } else if (WEBSOCKET_STATE_CLOSING == client->state && (CLOSE_FRAME_SENT_BIT & xEventGroupGetBits(client->status_bits))) { ESP_LOGD(TAG, " Waiting for TCP connection to be closed by the server"); @@ -1133,6 +1138,9 @@ esp_err_t esp_websocket_client_stop(esp_websocket_client_handle_t client) client->run = false; + if (client->task_handle) { + xTaskNotifyGive(client->task_handle); + } xEventGroupWaitBits(client->status_bits, STOPPED_BIT, false, true, portMAX_DELAY); client->state = WEBSOCKET_STATE_UNKNOW; return ESP_OK; @@ -1188,6 +1196,9 @@ static esp_err_t esp_websocket_client_close_with_optional_body(esp_websocket_cli // If could not close gracefully within timeout, stop the client and disconnect client->run = false; + if (client->task_handle) { + xTaskNotifyGive(client->task_handle); + } xEventGroupWaitBits(client->status_bits, STOPPED_BIT, false, true, portMAX_DELAY); client->state = WEBSOCKET_STATE_UNKNOW; return ESP_OK; @@ -1312,6 +1323,10 @@ esp_err_t esp_websocket_client_set_reconnect_timeout(esp_websocket_client_handle client->wait_timeout_ms = reconnect_timeout_ms; + if (client->task_handle) { + xTaskNotifyGive(client->task_handle); + } + return ESP_OK; } diff --git a/components/esp_websocket_client/include/esp_websocket_client.h b/components/esp_websocket_client/include/esp_websocket_client.h index ea0aa7411c..6ae3e111c8 100644 --- a/components/esp_websocket_client/include/esp_websocket_client.h +++ b/components/esp_websocket_client/include/esp_websocket_client.h @@ -417,7 +417,7 @@ size_t esp_websocket_client_get_ping_interval_sec(esp_websocket_client_handle_t esp_err_t esp_websocket_client_set_ping_interval_sec(esp_websocket_client_handle_t client, size_t ping_interval_sec); /** - * @brief Get the next reconnect timeout for client. Returns -1 when client is not initialized or automatic reconnect is disabled. + * @brief Get the reconnect timeout for client. Returns -1 when client is not initialized or automatic reconnect is disabled. * * @param[in] client The client * @@ -426,11 +426,10 @@ esp_err_t esp_websocket_client_set_ping_interval_sec(esp_websocket_client_handle int esp_websocket_client_get_reconnect_timeout(esp_websocket_client_handle_t client); /** - * @brief Set next reconnect timeout for client. + * @brief Set new reconnect timeout for client. * * Notes: - * - Changing this value when reconnection delay is already active does not immediately affect the active delay and may have unexpected result. - * - Good place to change this value is when handling WEBSOCKET_EVENT_DISCONNECTED or WEBSOCKET_EVENT_ERROR events. + * - This also updates already active reconnection delay, if any. * * @param[in] client The client * @param[in] reconnect_timeout_ms The new timeout