diff --git a/mongoose.c b/mongoose.c index 2e69cc1e..7314a7ad 100644 --- a/mongoose.c +++ b/mongoose.c @@ -4396,7 +4396,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) { #elif MG_ENABLE_POLL size_t i = 0, n = 0; for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) n++; - struct pollfd fds[n == 0 ? 1 : n]; // Avoid zero-length VLA + struct pollfd fds[n == 0 ? 1 : n]; // Avoid zero-length VLA memset(fds, 0, sizeof(fds)); for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next, i++) { @@ -4406,24 +4406,25 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) { fds[i].fd = FD(c); fds[i].events |= POLLIN; if (c->is_connecting || (c->send.len > 0 && c->is_tls_hs == 0)) { - fds[i].events |= POLLOUT; + fds[i].events |= POLLOUT; } + if (mg_tls_pending(c) > 0) ms = 0; // Don't wait if TLS is ready } } if (poll(fds, n, ms) < 0) { MG_ERROR(("poll failed, errno: %d", MG_SOCK_ERRNO)); - return; - } - - i = 0; - for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next, i++) { - if (c->is_closing || c->is_resolving || FD(c) == INVALID_SOCKET) { - // Socket not valid, ignore - } else { - c->is_readable = (unsigned)(fds[i].revents & POLLIN ? true : false); - c->is_writable = (unsigned)(fds[i].revents & POLLOUT ? true : false); - fds[i].revents = 0; + } else { + i = 0; + for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next, i++) { + if (c->is_closing || c->is_resolving || FD(c) == INVALID_SOCKET) { + // Socket not valid, ignore + } else { + c->is_readable = (unsigned) (fds[i].revents & POLLIN ? true : false); + c->is_writable = (unsigned) (fds[i].revents & POLLOUT ? true : false); + fds[i].revents = 0; + if (mg_tls_pending(c) > 0) c->is_readable = 1; + } } } #else diff --git a/mongoose.h b/mongoose.h index 52ceef7b..b6d7fb43 100644 --- a/mongoose.h +++ b/mongoose.h @@ -418,6 +418,10 @@ extern int SockSet(SOCKET hSock, int Type, int Prop, void *pbuf, int size); #define _DARWIN_UNLIMITED_SELECT 1 // No limit on file descriptors +#if !defined(MG_ENABLE_POLL) && defined(__linux__) +#define MG_ENABLE_POLL 1 +#endif + #include #include #include @@ -436,7 +440,7 @@ extern int SockSet(SOCKET hSock, int Type, int Prop, void *pbuf, int size); #include #include #include -#if defined(MG_ENABLE_POLL) && MG_ENABLE_POLL +#if MG_ENABLE_POLL #include #else #include diff --git a/src/arch_unix.h b/src/arch_unix.h index 424257dc..4812c794 100644 --- a/src/arch_unix.h +++ b/src/arch_unix.h @@ -4,6 +4,10 @@ #define _DARWIN_UNLIMITED_SELECT 1 // No limit on file descriptors +#if !defined(MG_ENABLE_POLL) && defined(__linux__) +#define MG_ENABLE_POLL 1 +#endif + #include #include #include @@ -22,7 +26,7 @@ #include #include #include -#if defined(MG_ENABLE_POLL) && MG_ENABLE_POLL +#if MG_ENABLE_POLL #include #else #include diff --git a/src/sock.c b/src/sock.c index 7c8e6d10..408deed5 100644 --- a/src/sock.c +++ b/src/sock.c @@ -478,7 +478,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) { #elif MG_ENABLE_POLL size_t i = 0, n = 0; for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) n++; - struct pollfd fds[n == 0 ? 1 : n]; // Avoid zero-length VLA + struct pollfd fds[n == 0 ? 1 : n]; // Avoid zero-length VLA memset(fds, 0, sizeof(fds)); for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next, i++) { @@ -488,24 +488,25 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) { fds[i].fd = FD(c); fds[i].events |= POLLIN; if (c->is_connecting || (c->send.len > 0 && c->is_tls_hs == 0)) { - fds[i].events |= POLLOUT; + fds[i].events |= POLLOUT; } + if (mg_tls_pending(c) > 0) ms = 0; // Don't wait if TLS is ready } } if (poll(fds, n, ms) < 0) { MG_ERROR(("poll failed, errno: %d", MG_SOCK_ERRNO)); - return; - } - - i = 0; - for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next, i++) { - if (c->is_closing || c->is_resolving || FD(c) == INVALID_SOCKET) { - // Socket not valid, ignore - } else { - c->is_readable = (unsigned)(fds[i].revents & POLLIN ? true : false); - c->is_writable = (unsigned)(fds[i].revents & POLLOUT ? true : false); - fds[i].revents = 0; + } else { + i = 0; + for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next, i++) { + if (c->is_closing || c->is_resolving || FD(c) == INVALID_SOCKET) { + // Socket not valid, ignore + } else { + c->is_readable = (unsigned) (fds[i].revents & POLLIN ? true : false); + c->is_writable = (unsigned) (fds[i].revents & POLLOUT ? true : false); + fds[i].revents = 0; + if (mg_tls_pending(c) > 0) c->is_readable = 1; + } } } #else