From: Florent Bruneau Date: Wed, 8 Oct 2008 06:34:01 +0000 (+0200) Subject: Let say an event is just a pointer. X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=521bf8a6290d26b27c332639fe8cc4e47c3d5c4f;p=apps%2Fpfixtools.git Let say an event is just a pointer. Signed-off-by: Florent Bruneau --- diff --git a/common/server.c b/common/server.c index 65e1a70..5a9e081 100644 --- a/common/server.c +++ b/common/server.c @@ -52,6 +52,7 @@ static void server_wipe(server_t *server) { server->listener = server->event = false; if (server->fd > 0) { + epoll_modify(server->fd, 0, NULL); close(server->fd); server->fd = -1; } @@ -167,12 +168,12 @@ event_t event_register(void *data) int fds[2]; if (pipe(fds) != 0) { UNIXERR("pipe"); - return INVALID_EVENT; + return NULL; } if (setnonblock(fds[0]) != 0) { close(fds[0]); close(fds[1]); - return INVALID_EVENT; + return NULL; } server_t *tmp = server_acquire(); @@ -181,20 +182,20 @@ event_t event_register(void *data) tmp->fd2 = fds[1]; tmp->data = data; epoll_register(fds[0], EPOLLIN, tmp); - return tmp->fd2; + return tmp; } bool event_fire(event_t event) { static const char *data = ""; - return write(event, data, 1) == 0; + return write(event->fd2, data, 1) == 0; } -static bool event_cancel(int event) +static bool event_cancel(event_t event) { char buff[32]; while (true) { - ssize_t res = read(event, buff, 32); + ssize_t res = read(event->fd, buff, 32); if (res == -1 && errno != EAGAIN && errno != EINTR) { UNIXERR("read"); return false; @@ -240,12 +241,12 @@ int server_loop(start_client_t starter, delete_client_t deleter, (void)start_client(d, starter, deleter); continue; } else if (d->event) { - if (!event_cancel(d->fd)) { + if (!event_cancel(d)) { server_release(d); continue; } if (handler) { - if (!handler(d->data, config)) { + if (!handler(d, config)) { server_release(d); } } diff --git a/common/server.h b/common/server.h index 28f4c35..dfa7eb0 100644 --- a/common/server.h +++ b/common/server.h @@ -39,16 +39,16 @@ #include "buffer.h" typedef struct server_t server_t; -typedef int event_t; +typedef server_t *event_t; -#define INVALID_EVENT (-1) +#define INVALID_EVENT (NULL) typedef void *(*start_listener_t)(void); typedef void (*delete_client_t)(void*); typedef void *(*start_client_t)(server_t*); typedef int (*run_client_t)(server_t*, void*); typedef bool (*refresh_t)(void*); -typedef bool (*event_handler_t)(void* data, void* config); +typedef bool (*event_handler_t)(event_t, void*); struct server_t { unsigned listener : 1; @@ -69,6 +69,7 @@ int start_server(int port, start_listener_t starter, delete_client_t deleter); event_t event_register(void *data); bool event_fire(event_t event); +#define event_data(event) ((event)->data) int server_loop(start_client_t starter, delete_client_t deleter, run_client_t runner, event_handler_t handler,