diff --git a/httplib.h b/httplib.h index 96f73b8..6bb6775 100644 --- a/httplib.h +++ b/httplib.h @@ -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 #include #include -#ifndef CPPHTTPLIB_USE_SELECT +#ifdef CPPHTTPLIB_USE_POLL #include #endif #include @@ -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(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(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_SELECT +#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 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(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 }