Merge branch 'bugfix/ws_client_fixes_v4.0' into 'release/v4.0'

ws_client: various fixes (v4.0)

See merge request espressif/esp-idf!6446
This commit is contained in:
Jiang Jiang Jian 2019-11-07 13:29:50 +08:00
commit 4c4b1da7e7
3 changed files with 35 additions and 24 deletions

View file

@ -62,6 +62,7 @@ typedef struct {
bool auto_reconnect; bool auto_reconnect;
void *user_context; void *user_context;
int network_timeout_ms; int network_timeout_ms;
char *subprotocol;
} websocket_config_storage_t; } websocket_config_storage_t;
typedef enum { typedef enum {
@ -173,6 +174,11 @@ static esp_err_t esp_websocket_client_set_config(esp_websocket_client_handle_t c
cfg->path = strdup(config->path); cfg->path = strdup(config->path);
ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->path, return ESP_ERR_NO_MEM); ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->path, return ESP_ERR_NO_MEM);
} }
if (config->subprotocol) {
free(cfg->subprotocol);
cfg->subprotocol = strdup(config->subprotocol);
ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->subprotocol, return ESP_ERR_NO_MEM);
}
cfg->network_timeout_ms = WEBSOCKET_NETWORK_TIMEOUT_MS; cfg->network_timeout_ms = WEBSOCKET_NETWORK_TIMEOUT_MS;
cfg->user_context = config->user_context; cfg->user_context = config->user_context;
@ -200,12 +206,23 @@ static esp_err_t esp_websocket_client_destroy_config(esp_websocket_client_handle
free(cfg->scheme); free(cfg->scheme);
free(cfg->username); free(cfg->username);
free(cfg->password); free(cfg->password);
free(cfg->subprotocol);
memset(cfg, 0, sizeof(websocket_config_storage_t)); memset(cfg, 0, sizeof(websocket_config_storage_t));
free(client->config); free(client->config);
client->config = NULL; client->config = NULL;
return ESP_OK; return ESP_OK;
} }
static void set_websocket_transport_optional_settings(esp_websocket_client_handle_t client, esp_transport_handle_t trans)
{
if (trans && client->config->path) {
esp_transport_ws_set_path(trans, client->config->path);
}
if (trans && client->config->subprotocol) {
esp_transport_ws_set_subprotocol(trans, client->config->subprotocol);
}
}
esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_client_config_t *config) esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_client_config_t *config)
{ {
esp_websocket_client_handle_t client = calloc(1, sizeof(struct esp_websocket_client)); esp_websocket_client_handle_t client = calloc(1, sizeof(struct esp_websocket_client));
@ -225,6 +242,9 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
client->lock = xSemaphoreCreateMutex(); client->lock = xSemaphoreCreateMutex();
ESP_WS_CLIENT_MEM_CHECK(TAG, client->lock, goto _websocket_init_fail); ESP_WS_CLIENT_MEM_CHECK(TAG, client->lock, goto _websocket_init_fail);
client->config = calloc(1, sizeof(websocket_config_storage_t));
ESP_WS_CLIENT_MEM_CHECK(TAG, client->config, goto _websocket_init_fail);
client->transport_list = esp_transport_list_init(); client->transport_list = esp_transport_list_init();
ESP_WS_CLIENT_MEM_CHECK(TAG, client->transport_list, goto _websocket_init_fail); ESP_WS_CLIENT_MEM_CHECK(TAG, client->transport_list, goto _websocket_init_fail);
@ -260,14 +280,11 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
esp_transport_set_default_port(wss, WEBSOCKET_SSL_DEFAULT_PORT); esp_transport_set_default_port(wss, WEBSOCKET_SSL_DEFAULT_PORT);
esp_transport_list_add(client->transport_list, wss, "wss"); esp_transport_list_add(client->transport_list, wss, "wss");
if (config->transport == WEBSOCKET_TRANSPORT_OVER_TCP) { if (config->transport == WEBSOCKET_TRANSPORT_OVER_SSL) {
asprintf(&client->config->scheme, "wss"); asprintf(&client->config->scheme, "wss");
ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->scheme, goto _websocket_init_fail); ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->scheme, goto _websocket_init_fail);
} }
client->config = calloc(1, sizeof(websocket_config_storage_t));
ESP_WS_CLIENT_MEM_CHECK(TAG, client->config, goto _websocket_init_fail);
if (config->uri) { if (config->uri) {
if (esp_websocket_client_set_uri(client, config->uri) != ESP_OK) { if (esp_websocket_client_set_uri(client, config->uri) != ESP_OK) {
ESP_LOGE(TAG, "Invalid uri"); ESP_LOGE(TAG, "Invalid uri");
@ -285,6 +302,9 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->scheme, goto _websocket_init_fail); ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->scheme, goto _websocket_init_fail);
} }
set_websocket_transport_optional_settings(client, esp_transport_list_get_transport(client->transport_list, "ws"));
set_websocket_transport_optional_settings(client, esp_transport_list_get_transport(client->transport_list, "wss"));
client->keepalive_tick_ms = _tick_get_ms(); client->keepalive_tick_ms = _tick_get_ms();
client->reconnect_tick_ms = _tick_get_ms(); client->reconnect_tick_ms = _tick_get_ms();
client->ping_tick_ms = _tick_get_ms(); client->ping_tick_ms = _tick_get_ms();
@ -374,15 +394,6 @@ esp_err_t esp_websocket_client_set_uri(esp_websocket_client_handle_t client, con
puri.field_data[UF_QUERY].len, uri + puri.field_data[UF_QUERY].off); puri.field_data[UF_QUERY].len, uri + puri.field_data[UF_QUERY].off);
} }
ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->path, return ESP_ERR_NO_MEM); ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->path, return ESP_ERR_NO_MEM);
esp_transport_handle_t trans = esp_transport_list_get_transport(client->transport_list, "ws");
if (trans) {
esp_transport_ws_set_path(trans, client->config->path);
}
trans = esp_transport_list_get_transport(client->transport_list, "wss");
if (trans) {
esp_transport_ws_set_path(trans, client->config->path);
}
} }
if (puri.field_data[UF_PORT].off) { if (puri.field_data[UF_PORT].off) {
client->config->port = strtol((const char*)(uri + puri.field_data[UF_PORT].off), NULL, 10); client->config->port = strtol((const char*)(uri + puri.field_data[UF_PORT].off), NULL, 10);

View file

@ -92,6 +92,7 @@ typedef struct {
int buffer_size; /*!< Websocket buffer size */ int buffer_size; /*!< Websocket buffer size */
const char *cert_pem; /*!< SSL Certification, PEM format as string, if the client requires to verify server */ const char *cert_pem; /*!< SSL Certification, PEM format as string, if the client requires to verify server */
esp_websocket_transport_t transport; /*!< Websocket transport type, see `esp_websocket_transport_t */ esp_websocket_transport_t transport; /*!< Websocket transport type, see `esp_websocket_transport_t */
char *subprotocol; /*!< Websocket subprotocol */
} esp_websocket_client_config_t; } esp_websocket_client_config_t;
/** /**

View file

@ -207,7 +207,7 @@ static int _ws_write(esp_transport_handle_t t, int opcode, int mask_flag, const
ws_header[header_len++] = (uint8_t)((len >> 8) & 0xFF); ws_header[header_len++] = (uint8_t)((len >> 8) & 0xFF);
ws_header[header_len++] = (uint8_t)((len >> 0) & 0xFF); ws_header[header_len++] = (uint8_t)((len >> 0) & 0xFF);
} }
if (len) {
if (mask_flag) { if (mask_flag) {
mask = &ws_header[header_len]; mask = &ws_header[header_len];
getrandom(ws_header + header_len, 4, 0); getrandom(ws_header + header_len, 4, 0);
@ -218,7 +218,6 @@ static int _ws_write(esp_transport_handle_t t, int opcode, int mask_flag, const
} }
} }
}
if (esp_transport_write(ws->parent, ws_header, header_len, timeout_ms) != header_len) { if (esp_transport_write(ws->parent, ws_header, header_len, timeout_ms) != header_len) {
ESP_LOGE(TAG, "Error write header"); ESP_LOGE(TAG, "Error write header");
return -1; return -1;
@ -257,7 +256,7 @@ static int ws_write(esp_transport_handle_t t, const char *b, int len, int timeou
// This behaviour could however be altered in IDF 5.0, since a separate API for sending // This behaviour could however be altered in IDF 5.0, since a separate API for sending
// messages with user defined opcodes has been introduced. // messages with user defined opcodes has been introduced.
ESP_LOGD(TAG, "Write PING message"); ESP_LOGD(TAG, "Write PING message");
return _ws_write(t, WS_OPCODE_PING | WS_FIN, 0, NULL, 0, timeout_ms); return _ws_write(t, WS_OPCODE_PING | WS_FIN, WS_MASK, NULL, 0, timeout_ms);
} }
return _ws_write(t, WS_OPCODE_BINARY | WS_FIN, WS_MASK, b, len, timeout_ms); return _ws_write(t, WS_OPCODE_BINARY | WS_FIN, WS_MASK, b, len, timeout_ms);
} }
@ -315,7 +314,7 @@ static int ws_read(esp_transport_handle_t t, char *buffer, int len, int timeout_
} }
// Then receive and process payload // Then receive and process payload
if ((rlen = esp_transport_read(ws->parent, buffer, payload_len, timeout_ms)) <= 0) { if (payload_len != 0 && (rlen = esp_transport_read(ws->parent, buffer, payload_len, timeout_ms)) <= 0) {
ESP_LOGE(TAG, "Error read data"); ESP_LOGE(TAG, "Error read data");
return rlen; return rlen;
} }