@@ -101,7 +101,7 @@ typedef int ssize_t;
101101#endif // strcasecmp
102102
103103typedef SOCKET socket_t ;
104- #ifndef CPPHTTPLIB_USE_SELECT
104+ #ifdef CPPHTTPLIB_USE_POLL
105105#define poll (fds, nfds, timeout ) WSAPoll(fds, nfds, timeout)
106106#endif
107107
@@ -111,7 +111,7 @@ typedef SOCKET socket_t;
111111#include < cstring>
112112#include < netdb.h>
113113#include < netinet/in.h>
114- #ifndef CPPHTTPLIB_USE_SELECT
114+ #ifdef CPPHTTPLIB_USE_POLL
115115#include < poll.h>
116116#endif
117117#include < pthread.h>
@@ -1032,7 +1032,15 @@ inline int close_socket(socket_t sock) {
10321032}
10331033
10341034inline int select_read (socket_t sock, time_t sec, time_t usec) {
1035- #ifdef CPPHTTPLIB_USE_SELECT
1035+ #ifdef CPPHTTPLIB_USE_POLL
1036+ struct pollfd pfd_read;
1037+ pfd_read.fd = sock;
1038+ pfd_read.events = POLLIN;
1039+
1040+ auto timeout = static_cast <int >(sec * 1000 + usec / 1000 );
1041+
1042+ return poll (&pfd_read, 1 , timeout);
1043+ #else
10361044 fd_set fds;
10371045 FD_ZERO (&fds);
10381046 FD_SET (sock, &fds);
@@ -1042,19 +1050,26 @@ inline int select_read(socket_t sock, time_t sec, time_t usec) {
10421050 tv.tv_usec = static_cast <long >(usec);
10431051
10441052 return select (static_cast <int >(sock + 1 ), &fds, nullptr , nullptr , &tv);
1045- #else
1053+ #endif
1054+ }
1055+
1056+ inline bool wait_until_socket_is_ready (socket_t sock, time_t sec, time_t usec) {
1057+ #ifdef CPPHTTPLIB_USE_POLL
10461058 struct pollfd pfd_read;
10471059 pfd_read.fd = sock;
1048- pfd_read.events = POLLIN;
1060+ pfd_read.events = POLLIN | POLLOUT ;
10491061
10501062 auto timeout = static_cast <int >(sec * 1000 + usec / 1000 );
10511063
1052- return poll (&pfd_read, 1 , timeout);
1053- #endif
1054- }
1055-
1056- inline bool wait_until_socket_is_ready (socket_t sock, time_t sec, time_t usec) {
1057- #ifdef CPPHTTPLIB_USE_SELECT
1064+ if (poll (&pfd_read, 1 , timeout) > 0 &&
1065+ pfd_read.revents & (POLLIN | POLLOUT)) {
1066+ int error = 0 ;
1067+ socklen_t len = sizeof (error);
1068+ return getsockopt (sock, SOL_SOCKET, SO_ERROR, reinterpret_cast <char *>(&error), &len) >= 0 &&
1069+ !error;
1070+ }
1071+ return false ;
1072+ #else
10581073 fd_set fdsr;
10591074 FD_ZERO (&fdsr);
10601075 FD_SET (sock, &fdsr);
@@ -1074,21 +1089,6 @@ inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) {
10741089 !error;
10751090 }
10761091 return false ;
1077- #else
1078- struct pollfd pfd_read;
1079- pfd_read.fd = sock;
1080- pfd_read.events = POLLIN | POLLOUT;
1081-
1082- auto timeout = static_cast <int >(sec * 1000 + usec / 1000 );
1083-
1084- if (poll (&pfd_read, 1 , timeout) > 0 &&
1085- pfd_read.revents & (POLLIN | POLLOUT)) {
1086- int error = 0 ;
1087- socklen_t len = sizeof (error);
1088- return getsockopt (sock, SOL_SOCKET, SO_ERROR, reinterpret_cast <char *>(&error), &len) >= 0 &&
1089- !error;
1090- }
1091- return false ;
10921092#endif
10931093}
10941094
0 commit comments