From: José Bollo Date: Mon, 25 Apr 2016 14:48:17 +0000 (+0200) Subject: avoid reentering MHD_run X-Git-Tag: blowfish_2.0.1~164 X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=commitdiff_plain;h=84d141ab55dbc409aed542fe5163f1ed16353beb;p=src%2Fapp-framework-binder.git avoid reentering MHD_run Change-Id: I54547f52c44b05573190cd226f71ee9d40181300 Signed-off-by: José Bollo --- diff --git a/src/afb-hsrv.c b/src/afb-hsrv.c index 73ad6a60..abec0ada 100644 --- a/src/afb-hsrv.c +++ b/src/afb-hsrv.c @@ -54,8 +54,15 @@ struct hsrv_alias { int dirfd; }; +enum afb_hsrv_state { + hsrv_idle = 0, + hsrv_run, + hsrv_rerun +}; + struct afb_hsrv { unsigned refcount; + enum afb_hsrv_state state; struct hsrv_handler *handlers; struct MHD_Daemon *httpd; struct upoll *upoll; @@ -203,6 +210,19 @@ static void end_handler(void *cls, struct MHD_Connection *connection, void **rec afb_hreq_free(hreq); } +static void handle_epoll_readable(struct afb_hsrv *hsrv) +{ + if (hsrv->state != hsrv_idle) + hsrv->state = hsrv_rerun; + else { + do { + hsrv->state = hsrv_run; + MHD_run(hsrv->httpd); + } while (hsrv->state == hsrv_rerun); + hsrv->state = hsrv_idle; + } +}; + static int new_client_handler(void *cls, const struct sockaddr *addr, socklen_t addrlen) { return MHD_YES; @@ -347,13 +367,13 @@ int afb_hsrv_start(struct afb_hsrv *hsrv, uint16_t port, unsigned int connection return 0; } - upoll = upoll_open(info->listen_fd, httpd); + upoll = upoll_open(info->listen_fd, hsrv); if (upoll == NULL) { MHD_stop_daemon(httpd); fprintf(stderr, "Error: connection to upoll of httpd failed"); return 0; } - upoll_on_readable(upoll, (void*)MHD_run); + upoll_on_readable(upoll, (void*)handle_epoll_readable); hsrv->httpd = httpd; hsrv->upoll = upoll;