Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: WaitOnSocket select error when sockfd above FD_SETSIZE #1410

Merged
merged 4 commits into from
May 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# include <io.h>
# include <winsock2.h>
#else
# include <poll.h>
# include <unistd.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)
taozhijiang marked this conversation as resolved.
Show resolved Hide resolved
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 = (0 != FD_ISSET(sockfd, &infd));
}
else
{
res = (0 != 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 = (0 != (fds[0].revents & POLLIN));
}
else
{
res = (0 != (fds[0].revents & POLLOUT));
}
}

#endif

return res;
}

Expand Down