From ceba8ae6205ae4babc95f37439c24dd51036a9f8 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Mon, 18 Mar 2019 22:00:08 -0700 Subject: [PATCH] fix bug with isReadyToWrite --- ixwebsocket/IXSelectInterruptPipe.cpp | 1 + ixwebsocket/IXSocket.cpp | 22 +++++++--------------- ixwebsocket/IXSocket.h | 3 ++- ixwebsocket/IXWebSocketTransport.cpp | 4 ++-- ws/test_ws.sh | 14 ++++++++++++-- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/ixwebsocket/IXSelectInterruptPipe.cpp b/ixwebsocket/IXSelectInterruptPipe.cpp index 4e53b97f..5444cbe3 100644 --- a/ixwebsocket/IXSelectInterruptPipe.cpp +++ b/ixwebsocket/IXSelectInterruptPipe.cpp @@ -122,6 +122,7 @@ namespace ix uint64_t value = 0; ::read(fd, &value, sizeof(value)); + return value; } diff --git a/ixwebsocket/IXSocket.cpp b/ixwebsocket/IXSocket.cpp index fa4f3230..780ce5a4 100644 --- a/ixwebsocket/IXSocket.cpp +++ b/ixwebsocket/IXSocket.cpp @@ -62,7 +62,6 @@ namespace ix FD_ZERO(&wfds); fd_set* fds = (readyToRead) ? &rfds : & wfds; - FD_SET(_sockfd, fds); // File descriptor used to interrupt select when needed @@ -76,9 +75,6 @@ namespace ix 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); @@ -95,7 +91,7 @@ namespace ix { pollResult = PollResultType_Timeout; } - else if (interruptFd != -1 && FD_ISSET(interruptFd, fds)) + else if (interruptFd != -1 && FD_ISSET(interruptFd, &rfds)) { uint64_t value = _selectInterrupt->read(); @@ -108,18 +104,14 @@ namespace ix pollResult = PollResultType_CloseRequest; } } - else if (sockfd != -1 && FD_ISSET(sockfd, fds)) + else if (sockfd != -1 && readyToRead && FD_ISSET(sockfd, &rfds)) { - if (readyToRead) - { - pollResult = PollResultType_ReadyForRead; - } - else - { - pollResult = PollResultType_ReadyForWrite; - } + pollResult = PollResultType_ReadyForRead; + } + else if (sockfd != -1 && !readyToRead && FD_ISSET(sockfd, &wfds)) + { + pollResult = PollResultType_ReadyForWrite; } - return pollResult; } diff --git a/ixwebsocket/IXSocket.h b/ixwebsocket/IXSocket.h index d7165e17..3df86292 100644 --- a/ixwebsocket/IXSocket.h +++ b/ixwebsocket/IXSocket.h @@ -50,7 +50,6 @@ namespace ix int timeoutSecs = kDefaultPollTimeout); bool wakeUpFromPoll(uint8_t wakeUpCode); - PollResultType select(bool readyToRead, int timeoutMs); PollResultType isReadyToWrite(int timeoutMs); PollResultType isReadyToRead(int timeoutMs); @@ -92,6 +91,8 @@ namespace ix std::mutex _socketMutex; private: + PollResultType select(bool readyToRead, int timeoutMs); + static const int kDefaultPollTimeout; static const int kDefaultPollNoTimeout; diff --git a/ixwebsocket/IXWebSocketTransport.cpp b/ixwebsocket/IXWebSocketTransport.cpp index df56a939..406b9b80 100644 --- a/ixwebsocket/IXWebSocketTransport.cpp +++ b/ixwebsocket/IXWebSocketTransport.cpp @@ -205,14 +205,14 @@ 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(10); + if (result == PollResultType_Error) { _socket->close(); setReadyState(CLOSED); break; } - // FIXME: why are we not getting PollResultType_ReadyForWrite ?? - else // if (result == PollResultType_ReadyForWrite) + else if (result == PollResultType_ReadyForWrite) { sendOnSocket(); } diff --git a/ws/test_ws.sh b/ws/test_ws.sh index 765b7061..587fd332 100644 --- a/ws/test_ws.sh +++ b/ws/test_ws.sh @@ -1,5 +1,15 @@ #!/bin/sh +# Handle Ctrl-C by killing all sub-processing AND exiting +trap cleanup INT + +function cleanup { + kill `cat /tmp/ws_test/pidfile.transfer` + kill `cat /tmp/ws_test/pidfile.receive` + kill `cat /tmp/ws_test/pidfile.send` + exit 1 +} + rm -rf /tmp/ws_test mkdir -p /tmp/ws_test @@ -21,11 +31,11 @@ done # Start a receiver mkdir -p /tmp/ws_test/receive cd /tmp/ws_test/receive -ws receive --delay 5 ws://127.0.0.1:8090 --pidfile /tmp/ws_test/pidfile.receive & +ws receive --delay 10 ws://127.0.0.1:8090 --pidfile /tmp/ws_test/pidfile.receive & mkdir /tmp/ws_test/send cd /tmp/ws_test/send -dd if=/dev/urandom of=20M_file count=10000 bs=1024 +dd if=/dev/urandom of=20M_file count=20000 bs=1024 # Start the sender job ws send --pidfile /tmp/ws_test/pidfile.send ws://127.0.0.1:8090 20M_file