Skip to content

Commit

Permalink
Reduce tcp packets sent by websocket client by merging header with th…
Browse files Browse the repository at this point in the history
…e payload
  • Loading branch information
0xFEEDC0DE64 committed Jan 17, 2024
1 parent 37cac66 commit 932c186
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions components/esp_websocket_client/esp_websocket_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -554,16 +554,24 @@ static int esp_websocket_client_send_with_exact_opcode(esp_websocket_client_hand
bool contained_fin = opcode & WS_TRANSPORT_OPCODES_FIN;

while (widx < len || opcode) { // allow for sending "current_opcode" only message with len==0
if (need_write > client->buffer_size) {
need_write = client->buffer_size;
if (need_write > client->buffer_size - MAX_WEBSOCKET_HEADER_SIZE) {
need_write = client->buffer_size - MAX_WEBSOCKET_HEADER_SIZE;
opcode = opcode & ~WS_TRANSPORT_OPCODES_FIN;
} else if (contained_fin) {
opcode = opcode | WS_TRANSPORT_OPCODES_FIN;
}
memcpy(client->tx_buffer, data + widx, need_write);
memcpy(client->tx_buffer + MAX_WEBSOCKET_HEADER_SIZE, data + widx, need_write);
// send with ws specific way and specific opcode
wlen = esp_transport_ws_send_raw(client->transport, opcode, (char *)client->tx_buffer, need_write,
(timeout == portMAX_DELAY) ? -1 : timeout * portTICK_PERIOD_MS);
wlen = esp_transport_ws_send_raw_optimized(client->transport, opcode, (char *)client->tx_buffer, need_write + MAX_WEBSOCKET_HEADER_SIZE,
(timeout == portMAX_DELAY) ? -1 : timeout * portTICK_PERIOD_MS);
if (wlen >= MAX_WEBSOCKET_HEADER_SIZE)
wlen -= MAX_WEBSOCKET_HEADER_SIZE;
else if (wlen > 0)
{
ESP_LOGW(TAG, "couldnt write enough for ws header, wlen=%d", wlen);
wlen = 0;
}

if (wlen < 0 || (wlen == 0 && need_write != 0)) {
ret = wlen;
esp_websocket_free_buf(client, true);
Expand Down

0 comments on commit 932c186

Please sign in to comment.