Skip to content

Commit 2e7f080

Browse files
committed
Fix benchmark test problem on Windows
1 parent 318a3fe commit 2e7f080

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

httplib.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3283,8 +3283,14 @@ inline ssize_t select_write(socket_t sock, time_t sec, time_t usec) {
32833283

32843284
inline Error wait_until_socket_is_ready(socket_t sock, time_t sec,
32853285
time_t usec) {
3286+
#if defined(__APPLE__) || defined(_WIN32)
3287+
// Use select() on macOS and Windows.
3288+
// WSAPoll() on Windows has known performance issues with localhost
3289+
// connections. See: https://daniel.haxx.se/blog/2012/10/10/wsapoll-is-broken/
32863290
#ifdef __APPLE__
3291+
// On macOS, check if socket descriptor exceeds FD_SETSIZE
32873292
if (sock >= FD_SETSIZE) { return Error::Connection; }
3293+
#endif
32883294

32893295
fd_set fdsr, fdsw;
32903296
FD_ZERO(&fdsr);
@@ -3297,7 +3303,12 @@ inline Error wait_until_socket_is_ready(socket_t sock, time_t sec,
32973303
tv.tv_usec = static_cast<decltype(tv.tv_usec)>(usec);
32983304

32993305
auto ret = handle_EINTR([&]() {
3306+
#ifdef _WIN32
3307+
// On Windows, the first parameter of select() is ignored
3308+
return select(0, &fdsr, &fdsw, nullptr, &tv);
3309+
#else
33003310
return select(static_cast<int>(sock + 1), &fdsr, &fdsw, nullptr, &tv);
3311+
#endif
33013312
});
33023313

33033314
if (ret == 0) { return Error::ConnectionTimeout; }
@@ -3313,6 +3324,7 @@ inline Error wait_until_socket_is_ready(socket_t sock, time_t sec,
33133324

33143325
return Error::Connection;
33153326
#else
3327+
// Use poll() on Linux and other POSIX systems
33163328
struct pollfd pfd_read;
33173329
pfd_read.fd = sock;
33183330
pfd_read.events = POLLIN | POLLOUT;

0 commit comments

Comments
 (0)