diff --git a/CMakeLists.txt b/CMakeLists.txt index 652324f5..7d4c63be 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,7 @@ set( IXWEBSOCKET_SOURCES ixwebsocket/IXSelectInterrupt.cpp ixwebsocket/IXSelectInterruptFactory.cpp ixwebsocket/IXConnectionState.cpp + ixwebsocket/IXWebSocketCloseConstants.cpp ) set( IXWEBSOCKET_HEADERS @@ -70,6 +71,7 @@ set( IXWEBSOCKET_HEADERS ixwebsocket/IXSelectInterrupt.h ixwebsocket/IXSelectInterruptFactory.h ixwebsocket/IXConnectionState.h + ixwebsocket/IXWebSocketCloseConstants.h ) if (UNIX) diff --git a/ixwebsocket/IXWebSocket.h b/ixwebsocket/IXWebSocket.h index 850e089c..331923ca 100644 --- a/ixwebsocket/IXWebSocket.h +++ b/ixwebsocket/IXWebSocket.h @@ -19,6 +19,7 @@ #include "IXWebSocketSendInfo.h" #include "IXWebSocketPerMessageDeflateOptions.h" #include "IXWebSocketHttpHeaders.h" +#include "IXWebSocketCloseConstants.h" #include "IXProgressCallback.h" namespace ix @@ -101,8 +102,8 @@ namespace ix void start(); // stop is synchronous - void stop(uint16_t code = 1000, - const std::string& reason = "Normal closure"); + void stop(uint16_t code = WebSocketCloseConstants::kNormalClosureCode, + const std::string& reason = WebSocketCloseConstants::kNormalClosureMessage); // Run in blocking mode, by connecting first manually, and then calling run. WebSocketInitResult connect(int timeoutSecs); diff --git a/ixwebsocket/IXWebSocketCloseConstants.cpp b/ixwebsocket/IXWebSocketCloseConstants.cpp new file mode 100644 index 00000000..85b69454 --- /dev/null +++ b/ixwebsocket/IXWebSocketCloseConstants.cpp @@ -0,0 +1,23 @@ +/* + * IXWebSocketCloseConstants.cpp + * Author: Benjamin Sergeant + * Copyright (c) 2019 Machine Zone, Inc. All rights reserved. + */ + +#include "IXWebSocketCloseConstants.h" + +namespace ix +{ + const uint16_t WebSocketCloseConstants::kNormalClosureCode(1000); + const uint16_t WebSocketCloseConstants::kInternalErrorCode(1011); + const uint16_t WebSocketCloseConstants::kAbnormalCloseCode(1006); + const uint16_t WebSocketCloseConstants::kProtocolErrorCode(1002); + const uint16_t WebSocketCloseConstants::kNoStatusCodeErrorCode(1005); + + const std::string WebSocketCloseConstants::kNormalClosureMessage("Normal closure"); + const std::string WebSocketCloseConstants::kInternalErrorMessage("Internal error"); + const std::string WebSocketCloseConstants::kAbnormalCloseMessage("Abnormal closure"); + const std::string WebSocketCloseConstants::kPingTimeoutMessage("Ping timeout"); + const std::string WebSocketCloseConstants::kProtocolErrorMessage("Protocol error"); + const std::string WebSocketCloseConstants::kNoStatusCodeErrorMessage("No status code"); +} diff --git a/ixwebsocket/IXWebSocketCloseConstants.h b/ixwebsocket/IXWebSocketCloseConstants.h new file mode 100644 index 00000000..81ac531b --- /dev/null +++ b/ixwebsocket/IXWebSocketCloseConstants.h @@ -0,0 +1,29 @@ +/* + * IXWebSocketCloseConstants.h + * Author: Benjamin Sergeant + * Copyright (c) 2019 Machine Zone, Inc. All rights reserved. + */ + +#pragma once + +#include +#include + +namespace ix +{ + struct WebSocketCloseConstants + { + static const uint16_t kNormalClosureCode; + static const uint16_t kInternalErrorCode; + static const uint16_t kAbnormalCloseCode; + static const uint16_t kProtocolErrorCode; + static const uint16_t kNoStatusCodeErrorCode; + + static const std::string kNormalClosureMessage; + static const std::string kInternalErrorMessage; + static const std::string kAbnormalCloseMessage; + static const std::string kPingTimeoutMessage; + static const std::string kProtocolErrorMessage; + static const std::string kNoStatusCodeErrorMessage; + }; +} diff --git a/ixwebsocket/IXWebSocketTransport.cpp b/ixwebsocket/IXWebSocketTransport.cpp index 758d2375..9741aae9 100644 --- a/ixwebsocket/IXWebSocketTransport.cpp +++ b/ixwebsocket/IXWebSocketTransport.cpp @@ -74,21 +74,11 @@ namespace ix const int WebSocketTransport::kClosingMaximumWaitingDelayInMs(200); constexpr size_t WebSocketTransport::kChunkSize; - const uint16_t WebSocketTransport::kInternalErrorCode(1011); - const uint16_t WebSocketTransport::kAbnormalCloseCode(1006); - const uint16_t WebSocketTransport::kProtocolErrorCode(1002); - const uint16_t WebSocketTransport::kNoStatusCodeErrorCode(1005); - const std::string WebSocketTransport::kInternalErrorMessage("Internal error"); - const std::string WebSocketTransport::kAbnormalCloseMessage("Abnormal closure"); - const std::string WebSocketTransport::kPingTimeoutMessage("Ping timeout"); - const std::string WebSocketTransport::kProtocolErrorMessage("Protocol error"); - const std::string WebSocketTransport::kNoStatusCodeErrorMessage("No status code"); - WebSocketTransport::WebSocketTransport() : _useMask(true), _readyState(ReadyState::CLOSED), - _closeCode(kInternalErrorCode), - _closeReason(kInternalErrorMessage), + _closeCode(WebSocketCloseConstants::kInternalErrorCode), + _closeReason(WebSocketCloseConstants::kInternalErrorMessage), _closeWireSize(0), _closeRemote(false), _enablePerMessageDeflate(false), @@ -221,8 +211,8 @@ namespace ix { std::lock_guard lock(_closeDataMutex); _onCloseCallback(_closeCode, _closeReason, _closeWireSize, _closeRemote); - _closeCode = kInternalErrorCode; - _closeReason = kInternalErrorMessage; + _closeCode = WebSocketCloseConstants::kInternalErrorCode; + _closeReason = WebSocketCloseConstants::kInternalErrorMessage; _closeWireSize = 0; _closeRemote = false; } @@ -292,7 +282,8 @@ namespace ix // ping response (PONG) exceeds the maximum delay, then close the connection if (pingTimeoutExceeded()) { - close(kInternalErrorCode, kPingTimeoutMessage); + close(WebSocketCloseConstants::kInternalErrorCode, + WebSocketCloseConstants::kPingTimeoutMessage); } // If ping is enabled and no ping has been sent for a duration // exceeding our ping interval, send a ping to the server. @@ -637,8 +628,8 @@ namespace ix else { // no close code received - code = kNoStatusCodeErrorCode; - reason = kNoStatusCodeErrorMessage; + code = WebSocketCloseConstants::kNoStatusCodeErrorCode; + reason = WebSocketCloseConstants::kNoStatusCodeErrorMessage; } // We receive a CLOSE frame from remote and are NOT the ones who triggered the close @@ -672,7 +663,9 @@ namespace ix else { // Unexpected frame type - close(kProtocolErrorCode, kProtocolErrorMessage, _rxbuf.size()); + close(WebSocketCloseConstants::kProtocolErrorCode, + WebSocketCloseConstants::kProtocolErrorMessage, + _rxbuf.size()); } // Erase the message that has been processed from the input/read buffer @@ -695,7 +688,9 @@ namespace ix // if we weren't closing, then close using abnormal close code and message else if (_readyState != ReadyState::CLOSED) { - closeSocketAndSwitchToClosedState(kAbnormalCloseCode, kAbnormalCloseMessage, 0, false); + closeSocketAndSwitchToClosedState(WebSocketCloseConstants::kAbnormalCloseCode, + WebSocketCloseConstants::kAbnormalCloseMessage, + 0, false); } } } @@ -1001,7 +996,7 @@ namespace ix bool compress = false; // if a status is set/was read - if (code != kNoStatusCodeErrorCode) + if (code != WebSocketCloseConstants::kNoStatusCodeErrorCode) { // See list of close events here: // https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent diff --git a/ixwebsocket/IXWebSocketTransport.h b/ixwebsocket/IXWebSocketTransport.h index 6aa1ac82..19dfafcf 100644 --- a/ixwebsocket/IXWebSocketTransport.h +++ b/ixwebsocket/IXWebSocketTransport.h @@ -25,6 +25,7 @@ #include "IXCancellationRequest.h" #include "IXWebSocketHandshake.h" #include "IXProgressCallback.h" +#include "IXWebSocketCloseConstants.h" namespace ix { @@ -91,8 +92,8 @@ namespace ix const OnProgressCallback& onProgressCallback); WebSocketSendInfo sendPing(const std::string& message); - void close(uint16_t code = 1000, - const std::string& reason = "Normal closure", + void close(uint16_t code = WebSocketCloseConstants::kNormalClosureCode, + const std::string& reason = WebSocketCloseConstants::kNormalClosureMessage, size_t closeWireSize = 0, bool remote = false);