Browse Source

[core] fdevent_process()

process fdevents in fdevent.c
personal/stbuehler/fix-fdevent
Glenn Strauss 3 years ago
parent
commit
653b0dac7c
  1. 29
      src/fdevent.c
  2. 5
      src/fdevent.h
  3. 21
      src/server.c

29
src/fdevent.c

@ -424,18 +424,6 @@ int fdevent_poll(fdevents *ev, int timeout_ms) {
return ev->poll(ev, timeout_ms);
}
int fdevent_event_get_revent(fdevents *ev, size_t ndx) {
if (ev->event_get_revent == NULL) SEGFAULT();
return ev->event_get_revent(ev, ndx);
}
int fdevent_event_get_fd(fdevents *ev, size_t ndx) {
if (ev->event_get_fd == NULL) SEGFAULT();
return ev->event_get_fd(ev, ndx);
}
fdevent_handler fdevent_get_handler(fdevents *ev, int fd) {
if (ev->fdarray[fd] == NULL) SEGFAULT();
if ((uintptr_t)ev->fdarray[fd] & 0x3) return NULL;
@ -610,10 +598,19 @@ int fdevent_accept_listenfd(int listenfd, struct sockaddr *addr, size_t *addrlen
}
int fdevent_event_next_fdndx(fdevents *ev, int ndx) {
if (ev->event_next_fdndx) return ev->event_next_fdndx(ev, ndx);
return -1;
void fdevent_process(server *srv, fdevents *ev, int n) {
/* n is the number of events */
int ndx = -1;
do {
ndx = ev->event_next_fdndx(ev, ndx);
/* not all fdevent handlers know how many fds got an event */
if (-1 == ndx) return;
else {
fdnode *fdn = ev->fdarray[ev->event_get_fd(ev, ndx)];
if (0 == ((uintptr_t)fdn & 0x3))
(*fdn->handler)(srv, fdn->ctx, ev->event_get_revent(ev, ndx));
}
} while (--n > 0);
}

5
src/fdevent.h

@ -51,14 +51,11 @@ void fdevent_event_set(fdevents *ev, int *fde_ndx, int fd, int events); /* event
void fdevent_event_add(fdevents *ev, int *fde_ndx, int fd, int event); /* events can be FDEVENT_IN or FDEVENT_OUT */
void fdevent_event_clr(fdevents *ev, int *fde_ndx, int fd, int event); /* events can be FDEVENT_IN or FDEVENT_OUT */
void fdevent_event_del(fdevents *ev, int *fde_ndx, int fd);
int fdevent_event_get_revent(fdevents *ev, size_t ndx);
int fdevent_event_get_fd(fdevents *ev, size_t ndx);
fdevent_handler fdevent_get_handler(fdevents *ev, int fd);
void * fdevent_get_context(fdevents *ev, int fd);
int fdevent_event_next_fdndx(fdevents *ev, int ndx);
int fdevent_poll(fdevents *ev, int timeout_ms);
void fdevent_process(server *srv, fdevents *ev, int n);
int fdevent_register(fdevents *ev, int fd, fdevent_handler handler, void *ctx);
int fdevent_unregister(fdevents *ev, int fd);

21
src/server.c

@ -2023,27 +2023,8 @@ static int server_main_loop (server * const srv) {
}
if ((n = fdevent_poll(srv->ev, 1000)) > 0) {
/* n is the number of events */
int fd;
int revents;
int fd_ndx;
fdevent_process(srv, srv->ev, n);
last_active_ts = srv->cur_ts;
fd_ndx = -1;
do {
fdevent_handler handler;
void *context;
fd_ndx = fdevent_event_next_fdndx (srv->ev, fd_ndx);
if (-1 == fd_ndx) break; /* not all fdevent handlers know how many fds got an event */
revents = fdevent_event_get_revent (srv->ev, fd_ndx);
fd = fdevent_event_get_fd (srv->ev, fd_ndx);
handler = fdevent_get_handler(srv->ev, fd);
context = fdevent_get_context(srv->ev, fd);
if (NULL != handler) {
(*handler)(srv, context, revents);
}
} while (--n > 0);
} else if (n < 0 && errno != EINTR) {
log_error_write(srv, __FILE__, __LINE__, "ss",
"fdevent_poll failed:",

Loading…
Cancel
Save