mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
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:
parent
f173016d86
commit
21d65931fc
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user