diff options
author | José Bollo <jose.bollo@iot.bzh> | 2016-03-16 13:42:51 +0100 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2016-03-16 13:42:51 +0100 |
commit | 2a94a1d9c8b6c9bd4a83b870dee1d3072120cb77 (patch) | |
tree | 4bffb02ebc8af891c97538f6980f149984de2a0d | |
parent | 623aa9c2f8296adcd3b2aed2c6811927fde3645d (diff) |
http-svc: switch to epoll model
This prepares to handle notification.
It has the side effect to make the server mono-threaded.
This might be temporarily or forever, depending on next
studies.
Change-Id: I8a8b2b68c78c33b3ca861180bf120cf09a24b05e
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r-- | src/http-svc.c | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/src/http-svc.c b/src/http-svc.c index 839fc2ff..acc12a88 100644 --- a/src/http-svc.c +++ b/src/http-svc.c @@ -32,6 +32,7 @@ #include <microhttpd.h> +#include <poll.h> #include <sys/stat.h> #include "../include/local-def.h" @@ -262,7 +263,7 @@ STATIC int newClient(void *cls, const struct sockaddr * addr, socklen_t addrlen) PUBLIC AFB_error httpdStart(AFB_session *session) { - + // compute fixed URL length at startup time apiUrlLen = strlen (session->config->rootapi); baseUrlLen= strlen (session->config->rootbase); @@ -281,12 +282,16 @@ PUBLIC AFB_error httpdStart(AFB_session *session) { } session->httpd = (void*) MHD_start_daemon( - MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, // use request and not threads + MHD_USE_EPOLL_LINUX_ONLY + | MHD_USE_TCP_FASTOPEN + | MHD_USE_DEBUG + , session->config->httpdPort, // port &newClient, NULL, // Tcp Accept call back + extra attribute &newRequest, session, // Http Request Call back + extra attribute MHD_OPTION_NOTIFY_COMPLETED, &endRequest, NULL, - MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 15, MHD_OPTION_END); // 15s + options-end + MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 15, // 15 seconds + MHD_OPTION_END); // options-end // TBD: MHD_OPTION_SOCK_ADDR if (session->httpd == NULL) { @@ -299,12 +304,26 @@ PUBLIC AFB_error httpdStart(AFB_session *session) { // infinite loop PUBLIC AFB_error httpdLoop(AFB_session *session) { int count = 0; + const union MHD_DaemonInfo *info; + struct pollfd pfd; + + info = MHD_get_daemon_info(session->httpd, + MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY); + if (info == NULL) { + printf("Error: httpLoop no pollfd"); + goto error; + } + pfd.fd = info->listen_fd; + pfd.events = POLLIN; + if (verbose) fprintf(stderr, "AFB:notice entering httpd waiting loop\n"); while (TRUE) { - sleep(3600); if (verbose) fprintf(stderr, "AFB:notice httpd alive [%d]\n", count++); + poll(&pfd, 1, 15000); // 15 seconds (as above timeout when starting) + MHD_run(session->httpd); } +error: // should never return from here return AFB_FATAL; } |