Skip to content

Commit d31ad02

Browse files
authored
Merge pull request #515 from david-cermak/fix/ws_dyn_buffer
[ws-client]: Fix usage of the dynamic buffer
2 parents 4977f96 + 9c54b72 commit d31ad02

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

components/esp_websocket_client/esp_websocket_client.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -550,9 +550,13 @@ static int esp_websocket_client_send_with_exact_opcode(esp_websocket_client_hand
550550
int ret = -1;
551551
int need_write = len;
552552
int wlen = 0, widx = 0;
553-
554553
bool contained_fin = opcode & WS_TRANSPORT_OPCODES_FIN;
555554

555+
if (esp_websocket_new_buf(client, true) != ESP_OK) {
556+
ESP_LOGE(TAG, "Failed to setup tx buffer");
557+
return -1;
558+
}
559+
556560
while (widx < len || opcode) { // allow for sending "current_opcode" only message with len==0
557561
if (need_write > client->buffer_size) {
558562
need_write = client->buffer_size;
@@ -1204,33 +1208,27 @@ int esp_websocket_client_send_fin(esp_websocket_client_handle_t client, TickType
12041208

12051209
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)
12061210
{
1207-
int ret = ESP_OK;
1211+
int ret = -1;
12081212
if (client == NULL || len < 0 || (data == NULL && len > 0)) {
12091213
ESP_LOGE(TAG, "Invalid arguments");
1210-
return ESP_FAIL;
1214+
return -1;
12111215
}
12121216

12131217
if (xSemaphoreTakeRecursive(client->lock, timeout) != pdPASS) {
12141218
ESP_LOGE(TAG, "Could not lock ws-client within %" PRIu32 " timeout", timeout);
1215-
return ESP_FAIL;
1219+
return -1;
12161220
}
12171221

12181222
if (!esp_websocket_client_is_connected(client)) {
12191223
ESP_LOGE(TAG, "Websocket client is not connected");
1220-
ret = ESP_FAIL;
12211224
goto unlock_and_return;
12221225
}
12231226

12241227
if (client->transport == NULL) {
12251228
ESP_LOGE(TAG, "Invalid transport");
1226-
ret = ESP_FAIL;
1227-
goto unlock_and_return;
1228-
}
1229-
if (esp_websocket_new_buf(client, true) != ESP_OK) {
1230-
ESP_LOGE(TAG, "Failed to setup tx buffer");
1231-
ret = ESP_FAIL;
12321229
goto unlock_and_return;
12331230
}
1231+
12341232
ret = esp_websocket_client_send_with_exact_opcode(client, opcode | WS_TRANSPORT_OPCODES_FIN, data, len, timeout);
12351233
if (ret < 0) {
12361234
ESP_LOGE(TAG, "Failed to send the buffer");

0 commit comments

Comments
 (0)