|
|
|
@ -47,7 +47,8 @@ struct event_timeout {
|
|
|
|
|
unsigned int flags;
|
|
|
|
|
struct timeval intervall;
|
|
|
|
|
struct timeval nextrun;
|
|
|
|
|
int (*callback)(void *privdata);
|
|
|
|
|
int (*callback)(int timerid, void *privdata);
|
|
|
|
|
int timerid;
|
|
|
|
|
void *privdata;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -60,13 +61,13 @@ struct event_fd * event_add_fd(
|
|
|
|
|
{
|
|
|
|
|
/* check valid filediskriptor */
|
|
|
|
|
if (fd < 0 || fd > FD_SETSIZE) {
|
|
|
|
|
log_print(LOG_ERROR, "event_add_fd(): invalid fd");
|
|
|
|
|
log_print(LOG_ERROR, "%s(): invalid fd", __FUNCTION__);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* check valid type (read/write) */
|
|
|
|
|
if (!(type & FD_TYPES)) {
|
|
|
|
|
log_print(LOG_ERROR, "event_add_fd(): invalid type");
|
|
|
|
|
log_print(LOG_ERROR, "%s(): invalid type", __FUNCTION__);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -74,7 +75,7 @@ struct event_fd * event_add_fd(
|
|
|
|
|
if (entry == NULL) {
|
|
|
|
|
entry = malloc(sizeof(struct event_fd));
|
|
|
|
|
if (entry == NULL) {
|
|
|
|
|
log_print(LOG_ERROR, "event_add_fd(): out of memory");
|
|
|
|
|
log_print(LOG_ERROR, "%s(): out of memory", __FUNCTION__);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -171,19 +172,21 @@ static void schedule_nextrun(struct event_timeout *entry, struct timeval *now)
|
|
|
|
|
|
|
|
|
|
struct event_timeout * event_add_timeout(
|
|
|
|
|
struct timeval *timeout,
|
|
|
|
|
int (*callback)(void *privdata),
|
|
|
|
|
int (*callback)(int timerid, void *privdata),
|
|
|
|
|
int timerid,
|
|
|
|
|
void *privdata)
|
|
|
|
|
{
|
|
|
|
|
struct event_timeout *entry;
|
|
|
|
|
entry = malloc(sizeof(struct event_timeout));
|
|
|
|
|
if (entry == NULL) {
|
|
|
|
|
log_print(LOG_ERROR, "event_add_timeout(): out of memory");
|
|
|
|
|
log_print(LOG_ERROR, "%s(): out of memory", __FUNCTION__);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entry->flags = 0;
|
|
|
|
|
memcpy(&entry->intervall, timeout, sizeof(entry->intervall));
|
|
|
|
|
entry->callback = callback;
|
|
|
|
|
entry->timerid = timerid;
|
|
|
|
|
entry->privdata = privdata;
|
|
|
|
|
|
|
|
|
|
struct timeval now;
|
|
|
|
@ -193,22 +196,36 @@ struct event_timeout * event_add_timeout(
|
|
|
|
|
return entry;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct event_timeout * event_add_timeout_ms(
|
|
|
|
|
int timeout_ms,
|
|
|
|
|
int (*callback)(int timerid, void *privdata),
|
|
|
|
|
int timerid,
|
|
|
|
|
void *privdata)
|
|
|
|
|
{
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
tv.tv_sec = timeout_ms / 1000;
|
|
|
|
|
tv.tv_usec = (timeout_ms % 1000) * 1000;
|
|
|
|
|
|
|
|
|
|
return event_add_timeout(&tv, callback, timerid, privdata);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void event_remove_timeout(struct event_timeout *entry)
|
|
|
|
|
{
|
|
|
|
|
/* mark the event as deleted -> remove in select() loop */
|
|
|
|
|
entry->flags |= EVENT_DELETE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int event_loop(int (*callback)(void *privdata), void *privdata)
|
|
|
|
|
int event_loop(int (*pre_select_cb)(int *maxfd, void *readfds, void *writefds, struct timeval *timeout, void *privdata),
|
|
|
|
|
int (*post_select_cb)(int retval, void *readfds, void *writefds, void *privdata),
|
|
|
|
|
void *privdata)
|
|
|
|
|
{
|
|
|
|
|
fd_set *fdsets = malloc(sizeof(fd_set) * 2);
|
|
|
|
|
if (fdsets == NULL) {
|
|
|
|
|
log_print(LOG_ERROR, "event_loop(): out of memory");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
struct timeval timeout, *timeout_p = NULL;
|
|
|
|
|
/* default value if no application timeout is present */
|
|
|
|
|
struct timeval timeout = {
|
|
|
|
|
.tv_sec = -1,
|
|
|
|
|
.tv_usec = -1,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!list_empty(&event_timeout_list)) {
|
|
|
|
|
struct timeval now;
|
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
@ -229,7 +246,7 @@ int event_loop(int (*callback)(void *privdata), void *privdata)
|
|
|
|
|
list_del(&entry->list);
|
|
|
|
|
|
|
|
|
|
/* execute callback, when callback returns 0 -> schedule event again */
|
|
|
|
|
if (entry->callback(entry->privdata)) {
|
|
|
|
|
if (entry->callback(entry->timerid, entry->privdata)) {
|
|
|
|
|
free(entry);
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
@ -242,14 +259,16 @@ int event_loop(int (*callback)(void *privdata), void *privdata)
|
|
|
|
|
|
|
|
|
|
/* calc select() timeout */
|
|
|
|
|
sub_timeval(&timeout, &entry->nextrun, &now);
|
|
|
|
|
timeout_p = &timeout;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fd_set *readfds = NULL, *writefds = NULL;
|
|
|
|
|
struct event_fd *entry, *tmp;
|
|
|
|
|
int maxfd = -1;
|
|
|
|
|
|
|
|
|
|
fd_set readfds, writefds;
|
|
|
|
|
FD_ZERO(&readfds);
|
|
|
|
|
FD_ZERO(&writefds);
|
|
|
|
|
|
|
|
|
|
list_for_each_entry_safe(entry, tmp, &event_fd_list, list) {
|
|
|
|
|
entry->flags &= ~EVENT_NEW;
|
|
|
|
|
|
|
|
|
@ -259,52 +278,54 @@ int event_loop(int (*callback)(void *privdata), void *privdata)
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (entry->flags & FD_READ) {
|
|
|
|
|
if (readfds == NULL) {
|
|
|
|
|
readfds = &fdsets[0];
|
|
|
|
|
FD_ZERO(readfds);
|
|
|
|
|
}
|
|
|
|
|
FD_SET(entry->fd, readfds);
|
|
|
|
|
}
|
|
|
|
|
if (entry->flags & FD_READ)
|
|
|
|
|
FD_SET(entry->fd, &readfds);
|
|
|
|
|
|
|
|
|
|
if (entry->flags & FD_WRITE) {
|
|
|
|
|
if (writefds == NULL) {
|
|
|
|
|
writefds = &fdsets[1];
|
|
|
|
|
FD_ZERO(writefds);
|
|
|
|
|
}
|
|
|
|
|
FD_SET(entry->fd, writefds);
|
|
|
|
|
}
|
|
|
|
|
if (entry->flags & FD_WRITE)
|
|
|
|
|
FD_SET(entry->fd, &writefds);
|
|
|
|
|
|
|
|
|
|
maxfd = (entry->fd > maxfd) ? entry->fd : maxfd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
maxfd++;
|
|
|
|
|
|
|
|
|
|
/* exit loop if callback returns true */
|
|
|
|
|
if (callback != NULL && callback(privdata) != 0)
|
|
|
|
|
if (pre_select_cb != NULL && pre_select_cb(&maxfd, (void *)&readfds, (void *)&writefds, &timeout, privdata) != 0)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
int i = select(maxfd +1, readfds, writefds, NULL, timeout_p);
|
|
|
|
|
if (i < 0 && errno == EINTR) {
|
|
|
|
|
int retval;
|
|
|
|
|
if (timeout.tv_sec == -1 && timeout.tv_usec == -1)
|
|
|
|
|
retval = select(maxfd, &readfds, &writefds, NULL, NULL);
|
|
|
|
|
else
|
|
|
|
|
retval = select(maxfd, &readfds, &writefds, NULL, &timeout);
|
|
|
|
|
|
|
|
|
|
/* exit loop if callback returns true */
|
|
|
|
|
if (post_select_cb != NULL && post_select_cb(retval, (void *)&readfds, (void *)&writefds, privdata) != 0)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (retval < 0 && errno == EINTR) {
|
|
|
|
|
errno = 0;
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
} else if (i < 0) {
|
|
|
|
|
log_print(LOG_ERROR, "event_loop(): select():");
|
|
|
|
|
} else if (retval < 0) {
|
|
|
|
|
log_print(LOG_ERROR, "%s(): select():", __FUNCTION__);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else if (i == 0) {
|
|
|
|
|
/* timeout */
|
|
|
|
|
if (retval == 0)
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list_for_each_entry(entry, &event_fd_list, list) {
|
|
|
|
|
if (((entry->flags & (FD_READ | EVENT_NEW)) == FD_READ) && FD_ISSET(entry->fd, readfds))
|
|
|
|
|
if (((entry->flags & (FD_READ | EVENT_NEW)) == FD_READ) && FD_ISSET(entry->fd, &readfds))
|
|
|
|
|
if (entry->read_cb(entry->fd, entry->read_priv) != 0)
|
|
|
|
|
entry->flags |= EVENT_DELETE;
|
|
|
|
|
|
|
|
|
|
if (((entry->flags & (FD_WRITE | EVENT_NEW)) == FD_WRITE) && FD_ISSET(entry->fd, writefds))
|
|
|
|
|
if (((entry->flags & (FD_WRITE | EVENT_NEW)) == FD_WRITE) && FD_ISSET(entry->fd, &writefds))
|
|
|
|
|
if (entry->write_cb(entry->fd, entry->write_priv) != 0)
|
|
|
|
|
entry->flags |= EVENT_DELETE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
free(fdsets);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|