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:
David Cermak 2019-04-30 11:41:39 +02:00
parent 801f5d6e82
commit db121a56e1
2 changed files with 10 additions and 16 deletions

@ -1 +1 @@
Subproject commit 39118d5182f804edc01ed030c8ba137a9c7f388e
Subproject commit 18b6f2c58231728805ff813d46021d07ed023dcb

View File

@ -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);
int payload_len;
int payload_len_buff = len;
char *data_ptr = buffer, opcode, mask, *mask_key = NULL;
char ws_header[MAX_WEBSOCKET_HEADER_SIZE];
char *data_ptr = ws_header, opcode, mask, *mask_key = NULL;
int rlen;
int poll_read;
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)
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");
return rlen;
}
@ -219,8 +219,6 @@ static int ws_read(esp_transport_handle_t t, char *buffer, int len, int timeout_
return rlen;
}
payload_len = data_ptr[0] << 8 | data_ptr[1];
payload_len_buff = len - 4;
data_ptr += 2;
} else if (payload_len == 127) {
// headerLen += 8;
header = 8;
@ -235,29 +233,25 @@ static int ws_read(esp_transport_handle_t t, char *buffer, int len, int timeout_
} else {
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) {
ESP_LOGD(TAG, "Actual data to receive (%d) are longer than ws buffer (%d)", payload_len, payload_len_buff);
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, len);
payload_len = len;
}
// 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");
return rlen;
}
if (mask) {
mask_key = data_ptr;
data_ptr += 4;
mask_key = buffer;
data_ptr = buffer + 4;
for (int i = 0; i < payload_len; i++) {
buffer[i] = (data_ptr[i] ^ mask_key[i % 4]);
}
} else {
memmove(buffer, data_ptr, payload_len);
}
return payload_len;
}