Skip to content

Commit 8ee0417

Browse files
Wrap poll()/WSAPoll() in a function
Instead of using a macro for poll() on Windows, which breaks when the implementation is compiled separately, add a detail::poll_wrapper() function that dispatches to either ::poll() or ::WSAPoll().
1 parent 37399af commit 8ee0417

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

httplib.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ using ssize_t = long;
192192
#define WSA_FLAG_NO_HANDLE_INHERIT 0x80
193193
#endif
194194

195+
using nfds_t = unsigned long;
195196
using socket_t = SOCKET;
196197
using socklen_t = int;
197-
#define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout)
198198

199199
#else // not _WIN32
200200

@@ -3240,6 +3240,14 @@ inline ssize_t send_socket(socket_t sock, const void *ptr, size_t size,
32403240
});
32413241
}
32423242

3243+
inline int poll_wrapper(struct pollfd *fds, nfds_t nfds, int timeout) {
3244+
#ifdef _WIN32
3245+
return ::WSAPoll(fds, nfds, timeout);
3246+
#else
3247+
return ::poll(fds, nfds, timeout);
3248+
#endif
3249+
}
3250+
32433251
template <bool Read>
32443252
inline ssize_t select_impl(socket_t sock, time_t sec, time_t usec) {
32453253
struct pollfd pfd;
@@ -3248,7 +3256,7 @@ inline ssize_t select_impl(socket_t sock, time_t sec, time_t usec) {
32483256

32493257
auto timeout = static_cast<int>(sec * 1000 + usec / 1000);
32503258

3251-
return handle_EINTR([&]() { return poll(&pfd, 1, timeout); });
3259+
return handle_EINTR([&]() { return poll_wrapper(&pfd, 1, timeout); });
32523260
}
32533261

32543262
inline ssize_t select_read(socket_t sock, time_t sec, time_t usec) {
@@ -3267,7 +3275,8 @@ inline Error wait_until_socket_is_ready(socket_t sock, time_t sec,
32673275

32683276
auto timeout = static_cast<int>(sec * 1000 + usec / 1000);
32693277

3270-
auto poll_res = handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); });
3278+
auto poll_res =
3279+
handle_EINTR([&]() { return poll_wrapper(&pfd_read, 1, timeout); });
32713280

32723281
if (poll_res == 0) { return Error::ConnectionTimeout; }
32733282

@@ -10377,8 +10386,4 @@ inline SSL_CTX *Client::ssl_context() const {
1037710386

1037810387
} // namespace httplib
1037910388

10380-
#ifdef _WIN32
10381-
#undef poll
10382-
#endif
10383-
1038410389
#endif // CPPHTTPLIB_HTTPLIB_H

0 commit comments

Comments
 (0)