diff --git a/components/esp_http_server/include/esp_http_server.h b/components/esp_http_server/include/esp_http_server.h index cfbe0efee2..7372003df3 100644 --- a/components/esp_http_server/include/esp_http_server.h +++ b/components/esp_http_server/include/esp_http_server.h @@ -415,14 +415,6 @@ typedef struct httpd_uri { #endif } httpd_uri_t; -/** - * @brief Structure for holding list of clients - */ -typedef struct httpd_client_list { - size_t active_clients; /*!< number of active clients in this struct */ - int client_fds[]; /*!< array of file descriptors of all active clients */ -} httpd_client_list_t; - /** * @brief Registers a URI handler * @@ -1484,14 +1476,15 @@ esp_err_t httpd_sess_update_lru_counter(httpd_handle_t handle, int sockfd); * @brief Returns list of current socket descriptors of active sessions * * @param[in] handle Handle to server returned by httpd_start - * @param[in] max_fds Maximum number of socket fds the supplied list could hold - * @param[out] fd_list Structure holding socket descriptors + * @param[in,out] fds In: Number of fds allocated in the supplied structure client_fds + * Out: Number of valid client fds returned in client_fds, + * @param[out] client_fds Array of client fds * * @return * - ESP_OK : Successfully retrieved session list - * - ESP_ERR_INVALID_ARG : Wrong arguments or list is longer than maximum + * - ESP_ERR_INVALID_ARG : Wrong arguments or list is longer than allocated */ -esp_err_t httpd_get_client_list(httpd_handle_t handle, size_t max_fds, httpd_client_list_t *fd_list); +esp_err_t httpd_get_client_list(httpd_handle_t handle, size_t *fds, int *client_fds); /** End of Session * @} diff --git a/components/esp_http_server/src/httpd_main.c b/components/esp_http_server/src/httpd_main.c index af536a9d87..67b86e1cd9 100644 --- a/components/esp_http_server/src/httpd_main.c +++ b/components/esp_http_server/src/httpd_main.c @@ -104,17 +104,18 @@ esp_err_t httpd_queue_work(httpd_handle_t handle, httpd_work_fn_t work, void *ar return ESP_OK; } -esp_err_t httpd_get_client_list(httpd_handle_t handle, size_t max_fds, httpd_client_list_t *fd_list) +esp_err_t httpd_get_client_list(httpd_handle_t handle, size_t *fds, int *client_fds) { struct httpd_data *hd = (struct httpd_data *) handle; - if (hd == NULL || max_fds == 0 || fd_list == NULL || max_fds < hd->config.max_open_sockets) { + if (hd == NULL || fds == NULL || *fds == 0 || client_fds == NULL || *fds < hd->config.max_open_sockets) { return ESP_ERR_INVALID_ARG; } - fd_list->active_clients = 0; + size_t max_fds = *fds; + *fds = 0; for (int i = 0; i < hd->config.max_open_sockets; ++i) { if (hd->hd_sd[i].fd != -1) { - if (fd_list->active_clients < max_fds) { - fd_list->client_fds[fd_list->active_clients++] = hd->hd_sd[i].fd; + if (*fds < max_fds) { + client_fds[(*fds)++] = hd->hd_sd[i].fd; } else { return ESP_ERR_INVALID_ARG; } diff --git a/examples/protocols/https_server/wss_server/main/wss_server_example.c b/examples/protocols/https_server/wss_server/main/wss_server_example.c index fe2954640f..9bd4c8e804 100644 --- a/examples/protocols/https_server/wss_server/main/wss_server_example.c +++ b/examples/protocols/https_server/wss_server/main/wss_server_example.c @@ -210,14 +210,9 @@ static void connect_handler(void* arg, esp_event_base_t event_base, } } +// Get all clients and send async message static void wss_server_send_messages(httpd_handle_t* server) { - // Get all clients and send async message - struct { - size_t active_clients; - int client_fds[max_clients]; - } client_list; - bool send_messages = true; // Send async message to all connected clients that use websocket protocol every 10 seconds @@ -227,10 +222,11 @@ static void wss_server_send_messages(httpd_handle_t* server) if (!*server) { // httpd might not have been created by now continue; } - - if (httpd_get_client_list(*server, max_clients, (httpd_client_list_t*)&client_list) == ESP_OK) { - for (size_t i=0; i < client_list.active_clients; ++i) { - int sock = client_list.client_fds[i]; + size_t clients = max_clients; + int client_fds[max_clients]; + if (httpd_get_client_list(*server, &clients, client_fds) == ESP_OK) { + for (size_t i=0; i < clients; ++i) { + int sock = client_fds[i]; if (httpd_ws_get_fd_info(*server, sock) == HTTPD_WS_CLIENT_WEBSOCKET) { ESP_LOGI(TAG, "Active client (fd=%d) -> sending async message", sock); struct async_resp_arg *resp_arg = malloc(sizeof(struct async_resp_arg));