tcp_transport: add websocket dynamic buffer feature

Free websocket transport buffer when connection succeed to save peak heap cost about WS_BUFFER_SIZE.
This commit is contained in:
Li Jingyi 2022-05-16 15:27:32 +08:00 committed by David Čermák
parent f173016d86
commit 21d65931fc
2 changed files with 21 additions and 0 deletions

View File

@ -13,6 +13,14 @@ menu "TCP Transport"
depends on WS_TRANSPORT
help
Size of the buffer used for constructing the HTTP Upgrade request during connect
config WS_DYNAMIC_BUFFER
bool "Using dynamic websocket transport buffer"
default n
depends on WS_TRANSPORT
help
If enable this option, websocket transport buffer will be freed after connection
succeed to save more heap.
endmenu
endmenu

View File

@ -152,6 +152,15 @@ static int ws_connect(esp_transport_handle_t t, const char *host, int port, int
unsigned char client_key[28] = {0};
const char *user_agent_ptr = (ws->user_agent) ? (ws->user_agent) : "ESP32 Websocket Client";
#ifdef CONFIG_WS_DYNAMIC_BUFFER
if (!ws->buffer) {
ws->buffer = malloc(WS_BUFFER_SIZE);
if (!ws->buffer) {
ESP_LOGE(TAG, "Cannot allocate buffer for connect, need-%d", WS_BUFFER_SIZE);
return -1;
}
}
#endif
size_t outlen = 0;
esp_crypto_base64_encode(client_key, sizeof(client_key), &outlen, random_key, sizeof(random_key));
@ -238,6 +247,10 @@ static int ws_connect(esp_transport_handle_t t, const char *host, int port, int
ESP_LOGE(TAG, "Invalid websocket key");
return -1;
}
#ifdef CONFIG_WS_DYNAMIC_BUFFER
free(ws->buffer);
ws->buffer = NULL;
#endif
return 0;
}