From 2a94a1d9c8b6c9bd4a83b870dee1d3072120cb77 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Bollo?= Date: Wed, 16 Mar 2016 13:42:51 +0100 Subject: [PATCH] http-svc: switch to epoll model MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 --- src/http-svc.c | 27 +++++++++++++++++++++++---- 1 file 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 +#include #include #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; } -- 2.16.6