Skip to content

Commit 2f39723

Browse files
Wrap poll()/WSAPoll() in a function and build compiled library on Windows (#2107)
* 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(). * Build compiled library on Windows
1 parent a9ba0a4 commit 2f39723

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

.github/workflows/test.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,17 @@ jobs:
9898
matrix:
9999
config:
100100
- with_ssl: false
101+
compiled: false
102+
run_tests: true
101103
name: without SSL
102104
- with_ssl: true
105+
compiled: false
106+
run_tests: true
103107
name: with SSL
108+
- with_ssl: false
109+
compiled: true
110+
run_tests: false
111+
name: compiled
104112
name: windows ${{ matrix.config.name }}
105113
steps:
106114
- name: Prepare Git for Checkout on Windows
@@ -128,12 +136,14 @@ jobs:
128136
-DCMAKE_BUILD_TYPE=Release
129137
-DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake
130138
-DHTTPLIB_TEST=ON
139+
-DHTTPLIB_COMPILE=${{ matrix.config.compiled && 'ON' || 'OFF' }}
131140
-DHTTPLIB_REQUIRE_ZLIB=ON
132141
-DHTTPLIB_REQUIRE_BROTLI=ON
133142
-DHTTPLIB_REQUIRE_OPENSSL=${{ matrix.config.with_ssl && 'ON' || 'OFF' }}
134143
- name: Build ${{ matrix.config.name }}
135144
run: cmake --build build --config Release -- /v:m /clp:ShowCommandLine
136145
- name: Run tests ${{ matrix.config.name }}
146+
if: ${{ matrix.config.run_tests }}
137147
run: ctest --output-on-failure --test-dir build -C Release
138148

139149
env:

httplib.h

+12-7
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

@@ -10367,8 +10376,4 @@ inline SSL_CTX *Client::ssl_context() const {
1036710376

1036810377
} // namespace httplib
1036910378

10370-
#ifdef _WIN32
10371-
#undef poll
10372-
#endif
10373-
1037410379
#endif // CPPHTTPLIB_HTTPLIB_H

0 commit comments

Comments
 (0)