This commit is contained in:
Olaf Rempel 2007-03-11 17:52:51 +01:00
parent b602d7c17f
commit 48ee67e1e9
3 changed files with 59 additions and 74 deletions

View File

@ -18,9 +18,9 @@ struct connection {
struct sockaddr_in src_addr; struct sockaddr_in src_addr;
struct sockaddr_in dst_addr; struct sockaddr_in dst_addr;
struct event_entry *src_event; struct event_fd *src_event;
struct event_entry *dst_event; struct event_fd *dst_event;
struct event_entry *timeout; struct event_timeout *timeout;
time_t login_time; time_t login_time;
}; };
@ -40,25 +40,24 @@ static struct connection * create_connection()
return con; return con;
} }
static int destroy_connection(struct connection *con) static void destroy_connection(struct connection *con)
{ {
if (con->timeout != NULL) if (con->timeout != NULL)
event_remove(con->timeout); event_remove_timeout(con->timeout);
int src_fd = event_get_fd(con->src_event); int src_fd = event_get_fd(con->src_event);
if (src_fd != -1) { if (src_fd != -1) {
close(src_fd); close(src_fd);
event_remove(con->src_event); event_remove_fd(con->src_event);
} }
int dst_fd = event_get_fd(con->dst_event); int dst_fd = event_get_fd(con->dst_event);
if (dst_fd != -1) { if (dst_fd != -1) {
close(dst_fd); close(dst_fd);
event_remove(con->dst_event); event_remove_fd(con->dst_event);
} }
free(con); free(con);
return 0;
} }
static int forward_handler(int fd, void *privdata) static int forward_handler(int fd, void *privdata)
@ -106,11 +105,8 @@ static int connect_handler(int fd, void *privdata)
event_add_writefd(con->dst_event, 0, NULL, NULL); event_add_writefd(con->dst_event, 0, NULL, NULL);
/* remove timeout */ /* remove timeout */
event_remove(con->timeout); event_remove_timeout(con->timeout);
con->timeout = NULL; con->timeout = NULL;
log_print(LOG_DEBUG, "connect_handler(): exit");
return 0; return 0;
} }
@ -157,7 +153,7 @@ static int client_handler(int fd, void *privdata)
log_print(LOG_INFO, "connecting to %s", get_sockaddr_buf(&con->dst_addr)); log_print(LOG_INFO, "connecting to %s", get_sockaddr_buf(&con->dst_addr));
struct timeval tv; struct timeval tv;
tv.tv_sec = 5; tv.tv_sec = 2;
tv.tv_usec = 0; tv.tv_usec = 0;
con->timeout = event_add_timeout(&tv, connect_timeout, con); con->timeout = event_add_timeout(&tv, connect_timeout, con);

62
event.c
View File

