aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--protocols/events.h4
-rw-r--r--protocols/events_libevent.c17
-rw-r--r--sock.h2
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 );
+}
diff --git a/sock.h b/sock.h
index 28d31de9..ff7034a6 100644
--- a/sock.h
+++ b/sock.h
@@ -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