From f62d817c710d3cd5ab56de85639ca1683aecbe44 Mon Sep 17 00:00:00 2001 From: Codewalker Date: Wed, 24 Dec 2014 12:08:05 -0600 Subject: [PATCH] Fix error spam in event loop when no connections are open. On Win32 at least, select() will immediately fail with an error if there are no file descriptors to wait for (max == 0). In the simple case of a single XMPP session and an event loop that runs only while the connection is open, this isn't a problem, but doesn't work for more complex scenarios. Add some handling so that if xmpp_run_once is called and there are no active connections, simply sleep for duration of the timeout and return. --- src/event.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/event.c b/src/event.c index 1d6fd4c..9efd1c1 100644 --- a/src/event.c +++ b/src/event.c @@ -37,11 +37,14 @@ #ifndef _WIN32 #include #include +#include +#define _sleep(x) usleep(x*1000) #else #include #define ETIMEDOUT WSAETIMEDOUT #define ECONNRESET WSAECONNRESET #define ECONNABORTED WSAECONNABORTED +#define _sleep(x) Sleep(x) #endif #include @@ -225,7 +228,13 @@ void xmpp_run_once(xmpp_ctx_t *ctx, const unsigned long timeout) } /* check for events */ - ret = select(max + 1, &rfds, &wfds, NULL, &tv); + if (max > 0) + ret = select(max + 1, &rfds, &wfds, NULL, &tv); + else { + if (timeout > 0) + _sleep(timeout); + return; + } /* select errored */ if (ret < 0) {