simplify handling of MHD_run
[src/app-framework-binder.git] / src / afb-hsrv.c
index 2c303d3..f200a96 100644 (file)
@@ -203,6 +203,13 @@ 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)
+{
+       upoll_on_readable(hsrv->upoll, NULL);
+       MHD_run(hsrv->httpd);
+       upoll_on_readable(hsrv->upoll, (void*)handle_epoll_readable);
+};
+
 static int new_client_handler(void *cls, const struct sockaddr *addr, socklen_t addrlen)
 {
        return MHD_YES;
@@ -336,7 +343,7 @@ int afb_hsrv_start(struct afb_hsrv *hsrv, uint16_t port, unsigned int connection
                MHD_OPTION_END);        /* options-end */
 
        if (httpd == NULL) {
-               printf("Error: httpStart invalid httpd port: %d", (int)port);
+               fprintf(stderr, "Error: httpStart invalid httpd port: %d", (int)port);
                return 0;
        }
 
@@ -347,13 +354,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;