Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(websocket): interruptable wait timeout #607

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading