diff options
author | Wilmer van der Gaast <wilmer@gaast.net> | 2006-05-14 01:28:31 +0200 |
---|---|---|
committer | Wilmer van der Gaast <wilmer@gaast.net> | 2006-05-14 01:28:31 +0200 |
commit | 3f199fcf6242fc0cc0e4e767ce2a505792d80fef (patch) | |
tree | 3d62ed0e3d2ebb7d7604a4dba866801aadb4cbe8 | |
parent | 0356ae3aa10bb6556d0ea881988831cad5e71f38 (diff) |
Added a closesocket() that properly removes event handlers before closing a socket.
-rw-r--r-- | protocols/events.h | 4 | ||||
-rw-r--r-- | protocols/events_libevent.c | 17 | ||||
-rw-r--r-- | sock.h | 2 |
3 files changed, 23 insertions, 0 deletions
diff --git a/protocols/events.h b/protocols/events.h index a61dc98c..37b94aab 100644 --- a/protocols/events.h +++ b/protocols/events.h @@ -60,4 +60,8 @@ G_MODULE_EXPORT gint b_timeout_add(gint timeout, b_event_handler func, gpointer G_MODULE_EXPORT void b_event_remove(gint id); G_MODULE_EXPORT gboolean b_event_remove_by_data(gpointer data); +#ifdef EVENTS_LIBEVENT +G_MODULE_EXPORT void closesocket(int fd); +#endif + #endif /* _EVENTS_H_ */ diff --git a/protocols/events_libevent.c b/protocols/events_libevent.c index 6e1ed98e..ad1864d2 100644 --- a/protocols/events_libevent.c +++ b/protocols/events_libevent.c @@ -29,6 +29,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> #include <sys/types.h> #include "proxy.h" @@ -216,3 +217,19 @@ gboolean b_event_remove_by_data( gpointer data ) event_debug( "FALSE!\n" ); return FALSE; } + +void closesocket( int fd ) +{ + struct b_event_data *b_ev; + + /* Since epoll() (the main reason we use libevent) automatically removes sockets from + the epoll() list when a socket gets closed and some modules have a habit of + closing sockets before removing event handlers, our and libevent's administration + get a little bit messed up. So this little function will remove the handlers + properly before closing a socket. */ + + if( ( b_ev = g_hash_table_lookup( read_hash, &fd ) ) || ( b_ev = g_hash_table_lookup( write_hash, &fd ) ) ) + b_event_remove( b_ev->id ); + + close( fd ); +} @@ -17,7 +17,9 @@ #define sock_make_nonblocking(fd) fcntl(fd, F_SETFL, O_NONBLOCK) #define sock_make_blocking(fd) fcntl(fd, F_SETFL, 0) #define sockerr_again() (errno == EINPROGRESS || errno == EINTR) +#ifndef EVENTS_LIBEVENT #define closesocket(a) close(a) +#endif #else # include <winsock2.h> # ifndef _MSC_VER |