mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
Add options for esp_http_client and esp_websocket_client to support keepalive
This commit is contained in:
parent
f946e296a2
commit
9933037c0d
@ -119,6 +119,7 @@ struct esp_http_client {
|
||||
bool first_line_prepared;
|
||||
int header_index;
|
||||
bool is_async;
|
||||
esp_transport_keep_alive_t keep_alive_cfg;
|
||||
};
|
||||
|
||||
typedef struct esp_http_client esp_http_client_t;
|
||||
@ -139,6 +140,9 @@ static const char *DEFAULT_HTTP_PROTOCOL = "HTTP/1.1";
|
||||
static const char *DEFAULT_HTTP_PATH = "/";
|
||||
static int DEFAULT_MAX_REDIRECT = 10;
|
||||
static int DEFAULT_TIMEOUT_MS = 5000;
|
||||
static const int DEFAULT_KEEP_ALIVE_IDLE = 5;
|
||||
static const int DEFAULT_KEEP_ALIVE_INTERVAL= 5;
|
||||
static const int DEFAULT_KEEP_ALIVE_COUNT= 3;
|
||||
|
||||
static const char *HTTP_METHOD_MAPPING[] = {
|
||||
"GET",
|
||||
@ -492,7 +496,7 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
|
||||
{
|
||||
|
||||
esp_http_client_handle_t client;
|
||||
esp_transport_handle_t tcp;
|
||||
esp_transport_handle_t tcp = NULL;
|
||||
bool _success;
|
||||
|
||||
_success = (
|
||||
@ -523,8 +527,15 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
|
||||
ESP_LOGE(TAG, "Error initialize transport");
|
||||
goto error;
|
||||
}
|
||||
if (config->keep_alive_enable == true) {
|
||||
client->keep_alive_cfg.keep_alive_enable = true;
|
||||
client->keep_alive_cfg.keep_alive_idle = (config->keep_alive_idle == 0) ? DEFAULT_KEEP_ALIVE_IDLE : config->keep_alive_idle;
|
||||
client->keep_alive_cfg.keep_alive_interval = (config->keep_alive_interval == 0) ? DEFAULT_KEEP_ALIVE_INTERVAL : config->keep_alive_interval;
|
||||
client->keep_alive_cfg.keep_alive_count = (config->keep_alive_count == 0) ? DEFAULT_KEEP_ALIVE_COUNT : config->keep_alive_count;
|
||||
esp_transport_tcp_set_keep_alive(tcp, &client->keep_alive_cfg);
|
||||
}
|
||||
#ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS
|
||||
esp_transport_handle_t ssl;
|
||||
esp_transport_handle_t ssl = NULL;
|
||||
_success = (
|
||||
(ssl = esp_transport_ssl_init()) &&
|
||||
(esp_transport_set_default_port(ssl, DEFAULT_HTTPS_PORT) == ESP_OK) &&
|
||||
@ -553,6 +564,10 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
|
||||
if (config->skip_cert_common_name_check) {
|
||||
esp_transport_ssl_skip_common_name_check(ssl);
|
||||
}
|
||||
|
||||
if (config->keep_alive_enable == true) {
|
||||
esp_transport_ssl_set_keep_alive(ssl, &client->keep_alive_cfg);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (_set_config(client, config) != ESP_OK) {
|
||||
|
@ -123,6 +123,10 @@ typedef struct {
|
||||
bool is_async; /*!< Set asynchronous mode, only supported with HTTPS for now */
|
||||
bool use_global_ca_store; /*!< Use a global ca_store for all the connections in which this bool is set. */
|
||||
bool skip_cert_common_name_check; /*!< Skip any validation of server certificate CN field */
|
||||
bool keep_alive_enable; /*!< Enable keep-alive timeout */
|
||||
int keep_alive_idle; /*!< Keep-alive idle time. Default is 5 (second) */
|
||||
int keep_alive_interval; /*!< Keep-alive interval time. Default is 5 (second) */
|
||||
int keep_alive_count; /*!< Keep-alive packet retry send count. Default is 3 counts */
|
||||
} esp_http_client_config_t;
|
||||
|
||||
/**
|
||||
|
@ -39,6 +39,9 @@ static const char *TAG = "WEBSOCKET_CLIENT";
|
||||
#define WEBSOCKET_NETWORK_TIMEOUT_MS (10*1000)
|
||||
#define WEBSOCKET_PING_TIMEOUT_MS (10*1000)
|
||||
#define WEBSOCKET_EVENT_QUEUE_SIZE (1)
|
||||
#define WEBSOCKET_KEEP_ALIVE_IDLE (5)
|
||||
#define WEBSOCKET_KEEP_ALIVE_INTERVAL (5)
|
||||
#define WEBSOCKET_KEEP_ALIVE_COUNT (3)
|
||||
|
||||
#define ESP_WS_CLIENT_MEM_CHECK(TAG, a, action) if (!(a)) { \
|
||||
ESP_LOGE(TAG,"%s:%d (%s): %s", __FILE__, __LINE__, __FUNCTION__, "Memory exhausted"); \
|
||||
@ -101,6 +104,7 @@ struct esp_websocket_client {
|
||||
ws_transport_opcodes_t last_opcode;
|
||||
int payload_len;
|
||||
int payload_offset;
|
||||
esp_transport_keep_alive_t keep_alive_cfg;
|
||||
};
|
||||
|
||||
static uint64_t _tick_get_ms(void)
|
||||
@ -276,6 +280,13 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (config->keep_alive_enable == true) {
|
||||
client->keep_alive_cfg.keep_alive_enable = true;
|
||||
client->keep_alive_cfg.keep_alive_idle = (config->keep_alive_idle == 0) ? WEBSOCKET_KEEP_ALIVE_IDLE : config->keep_alive_idle;
|
||||
client->keep_alive_cfg.keep_alive_interval = (config->keep_alive_interval == 0) ? WEBSOCKET_KEEP_ALIVE_INTERVAL : config->keep_alive_interval;
|
||||
client->keep_alive_cfg.keep_alive_count = (config->keep_alive_count == 0) ? WEBSOCKET_KEEP_ALIVE_COUNT : config->keep_alive_count;
|
||||
}
|
||||
|
||||
client->lock = xSemaphoreCreateRecursiveMutex();
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, client->lock, goto _websocket_init_fail);
|
||||
|
||||
@ -289,6 +300,7 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, tcp, goto _websocket_init_fail);
|
||||
|
||||
esp_transport_set_default_port(tcp, WEBSOCKET_TCP_DEFAULT_PORT);
|
||||
esp_transport_tcp_set_keep_alive(tcp, &client->keep_alive_cfg);
|
||||
esp_transport_list_add(client->transport_list, tcp, "_tcp"); // need to save to transport list, for cleanup
|
||||
|
||||
|
||||
@ -309,6 +321,7 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
|
||||
if (config->cert_pem) {
|
||||
esp_transport_ssl_set_cert_data(ssl, config->cert_pem, strlen(config->cert_pem));
|
||||
}
|
||||
esp_transport_ssl_set_keep_alive(ssl, &client->keep_alive_cfg);
|
||||
esp_transport_list_add(client->transport_list, ssl, "_ssl"); // need to save to transport list, for cleanup
|
||||
|
||||
esp_transport_handle_t wss = esp_transport_ws_init(ssl);
|
||||
|
@ -85,6 +85,10 @@ typedef struct {
|
||||
char *subprotocol; /*!< Websocket subprotocol */
|
||||
char *user_agent; /*!< Websocket user-agent */
|
||||
char *headers; /*!< Websocket additional headers */
|
||||
bool keep_alive_enable; /*!< Enable keep-alive timeout */
|
||||
int keep_alive_idle; /*!< Keep-alive idle time. Default is 5 (second) */
|
||||
int keep_alive_interval; /*!< Keep-alive interval time. Default is 5 (second) */
|
||||
int keep_alive_count; /*!< Keep-alive packet retry send count. Default is 3 counts */
|
||||
} esp_websocket_client_config_t;
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user