Changed back to select as default

This commit is contained in:
yhirose 2019-10-03 12:51:29 -04:00
parent 6d8302313c
commit 1f86e41d97

View file

@ -101,7 +101,7 @@ typedef int ssize_t;
#endif // strcasecmp
typedef SOCKET socket_t;
#ifndef CPPHTTPLIB_USE_SELECT
#ifdef CPPHTTPLIB_USE_POLL
#define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout)
#endif
@ -111,7 +111,7 @@ typedef SOCKET socket_t;
#include <cstring>
#include <netdb.h>
#include <netinet/in.h>
#ifndef CPPHTTPLIB_USE_SELECT
#ifdef CPPHTTPLIB_USE_POLL
#include <poll.h>
#endif
#include <pthread.h>
@ -1032,7 +1032,15 @@ inline int close_socket(socket_t sock) {
}
inline int select_read(socket_t sock, time_t sec, time_t usec) {
#ifdef CPPHTTPLIB_USE_SELECT
#ifdef CPPHTTPLIB_USE_POLL
struct pollfd pfd_read;
pfd_read.fd = sock;
pfd_read.events = POLLIN;
auto timeout = static_cast<int>(sec * 1000 + usec / 1000);
return poll(&pfd_read, 1, timeout);
#else
fd_set fds;
FD_ZERO(&fds);
FD_SET(sock, &fds);
@ -1042,19 +1050,26 @@ inline int select_read(socket_t sock, time_t sec, time_t usec) {
tv.tv_usec = static_cast<long>(usec);
return select(static_cast<int>(sock + 1), &fds, nullptr, nullptr, &tv);
#else
struct pollfd pfd_read;
pfd_read.fd = sock;
pfd_read.events = POLLIN;
auto timeout = static_cast<int>(sec * 1000 + usec / 1000);
return poll(&pfd_read, 1, timeout);
#endif
}
inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) {
#ifdef CPPHTTPLIB_USE_SELECT
#ifdef CPPHTTPLIB_USE_POLL
struct pollfd pfd_read;
pfd_read.fd = sock;
pfd_read.events = POLLIN | POLLOUT;
auto timeout = static_cast<int>(sec * 1000 + usec / 1000);
if (poll(&pfd_read, 1, timeout) > 0 &&
pfd_read.revents & (POLLIN | POLLOUT)) {
int error = 0;
socklen_t len = sizeof(error);
return getsockopt(sock, SOL_SOCKET, SO_ERROR, reinterpret_cast<char*>(&error), &len) >= 0 &&
!error;
}
return false;
#else
fd_set fdsr;
FD_ZERO(&fdsr);
FD_SET(sock, &fdsr);
@ -1074,21 +1089,6 @@ inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) {
!error;
}
return false;
#else
struct pollfd pfd_read;
pfd_read.fd = sock;
pfd_read.events = POLLIN | POLLOUT;
auto timeout = static_cast<int>(sec * 1000 + usec / 1000);
if (poll(&pfd_read, 1, timeout) > 0 &&
pfd_read.revents & (POLLIN | POLLOUT)) {
int error = 0;
socklen_t len = sizeof(error);
return getsockopt(sock, SOL_SOCKET, SO_ERROR, reinterpret_cast<char*>(&error), &len) >= 0 &&
!error;
}
return false;
#endif
}