Fix poll support for win32

This commit is contained in:
Sergey Lyubka committed 2022-07-02 12:45:44 +01:00
1 parent 0947ce9234
commit dca43f430b
3 files changed
+42 -19

No files matched your search

+20 -9
View File
@@ -4366,7 +4366,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
eSELECT_READ | eSELECT_EXCEPT | eSELECT_WRITE); eSELECT_READ | eSELECT_EXCEPT | eSELECT_WRITE);
} }
#elif MG_ENABLE_POLL #elif MG_ENABLE_POLL
size_t n = 0; unsigned long n = 0;
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) n++; for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) n++;
struct pollfd *fds = (struct pollfd *) alloca(n * sizeof(fds[0])); struct pollfd *fds = (struct pollfd *) alloca(n * sizeof(fds[0]));
if (n > 0) memset(fds, 0, sizeof(n * sizeof(fds[0]))); if (n > 0) memset(fds, 0, sizeof(n * sizeof(fds[0])));
@@ -4375,28 +4375,39 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
c->is_readable = c->is_writable = 0; c->is_readable = c->is_writable = 0;
if (skip_iotest(c)) { if (skip_iotest(c)) {
// Socket not valid, ignore // Socket not valid, ignore
} else if (mg_tls_pending(c) > 0) {
ms = 1; // Don't wait if TLS is ready
} else { } else {
fds[n].fd = FD(c); fds[n].fd = FD(c);
fds[n].events = POLLERR; fds[n].events = POLLERR;
if (can_read(c)) fds[n].events |= POLLIN; if (can_read(c)) fds[n].events |= POLLIN;
if (can_write(c)) fds[n].events |= POLLOUT; if (can_write(c)) fds[n].events |= POLLOUT;
n++; n++;
if (mg_tls_pending(c) > 0) ms = 0; // Don't wait if TLS is ready
} }
} }
// MG_INFO(("poll n=%d ms=%d", (int) n, ms));
if (poll(fds, n, ms) < 0) { if (poll(fds, n, ms) < 0) {
MG_ERROR(("poll failed, errno: %d", MG_SOCK_ERRNO)); #if MG_ARCH == MG_ARCH_WIN32
} else { if (n == 0) Sleep(ms); // On Windows, poll fails if no sockets
n = 0; #endif
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) { if (n > 0) memset(fds, 0, sizeof(n * sizeof(fds[0])));
if (skip_iotest(c)) continue; }
n = 0;
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) {
if (skip_iotest(c)) {
// Socket not valid, ignore
} else if (mg_tls_pending(c) > 0) {
c->is_readable = 1;
} else {
if (fds[n].revents & POLLERR) { if (fds[n].revents & POLLERR) {
mg_error(c, "socket error"); mg_error(c, "socket error");
} else { } else {
c->is_readable = (unsigned) (fds[n].revents & POLLIN ? 1 : 0); c->is_readable =
(unsigned) (fds[n].revents & POLLIN || fds[n].revents & POLLHUP
? 1
: 0);
c->is_writable = (unsigned) (fds[n].revents & POLLOUT ? 1 : 0); c->is_writable = (unsigned) (fds[n].revents & POLLOUT ? 1 : 0);
if (mg_tls_pending(c) > 0) c->is_readable = 1;
} }
n++; n++;
} }
+20 -9
View File
@@ -518,7 +518,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
eSELECT_READ | eSELECT_EXCEPT | eSELECT_WRITE); eSELECT_READ | eSELECT_EXCEPT | eSELECT_WRITE);
} }
#elif MG_ENABLE_POLL #elif MG_ENABLE_POLL
size_t n = 0; unsigned long n = 0;
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) n++; for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) n++;
struct pollfd *fds = (struct pollfd *) alloca(n * sizeof(fds[0])); struct pollfd *fds = (struct pollfd *) alloca(n * sizeof(fds[0]));
if (n > 0) memset(fds, 0, sizeof(n * sizeof(fds[0]))); if (n > 0) memset(fds, 0, sizeof(n * sizeof(fds[0])));
@@ -527,28 +527,39 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
c->is_readable = c->is_writable = 0; c->is_readable = c->is_writable = 0;
if (skip_iotest(c)) { if (skip_iotest(c)) {
// Socket not valid, ignore // Socket not valid, ignore
} else if (mg_tls_pending(c) > 0) {
ms = 1; // Don't wait if TLS is ready
} else { } else {
fds[n].fd = FD(c); fds[n].fd = FD(c);
fds[n].events = POLLERR; fds[n].events = POLLERR;
if (can_read(c)) fds[n].events |= POLLIN; if (can_read(c)) fds[n].events |= POLLIN;
if (can_write(c)) fds[n].events |= POLLOUT; if (can_write(c)) fds[n].events |= POLLOUT;
n++; n++;
if (mg_tls_pending(c) > 0) ms = 0; // Don't wait if TLS is ready
} }
} }
// MG_INFO(("poll n=%d ms=%d", (int) n, ms));
if (poll(fds, n, ms) < 0) { if (poll(fds, n, ms) < 0) {
MG_ERROR(("poll failed, errno: %d", MG_SOCK_ERRNO)); #if MG_ARCH == MG_ARCH_WIN32
} else { if (n == 0) Sleep(ms); // On Windows, poll fails if no sockets
n = 0; #endif
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) { if (n > 0) memset(fds, 0, sizeof(n * sizeof(fds[0])));
if (skip_iotest(c)) continue; }
n = 0;
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) {
if (skip_iotest(c)) {
// Socket not valid, ignore
} else if (mg_tls_pending(c) > 0) {
c->is_readable = 1;
} else {
if (fds[n].revents & POLLERR) { if (fds[n].revents & POLLERR) {
mg_error(c, "socket error"); mg_error(c, "socket error");
} else { } else {
c->is_readable = (unsigned) (fds[n].revents & POLLIN ? 1 : 0); c->is_readable =
(unsigned) (fds[n].revents & POLLIN || fds[n].revents & POLLHUP
? 1
: 0);
c->is_writable = (unsigned) (fds[n].revents & POLLOUT ? 1 : 0); c->is_writable = (unsigned) (fds[n].revents & POLLOUT ? 1 : 0);
if (mg_tls_pending(c) > 0) c->is_readable = 1;
} }
n++; n++;
} }
+2 -1
View File
@@ -1022,7 +1022,8 @@ static void test_http_no_content_length(void) {
mg_mgr_init(&mgr); mg_mgr_init(&mgr);
mg_http_listen(&mgr, url, f4, (void *) buf1); mg_http_listen(&mgr, url, f4, (void *) buf1);
mg_http_connect(&mgr, url, f4c, (void *) buf2); mg_http_connect(&mgr, url, f4c, (void *) buf2);
for (i = 0; i < 100 && strchr(buf2, 'c') == NULL; i++) mg_mgr_poll(&mgr, 1); for (i = 0; i < 1000 && strchr(buf2, 'c') == NULL; i++) mg_mgr_poll(&mgr, 10);
MG_INFO(("[%s] [%s]", buf1, buf2));
ASSERT(strcmp(buf1, "mc") == 0); ASSERT(strcmp(buf1, "mc") == 0);
ASSERT(strcmp(buf2, "fcm") == 0); // See #1475 ASSERT(strcmp(buf2, "fcm") == 0); // See #1475
mg_mgr_free(&mgr); mg_mgr_free(&mgr);