fixes, renaming, spaces, changed close timeout to 200ms

This commit is contained in:
Alexandre Konieczny 2019-04-26 11:24:34 +02:00
parent eee99ecfc9
commit 6beecc0aa8
4 changed files with 25 additions and 13 deletions

View File

@ -216,6 +216,11 @@ namespace ix
return getReadyState() == WebSocket_ReadyState_Closing; return getReadyState() == WebSocket_ReadyState_Closing;
} }
bool WebSocket::isConnectedOrClosing() const
{
return isConnected() || isClosing();
}
void WebSocket::close(uint16_t code, const std::string& reason) void WebSocket::close(uint16_t code, const std::string& reason)
{ {
_ws.close(code, reason); _ws.close(code, reason);
@ -313,8 +318,8 @@ namespace ix
// 4. In blocking mode, getting out of this function is triggered by // 4. In blocking mode, getting out of this function is triggered by
// an explicit disconnection from the callback, or by the remote end // an explicit disconnection from the callback, or by the remote end
// closing the connection, ie isConnected() == false. // closing the connection, ie isConnectedOrClosing() == false.
if (!_thread.joinable() && !(isConnected() || isClosing()) && !_automaticReconnection) return; if (!_thread.joinable() && !isConnectedOrClosing() && !_automaticReconnection) return;
} }
} }

View File

@ -137,6 +137,7 @@ namespace ix
bool isConnected() const; bool isConnected() const;
bool isClosing() const; bool isClosing() const;
bool isConnectedOrClosing() const;
void reconnectPerpetuallyIfDisconnected(); void reconnectPerpetuallyIfDisconnected();
std::string readyStateToString(ReadyState readyState); std::string readyStateToString(ReadyState readyState);
static void invokeTrafficTrackerCallback(size_t size, bool incoming); static void invokeTrafficTrackerCallback(size_t size, bool incoming);

View File

@ -68,7 +68,7 @@ namespace ix
const int WebSocketTransport::kDefaultPingIntervalSecs(-1); const int WebSocketTransport::kDefaultPingIntervalSecs(-1);
const int WebSocketTransport::kDefaultPingTimeoutSecs(-1); const int WebSocketTransport::kDefaultPingTimeoutSecs(-1);
const bool WebSocketTransport::kDefaultEnablePong(true); const bool WebSocketTransport::kDefaultEnablePong(true);
const int WebSocketTransport::kClosingMaximumWaitingDelayInMs(100); const int WebSocketTransport::kClosingMaximumWaitingDelayInMs(200);
constexpr size_t WebSocketTransport::kChunkSize; constexpr size_t WebSocketTransport::kChunkSize;
const uint16_t WebSocketTransport::kInternalErrorCode(1011); const uint16_t WebSocketTransport::kInternalErrorCode(1011);
@ -255,9 +255,9 @@ namespace ix
WebSocketTransport::PollPostTreatment WebSocketTransport::poll() WebSocketTransport::PollPostTreatment WebSocketTransport::poll()
{ {
// we need to have no timeout if state is CLOSING // 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) if (_readyState == OPEN)
{ {
@ -338,6 +338,7 @@ namespace ix
if (_readyState == CLOSING && closingDelayExceeded()) if (_readyState == CLOSING && closingDelayExceeded())
{ {
_rxbuf.clear();
// close code and reason were set when calling close() // close code and reason were set when calling close()
_socket->close(); _socket->close();
setReadyState(CLOSED); setReadyState(CLOSED);
@ -405,7 +406,8 @@ namespace ix
// | Payload Data continued ... | // | Payload Data continued ... |
// +---------------------------------------------------------------+ // +---------------------------------------------------------------+
// //
void WebSocketTransport::dispatch(WebSocketTransport::PollPostTreatment pollPostTreatment, const OnMessageCallback& onMessageCallback) void WebSocketTransport::dispatch(WebSocketTransport::PollPostTreatment pollPostTreatment,
const OnMessageCallback& onMessageCallback)
{ {
while (true) while (true)
{ {
@ -564,7 +566,7 @@ namespace ix
// We receive a CLOSE frame from remote and are NOT the ones who triggered the close // We receive a CLOSE frame from remote and are NOT the ones who triggered the close
if (_readyState != CLOSING) if (_readyState != CLOSING)
{ {
//send back the CLOSE frame // send back the CLOSE frame
sendCloseFrame(code, reason); sendCloseFrame(code, reason);
_socket->wakeUpFromPoll(Socket::kCloseRequest); _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 // 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 // the received and processed data, then close using abnormal close code and message
if (_readyState != CLOSED && _readyState != CLOSING && pollPostTreatment == CHECK_OR_RAISE_ABNORMAL_CLOSE_AFTER_DISPATCH) 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) void WebSocketTransport::closeSocketAndSwitchToClosedState(uint16_t code, const std::string& reason, size_t closeWireSize, bool remote)
{ {
_socket->close(); _socket->close();
{ {
std::lock_guard<std::mutex> lock(_closeDataMutex); std::lock_guard<std::mutex> lock(_closeDataMutex);
_closeCode = code; _closeCode = code;
@ -930,7 +936,6 @@ namespace ix
if (_readyState == CLOSING || _readyState == CLOSED) return; if (_readyState == CLOSING || _readyState == CLOSED) return;
sendCloseFrame(code, reason); sendCloseFrame(code, reason);
{ {
std::lock_guard<std::mutex> lock(_closeDataMutex); std::lock_guard<std::mutex> lock(_closeDataMutex);
_closeCode = code; _closeCode = code;

View File

@ -99,7 +99,8 @@ namespace ix
ReadyStateValues getReadyState() const; ReadyStateValues getReadyState() const;
void setReadyState(ReadyStateValues readyStateValue); void setReadyState(ReadyStateValues readyStateValue);
void setOnCloseCallback(const OnCloseCallback& onCloseCallback); void setOnCloseCallback(const OnCloseCallback& onCloseCallback);
void dispatch(PollPostTreatment pollPostTreatment, const OnMessageCallback& onMessageCallback); void dispatch(PollPostTreatment pollPostTreatment,
const OnMessageCallback& onMessageCallback);
size_t bufferedAmount() const; size_t bufferedAmount() const;
private: private: