rebase poll branch

This commit is contained in:
Benjamin Sergeant 2019-01-26 20:50:25 -08:00
parent f09434263c
commit 7a05a11014

View File

@ -15,6 +15,7 @@
#include <stdint.h> #include <stdint.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/types.h> #include <sys/types.h>
#include <poll.h>
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
@ -37,22 +38,21 @@ namespace ix
void Socket::poll(const OnPollCallback& onPollCallback, int timeoutSecs) void Socket::poll(const OnPollCallback& onPollCallback, int timeoutSecs)
{ {
fd_set rfds; if (_sockfd == -1)
FD_ZERO(&rfds); {
FD_SET(_sockfd, &rfds); onPollCallback(PollResultType_Error);
return;
}
#ifdef __linux__ #ifdef __linux__
FD_SET(_eventfd.getFd(), &rfds); FD_SET(_eventfd.getFd(), &rfds);
#endif #endif
struct pollfd fds[1];
fds[0].fd = _sockfd;
fds[0].events = POLLIN | POLLHUP | POLLERR;
struct timeval timeout; int timeout_msecs = timeoutSecs * 1000;
timeout.tv_sec = timeoutSecs; int ret = ::poll(fds, 1, timeout_msecs);
timeout.tv_usec = 0;
int sockfd = _sockfd;
int nfds = (std::max)(sockfd, _eventfd.getFd());
int ret = select(nfds + 1, &rfds, nullptr, nullptr,
(timeoutSecs == kDefaultPollNoTimeout) ? nullptr : &timeout);
PollResultType pollResult = PollResultType_ReadyForRead; PollResultType pollResult = PollResultType_ReadyForRead;
if (ret < 0) if (ret < 0)
@ -65,6 +65,7 @@ namespace ix
} }
onPollCallback(pollResult); onPollCallback(pollResult);
} }
void Socket::wakeUpFromPoll() void Socket::wakeUpFromPoll()
@ -92,6 +93,9 @@ namespace ix
if (_sockfd == -1) return; if (_sockfd == -1) return;
#if 1
::shutdown(_sockfd, SHUT_RDWR);
#endif
closeSocket(_sockfd); closeSocket(_sockfd);
_sockfd = -1; _sockfd = -1;
} }