|
|
@@ -52,6 +52,8 @@ static const char *TAG = "WEBSOCKET_CLIENT";
|
|
|
}
|
|
|
|
|
|
const static int STOPPED_BIT = BIT0;
|
|
|
+const static int CLOSE_FRAME_SENT_BIT = BIT1; // Indicates that a close frame was sent by the client
|
|
|
+ // and we are waiting for the server to continue with clean close
|
|
|
|
|
|
ESP_EVENT_DEFINE_BASE(WEBSOCKET_EVENTS);
|
|
|
|
|
|
@@ -80,6 +82,7 @@ typedef enum {
|
|
|
WEBSOCKET_STATE_INIT,
|
|
|
WEBSOCKET_STATE_CONNECTED,
|
|
|
WEBSOCKET_STATE_WAIT_TIMEOUT,
|
|
|
+ WEBSOCKET_STATE_CLOSING,
|
|
|
} websocket_client_state_t;
|
|
|
|
|
|
struct esp_websocket_client {
|
|
|
@@ -493,14 +496,20 @@ static esp_err_t esp_websocket_client_recv(esp_websocket_client_handle_t client)
|
|
|
const char *data = (client->payload_len == 0) ? NULL : client->rx_buffer;
|
|
|
esp_transport_ws_send_raw(client->transport, WS_TRANSPORT_OPCODES_PONG | WS_TRANSPORT_OPCODES_FIN, data, client->payload_len,
|
|
|
client->config->network_timeout_ms);
|
|
|
- }
|
|
|
- else if (client->last_opcode == WS_TRANSPORT_OPCODES_PONG) {
|
|
|
+ } else if (client->last_opcode == WS_TRANSPORT_OPCODES_PONG) {
|
|
|
client->wait_for_pong_resp = false;
|
|
|
+ } else if (client->last_opcode == WS_TRANSPORT_OPCODES_CLOSE) {
|
|
|
+ ESP_LOGD(TAG, "Received close frame");
|
|
|
+ client->state = WEBSOCKET_STATE_CLOSING;
|
|
|
}
|
|
|
|
|
|
return ESP_OK;
|
|
|
}
|
|
|
|
|
|
+static int esp_websocket_client_send_with_opcode(esp_websocket_client_handle_t client, ws_transport_opcodes_t opcode, const uint8_t *data, int len, TickType_t timeout);
|
|
|
+
|
|
|
+static int esp_websocket_client_send_close(esp_websocket_client_handle_t client, int code, const char *additional_data, int total_len, TickType_t timeout);
|
|
|
+
|
|
|
static void esp_websocket_client_task(void *pv)
|
|
|
{
|
|
|
const int lock_timeout = portMAX_DELAY;
|
|
|
@@ -520,7 +529,7 @@ static void esp_websocket_client_task(void *pv)
|
|
|
}
|
|
|
|
|
|
client->state = WEBSOCKET_STATE_INIT;
|
|
|
- xEventGroupClearBits(client->status_bits, STOPPED_BIT);
|
|
|
+ xEventGroupClearBits(client->status_bits, STOPPED_BIT | CLOSE_FRAME_SENT_BIT);
|
|
|
int read_select = 0;
|
|
|
while (client->run) {
|
|
|
if (xSemaphoreTakeRecursive(client->lock, lock_timeout) != pdPASS) {
|
|
|
@@ -550,22 +559,25 @@ static void esp_websocket_client_task(void *pv)
|
|
|
|
|
|
break;
|
|
|
case WEBSOCKET_STATE_CONNECTED:
|
|
|
- if (_tick_get_ms() - client->ping_tick_ms > WEBSOCKET_PING_TIMEOUT_MS) {
|
|
|
- client->ping_tick_ms = _tick_get_ms();
|
|
|
- ESP_LOGD(TAG, "Sending PING...");
|
|
|
- esp_transport_ws_send_raw(client->transport, WS_TRANSPORT_OPCODES_PING | WS_TRANSPORT_OPCODES_FIN, NULL, 0, client->config->network_timeout_ms);
|
|
|
-
|
|
|
- if (!client->wait_for_pong_resp && client->config->pingpong_timeout_sec) {
|
|
|
- client->pingpong_tick_ms = _tick_get_ms();
|
|
|
- client->wait_for_pong_resp = true;
|
|
|
+ if ((CLOSE_FRAME_SENT_BIT & xEventGroupGetBits(client->status_bits)) == 0) { // only send and check for PING
|
|
|
+ // if closing hasn't been initiated
|
|
|
+ if (_tick_get_ms() - client->ping_tick_ms > WEBSOCKET_PING_TIMEOUT_MS) {
|
|
|
+ client->ping_tick_ms = _tick_get_ms();
|
|
|
+ ESP_LOGD(TAG, "Sending PING...");
|
|
|
+ esp_transport_ws_send_raw(client->transport, WS_TRANSPORT_OPCODES_PING | WS_TRANSPORT_OPCODES_FIN, NULL, 0, client->config->network_timeout_ms);
|
|
|
+
|
|
|
+ if (!client->wait_for_pong_resp && client->config->pingpong_timeout_sec) {
|
|
|
+ client->pingpong_tick_ms = _tick_get_ms();
|
|
|
+ client->wait_for_pong_resp = true;
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- if ( _tick_get_ms() - client->pingpong_tick_ms > client->config->pingpong_timeout_sec*1000 ) {
|
|
|
- if (client->wait_for_pong_resp) {
|
|
|
- ESP_LOGE(TAG, "Error, no PONG received for more than %d seconds after PING", client->config->pingpong_timeout_sec);
|
|
|
- esp_websocket_client_abort_connection(client);
|
|
|
- break;
|
|
|
+ if ( _tick_get_ms() - client->pingpong_tick_ms > client->config->pingpong_timeout_sec*1000 ) {
|
|
|
+ if (client->wait_for_pong_resp) {
|
|
|
+ ESP_LOGE(TAG, "Error, no PONG received for more than %d seconds after PING", client->config->pingpong_timeout_sec);
|
|
|
+ esp_websocket_client_abort_connection(client);
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -593,6 +605,17 @@ static void esp_websocket_client_task(void *pv)
|
|
|
ESP_LOGD(TAG, "Reconnecting...");
|
|
|
}
|
|
|
break;
|
|
|
+ case WEBSOCKET_STATE_CLOSING:
|
|
|
+ // if closing not initiated by the client echo the close message back
|
|
|
+ if ((CLOSE_FRAME_SENT_BIT & xEventGroupGetBits(client->status_bits)) == 0) {
|
|
|
+ ESP_LOGD(TAG, "Closing initiated by the server, sending close frame");
|
|
|
+ esp_transport_ws_send_raw(client->transport, WS_TRANSPORT_OPCODES_CLOSE | WS_TRANSPORT_OPCODES_FIN, NULL, 0, client->config->network_timeout_ms);
|
|
|
+ xEventGroupSetBits(client->status_bits, CLOSE_FRAME_SENT_BIT);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ ESP_LOGD(TAG, "Client run iteration in a default state: %d", client->state);
|
|
|
+ break;
|
|
|
}
|
|
|
xSemaphoreGiveRecursive(client->lock);
|
|
|
if (WEBSOCKET_STATE_CONNECTED == client->state) {
|
|
|
@@ -604,6 +627,21 @@ static void esp_websocket_client_task(void *pv)
|
|
|
} else if (WEBSOCKET_STATE_WAIT_TIMEOUT == client->state) {
|
|
|
// waiting for reconnecting...
|
|
|
vTaskDelay(client->wait_timeout_ms / 2 / portTICK_RATE_MS);
|
|
|
+ } 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");
|
|
|
+ int ret = esp_transport_ws_poll_connection_closed(client->transport, 1000);
|
|
|
+ if (ret == 0) {
|
|
|
+ // still waiting
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (ret < 0) {
|
|
|
+ ESP_LOGW(TAG, "Connection terminated while waiting for clean TCP close");
|
|
|
+ }
|
|
|
+ client->run = false;
|
|
|
+ client->state = WEBSOCKET_STATE_UNKNOW;
|
|
|
+ esp_websocket_client_dispatch_event(client, WEBSOCKET_EVENT_CLOSED, NULL, 0);
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -626,7 +664,7 @@ esp_err_t esp_websocket_client_start(esp_websocket_client_handle_t client)
|
|
|
ESP_LOGE(TAG, "Error create websocket task");
|
|
|
return ESP_FAIL;
|
|
|
}
|
|
|
- xEventGroupClearBits(client->status_bits, STOPPED_BIT);
|
|
|
+ xEventGroupClearBits(client->status_bits, STOPPED_BIT | CLOSE_FRAME_SENT_BIT);
|
|
|
return ESP_OK;
|
|
|
}
|
|
|
|
|
|
@@ -645,30 +683,87 @@ esp_err_t esp_websocket_client_stop(esp_websocket_client_handle_t client)
|
|
|
return ESP_OK;
|
|
|
}
|
|
|
|
|
|
-static int esp_websocket_client_send_with_opcode(esp_websocket_client_handle_t client, ws_transport_opcodes_t opcode, const char *data, int len, TickType_t timeout);
|
|
|
+static int esp_websocket_client_send_close(esp_websocket_client_handle_t client, int code, const char *additional_data, int total_len, TickType_t timeout)
|
|
|
+{
|
|
|
+ uint8_t *close_status_data = NULL;
|
|
|
+ // RFC6455#section-5.5.1: The Close frame MAY contain a body (indicated by total_len >= 2)
|
|
|
+ if (total_len >= 2) {
|
|
|
+ close_status_data = calloc(1, total_len);
|
|
|
+ ESP_WS_CLIENT_MEM_CHECK(TAG, close_status_data, return -1);
|
|
|
+ // RFC6455#section-5.5.1: The first two bytes of the body MUST be a 2-byte representing a status
|
|
|
+ uint16_t *code_network_order = (uint16_t *) close_status_data;
|
|
|
+ *code_network_order = htons(code);
|
|
|
+ memcpy(close_status_data + 2, additional_data, total_len - 2);
|
|
|
+ }
|
|
|
+ int ret = esp_websocket_client_send_with_opcode(client, WS_TRANSPORT_OPCODES_CLOSE, close_status_data, total_len, timeout);
|
|
|
+ free(close_status_data);
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static esp_err_t esp_websocket_client_close_with_optional_body(esp_websocket_client_handle_t client, bool send_body, int code, const char *data, int len, TickType_t timeout)
|
|
|
+{
|
|
|
+ if (client == NULL) {
|
|
|
+ return ESP_ERR_INVALID_ARG;
|
|
|
+ }
|
|
|
+ if (!client->run) {
|
|
|
+ ESP_LOGW(TAG, "Client was not started");
|
|
|
+ return ESP_FAIL;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (send_body) {
|
|
|
+ esp_websocket_client_send_close(client, code, data, len + 2, portMAX_DELAY); // len + 2 -> always sending the code
|
|
|
+ } else {
|
|
|
+ esp_websocket_client_send_close(client, 0, NULL, 0, portMAX_DELAY); // only opcode frame
|
|
|
+ }
|
|
|
+
|
|
|
+ // Set closing bit to prevent from sending PING frames while connected
|
|
|
+ xEventGroupSetBits(client->status_bits, CLOSE_FRAME_SENT_BIT);
|
|
|
+
|
|
|
+ if (STOPPED_BIT & xEventGroupWaitBits(client->status_bits, STOPPED_BIT, false, true, timeout)) {
|
|
|
+ return ESP_OK;
|
|
|
+ }
|
|
|
+
|
|
|
+ // If could not close gracefully within timeout, stop the client and disconnect
|
|
|
+ client->run = false;
|
|
|
+ xEventGroupWaitBits(client->status_bits, STOPPED_BIT, false, true, portMAX_DELAY);
|
|
|
+ client->state = WEBSOCKET_STATE_UNKNOW;
|
|
|
+ return ESP_OK;
|
|
|
+}
|
|
|
+
|
|
|
+esp_err_t esp_websocket_client_close_with_code(esp_websocket_client_handle_t client, int code, const char *data, int len, TickType_t timeout)
|
|
|
+{
|
|
|
+ return esp_websocket_client_close_with_optional_body(client, true, code, data, len, timeout);
|
|
|
+}
|
|
|
+
|
|
|
+esp_err_t esp_websocket_client_close(esp_websocket_client_handle_t client, TickType_t timeout)
|
|
|
+{
|
|
|
+ return esp_websocket_client_close_with_optional_body(client, false, 0, NULL, 0, timeout);
|
|
|
+}
|
|
|
|
|
|
int esp_websocket_client_send_text(esp_websocket_client_handle_t client, const char *data, int len, TickType_t timeout)
|
|
|
{
|
|
|
- return esp_websocket_client_send_with_opcode(client, WS_TRANSPORT_OPCODES_TEXT, data, len, timeout);
|
|
|
+ return esp_websocket_client_send_with_opcode(client, WS_TRANSPORT_OPCODES_TEXT, (const uint8_t *)data, len, timeout);
|
|
|
}
|
|
|
|
|
|
int esp_websocket_client_send(esp_websocket_client_handle_t client, const char *data, int len, TickType_t timeout)
|
|
|
{
|
|
|
- return esp_websocket_client_send_with_opcode(client, WS_TRANSPORT_OPCODES_BINARY, data, len, timeout);
|
|
|
+ return esp_websocket_client_send_with_opcode(client, WS_TRANSPORT_OPCODES_BINARY, (const uint8_t *)data, len, timeout);
|
|
|
}
|
|
|
|
|
|
int esp_websocket_client_send_bin(esp_websocket_client_handle_t client, const char *data, int len, TickType_t timeout)
|
|
|
{
|
|
|
- return esp_websocket_client_send_with_opcode(client, WS_TRANSPORT_OPCODES_BINARY, data, len, timeout);
|
|
|
+ return esp_websocket_client_send_with_opcode(client, WS_TRANSPORT_OPCODES_BINARY, (const uint8_t *)data, len, timeout);
|
|
|
}
|
|
|
|
|
|
-static int esp_websocket_client_send_with_opcode(esp_websocket_client_handle_t client, ws_transport_opcodes_t opcode, const char *data, int len, TickType_t timeout)
|
|
|
+static int esp_websocket_client_send_with_opcode(esp_websocket_client_handle_t client, ws_transport_opcodes_t opcode, const uint8_t *data, int len, TickType_t timeout)
|
|
|
{
|
|
|
int need_write = len;
|
|
|
int wlen = 0, widx = 0;
|
|
|
int ret = ESP_FAIL;
|
|
|
|
|
|
- if (client == NULL || data == NULL || len <= 0) {
|
|
|
+ if (client == NULL || len < 0 ||
|
|
|
+ (opcode != WS_TRANSPORT_OPCODES_CLOSE && (data == NULL || len <= 0))) {
|
|
|
ESP_LOGE(TAG, "Invalid arguments");
|
|
|
return ESP_FAIL;
|
|
|
}
|
|
|
@@ -688,7 +783,7 @@ static int esp_websocket_client_send_with_opcode(esp_websocket_client_handle_t c
|
|
|
goto unlock_and_return;
|
|
|
}
|
|
|
uint32_t current_opcode = opcode;
|
|
|
- while (widx < len) {
|
|
|
+ while (widx < len || current_opcode) { // allow for sending "current_opcode" only message with len==0
|
|
|
if (need_write > client->buffer_size) {
|
|
|
need_write = client->buffer_size;
|
|
|
} else {
|
|
|
@@ -698,7 +793,7 @@ static int esp_websocket_client_send_with_opcode(esp_websocket_client_handle_t c
|
|
|
// send with ws specific way and specific opcode
|
|
|
wlen = esp_transport_ws_send_raw(client->transport, current_opcode, (char *)client->tx_buffer, need_write,
|
|
|
(timeout==portMAX_DELAY)? -1 : timeout * portTICK_PERIOD_MS);
|
|
|
- if (wlen <= 0) {
|
|
|
+ if (wlen < 0 || (wlen == 0 && need_write != 0)) {
|
|
|
ret = wlen;
|
|
|
ESP_LOGE(TAG, "Network error: esp_transport_write() returned %d, errno=%d", ret, errno);
|
|
|
esp_websocket_client_abort_connection(client);
|