@@ -101,7 +101,7 @@ typedef int ssize_t;
101
101
#endif // strcasecmp
102
102
103
103
typedef SOCKET socket_t ;
104
- #ifndef CPPHTTPLIB_USE_SELECT
104
+ #ifdef CPPHTTPLIB_USE_POLL
105
105
#define poll (fds, nfds, timeout ) WSAPoll(fds, nfds, timeout)
106
106
#endif
107
107
@@ -111,7 +111,7 @@ typedef SOCKET socket_t;
111
111
#include < cstring>
112
112
#include < netdb.h>
113
113
#include < netinet/in.h>
114
- #ifndef CPPHTTPLIB_USE_SELECT
114
+ #ifdef CPPHTTPLIB_USE_POLL
115
115
#include < poll.h>
116
116
#endif
117
117
#include < pthread.h>
@@ -1032,7 +1032,15 @@ inline int close_socket(socket_t sock) {
1032
1032
}
1033
1033
1034
1034
inline 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
1036
1044
fd_set fds;
1037
1045
FD_ZERO (&fds);
1038
1046
FD_SET (sock, &fds);
@@ -1042,19 +1050,26 @@ inline int select_read(socket_t sock, time_t sec, time_t usec) {
1042
1050
tv.tv_usec = static_cast <long >(usec);
1043
1051
1044
1052
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
1046
1058
struct pollfd pfd_read;
1047
1059
pfd_read.fd = sock;
1048
- pfd_read.events = POLLIN;
1060
+ pfd_read.events = POLLIN | POLLOUT ;
1049
1061
1050
1062
auto timeout = static_cast <int >(sec * 1000 + usec / 1000 );
1051
1063
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
1058
1073
fd_set fdsr;
1059
1074
FD_ZERO (&fdsr);
1060
1075
FD_SET (sock, &fdsr);
@@ -1074,21 +1089,6 @@ inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) {
1074
1089
!error;
1075
1090
}
1076
1091
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 ;
1092
1092
#endif
1093
1093
}
1094
1094
0 commit comments