tcp_transport: modified ws_read to read payload directly to the read buffer and separately from header bytes
Previous version read all data to the buffer including header which reduced maximum payload read. This version uses a local array to receive header and reads payload bytes to the buffer
cherry-picked from db121a56e1
This commit is contained in:
parent
ddbce78bff
commit
814ee9db7f
2 changed files with 13 additions and 17 deletions
|
@ -1 +1 @@
|
||||||
Subproject commit f08f3b678717865234637164a29ed3a63e756ca7
|
Subproject commit 6f1fc3785af8cc7765f78dd93f09aeb0109cd2b7
|
|
@ -184,8 +184,8 @@ static int ws_read(esp_transport_handle_t t, char *buffer, int len, int timeout_
|
||||||
{
|
{
|
||||||
transport_ws_t *ws = esp_transport_get_context_data(t);
|
transport_ws_t *ws = esp_transport_get_context_data(t);
|
||||||
int payload_len;
|
int payload_len;
|
||||||
int payload_len_buff = len;
|
char ws_header[MAX_WEBSOCKET_HEADER_SIZE];
|
||||||
char *data_ptr = buffer, opcode, mask, *mask_key = NULL;
|
char *data_ptr = ws_header, opcode, mask, *mask_key = NULL;
|
||||||
int rlen;
|
int rlen;
|
||||||
int poll_read;
|
int poll_read;
|
||||||
if ((poll_read = esp_transport_poll_read(ws->parent, timeout_ms)) <= 0) {
|
if ((poll_read = esp_transport_poll_read(ws->parent, timeout_ms)) <= 0) {
|
||||||
|
@ -194,7 +194,7 @@ static int ws_read(esp_transport_handle_t t, char *buffer, int len, int timeout_
|
||||||
|
|
||||||
// Receive and process header first (based on header size)
|
// Receive and process header first (based on header size)
|
||||||
int header = 2;
|
int header = 2;
|
||||||
if ((rlen = esp_transport_read(ws->parent, buffer, header, timeout_ms)) <= 0) {
|
if ((rlen = esp_transport_read(ws->parent, data_ptr, header, timeout_ms)) <= 0) {
|
||||||
ESP_LOGE(TAG, "Error read data");
|
ESP_LOGE(TAG, "Error read data");
|
||||||
return rlen;
|
return rlen;
|
||||||
}
|
}
|
||||||
|
@ -211,8 +211,6 @@ static int ws_read(esp_transport_handle_t t, char *buffer, int len, int timeout_
|
||||||
return rlen;
|
return rlen;
|
||||||
}
|
}
|
||||||
payload_len = data_ptr[0] << 8 | data_ptr[1];
|
payload_len = data_ptr[0] << 8 | data_ptr[1];
|
||||||
payload_len_buff = len - 4;
|
|
||||||
data_ptr += 2;
|
|
||||||
} else if (payload_len == 127) {
|
} else if (payload_len == 127) {
|
||||||
// headerLen += 8;
|
// headerLen += 8;
|
||||||
header = 8;
|
header = 8;
|
||||||
|
@ -227,27 +225,25 @@ static int ws_read(esp_transport_handle_t t, char *buffer, int len, int timeout_
|
||||||
} else {
|
} else {
|
||||||
payload_len = data_ptr[4] << 24 | data_ptr[5] << 16 | data_ptr[6] << 8 | data_ptr[7];
|
payload_len = data_ptr[4] << 24 | data_ptr[5] << 16 | data_ptr[6] << 8 | data_ptr[7];
|
||||||
}
|
}
|
||||||
data_ptr += 8;
|
|
||||||
payload_len_buff = len - 10;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (payload_len > len) {
|
||||||
|
ESP_LOGD(TAG, "Actual data to receive (%d) are longer than ws buffer (%d)", payload_len, len);
|
||||||
|
payload_len = len;
|
||||||
|
}
|
||||||
|
|
||||||
// Then receive and process payload
|
// Then receive and process payload
|
||||||
if ((rlen = esp_transport_read(ws->parent, data_ptr, payload_len, timeout_ms)) <= 0) {
|
if ((rlen = esp_transport_read(ws->parent, buffer, payload_len, timeout_ms)) <= 0) {
|
||||||
ESP_LOGE(TAG, "Error read data");
|
ESP_LOGE(TAG, "Error read data");
|
||||||
return rlen;
|
return rlen;
|
||||||
}
|
}
|
||||||
if (payload_len > payload_len_buff) {
|
|
||||||
ESP_LOGD(TAG, "Actual data received (%d) are longer than mqtt buffer (%d)", payload_len, payload_len_buff);
|
|
||||||
payload_len = payload_len_buff;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mask) {
|
if (mask) {
|
||||||
mask_key = data_ptr;
|
mask_key = buffer;
|
||||||
data_ptr += 4;
|
data_ptr = buffer + 4;
|
||||||
for (int i = 0; i < payload_len; i++) {
|
for (int i = 0; i < payload_len; i++) {
|
||||||
buffer[i] = (data_ptr[i] ^ mask_key[i % 4]);
|
buffer[i] = (data_ptr[i] ^ mask_key[i % 4]);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
memmove(buffer, data_ptr, payload_len);
|
|
||||||
}
|
}
|
||||||
return payload_len;
|
return payload_len;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue