2009-10-11 14:31:42 +00:00
|
|
|
#include "fdevent.h"
|
|
|
|
#include "buffer.h"
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <fcntl.h>
|
2005-08-31 09:16:18 +00:00
|
|
|
#include <assert.h>
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
#ifdef USE_SELECT
|
|
|
|
|
|
|
|
static int fdevent_select_reset(fdevents *ev) {
|
|
|
|
FD_ZERO(&(ev->select_set_read));
|
|
|
|
FD_ZERO(&(ev->select_set_write));
|
|
|
|
FD_ZERO(&(ev->select_set_error));
|
|
|
|
ev->select_max_fd = -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fdevent_select_event_del(fdevents *ev, int fde_ndx, int fd) {
|
|
|
|
if (fde_ndx < 0) return -1;
|
|
|
|
|
|
|
|
FD_CLR(fd, &(ev->select_set_read));
|
|
|
|
FD_CLR(fd, &(ev->select_set_write));
|
|
|
|
FD_CLR(fd, &(ev->select_set_error));
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fdevent_select_event_add(fdevents *ev, int fde_ndx, int fd, int events) {
|
|
|
|
UNUSED(fde_ndx);
|
|
|
|
|
2005-08-31 09:16:18 +00:00
|
|
|
/* we should be protected by max-fds, but you never know */
|
2009-07-10 16:16:11 +00:00
|
|
|
assert(fd < ((int)FD_SETSIZE));
|
2005-08-31 09:16:18 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (events & FDEVENT_IN) {
|
|
|
|
FD_SET(fd, &(ev->select_set_read));
|
|
|
|
FD_CLR(fd, &(ev->select_set_write));
|
|
|
|
}
|
|
|
|
if (events & FDEVENT_OUT) {
|
|
|
|
FD_CLR(fd, &(ev->select_set_read));
|
|
|
|
FD_SET(fd, &(ev->select_set_write));
|
|
|
|
}
|
|
|
|
FD_SET(fd, &(ev->select_set_error));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (fd > ev->select_max_fd) ev->select_max_fd = fd;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fdevent_select_poll(fdevents *ev, int timeout_ms) {
|
|
|
|
struct timeval tv;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
tv.tv_sec = timeout_ms / 1000;
|
|
|
|
tv.tv_usec = (timeout_ms % 1000) * 1000;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
ev->select_read = ev->select_set_read;
|
|
|
|
ev->select_write = ev->select_set_write;
|
|
|
|
ev->select_error = ev->select_set_error;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return select(ev->select_max_fd + 1, &(ev->select_read), &(ev->select_write), &(ev->select_error), &tv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fdevent_select_event_get_revent(fdevents *ev, size_t ndx) {
|
|
|
|
int revents = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (FD_ISSET(ndx, &(ev->select_read))) {
|
|
|
|
revents |= FDEVENT_IN;
|
|
|
|
}
|
|
|
|
if (FD_ISSET(ndx, &(ev->select_write))) {
|
|
|
|
revents |= FDEVENT_OUT;
|
|
|
|
}
|
|
|
|
if (FD_ISSET(ndx, &(ev->select_error))) {
|
|
|
|
revents |= FDEVENT_ERR;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return revents;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fdevent_select_event_get_fd(fdevents *ev, size_t ndx) {
|
|
|
|
UNUSED(ev);
|
|
|
|
|
|
|
|
return ndx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fdevent_select_event_next_fdndx(fdevents *ev, int ndx) {
|
|
|
|
int i;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
i = (ndx < 0) ? 0 : ndx + 1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (; i < ev->select_max_fd + 1; i++) {
|
|
|
|
if (FD_ISSET(i, &(ev->select_read))) break;
|
|
|
|
if (FD_ISSET(i, &(ev->select_write))) break;
|
|
|
|
if (FD_ISSET(i, &(ev->select_error))) break;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fdevent_select_init(fdevents *ev) {
|
|
|
|
ev->type = FDEVENT_HANDLER_SELECT;
|
|
|
|
#define SET(x) \
|
|
|
|
ev->x = fdevent_select_##x;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
SET(reset);
|
|
|
|
SET(poll);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
SET(event_del);
|
|
|
|
SET(event_add);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
SET(event_next_fdndx);
|
|
|
|
SET(event_get_fd);
|
|
|
|
SET(event_get_revent);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
int fdevent_select_init(fdevents *ev) {
|
|
|
|
UNUSED(ev);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|