use milliseconds
This commit is contained in:
parent
80e330d4c3
commit
eb6ee52aaa
@ -49,12 +49,12 @@ namespace ix
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
PollResultType pollResult = isReadyToRead(timeoutSecs, 0);
|
PollResultType pollResult = isReadyToRead(1000 * timeoutSecs);
|
||||||
|
|
||||||
if (onPollCallback) onPollCallback(pollResult);
|
if (onPollCallback) onPollCallback(pollResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
PollResultType Socket::select(bool readyToRead, int timeoutSecs, int timeoutMs)
|
PollResultType Socket::select(bool readyToRead, int timeoutMs)
|
||||||
{
|
{
|
||||||
fd_set rfds;
|
fd_set rfds;
|
||||||
fd_set wfds;
|
fd_set wfds;
|
||||||
@ -73,15 +73,18 @@ namespace ix
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct timeval timeout;
|
struct timeval timeout;
|
||||||
timeout.tv_sec = timeoutSecs;
|
timeout.tv_sec = timeoutMs / 1000;
|
||||||
timeout.tv_usec = 1000 * timeoutMs;
|
timeout.tv_usec = (timeoutMs < 1000) ? 0 : 1000 * (timeoutMs % 1000);
|
||||||
|
|
||||||
|
//std::cerr << "timeout.tv_sec = " << timeout.tv_sec << std::endl;
|
||||||
|
//std::cerr << "timeout.tv_usec = " << timeout.tv_sec << std::endl;
|
||||||
|
|
||||||
// Compute the highest fd.
|
// Compute the highest fd.
|
||||||
int sockfd = _sockfd;
|
int sockfd = _sockfd;
|
||||||
int nfds = (std::max)(sockfd, interruptFd);
|
int nfds = (std::max)(sockfd, interruptFd);
|
||||||
|
|
||||||
int ret = ::select(nfds + 1, &rfds, &wfds, nullptr,
|
int ret = ::select(nfds + 1, &rfds, &wfds, nullptr,
|
||||||
(timeoutSecs < 0) ? nullptr : &timeout);
|
(timeoutMs < 0) ? nullptr : &timeout);
|
||||||
|
|
||||||
PollResultType pollResult = PollResultType_ReadyForRead;
|
PollResultType pollResult = PollResultType_ReadyForRead;
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
@ -121,16 +124,16 @@ namespace ix
|
|||||||
return pollResult;
|
return pollResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
PollResultType Socket::isReadyToRead(int timeoutSecs, int timeoutMs)
|
PollResultType Socket::isReadyToRead(int timeoutMs)
|
||||||
{
|
{
|
||||||
bool readyToRead = true;
|
bool readyToRead = true;
|
||||||
return select(readyToRead, timeoutSecs, timeoutMs);
|
return select(readyToRead, timeoutMs);
|
||||||
}
|
}
|
||||||
|
|
||||||
PollResultType Socket::isReadyToWrite(int timeoutSecs, int timeoutMs)
|
PollResultType Socket::isReadyToWrite(int timeoutMs)
|
||||||
{
|
{
|
||||||
bool readyToRead = false;
|
bool readyToRead = false;
|
||||||
return select(readyToRead, timeoutSecs, timeoutMs);
|
return select(readyToRead, timeoutMs);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wake up from poll/select by writing to the pipe which is watched by select
|
// Wake up from poll/select by writing to the pipe which is watched by select
|
||||||
@ -262,7 +265,7 @@ namespace ix
|
|||||||
{
|
{
|
||||||
// Wait with a 1ms timeout until the socket is ready to read.
|
// Wait with a 1ms timeout until the socket is ready to read.
|
||||||
// This way we are not busy looping
|
// This way we are not busy looping
|
||||||
if (isReadyToRead(0, 1) == PollResultType_Error)
|
if (isReadyToRead(1) == PollResultType_Error)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -331,7 +334,7 @@ namespace ix
|
|||||||
|
|
||||||
// Wait with a 1ms timeout until the socket is ready to read.
|
// Wait with a 1ms timeout until the socket is ready to read.
|
||||||
// This way we are not busy looping
|
// This way we are not busy looping
|
||||||
if (isReadyToRead(0, 1) == PollResultType_Error)
|
if (isReadyToRead(1) == PollResultType_Error)
|
||||||
{
|
{
|
||||||
return std::make_pair(false, std::string());
|
return std::make_pair(false, std::string());
|
||||||
}
|
}
|
||||||
|
@ -50,9 +50,9 @@ namespace ix
|
|||||||
int timeoutSecs = kDefaultPollTimeout);
|
int timeoutSecs = kDefaultPollTimeout);
|
||||||
bool wakeUpFromPoll(uint8_t wakeUpCode);
|
bool wakeUpFromPoll(uint8_t wakeUpCode);
|
||||||
|
|
||||||
PollResultType select(bool readyToRead, int timeoutSecs, int timeoutMs);
|
PollResultType select(bool readyToRead, int timeoutMs);
|
||||||
PollResultType isReadyToWrite(int timeoutSecs, int timeoutMs);
|
PollResultType isReadyToWrite(int timeoutMs);
|
||||||
PollResultType isReadyToRead(int timeoutSecs, int timeoutMs);
|
PollResultType isReadyToRead(int timeoutMs);
|
||||||
|
|
||||||
// Virtual methods
|
// Virtual methods
|
||||||
virtual bool connect(const std::string& url,
|
virtual bool connect(const std::string& url,
|
||||||
|
@ -204,7 +204,7 @@ namespace ix
|
|||||||
{
|
{
|
||||||
// Wait with a 10ms timeout until the socket is ready to write.
|
// Wait with a 10ms timeout until the socket is ready to write.
|
||||||
// This way we are not busy looping
|
// This way we are not busy looping
|
||||||
PollResultType result = _socket->isReadyToWrite(0, 10);
|
PollResultType result = _socket->isReadyToWrite(10);
|
||||||
if (result == PollResultType_Error)
|
if (result == PollResultType_Error)
|
||||||
{
|
{
|
||||||
_socket->close();
|
_socket->close();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user