From c02849e26998a9ce2acde7c1b05b3f189cfd0549 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 29 Sep 2019 19:43:22 -0400 Subject: [PATCH] Removed CPPHTTPLIB_USE_POLL, added CPPHTTPLIB_USE_SELECT --- httplib.h | 55 +++++++++++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/httplib.h b/httplib.h index 0a73125..b3441e7 100644 --- a/httplib.h +++ b/httplib.h @@ -21,7 +21,6 @@ #define CPPHTTPLIB_PAYLOAD_MAX_LENGTH (std::numeric_limits::max)() #define CPPHTTPLIB_RECV_BUFSIZ size_t(4096u) #define CPPHTTPLIB_THREAD_POOL_COUNT 8 -#define CPPHTTPLIB_USE_POLL #ifdef _WIN32 #ifndef _CRT_SECURE_NO_WARNINGS @@ -73,7 +72,7 @@ typedef int ssize_t; #endif // strcasecmp typedef SOCKET socket_t; -#ifdef CPPHTTPLIB_USE_POLL +#ifndef CPPHTTPLIB_USE_SELECT #define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout) #endif @@ -83,7 +82,7 @@ typedef SOCKET socket_t; #include #include #include -#ifdef CPPHTTPLIB_USE_POLL +#ifndef CPPHTTPLIB_USE_SELECT #include #endif #include @@ -1003,15 +1002,7 @@ inline int close_socket(socket_t sock) { } inline int select_read(socket_t sock, time_t sec, time_t usec) { -#ifdef CPPHTTPLIB_USE_POLL - struct pollfd pfd_read; - pfd_read.fd = sock; - pfd_read.events = POLLIN; - - auto timeout = static_cast(sec * 1000 + usec / 1000); - - return poll(&pfd_read, 1, timeout); -#else +#ifdef CPPHTTPLIB_USE_SELECT fd_set fds; FD_ZERO(&fds); FD_SET(sock, &fds); @@ -1021,26 +1012,19 @@ inline int select_read(socket_t sock, time_t sec, time_t usec) { tv.tv_usec = static_cast(usec); return select(static_cast(sock + 1), &fds, nullptr, nullptr, &tv); +#else + struct pollfd pfd_read; + pfd_read.fd = sock; + pfd_read.events = POLLIN; + + auto timeout = static_cast(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_POLL - struct pollfd pfd_read; - pfd_read.fd = sock; - pfd_read.events = POLLIN | POLLOUT; - - auto timeout = static_cast(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(&error), &len) >= 0 && - !error; - } - return false; -#else +#ifdef CPPHTTPLIB_USE_SELECT fd_set fdsr; FD_ZERO(&fdsr); FD_SET(sock, &fdsr); @@ -1060,6 +1044,21 @@ 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(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(&error), &len) >= 0 && + !error; + } + return false; #endif }