mirror of
https://github.com/cesanta/mongoose.git
synced 2025-01-14 17:58:11 +08:00
Integrate #1561 with some minor tweaks - add MG_ENABLE_POLL for poll() support
This commit is contained in:
parent
f72b27d1c1
commit
5b448ec7a6
33
mongoose.c
33
mongoose.c
@ -3926,6 +3926,7 @@ struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr, const char *url,
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#if MG_ENABLE_SOCKET
|
#if MG_ENABLE_SOCKET
|
||||||
#if MG_ARCH == MG_ARCH_WIN32 && MG_ENABLE_WINSOCK
|
#if MG_ARCH == MG_ARCH_WIN32 && MG_ENABLE_WINSOCK
|
||||||
#define MG_SOCK_ERRNO WSAGetLastError()
|
#define MG_SOCK_ERRNO WSAGetLastError()
|
||||||
@ -4393,6 +4394,38 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
|
|||||||
FreeRTOS_FD_CLR(c->fd, mgr->ss,
|
FreeRTOS_FD_CLR(c->fd, mgr->ss,
|
||||||
eSELECT_READ | eSELECT_EXCEPT | eSELECT_WRITE);
|
eSELECT_READ | eSELECT_EXCEPT | eSELECT_WRITE);
|
||||||
}
|
}
|
||||||
|
#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
|
||||||
|
|
||||||
|
memset(fds, 0, sizeof(fds));
|
||||||
|
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next, i++) {
|
||||||
|
if (c->is_closing || c->is_resolving || FD(c) == INVALID_SOCKET) {
|
||||||
|
// No valid socket yet, ignore
|
||||||
|
} else {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (poll(fds, n, ms) < 0) {
|
||||||
|
MG_ERROR(("poll failed, errno: %d", MG_SOCK_ERRNO));
|
||||||
|
} 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) {
|
||||||
|
// No valid socket yet, 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
|
#else
|
||||||
struct timeval tv = {ms / 1000, (ms % 1000) * 1000}, tv_zero = {0, 0};
|
struct timeval tv = {ms / 1000, (ms % 1000) * 1000}, tv_zero = {0, 0};
|
||||||
struct mg_connection *c;
|
struct mg_connection *c;
|
||||||
|
@ -436,7 +436,11 @@ extern int SockSet(SOCKET hSock, int Type, int Prop, void *pbuf, int size);
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#if MG_ENABLE_POLL
|
||||||
|
#include <poll.h>
|
||||||
|
#else
|
||||||
#include <sys/select.h>
|
#include <sys/select.h>
|
||||||
|
#endif
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
@ -578,6 +582,10 @@ int sscanf(const char *, const char *, ...);
|
|||||||
#define MG_ENABLE_MIP 0
|
#define MG_ENABLE_MIP 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef MG_ENABLE_POLL
|
||||||
|
#define MG_ENABLE_POLL 0
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef MG_ENABLE_FATFS
|
#ifndef MG_ENABLE_FATFS
|
||||||
#define MG_ENABLE_FATFS 0
|
#define MG_ENABLE_FATFS 0
|
||||||
#endif
|
#endif
|
||||||
|
@ -22,7 +22,11 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#if MG_ENABLE_POLL
|
||||||
|
#include <poll.h>
|
||||||
|
#else
|
||||||
#include <sys/select.h>
|
#include <sys/select.h>
|
||||||
|
#endif
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
@ -4,6 +4,10 @@
|
|||||||
#define MG_ENABLE_MIP 0
|
#define MG_ENABLE_MIP 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef MG_ENABLE_POLL
|
||||||
|
#define MG_ENABLE_POLL 0
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef MG_ENABLE_FATFS
|
#ifndef MG_ENABLE_FATFS
|
||||||
#define MG_ENABLE_FATFS 0
|
#define MG_ENABLE_FATFS 0
|
||||||
#endif
|
#endif
|
||||||
|
33
src/sock.c
33
src/sock.c
@ -1,3 +1,4 @@
|
|||||||
|
#include "arch.h"
|
||||||
#include "dns.h"
|
#include "dns.h"
|
||||||
#include "event.h"
|
#include "event.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
@ -475,6 +476,38 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
|
|||||||
FreeRTOS_FD_CLR(c->fd, mgr->ss,
|
FreeRTOS_FD_CLR(c->fd, mgr->ss,
|
||||||
eSELECT_READ | eSELECT_EXCEPT | eSELECT_WRITE);
|
eSELECT_READ | eSELECT_EXCEPT | eSELECT_WRITE);
|
||||||
}
|
}
|
||||||
|
#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
|
||||||
|
|
||||||
|
memset(fds, 0, sizeof(fds));
|
||||||
|
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next, i++) {
|
||||||
|
if (c->is_closing || c->is_resolving || FD(c) == INVALID_SOCKET) {
|
||||||
|
// No valid socket yet, ignore
|
||||||
|
} else {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (poll(fds, n, ms) < 0) {
|
||||||
|
MG_ERROR(("poll failed, errno: %d", MG_SOCK_ERRNO));
|
||||||
|
} 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) {
|
||||||
|
// No valid socket yet, 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
|
#else
|
||||||
struct timeval tv = {ms / 1000, (ms % 1000) * 1000}, tv_zero = {0, 0};
|
struct timeval tv = {ms / 1000, (ms % 1000) * 1000}, tv_zero = {0, 0};
|
||||||
struct mg_connection *c;
|
struct mg_connection *c;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user