@@ -1198,7 +1198,22 @@ inline int close_socket(socket_t sock) {
11981198#endif
11991199}
12001200
1201- inline int select_read (socket_t sock, time_t sec, time_t usec) {
1201+ template <typename T>
1202+ inline ssize_t handle_EINTR (T fn) {
1203+ ssize_t res = false ;
1204+ while (true ) {
1205+ res = fn ();
1206+ if (res < 0 && errno == EINTR) {
1207+ continue ;
1208+ }
1209+ break ;
1210+ }
1211+ return res;
1212+ }
1213+
1214+ #define HANDLE_EINTR (method, ...) (handle_EINTR([&]() { return method (__VA_ARGS__); }))
1215+
1216+ inline ssize_t select_read (socket_t sock, time_t sec, time_t usec) {
12021217#ifdef CPPHTTPLIB_USE_POLL
12031218 struct pollfd pfd_read;
12041219 pfd_read.fd = sock;
@@ -1216,11 +1231,11 @@ inline int select_read(socket_t sock, time_t sec, time_t usec) {
12161231 tv.tv_sec = static_cast <long >(sec);
12171232 tv.tv_usec = static_cast <decltype (tv.tv_usec )>(usec);
12181233
1219- return select ( static_cast <int >(sock + 1 ), &fds, nullptr , nullptr , &tv);
1234+ return HANDLE_EINTR (select, static_cast <int >(sock + 1 ), &fds, nullptr , nullptr , &tv);
12201235#endif
12211236}
12221237
1223- inline int select_write (socket_t sock, time_t sec, time_t usec) {
1238+ inline ssize_t select_write (socket_t sock, time_t sec, time_t usec) {
12241239#ifdef CPPHTTPLIB_USE_POLL
12251240 struct pollfd pfd_read;
12261241 pfd_read.fd = sock;
@@ -1238,7 +1253,7 @@ inline int select_write(socket_t sock, time_t sec, time_t usec) {
12381253 tv.tv_sec = static_cast <long >(sec);
12391254 tv.tv_usec = static_cast <decltype (tv.tv_usec )>(usec);
12401255
1241- return select ( static_cast <int >(sock + 1 ), nullptr , &fds, nullptr , &tv);
1256+ return HANDLE_EINTR (select, static_cast <int >(sock + 1 ), nullptr , &fds, nullptr , &tv);
12421257#endif
12431258}
12441259
@@ -1250,7 +1265,7 @@ inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) {
12501265
12511266 auto timeout = static_cast <int >(sec * 1000 + usec / 1000 );
12521267
1253- int poll_res = HANDLE_EINTR (poll, &pfd_read, 1 , timeout);
1268+ auto poll_res = HANDLE_EINTR (poll, &pfd_read, 1 , timeout);
12541269 if (poll_res > 0 && pfd_read.revents & (POLLIN | POLLOUT)) {
12551270 int error = 0 ;
12561271 socklen_t len = sizeof (error);
@@ -1271,7 +1286,7 @@ inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) {
12711286 tv.tv_sec = static_cast <long >(sec);
12721287 tv.tv_usec = static_cast <decltype (tv.tv_usec )>(usec);
12731288
1274- if (select ( static_cast <int >(sock + 1 ), &fdsr, &fdsw, &fdse, &tv) > 0 &&
1289+ if (HANDLE_EINTR (select, static_cast <int >(sock + 1 ), &fdsr, &fdsw, &fdse, &tv) > 0 &&
12751290 (FD_ISSET (sock, &fdsr) || FD_ISSET (sock, &fdsw))) {
12761291 int error = 0 ;
12771292 socklen_t len = sizeof (error);
@@ -2617,21 +2632,6 @@ inline std::string SHA_512(const std::string &s) {
26172632}
26182633#endif
26192634
2620- template <typename T>
2621- inline ssize_t handle_EINTR (T fn) {
2622- ssize_t res = false ;
2623- while (true ) {
2624- res = fn ();
2625- if (res < 0 && errno == EINTR) {
2626- continue ;
2627- }
2628- break ;
2629- }
2630- return res;
2631- }
2632-
2633- #define HANDLE_EINTR (method, ...) (handle_EINTR([&]() { return method (__VA_ARGS__); }))
2634-
26352635#ifdef _WIN32
26362636class WSInit {
26372637public:
@@ -2977,7 +2977,7 @@ inline ssize_t SocketStream::write(const char *ptr, size_t size) {
29772977 }
29782978 return send (sock_, ptr, static_cast <int >(size), 0 );
29792979#else
2980- return send ( sock_, ptr, size, 0 );
2980+ return HANDLE_EINTR (send, sock_, ptr, size, 0 );
29812981#endif
29822982}
29832983
0 commit comments