kopia lustrzana https://github.com/espressif/esp-idf
components: Support bind socket to specified interface in esp_http_client and esp_websocket_client component
rodzic
c62cbd1254
commit
bead3599ab
|
@ -122,6 +122,7 @@ struct esp_http_client {
|
|||
int header_index;
|
||||
bool is_async;
|
||||
esp_transport_keep_alive_t keep_alive_cfg;
|
||||
struct ifreq *if_name;
|
||||
};
|
||||
|
||||
typedef struct esp_http_client esp_http_client_t;
|
||||
|
@ -576,6 +577,7 @@ 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;
|
||||
|
@ -583,6 +585,14 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
|
|||
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);
|
||||
}
|
||||
|
||||
if (config->if_name) {
|
||||
client->if_name = calloc(1, sizeof(struct ifreq) + 1);
|
||||
HTTP_MEM_CHECK(TAG, client->if_name, goto error);
|
||||
memcpy(client->if_name, config->if_name, sizeof(struct ifreq));
|
||||
esp_transport_tcp_set_interface_name(tcp, client->if_name);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS
|
||||
esp_transport_handle_t ssl = NULL;
|
||||
_success = (
|
||||
|
@ -719,7 +729,9 @@ esp_err_t esp_http_client_cleanup(esp_http_client_handle_t client)
|
|||
free(client->response->buffer);
|
||||
free(client->response);
|
||||
}
|
||||
|
||||
if (client->if_name) {
|
||||
free(client->if_name);
|
||||
}
|
||||
free(client->parser);
|
||||
free(client->parser_settings);
|
||||
_clear_connection_info(client);
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#include "http_parser.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_err.h"
|
||||
#include <sys/socket.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
@ -135,6 +136,7 @@ typedef struct {
|
|||
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 */
|
||||
struct ifreq *if_name; /*!< The name of interface for data to go through. Use the default interface without setting */
|
||||
} esp_http_client_config_t;
|
||||
|
||||
/**
|
||||
|
|
|
@ -121,6 +121,7 @@ struct esp_websocket_client {
|
|||
int payload_len;
|
||||
int payload_offset;
|
||||
esp_transport_keep_alive_t keep_alive_cfg;
|
||||
struct ifreq *if_name;
|
||||
};
|
||||
|
||||
static uint64_t _tick_get_ms(void)
|
||||
|
@ -316,6 +317,12 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
|
|||
client->keep_alive_cfg.keep_alive_count = (config->keep_alive_count == 0) ? WEBSOCKET_KEEP_ALIVE_COUNT : config->keep_alive_count;
|
||||
}
|
||||
|
||||
if (config->if_name) {
|
||||
client->if_name = calloc(1, sizeof(struct ifreq) + 1);
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, client->if_name, goto _websocket_init_fail);
|
||||
memcpy(client->if_name, config->if_name, sizeof(struct ifreq));
|
||||
}
|
||||
|
||||
client->lock = xSemaphoreCreateRecursiveMutex();
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, client->lock, goto _websocket_init_fail);
|
||||
|
||||
|
@ -331,7 +338,7 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
|
|||
esp_transport_set_default_port(tcp, WEBSOCKET_TCP_DEFAULT_PORT);
|
||||
esp_transport_list_add(client->transport_list, tcp, "_tcp"); // need to save to transport list, for cleanup
|
||||
esp_transport_tcp_set_keep_alive(tcp, &client->keep_alive_cfg);
|
||||
|
||||
esp_transport_tcp_set_interface_name(tcp, client->if_name);
|
||||
|
||||
esp_transport_handle_t ws = esp_transport_ws_init(tcp);
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, ws, goto _websocket_init_fail);
|
||||
|
@ -448,6 +455,9 @@ esp_err_t esp_websocket_client_destroy(esp_websocket_client_handle_t client)
|
|||
if (client->event_handle) {
|
||||
esp_event_loop_delete(client->event_handle);
|
||||
}
|
||||
if (client->if_name) {
|
||||
free(client->if_name);
|
||||
}
|
||||
esp_websocket_client_destroy_config(client);
|
||||
esp_transport_list_destroy(client->transport_list);
|
||||
vQueueDelete(client->lock);
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include "freertos/FreeRTOS.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_event.h"
|
||||
#include <sys/socket.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
@ -100,6 +101,7 @@ typedef struct {
|
|||
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 */
|
||||
size_t ping_interval_sec; /*!< Websocket ping interval, defaults to 10 seconds if not set */
|
||||
struct ifreq *if_name; /*!< The name of interface for data to go through. Use the default interface without setting */
|
||||
} esp_websocket_client_config_t;
|
||||
|
||||
/**
|
||||
|
|
Ładowanie…
Reference in New Issue