@ -32,29 +32,27 @@
static LIST_HEAD(event_fd_list); static LIST_HEAD(event_fd_list);
static LIST_HEAD(event_timeout_list); static LIST_HEAD(event_timeout_list);
struct event_entry { struct event_fd {
struct list_head list; struct list_head list;
unsigned int flags; unsigned int flags;
union {
struct {
int fd; int fd;
int (*read_cb)(int fd, void *privdata); int (*read_cb)(int fd, void *privdata);
int (*write_cb)(int fd, void *privdata); int (*write_cb)(int fd, void *privdata);
void *read_priv; void *read_priv;
void *write_priv; void *write_priv;
}; };
struct {
struct event_timeout {
struct list_head list;
unsigned int flags;
struct timeval intervall; struct timeval intervall;
struct timeval nextrun; struct timeval nextrun;
int (*callback)(void *privdata); int (*callback)(void *privdata);
void *privdata; void *privdata;
}; };
};
};
struct event_entry * event_add_fd( struct event_fd * event_add_fd(
struct event_entry *entry, struct event_fd *entry,
int fd, int fd,
int type, int type,
int (*callback)(int fd, void *privdata), int (*callback)(int fd, void *privdata),
@ -74,13 +72,13 @@ struct event_entry * event_add_fd(
/* create new entry */ /* create new entry */
if (entry == NULL) { if (entry == NULL) {
entry = malloc(sizeof(struct event_entry)); entry = malloc(sizeof(struct event_fd));
if (entry == NULL) { if (entry == NULL) {
log_print(LOG_ERROR, "event_add_fd(): out of memory"); log_print(LOG_ERROR, "event_add_fd(): out of memory");
return NULL; return NULL;
} }
entry->flags = EVENT_FD_CB; memset(entry, 0, sizeof(struct event_fd));
entry->fd = fd; entry->fd = fd;
/* put it on the list */ /* put it on the list */
@ -102,27 +100,12 @@ struct event_entry * event_add_fd(
return entry; return entry;
} }
int event_get_fd(struct event_entry *entry) int event_get_fd(struct event_fd *entry)
{ {
if (entry == NULL || !(entry->flags & EVENT_FD_CB)) return (entry != NULL) ? entry->fd: -1;
return -1;
return entry->fd;
} }
void event_remove_fd(int fd) void event_remove_fd(struct event_fd *entry)
{
struct event_entry *entry, *tmp;
list_for_each_entry_safe(entry, tmp, &event_fd_list, list) {
if (entry->fd == fd) {
entry->flags |= EVENT_DELETE;
return;
}
}
return;
}
void event_remove(struct event_entry *entry)
{ {
/* mark the event as deleted -> remove in select() loop */ /* mark the event as deleted -> remove in select() loop */
entry->flags |= EVENT_DELETE; entry->flags |= EVENT_DELETE;
@ -167,11 +150,11 @@ static int cmp_timeval(struct timeval *a, struct timeval *b)
return 0; return 0;
} }
static void schedule_nextrun(struct event_entry *entry, struct timeval *now) static void schedule_nextrun(struct event_timeout *entry, struct timeval *now)
{ {
add_timeval(&entry->nextrun, now, &entry->intervall); add_timeval(&entry->nextrun, now, &entry->intervall);
struct event_entry *search; struct event_timeout *search;
list_for_each_entry(search, &event_timeout_list, list) { list_for_each_entry(search, &event_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);
@ -186,19 +169,19 @@ static void schedule_nextrun(struct event_entry *entry, struct timeval *now)
list_add_tail(&entry->list, &event_timeout_list); list_add_tail(&entry->list, &event_timeout_list);
} }
struct event_entry * event_add_timeout( struct event_timeout * event_add_timeout(
struct timeval *timeout, struct timeval *timeout,
int (*callback)(void *privdata), int (*callback)(void *privdata),
void *privdata) void *privdata)
{ {
struct event_entry *entry; struct event_timeout *entry;
entry = malloc(sizeof(struct event_entry)); entry = malloc(sizeof(struct event_timeout));
if (entry == NULL) { if (entry == NULL) {
log_print(LOG_ERROR, "event_add_timeout(): out of memory"); log_print(LOG_ERROR, "event_add_timeout(): out of memory");
return NULL; return NULL;
} }
entry->flags = EVENT_TIMEOUT; entry->flags = 0;
memcpy(&entry->intervall, timeout, sizeof(entry->intervall)); memcpy(&entry->intervall, timeout, sizeof(entry->intervall));
entry->callback = callback; entry->callback = callback;
entry->privdata = privdata; entry->privdata = privdata;
@ -210,6 +193,11 @@ struct event_entry * event_add_timeout(
return entry; return entry;
} }
void event_remove_timeout(struct event_timeout *entry)
{
/* mark the event as deleted -> remove in select() loop */
entry->flags |= EVENT_DELETE;
}
int event_loop(void) int event_loop(void)
{ {
@ -221,7 +209,7 @@ int event_loop(void)
while (1) { while (1) {
fd_set *readfds = NULL, *writefds = NULL; fd_set *readfds = NULL, *writefds = NULL;
struct event_entry *entry, *tmp; struct event_fd *entry, *tmp;
list_for_each_entry_safe(entry, tmp, &event_fd_list, list) { list_for_each_entry_safe(entry, tmp, &event_fd_list, list) {
entry->flags &= ~EVENT_NEW; entry->flags &= ~EVENT_NEW;
@ -251,7 +239,7 @@ int event_loop(void)
struct timeval now; struct timeval now;
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
struct event_entry *entry, *tmp; struct event_timeout *entry, *tmp;
list_for_each_entry_safe(entry, tmp, &event_timeout_list, list) { list_for_each_entry_safe(entry, tmp, &event_timeout_list, list) {
if (entry->flags & EVENT_DELETE) { if (entry->flags & EVENT_DELETE) {
list_del(&entry->list); list_del(&entry->list);

29
event.h
View File

@ -3,14 +3,11 @@
#include <sys/time.h> #include <sys/time.h>
#define EVENT_FD_CB 0x10000000 #define EVENT_NEW 0x1000
#define EVENT_TIMEOUT 0x20000000 #define EVENT_DELETE 0x2000
#define EVENT_NEW 0x01000000 #define FD_READ 0x0001
#define EVENT_DELETE 0x02000000 #define FD_WRITE 0x0002
#define FD_READ 0x01
#define FD_WRITE 0x02
#define FD_TYPES (FD_READ | FD_WRITE) #define FD_TYPES (FD_READ | FD_WRITE)
#define event_add_readfd(entry, fd, callback, privdata) \ #define event_add_readfd(entry, fd, callback, privdata) \
@ -19,22 +16,26 @@
#define event_add_writefd(entry, fd, callback, privdata) \ #define event_add_writefd(entry, fd, callback, privdata) \
event_add_fd(entry, fd, FD_WRITE, callback, privdata) event_add_fd(entry, fd, FD_WRITE, callback, privdata)
struct event_entry * event_add_fd( /* inner details are not visible to external users (TODO: size unknown) */
struct event_entry *entry, struct event_fd;
struct event_timeout;
struct event_fd * event_add_fd(
struct event_fd *entry,
int fd, int fd,
int type, int type,
int (*callback)(int fd, void *privdata), int (*callback)(int fd, void *privdata),
void *privdata); void *privdata);
struct event_entry * event_add_timeout( int event_get_fd(struct event_fd *entry);
void event_remove_fd(struct event_fd *entry);
struct event_timeout * event_add_timeout(
struct timeval *timeout, struct timeval *timeout,
int (*callback)(void *privdata), int (*callback)(void *privdata),
void *privdata); void *privdata);
int event_get_fd(struct event_entry *entry); void event_remove_timeout(struct event_timeout *entry);
void event_remove_fd(int fd);
void event_remove(struct event_entry *entry);
int event_loop(void); int event_loop(void);