mirror of
https://github.com/espressif/esp-idf.git
synced 2024-09-20 00:36:01 -04:00
http_server: Simplified httpd_get_client_list() to return clients in int array
This commit is contained in:
parent
2f22a43a5d
commit
6f3fa81863
@ -415,14 +415,6 @@ typedef struct httpd_uri {
|
|||||||
#endif
|
#endif
|
||||||
} httpd_uri_t;
|
} 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
|
* @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
|
* @brief Returns list of current socket descriptors of active sessions
|
||||||
*
|
*
|
||||||
* @param[in] handle Handle to server returned by httpd_start
|
* @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[in,out] fds In: Number of fds allocated in the supplied structure client_fds
|
||||||
* @param[out] fd_list Structure holding socket descriptors
|
* Out: Number of valid client fds returned in client_fds,
|
||||||
|
* @param[out] client_fds Array of client fds
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
* - ESP_OK : Successfully retrieved session list
|
* - 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
|
/** End of Session
|
||||||
* @}
|
* @}
|
||||||
|
@ -104,17 +104,18 @@ esp_err_t httpd_queue_work(httpd_handle_t handle, httpd_work_fn_t work, void *ar
|
|||||||
return ESP_OK;
|
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;
|
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;
|
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) {
|
for (int i = 0; i < hd->config.max_open_sockets; ++i) {
|
||||||
if (hd->hd_sd[i].fd != -1) {
|
if (hd->hd_sd[i].fd != -1) {
|
||||||
if (fd_list->active_clients < max_fds) {
|
if (*fds < max_fds) {
|
||||||
fd_list->client_fds[fd_list->active_clients++] = hd->hd_sd[i].fd;
|
client_fds[(*fds)++] = hd->hd_sd[i].fd;
|
||||||
} else {
|
} else {
|
||||||
return ESP_ERR_INVALID_ARG;
|
return ESP_ERR_INVALID_ARG;
|
||||||
}
|
}
|
||||||
|
@ -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)
|
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;
|
bool send_messages = true;
|
||||||
|
|
||||||
// Send async message to all connected clients that use websocket protocol every 10 seconds
|
// 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
|
if (!*server) { // httpd might not have been created by now
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
size_t clients = max_clients;
|
||||||
if (httpd_get_client_list(*server, max_clients, (httpd_client_list_t*)&client_list) == ESP_OK) {
|
int client_fds[max_clients];
|
||||||
for (size_t i=0; i < client_list.active_clients; ++i) {
|
if (httpd_get_client_list(*server, &clients, client_fds) == ESP_OK) {
|
||||||
int sock = client_list.client_fds[i];
|
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) {
|
if (httpd_ws_get_fd_info(*server, sock) == HTTPD_WS_CLIENT_WEBSOCKET) {
|
||||||
ESP_LOGI(TAG, "Active client (fd=%d) -> sending async message", sock);
|
ESP_LOGI(TAG, "Active client (fd=%d) -> sending async message", sock);
|
||||||
struct async_resp_arg *resp_arg = malloc(sizeof(struct async_resp_arg));
|
struct async_resp_arg *resp_arg = malloc(sizeof(struct async_resp_arg));
|
||||||
|
Loading…
Reference in New Issue
Block a user