Skip to content

Commit 8d225af

Browse files
authored
Remove macOS select() fallback (#2365)
* Remove macOS select() fallback macOS has supported `poll` for a long time now, so there's no need for the specific `select` code paths. With this commit, we can successfully build on visionOS. Signed-off-by: Adrien Gallouët <adrien@gallouet.fr> * Fix coding style Signed-off-by: Adrien Gallouët <adrien@gallouet.fr> --------- Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
1 parent ed5c5d3 commit 8d225af

File tree

1 file changed

+7
-65
lines changed

1 file changed

+7
-65
lines changed

httplib.h

Lines changed: 7 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4600,78 +4600,32 @@ inline int poll_wrapper(struct pollfd *fds, nfds_t nfds, int timeout) {
46004600
#endif
46014601
}
46024602

4603-
template <bool Read>
4604-
inline ssize_t select_impl(socket_t sock, time_t sec, time_t usec) {
4605-
#ifdef __APPLE__
4606-
if (sock >= FD_SETSIZE) { return -1; }
4607-
4608-
fd_set fds, *rfds, *wfds;
4609-
FD_ZERO(&fds);
4610-
FD_SET(sock, &fds);
4611-
rfds = (Read ? &fds : nullptr);
4612-
wfds = (Read ? nullptr : &fds);
4613-
4614-
timeval tv;
4615-
tv.tv_sec = static_cast<long>(sec);
4616-
tv.tv_usec = static_cast<decltype(tv.tv_usec)>(usec);
4617-
4618-
return handle_EINTR([&]() {
4619-
return select(static_cast<int>(sock + 1), rfds, wfds, nullptr, &tv);
4620-
});
4621-
#else
4603+
inline ssize_t select_impl(socket_t sock, short events, time_t sec,
4604+
time_t usec) {
46224605
struct pollfd pfd;
46234606
pfd.fd = sock;
4624-
pfd.events = (Read ? POLLIN : POLLOUT);
4607+
pfd.events = events;
4608+
pfd.revents = 0;
46254609

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

46284612
return handle_EINTR([&]() { return poll_wrapper(&pfd, 1, timeout); });
4629-
#endif
46304613
}
46314614

46324615
inline ssize_t select_read(socket_t sock, time_t sec, time_t usec) {
4633-
return select_impl<true>(sock, sec, usec);
4616+
return select_impl(sock, POLLIN, sec, usec);
46344617
}
46354618

46364619
inline ssize_t select_write(socket_t sock, time_t sec, time_t usec) {
4637-
return select_impl<false>(sock, sec, usec);
4620+
return select_impl(sock, POLLOUT, sec, usec);
46384621
}
46394622

46404623
inline Error wait_until_socket_is_ready(socket_t sock, time_t sec,
46414624
time_t usec) {
4642-
#ifdef __APPLE__
4643-
if (sock >= FD_SETSIZE) { return Error::Connection; }
4644-
4645-
fd_set fdsr, fdsw;
4646-
FD_ZERO(&fdsr);
4647-
FD_ZERO(&fdsw);
4648-
FD_SET(sock, &fdsr);
4649-
FD_SET(sock, &fdsw);
4650-
4651-
timeval tv;
4652-
tv.tv_sec = static_cast<long>(sec);
4653-
tv.tv_usec = static_cast<decltype(tv.tv_usec)>(usec);
4654-
4655-
auto ret = handle_EINTR([&]() {
4656-
return select(static_cast<int>(sock + 1), &fdsr, &fdsw, nullptr, &tv);
4657-
});
4658-
4659-
if (ret == 0) { return Error::ConnectionTimeout; }
4660-
4661-
if (ret > 0 && (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) {
4662-
auto error = 0;
4663-
socklen_t len = sizeof(error);
4664-
auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR,
4665-
reinterpret_cast<char *>(&error), &len);
4666-
auto successful = res >= 0 && !error;
4667-
return successful ? Error::Success : Error::Connection;
4668-
}
4669-
4670-
return Error::Connection;
4671-
#else
46724625
struct pollfd pfd_read;
46734626
pfd_read.fd = sock;
46744627
pfd_read.events = POLLIN | POLLOUT;
4628+
pfd_read.revents = 0;
46754629

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

@@ -4690,7 +4644,6 @@ inline Error wait_until_socket_is_ready(socket_t sock, time_t sec,
46904644
}
46914645

46924646
return Error::Connection;
4693-
#endif
46944647
}
46954648

46964649
inline bool is_socket_alive(socket_t sock) {
@@ -10474,17 +10427,6 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
1047410427
res.version = "HTTP/1.1";
1047510428
res.headers = default_headers_;
1047610429

10477-
#ifdef __APPLE__
10478-
// Socket file descriptor exceeded FD_SETSIZE...
10479-
if (strm.socket() >= FD_SETSIZE) {
10480-
Headers dummy;
10481-
detail::read_headers(strm, dummy);
10482-
res.status = StatusCode::InternalServerError_500;
10483-
output_error_log(Error::ExceedMaxSocketDescriptorCount, &req);
10484-
return write_response(strm, close_connection, req, res);
10485-
}
10486-
#endif
10487-
1048810430
// Request line and headers
1048910431
if (!parse_request_line(line_reader.ptr(), req)) {
1049010432
res.status = StatusCode::BadRequest_400;

0 commit comments

Comments
 (0)