mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
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
This commit is contained in:
parent
801f5d6e82
commit
db121a56e1
@ -1 +1 @@
|
|||||||
Subproject commit 39118d5182f804edc01ed030c8ba137a9c7f388e
|
Subproject commit 18b6f2c58231728805ff813d46021d07ed023dcb
|
@ -192,8 +192,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) {
|
||||||
@ -202,7 +202,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;
|
||||||
}
|
}
|
||||||
@ -219,8 +219,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;
|
||||||
@ -235,29 +233,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 > payload_len_buff) {
|
if (payload_len > len) {
|
||||||
ESP_LOGD(TAG, "Actual data to receive (%d) are longer than ws buffer (%d)", payload_len, payload_len_buff);
|
ESP_LOGD(TAG, "Actual data to receive (%d) are longer than ws buffer (%d)", payload_len, len);
|
||||||
payload_len = payload_len_buff;
|
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 (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…
x
Reference in New Issue
Block a user