Skip to content

Commit

Permalink
feat(websocket): interruptable wait timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
erkia committed Jul 3, 2024
1 parent 25d8423 commit 0ccc3a6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
17 changes: 16 additions & 1 deletion components/esp_websocket_client/esp_websocket_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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
Expand Down

0 comments on commit 0ccc3a6

Please sign in to comment.