mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
Run libwebsockets in a worker thread
This commit is contained in:
parent
51e0930d39
commit
96190a5822
4 changed files with 32 additions and 20 deletions
|
@ -7,6 +7,9 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <libconfig.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
/* Forward declarations */
|
||||
|
@ -24,6 +27,8 @@ struct web {
|
|||
const char *htdocs; /**< The root directory for files served via HTTP. */
|
||||
const char *ssl_cert; /**< Path to the SSL certitifcate for HTTPS / WSS. */
|
||||
const char *ssl_private_key; /**< Path to the SSL private key for HTTPS / WSS. */
|
||||
|
||||
pthread_t thread;
|
||||
};
|
||||
|
||||
/** Initialize the web interface.
|
||||
|
@ -39,7 +44,4 @@ int web_start(struct web *w);
|
|||
int web_stop(struct web *w);
|
||||
|
||||
/** Parse HTTPd and WebSocket related options */
|
||||
int web_parse(struct web *w, config_setting_t *lcs);
|
||||
|
||||
/** libwebsockets service routine. Call periodically */
|
||||
int web_service(struct web *w);
|
||||
int web_parse(struct web *w, config_setting_t *lcs);
|
32
lib/web.c
32
lib/web.c
|
@ -122,6 +122,18 @@ static void logger(int level, const char *msg) {
|
|||
}
|
||||
}
|
||||
|
||||
static void * worker(void *ctx)
|
||||
{
|
||||
struct web *w = ctx;
|
||||
|
||||
assert(w->state == STATE_STARTED);
|
||||
|
||||
for (;;)
|
||||
lws_service(w->context, 100);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int web_init(struct web *w, struct api *a)
|
||||
{
|
||||
lws_set_log_level((1 << LLL_COUNT) - 1, logger);
|
||||
|
@ -154,6 +166,8 @@ int web_parse(struct web *w, config_setting_t *cfg)
|
|||
|
||||
int web_start(struct web *w)
|
||||
{
|
||||
int ret;
|
||||
|
||||
/* Start server */
|
||||
struct lws_context_creation_info ctx_info = {
|
||||
.options = LWS_SERVER_OPTION_EXPLICIT_VHOSTS | LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT,
|
||||
|
@ -185,10 +199,14 @@ int web_start(struct web *w)
|
|||
if (w->vhost == NULL)
|
||||
error("WebSocket: failed to initialize server");
|
||||
}
|
||||
|
||||
ret = pthread_create(&w->thread, NULL, worker, w);
|
||||
if (ret)
|
||||
error("Failed to start Web worker");
|
||||
|
||||
w->state = STATE_STARTED;
|
||||
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int web_stop(struct web *w)
|
||||
|
@ -198,6 +216,11 @@ int web_stop(struct web *w)
|
|||
if (w->state == STATE_STARTED)
|
||||
lws_cancel_service(w->context);
|
||||
|
||||
/** @todo Wait for all connections to be closed */
|
||||
|
||||
pthread_cancel(w->thread);
|
||||
pthread_join(w->thread, NULL);
|
||||
|
||||
w->state = STATE_STOPPED;
|
||||
|
||||
return 0;
|
||||
|
@ -215,10 +238,3 @@ int web_destroy(struct web *w)
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int web_service(struct web *w)
|
||||
{
|
||||
assert(w->state == STATE_STARTED);
|
||||
|
||||
return lws_service(w->context, 10);
|
||||
}
|
||||
|
|
|
@ -123,8 +123,6 @@ int main(int argc, char *argv[])
|
|||
|
||||
last = time_now();
|
||||
}
|
||||
|
||||
web_service(&sn.web); /** @todo Maybe we should move this to another thread */
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -247,12 +247,8 @@ int main(int argc, char *argv[])
|
|||
pthread_create(&recvv.thread, NULL, recv_loop, NULL);
|
||||
pthread_create(&sendd.thread, NULL, send_loop, NULL);
|
||||
|
||||
for (;;) {
|
||||
if (node->_vt->start == websocket_start)
|
||||
web_service(&sn.web);
|
||||
else
|
||||
sleep(1);
|
||||
}
|
||||
for (;;)
|
||||
sleep(1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue