Merge branch 'feature/mqtt_improve_recv_3.2' into 'release/v3.2'

MQTT improved receive (backport 3.2)

See merge request espressif/esp-idf!5098
This commit is contained in:
Angus Gratton 2019-08-07 16:05:07 +08:00
commit 1beac49f3a
2 changed files with 13 additions and 17 deletions

@ -1 +1 @@
Subproject commit f08f3b678717865234637164a29ed3a63e756ca7
Subproject commit 6f1fc3785af8cc7765f78dd93f09aeb0109cd2b7

View file

@ -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);
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) {
@ -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)
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;
}
@ -211,8 +211,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;
@ -227,27 +225,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 > 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 (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) {
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;
}