This commit is contained in:
Olaf Rempel 2007-03-11 13:59:40 +01:00
parent 7edf765697
commit 1eb9d49ae6
2 changed files with 68 additions and 55 deletions

103
event.c
View File

@ -46,7 +46,7 @@ struct fd_entry {
struct timeout_entry { struct timeout_entry {
struct list_head list; struct list_head list;
struct timeval timeout; struct timeval intervall;
struct timeval nextrun; struct timeval nextrun;
int (*callback)(void *privdata); int (*callback)(void *privdata);
void *privdata; void *privdata;
@ -77,46 +77,39 @@ int event_remove_fd(int fd)
{ {
struct fd_entry *entry, *tmp; struct fd_entry *entry, *tmp;
list_for_each_entry_safe(entry, tmp, &fd_list, list) { list_for_each_entry_safe(entry, tmp, &fd_list, list) {
if (entry->fd == fd) { if (entry->fd == fd)
entry->flags |= FD_DELETE; entry->flags |= FD_DELETE;
return 0;
}
} }
return -1; return 0;
} }
static void calc_nextrun(struct timeval *timeout, struct timeval *nextrun) static void add_timeval(struct timeval *ret, struct timeval *a, struct timeval *b)
{ {
struct timeval now; ret->tv_usec = a->tv_usec + b->tv_usec;
gettimeofday(&now, NULL); ret->tv_sec = a->tv_sec + b->tv_sec;
nextrun->tv_usec = now.tv_usec + timeout->tv_usec; if (ret->tv_usec >= 1000000) {
nextrun->tv_sec = now.tv_sec + timeout->tv_sec; ret->tv_usec -= 1000000;
ret->tv_sec++;
if (nextrun->tv_usec >= 1000000) {
nextrun->tv_usec -= 1000000;
nextrun->tv_sec++;
} }
} }
static void calc_timeout(struct timeval *timeout, struct timeval *nextrun) static void sub_timeval(struct timeval *ret, struct timeval *a, struct timeval *b)
{ {
struct timeval now; ret->tv_usec = a->tv_usec - b->tv_usec;
gettimeofday(&now, NULL); ret->tv_sec = a->tv_sec - b->tv_sec;
timeout->tv_usec = nextrun->tv_usec - now.tv_usec; if (ret->tv_usec < 0) {
timeout->tv_sec = nextrun->tv_sec - now.tv_sec; ret->tv_usec += 1000000;
ret->tv_sec--;
if (timeout->tv_usec < 0) {
timeout->tv_usec += 1000000;
timeout->tv_sec--;
} }
} }
static void schedule_nextrun(struct timeout_entry *entry) static void schedule_nextrun(struct timeout_entry *entry, struct timeval *now)
{ {
add_timeval(&entry->nextrun, now, &entry->intervall);
struct timeout_entry *search; struct timeout_entry *search;
list_for_each_entry(search, &timeout_list, list) { list_for_each_entry(search, &timeout_list, list) {
if (search->nextrun.tv_sec > entry->nextrun.tv_sec) { if (search->nextrun.tv_sec > entry->nextrun.tv_sec) {
list_add_tail(&entry->list, &search->list); list_add_tail(&entry->list, &search->list);
@ -140,15 +133,21 @@ int event_add_timeout(struct timeval *timeout, int (*callback)(void *privdata),
return -1; return -1;
} }
memcpy(&entry->timeout, timeout, sizeof(entry->timeout)); memcpy(&entry->intervall, timeout, sizeof(entry->intervall));
entry->callback = callback; entry->callback = callback;
entry->privdata = privdata; entry->privdata = privdata;
calc_nextrun(&entry->timeout, &entry->nextrun); struct timeval now;
schedule_nextrun(entry); gettimeofday(&now, NULL);
schedule_nextrun(entry, &now);
return 0; return 0;
} }
int event_remove_timeout(int id)
{
}
int event_loop(void) int event_loop(void)
{ {
fd_set *fdsets = malloc(sizeof(fd_set) * 3); fd_set *fdsets = malloc(sizeof(fd_set) * 3);
@ -192,30 +191,37 @@ int event_loop(void)
struct timeval timeout, *timeout_p = NULL; struct timeval timeout, *timeout_p = NULL;
if (!list_empty(&timeout_list)) { if (!list_empty(&timeout_list)) {
struct timeval now;
gettimeofday(&now, NULL);
struct timeout_entry *entry, *tmp; struct timeout_entry *entry, *tmp;
list_for_each_entry_safe(entry, tmp, &timeout_list, list) { list_for_each_entry_safe(entry, tmp, &timeout_list, list) {
/* timeout elapsed */
if (entry->nextrun.tv_sec < now.tv_sec ||
(entry->nextrun.tv_sec == now.tv_sec && entry->nextrun.tv_usec < now.tv_usec)) {
calc_timeout(&timeout, &entry->nextrun); list_del(&entry->list);
if (timeout.tv_sec >= 0 && timeout.tv_usec > 0) { if (entry->callback(entry->privdata)) {
timeout_p = &timeout; free(entry);
break;
}
// delayed timeout, exec NOW! } else {
list_del(&entry->list); schedule_nextrun(entry, &now);
int ret = entry->callback(entry->privdata); }
if (ret == 0) {
calc_nextrun(&entry->timeout, &entry->nextrun);
schedule_nextrun(entry);
} else { } else {
free(entry); break;
} }
} }
entry = list_entry(timeout_list.next, typeof(*entry), list);
/* calc select() timeout */
sub_timeval(&timeout, &entry->nextrun, &now);
timeout_p = &timeout;
} }
int i = select(FD_SETSIZE, readfds, writefds, exceptfds, timeout_p); int i = select(FD_SETSIZE, readfds, writefds, exceptfds, timeout_p);
if (i < 0) { if (i <= 0) {
/* On error, -1 is returned, and errno is set /* On error, -1 is returned, and errno is set
* appropriately; the sets and timeout become * appropriately; the sets and timeout become
* undefined, so do not rely on their contents * undefined, so do not rely on their contents
@ -223,23 +229,10 @@ int event_loop(void)
*/ */
continue; continue;
} else if (i == 0 && !list_empty(&timeout_list)) {
struct timeout_entry *entry;
entry = list_entry(timeout_list.next, typeof(*entry), list);
list_del(&entry->list);
int ret = entry->callback(entry->privdata);
if (ret == 0) {
calc_nextrun(&entry->timeout, &entry->nextrun);
schedule_nextrun(entry);
} else {
free(entry);
}
} else { } else {
list_for_each_entry(entry, &fd_list, list) { list_for_each_entry(entry, &fd_list, list) {
if ((entry->flags & FD_NEW) != 0) { if ((entry->flags & FD_NEW) != 0) {
/* entry has just been added, execute it next round */
continue; continue;
} else if ((entry->flags & FD_READ) != 0) { } else if ((entry->flags & FD_READ) != 0) {

View File

@ -2,6 +2,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include "configfile.h" #include "configfile.h"
#include "connection.h" #include "connection.h"
#include "event.h" #include "event.h"
@ -26,8 +29,15 @@ static int listen_init(const char *value, void *privdata)
return 0; return 0;
} }
static int trigger(void *privdata)
{
log_print(LOG_DEBUG, "%s", (char *)privdata);
return 0;
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
/*
if (config_parse("telnetproxy.conf") == -1) if (config_parse("telnetproxy.conf") == -1)
return -1; return -1;
@ -42,6 +52,16 @@ int main(int argc, char *argv[])
log_print(LOG_ERROR, "no admin_listen sockets defined!"); log_print(LOG_ERROR, "no admin_listen sockets defined!");
return -1; return -1;
} }
*/
struct timeval tv;
tv.tv_usec = 0;
tv.tv_sec = 1;
event_add_timeout(&tv, trigger, "trigger 1");
tv.tv_sec = 2;
event_add_timeout(&tv, trigger, "trigger 2");
tv.tv_sec = 3;
event_add_timeout(&tv, trigger, "trigger 3");
event_loop(); event_loop();