Code Review
/
src
/
app-framework-binder.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
review
|
tree
raw
|
inline
| side by side
fdev: Improve fdev handling with epoll
[src/app-framework-binder.git]
/
src
/
afb-hsrv.c
diff --git
a/src/afb-hsrv.c
b/src/afb-hsrv.c
index
87895a7
..
6cb0bb4
100644
(file)
--- a/
src/afb-hsrv.c
+++ b/
src/afb-hsrv.c
@@
-1,5
+1,5
@@
/*
/*
- * Copyright (C) 2016, 2017 "IoT.bzh"
+ * Copyright (C) 2016, 2017
, 2018
"IoT.bzh"
* Author: José Bollo <jose.bollo@iot.bzh>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Author: José Bollo <jose.bollo@iot.bzh>
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@
-18,6
+18,7
@@
#define _GNU_SOURCE
#include <stdint.h>
#define _GNU_SOURCE
#include <stdint.h>
+#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
@@
-27,18
+28,19
@@
#include <sys/stat.h>
#include <microhttpd.h>
#include <sys/stat.h>
#include <microhttpd.h>
-#include <systemd/sd-event.h>
#include "afb-method.h"
#include "afb-context.h"
#include "afb-xreq.h"
#include "afb-hreq.h"
#include "afb-hsrv.h"
#include "afb-method.h"
#include "afb-context.h"
#include "afb-xreq.h"
#include "afb-hreq.h"
#include "afb-hsrv.h"
-#include <afb/afb-req-itf.h>
+#include "afb-fdev.h"
+#include "fdev.h"
#include "verbose.h"
#include "locale-root.h"
#include "verbose.h"
#include "locale-root.h"
-#include "afb-common.h"
+#include "afb-systemd.h"
+#include "jobs.h"
#define JSON_CONTENT "application/json"
#define FORM_CONTENT MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA
#define JSON_CONTENT "application/json"
#define FORM_CONTENT MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA
@@
-62,8
+64,7
@@
struct afb_hsrv {
unsigned refcount;
struct hsrv_handler *handlers;
struct MHD_Daemon *httpd;
unsigned refcount;
struct hsrv_handler *handlers;
struct MHD_Daemon *httpd;
- sd_event_source *evsrc;
- int in_run;
+ struct fdev *fdev;
char *cache_to;
};
char *cache_to;
};
@@
-235,25
+236,27
@@
static void end_handler(void *cls, struct MHD_Connection *connection, void **rec
}
}
}
}
-
void run_micro_httpd(struct afb_hsrv *hsrv
)
+
static void do_run(int signum, void *arg
)
{
{
- if (hsrv->in_run != 0)
- hsrv->in_run = 2;
- else {
- sd_event_source_set_io_events(hsrv->evsrc, 0);
- do {
- hsrv->in_run = 1;
- MHD_run(hsrv->httpd);
- } while(hsrv->in_run == 2);
- hsrv->in_run = 0;
- sd_event_source_set_io_events(hsrv->evsrc, EPOLLIN);
+ MHD_UNSIGNED_LONG_LONG to;
+ struct afb_hsrv *hsrv = arg;
+
+ if (!signum) {
+ do { MHD_run(hsrv->httpd); } while(MHD_get_timeout(hsrv->httpd, &to) == MHD_YES && !to);
}
}
+ fdev_set_events(hsrv->fdev, EPOLLIN);
+}
+
+void run_micro_httpd(struct afb_hsrv *hsrv)
+{
+ fdev_set_events(hsrv->fdev, 0);
+ if (jobs_queue(hsrv, 0, do_run, hsrv) < 0)
+ do_run(0, hsrv);
}
}
-static
int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *hsr
v)
+static
void listen_callback(void *hsrv, uint32_t revents, struct fdev *fde
v)
{
run_micro_httpd(hsrv);
{
run_micro_httpd(hsrv);
- return 0;
}
static int new_client_handler(void *cls, const struct sockaddr *addr, socklen_t addrlen)
}
static int new_client_handler(void *cls, const struct sockaddr *addr, socklen_t addrlen)
@@
-391,8
+394,7
@@
int afb_hsrv_set_cache_timeout(struct afb_hsrv *hsrv, int duration)
int afb_hsrv_start(struct afb_hsrv *hsrv, uint16_t port, unsigned int connection_timeout)
{
int afb_hsrv_start(struct afb_hsrv *hsrv, uint16_t port, unsigned int connection_timeout)
{
- sd_event_source *evsrc;
- int rc;
+ struct fdev *fdev;
struct MHD_Daemon *httpd;
const union MHD_DaemonInfo *info;
struct MHD_Daemon *httpd;
const union MHD_DaemonInfo *info;
@@
-417,24
+419,26
@@
int afb_hsrv_start(struct afb_hsrv *hsrv, uint16_t port, unsigned int connection
return 0;
}
return 0;
}
-
rc = sd_event_add_io(afb_common_get_event_loop(), &evsrc, info->listen_fd, EPOLLIN, io_event_callback, hsrv
);
- if (
rc < 0
) {
+
fdev = afb_fdev_create(info->listen_fd
);
+ if (
fdev == NULL
) {
MHD_stop_daemon(httpd);
MHD_stop_daemon(httpd);
- errno = -rc;
ERROR("connection to events for httpd failed");
return 0;
}
ERROR("connection to events for httpd failed");
return 0;
}
+ fdev_set_autoclose(fdev, 0);
+ fdev_set_events(fdev, EPOLLIN);
+ fdev_set_callback(fdev, listen_callback, hsrv);
hsrv->httpd = httpd;
hsrv->httpd = httpd;
- hsrv->
evsrc = evsrc
;
+ hsrv->
fdev = fdev
;
return 1;
}
void afb_hsrv_stop(struct afb_hsrv *hsrv)
{
return 1;
}
void afb_hsrv_stop(struct afb_hsrv *hsrv)
{
- if (hsrv->
evsrc
!= NULL) {
-
sd_event_source_unref(hsrv->evsrc
);
- hsrv->
evsrc
= NULL;
+ if (hsrv->
fdev
!= NULL) {
+
fdev_unref(hsrv->fdev
);
+ hsrv->
fdev
= NULL;
}
if (hsrv->httpd != NULL)
MHD_stop_daemon(hsrv->httpd);
}
if (hsrv->httpd != NULL)
MHD_stop_daemon(hsrv->httpd);