Commit e23ad017 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

httpd: use linked list for clients

parent 4c3ecdb7
...@@ -97,8 +97,8 @@ struct httpd_host_t ...@@ -97,8 +97,8 @@ struct httpd_host_t
int i_url; int i_url;
httpd_url_t **url; httpd_url_t **url;
int i_client; size_t client_count;
httpd_client_t **client; struct vlc_list clients;
/* TLS data */ /* TLS data */
vlc_tls_creds_t *p_tls; vlc_tls_creds_t *p_tls;
...@@ -144,6 +144,7 @@ struct httpd_client_t ...@@ -144,6 +144,7 @@ struct httpd_client_t
httpd_url_t *url; httpd_url_t *url;
vlc_tls_t *sock; vlc_tls_t *sock;
struct vlc_list node;
int i_ref; int i_ref;
bool b_stream_mode; bool b_stream_mode;
...@@ -167,7 +168,6 @@ struct httpd_client_t ...@@ -167,7 +168,6 @@ struct httpd_client_t
/* */ /* */
httpd_message_t query; /* client -> httpd */ httpd_message_t query; /* client -> httpd */
httpd_message_t answer; /* httpd -> client */ httpd_message_t answer; /* httpd -> client */
}; };
...@@ -963,8 +963,8 @@ static httpd_host_t *httpd_HostCreate(vlc_object_t *p_this, ...@@ -963,8 +963,8 @@ static httpd_host_t *httpd_HostCreate(vlc_object_t *p_this,
host->port = port; host->port = port;
host->i_url = 0; host->i_url = 0;
host->url = NULL; host->url = NULL;
host->i_client = 0; host->client_count = 0;
host->client = NULL; vlc_list_init(&host->clients);
host->p_tls = p_tls; host->p_tls = p_tls;
/* create the thread */ /* create the thread */
...@@ -1000,6 +1000,8 @@ error: ...@@ -1000,6 +1000,8 @@ error:
/* delete a host */ /* delete a host */
void httpd_HostDelete(httpd_host_t *host) void httpd_HostDelete(httpd_host_t *host)
{ {
httpd_client_t *client;
vlc_mutex_lock(&httpd.mutex); vlc_mutex_lock(&httpd.mutex);
if (atomic_fetch_sub_explicit(&host->ref, 1, memory_order_relaxed) > 1) { if (atomic_fetch_sub_explicit(&host->ref, 1, memory_order_relaxed) > 1) {
...@@ -1018,11 +1020,10 @@ void httpd_HostDelete(httpd_host_t *host) ...@@ -1018,11 +1020,10 @@ void httpd_HostDelete(httpd_host_t *host)
for (int i = 0; i < host->i_url; i++) for (int i = 0; i < host->i_url; i++)
msg_Err(host, "url still registered: %s", host->url[i]->psz_url); msg_Err(host, "url still registered: %s", host->url[i]->psz_url);
for (int i = 0; i < host->i_client; i++) { vlc_list_foreach(client, &host->clients, node) {
msg_Warn(host, "client still connected"); msg_Warn(host, "client still connected");
httpd_ClientDestroy(host->client[i]); httpd_ClientDestroy(client);
} }
TAB_CLEAN(host->i_client, host->client);
vlc_tls_Delete(host->p_tls); vlc_tls_Delete(host->p_tls);
net_ListenClose(host->fds); net_ListenClose(host->fds);
...@@ -1083,6 +1084,7 @@ int httpd_UrlCatch(httpd_url_t *url, int i_msg, httpd_callback_t cb, ...@@ -1083,6 +1084,7 @@ int httpd_UrlCatch(httpd_url_t *url, int i_msg, httpd_callback_t cb,
void httpd_UrlDelete(httpd_url_t *url) void httpd_UrlDelete(httpd_url_t *url)
{ {
httpd_host_t *host = url->host; httpd_host_t *host = url->host;
httpd_client_t *client;
vlc_mutex_lock(&host->lock); vlc_mutex_lock(&host->lock);
TAB_REMOVE(host->i_url, host->url, url); TAB_REMOVE(host->i_url, host->url, url);
...@@ -1092,17 +1094,14 @@ void httpd_UrlDelete(httpd_url_t *url) ...@@ -1092,17 +1094,14 @@ void httpd_UrlDelete(httpd_url_t *url)
free(url->psz_user); free(url->psz_user);
free(url->psz_password); free(url->psz_password);
for (int i = 0; i < host->i_client; i++) { vlc_list_foreach(client, &host->clients, node) {
httpd_client_t *client = host->client[i];
if (client->url != url) if (client->url != url)
continue; continue;
/* TODO complete it */ /* TODO complete it */
msg_Warn(host, "force closing connections"); msg_Warn(host, "force closing connections");
TAB_REMOVE(host->i_client, host->client, client); host->client_count--;
httpd_ClientDestroy(client); httpd_ClientDestroy(client);
i--;
} }
free(url); free(url);
vlc_mutex_unlock(&host->lock); vlc_mutex_unlock(&host->lock);
...@@ -1204,6 +1203,7 @@ char* httpd_ServerIP(const httpd_client_t *cl, char *ip, int *port) ...@@ -1204,6 +1203,7 @@ char* httpd_ServerIP(const httpd_client_t *cl, char *ip, int *port)
static void httpd_ClientDestroy(httpd_client_t *cl) static void httpd_ClientDestroy(httpd_client_t *cl)
{ {
vlc_list_remove(&cl->node);
vlc_tls_Close(cl->sock); vlc_tls_Close(cl->sock);
httpd_MsgClean(&cl->answer); httpd_MsgClean(&cl->answer);
httpd_MsgClean(&cl->query); httpd_MsgClean(&cl->query);
...@@ -1677,7 +1677,7 @@ auth_failed: ...@@ -1677,7 +1677,7 @@ auth_failed:
static void httpdLoop(httpd_host_t *host) static void httpdLoop(httpd_host_t *host)
{ {
struct pollfd ufd[host->nfd + host->i_client]; struct pollfd ufd[host->nfd + host->client_count];
unsigned nfd; unsigned nfd;
for (nfd = 0; nfd < host->nfd; nfd++) { for (nfd = 0; nfd < host->nfd; nfd++) {
ufd[nfd].fd = host->fds[nfd]; ufd[nfd].fd = host->fds[nfd];
...@@ -1695,17 +1695,17 @@ static void httpdLoop(httpd_host_t *host) ...@@ -1695,17 +1695,17 @@ static void httpdLoop(httpd_host_t *host)
mtime_t now = mdate(); mtime_t now = mdate();
bool b_low_delay = false; bool b_low_delay = false;
httpd_client_t *cl;
int canc = vlc_savecancel(); int canc = vlc_savecancel();
for (int i_client = 0; i_client < host->i_client; i_client++) { vlc_list_foreach(cl, &host->clients, node) {
int64_t i_offset; int64_t i_offset;
httpd_client_t *cl = host->client[i_client];
if (cl->i_ref < 0 || (cl->i_ref == 0 && if (cl->i_ref < 0 || (cl->i_ref == 0 &&
(cl->i_state == HTTPD_CLIENT_DEAD || (cl->i_state == HTTPD_CLIENT_DEAD ||
(cl->i_activity_timeout > 0 && (cl->i_activity_timeout > 0 &&
cl->i_activity_date+cl->i_activity_timeout < now)))) { cl->i_activity_date+cl->i_activity_timeout < now)))) {
TAB_REMOVE(host->i_client, host->client, cl); host->client_count--;
i_client--;
httpd_ClientDestroy(cl); httpd_ClientDestroy(cl);
continue; continue;
} }
...@@ -1957,8 +1957,7 @@ static void httpdLoop(httpd_host_t *host) ...@@ -1957,8 +1957,7 @@ static void httpdLoop(httpd_host_t *host)
now = mdate(); now = mdate();
nfd = host->nfd; nfd = host->nfd;
for (int i_client = 0; i_client < host->i_client; i_client++) { vlc_list_foreach(cl, &host->clients, node) {
httpd_client_t *cl = host->client[i_client];
const struct pollfd *pufd = &ufd[nfd]; const struct pollfd *pufd = &ufd[nfd];
assert(pufd < &ufd[sizeof(ufd) / sizeof(ufd[0])]); assert(pufd < &ufd[sizeof(ufd) / sizeof(ufd[0])]);
...@@ -1983,7 +1982,6 @@ static void httpdLoop(httpd_host_t *host) ...@@ -1983,7 +1982,6 @@ static void httpdLoop(httpd_host_t *host)
/* Handle server sockets (accept new connections) */ /* Handle server sockets (accept new connections) */
for (nfd = 0; nfd < host->nfd; nfd++) { for (nfd = 0; nfd < host->nfd; nfd++) {
httpd_client_t *cl;
int fd = ufd[nfd].fd; int fd = ufd[nfd].fd;
assert (fd == host->fds[nfd]); assert (fd == host->fds[nfd]);
...@@ -2024,7 +2022,8 @@ static void httpdLoop(httpd_host_t *host) ...@@ -2024,7 +2022,8 @@ static void httpdLoop(httpd_host_t *host)
if (host->p_tls != NULL) if (host->p_tls != NULL)
cl->i_state = HTTPD_CLIENT_TLS_HS_OUT; cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
TAB_APPEND(host->i_client, host->client, cl); host->client_count++;
vlc_list_append(&cl->node, &host->clients);
} }
vlc_mutex_unlock(&host->lock); vlc_mutex_unlock(&host->lock);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment