From 6beecc0aa8f08fa4eb2c03a7511e8e01d737a5b8 Mon Sep 17 00:00:00 2001 From: Alexandre Konieczny Date: Fri, 26 Apr 2019 11:24:34 +0200 Subject: [PATCH] fixes, renaming, spaces, changed close timeout to 200ms --- ixwebsocket/IXWebSocket.cpp | 9 +++++++-- ixwebsocket/IXWebSocket.h | 1 + ixwebsocket/IXWebSocketTransport.cpp | 25 +++++++++++++++---------- ixwebsocket/IXWebSocketTransport.h | 3 ++- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/ixwebsocket/IXWebSocket.cpp b/ixwebsocket/IXWebSocket.cpp index a40892d1..5aa678e4 100644 --- a/ixwebsocket/IXWebSocket.cpp +++ b/ixwebsocket/IXWebSocket.cpp @@ -216,6 +216,11 @@ namespace ix return getReadyState() == WebSocket_ReadyState_Closing; } + bool WebSocket::isConnectedOrClosing() const + { + return isConnected() || isClosing(); + } + void WebSocket::close(uint16_t code, const std::string& reason) { _ws.close(code, reason); @@ -313,8 +318,8 @@ namespace ix // 4. In blocking mode, getting out of this function is triggered by // an explicit disconnection from the callback, or by the remote end - // closing the connection, ie isConnected() == false. - if (!_thread.joinable() && !(isConnected() || isClosing()) && !_automaticReconnection) return; + // closing the connection, ie isConnectedOrClosing() == false. + if (!_thread.joinable() && !isConnectedOrClosing() && !_automaticReconnection) return; } } diff --git a/ixwebsocket/IXWebSocket.h b/ixwebsocket/IXWebSocket.h index de4456b5..3b2e8b71 100644 --- a/ixwebsocket/IXWebSocket.h +++ b/ixwebsocket/IXWebSocket.h @@ -137,6 +137,7 @@ namespace ix bool isConnected() const; bool isClosing() const; + bool isConnectedOrClosing() const; void reconnectPerpetuallyIfDisconnected(); std::string readyStateToString(ReadyState readyState); static void invokeTrafficTrackerCallback(size_t size, bool incoming); diff --git a/ixwebsocket/IXWebSocketTransport.cpp b/ixwebsocket/IXWebSocketTransport.cpp index 1a73261e..4696745e 100644 --- a/ixwebsocket/IXWebSocketTransport.cpp +++ b/ixwebsocket/IXWebSocketTransport.cpp @@ -68,7 +68,7 @@ namespace ix const int WebSocketTransport::kDefaultPingIntervalSecs(-1); const int WebSocketTransport::kDefaultPingTimeoutSecs(-1); const bool WebSocketTransport::kDefaultEnablePong(true); - const int WebSocketTransport::kClosingMaximumWaitingDelayInMs(100); + const int WebSocketTransport::kClosingMaximumWaitingDelayInMs(200); constexpr size_t WebSocketTransport::kChunkSize; const uint16_t WebSocketTransport::kInternalErrorCode(1011); @@ -255,9 +255,9 @@ namespace ix WebSocketTransport::PollPostTreatment WebSocketTransport::poll() { // we need to have no timeout if state is CLOSING - int timeoutDelayinS = (_readyState == CLOSING) ? 0 : _pingIntervalOrTimeoutGCDSecs; + int timeoutDelaySecs = (_readyState == CLOSING) ? 0 : _pingIntervalOrTimeoutGCDSecs; - PollResultType pollResult = _socket->poll(timeoutDelayinS); + PollResultType pollResult = _socket->poll(timeoutDelaySecs); if (_readyState == OPEN) { @@ -338,6 +338,7 @@ namespace ix if (_readyState == CLOSING && closingDelayExceeded()) { + _rxbuf.clear(); // close code and reason were set when calling close() _socket->close(); setReadyState(CLOSED); @@ -405,7 +406,8 @@ namespace ix // | Payload Data continued ... | // +---------------------------------------------------------------+ // - void WebSocketTransport::dispatch(WebSocketTransport::PollPostTreatment pollPostTreatment, const OnMessageCallback& onMessageCallback) + void WebSocketTransport::dispatch(WebSocketTransport::PollPostTreatment pollPostTreatment, + const OnMessageCallback& onMessageCallback) { while (true) { @@ -564,7 +566,7 @@ namespace ix // We receive a CLOSE frame from remote and are NOT the ones who triggered the close if (_readyState != CLOSING) { - //send back the CLOSE frame + // send back the CLOSE frame sendCloseFrame(code, reason); _socket->wakeUpFromPoll(Socket::kCloseRequest); @@ -593,10 +595,15 @@ namespace ix } // if an abnormal closure was raised in poll, and nothing else triggered a CLOSED state in - // the received and processed data, then close uising abnormal close code and message - if (_readyState != CLOSED && _readyState != CLOSING && pollPostTreatment == CHECK_OR_RAISE_ABNORMAL_CLOSE_AFTER_DISPATCH) + // the received and processed data, then close using abnormal close code and message + if (pollPostTreatment == CHECK_OR_RAISE_ABNORMAL_CLOSE_AFTER_DISPATCH) { - closeSocketAndSwitchToClosedState(kAbnormalCloseCode, kAbnormalCloseMessage, 0, true); + _rxbuf.clear(); + + if (_readyState != CLOSED) + { + closeSocketAndSwitchToClosedState(kAbnormalCloseCode, kAbnormalCloseMessage, 0, true); + } } } @@ -912,7 +919,6 @@ namespace ix void WebSocketTransport::closeSocketAndSwitchToClosedState(uint16_t code, const std::string& reason, size_t closeWireSize, bool remote) { _socket->close(); - { std::lock_guard lock(_closeDataMutex); _closeCode = code; @@ -930,7 +936,6 @@ namespace ix if (_readyState == CLOSING || _readyState == CLOSED) return; sendCloseFrame(code, reason); - { std::lock_guard lock(_closeDataMutex); _closeCode = code; diff --git a/ixwebsocket/IXWebSocketTransport.h b/ixwebsocket/IXWebSocketTransport.h index 9b94949b..473f3f98 100644 --- a/ixwebsocket/IXWebSocketTransport.h +++ b/ixwebsocket/IXWebSocketTransport.h @@ -99,7 +99,8 @@ namespace ix ReadyStateValues getReadyState() const; void setReadyState(ReadyStateValues readyStateValue); void setOnCloseCallback(const OnCloseCallback& onCloseCallback); - void dispatch(PollPostTreatment pollPostTreatment, const OnMessageCallback& onMessageCallback); + void dispatch(PollPostTreatment pollPostTreatment, + const OnMessageCallback& onMessageCallback); size_t bufferedAmount() const; private: