From eb6ee52aaa0a08e54a7c76dc7d0db38f1e551065 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Mon, 18 Mar 2019 17:54:51 -0700 Subject: [PATCH] use milliseconds --- ixwebsocket/IXSocket.cpp | 25 ++++++++++++++----------- ixwebsocket/IXSocket.h | 6 +++--- ixwebsocket/IXWebSocketTransport.cpp | 2 +- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/ixwebsocket/IXSocket.cpp b/ixwebsocket/IXSocket.cpp index 7ca03bc8..fa4f3230 100644 --- a/ixwebsocket/IXSocket.cpp +++ b/ixwebsocket/IXSocket.cpp @@ -49,12 +49,12 @@ namespace ix return; } - PollResultType pollResult = isReadyToRead(timeoutSecs, 0); + PollResultType pollResult = isReadyToRead(1000 * timeoutSecs); 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 wfds; @@ -73,15 +73,18 @@ namespace ix } struct timeval timeout; - timeout.tv_sec = timeoutSecs; - timeout.tv_usec = 1000 * timeoutMs; + timeout.tv_sec = timeoutMs / 1000; + 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. int sockfd = _sockfd; int nfds = (std::max)(sockfd, interruptFd); int ret = ::select(nfds + 1, &rfds, &wfds, nullptr, - (timeoutSecs < 0) ? nullptr : &timeout); + (timeoutMs < 0) ? nullptr : &timeout); PollResultType pollResult = PollResultType_ReadyForRead; if (ret < 0) @@ -121,16 +124,16 @@ namespace ix return pollResult; } - PollResultType Socket::isReadyToRead(int timeoutSecs, int timeoutMs) + PollResultType Socket::isReadyToRead(int timeoutMs) { 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; - return select(readyToRead, timeoutSecs, timeoutMs); + return select(readyToRead, timeoutMs); } // 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. // This way we are not busy looping - if (isReadyToRead(0, 1) == PollResultType_Error) + if (isReadyToRead(1) == PollResultType_Error) { return false; } @@ -331,7 +334,7 @@ namespace ix // Wait with a 1ms timeout until the socket is ready to read. // 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()); } diff --git a/ixwebsocket/IXSocket.h b/ixwebsocket/IXSocket.h index e7bab660..d7165e17 100644 --- a/ixwebsocket/IXSocket.h +++ b/ixwebsocket/IXSocket.h @@ -50,9 +50,9 @@ namespace ix int timeoutSecs = kDefaultPollTimeout); bool wakeUpFromPoll(uint8_t wakeUpCode); - PollResultType select(bool readyToRead, int timeoutSecs, int timeoutMs); - PollResultType isReadyToWrite(int timeoutSecs, int timeoutMs); - PollResultType isReadyToRead(int timeoutSecs, int timeoutMs); + PollResultType select(bool readyToRead, int timeoutMs); + PollResultType isReadyToWrite(int timeoutMs); + PollResultType isReadyToRead(int timeoutMs); // Virtual methods virtual bool connect(const std::string& url, diff --git a/ixwebsocket/IXWebSocketTransport.cpp b/ixwebsocket/IXWebSocketTransport.cpp index 6f74cd07..e798c24b 100644 --- a/ixwebsocket/IXWebSocketTransport.cpp +++ b/ixwebsocket/IXWebSocketTransport.cpp @@ -204,7 +204,7 @@ namespace ix { // Wait with a 10ms timeout until the socket is ready to write. // This way we are not busy looping - PollResultType result = _socket->isReadyToWrite(0, 10); + PollResultType result = _socket->isReadyToWrite(10); if (result == PollResultType_Error) { _socket->close();