Skip to content

Commit

Permalink
fix: select error when sockfd above FD_SETSIZE
Browse files Browse the repository at this point in the history
  • Loading branch information
ztao committed May 19, 2022
1 parent 63803d1 commit 3bbaaa1
Showing 1 changed file with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# include <winsock2.h>
#else
# include <unistd.h>
# include <poll.h>
#endif
#include <curl/curl.h>

Expand Down Expand Up @@ -443,13 +444,19 @@ class HttpOperation
* @param sockfd
* @param for_recv
* @param timeout_ms
* @return
* @return true if expected events occur, false if timeout or error happen
*/
static int WaitOnSocket(curl_socket_t sockfd, int for_recv, long timeout_ms)
static bool WaitOnSocket(curl_socket_t sockfd, int for_recv, long timeout_ms)
{
bool res = false;

#if defined(_WIN32)

if (sockfd > FD_SETSIZE)
return false;

struct timeval tv;
fd_set infd, outfd, errfd;
int res;

tv.tv_sec = timeout_ms / 1000;
tv.tv_usec = (timeout_ms % 1000) * 1000;
Expand All @@ -470,7 +477,47 @@ class HttpOperation
}

/* select() returns the number of signalled sockets or -1 */
res = select((int)sockfd + 1, &infd, &outfd, &errfd, &tv);
if (select((int)sockfd + 1, &infd, &outfd, &errfd, &tv) > 0)
{
if (for_recv)
{
res = FD_ISSET(sockfd, &infd);
}
else
{
res = FD_ISSET(sockfd, &outfd);
}
}

#else

struct pollfd fds[1];
::memset(fds, 0 , sizeof(fds));

fds[0].fd = sockfd;
if (for_recv)
{
fds[0].events = POLLIN;
}
else
{
fds[0].events = POLLOUT;
}

if (poll(fds, 1, timeout_ms) > 0)
{
if (for_recv)
{
res = fds[0].revents & POLLIN;
}
else
{
res = fds[0].revents & POLLOUT;
}
}

#endif

return res;
}

Expand Down

0 comments on commit 3bbaaa1

Please sign in to comment.