From a764621453b08f52a10c1c722fe335f0e7789292 Mon Sep 17 00:00:00 2001 From: mikee47 Date: Mon, 28 Feb 2022 16:07:25 +0000 Subject: [PATCH] Refactor --- .../Http/Websocket/WebsocketConnection.cpp | 22 ++++---- .../Http/Websocket/WebsocketConnection.h | 54 +++++++++---------- .../Network/src/Network/WebsocketClient.cpp | 19 +++---- 3 files changed, 46 insertions(+), 49 deletions(-) diff --git a/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.cpp b/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.cpp index 680f7b8b4c..df198c45e9 100644 --- a/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.cpp +++ b/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.cpp @@ -26,12 +26,14 @@ WebsocketList WebsocketConnection::websocketList; /** @brief ws_parser function table * @note stored in flash memory; as it is word-aligned it can be accessed directly */ -const ws_parser_callbacks_t WebsocketConnection::parserSettings PROGMEM = {.on_data_begin = staticOnDataBegin, - .on_data_payload = staticOnDataPayload, - .on_data_end = staticOnDataEnd, - .on_control_begin = staticOnControlBegin, - .on_control_payload = staticOnControlPayload, - .on_control_end = staticOnControlEnd}; +const ws_parser_callbacks_t WebsocketConnection::parserSettings PROGMEM = { + .on_data_begin = staticOnDataBegin, + .on_data_payload = staticOnDataPayload, + .on_data_end = staticOnDataEnd, + .on_control_begin = staticOnControlBegin, + .on_control_payload = staticOnControlPayload, + .on_control_end = staticOnControlEnd, +}; /** @brief Boilerplate code for ws_parser callbacks * @note Obtain connection object and check it @@ -57,7 +59,7 @@ bool WebsocketConnection::bind(HttpRequest& request, HttpResponse& response) return false; } - state = eWSCS_Open; + state = State::Open; String token = request.headers[HTTP_HEADER_SEC_WEBSOCKET_KEY]; token.trim(); token += WSSTR_SECRET; @@ -257,7 +259,7 @@ bool WebsocketConnection::send(IDataSourceStream* source, ws_frame_type_t type, packet[i++] = (available >> 24) & 0xFF; packet[i++] = (available >> 16) & 0xFF; packet[i++] = (available >> 8) & 0xFF; - packet[i++] = (available)&0xFF; + packet[i++] = available & 0xFF; } if(useMask) { @@ -296,8 +298,8 @@ void WebsocketConnection::close() { debug_d("Terminating Websocket connection."); websocketList.removeElement(this); - if(state != eWSCS_Closed) { - state = eWSCS_Closed; + if(state != State::Closed) { + state = State::Closed; if(isClientConnection) { send(nullptr, 0, WS_FRAME_CLOSE); } diff --git a/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.h b/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.h index 536b2d565b..5e3fc9c744 100644 --- a/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.h +++ b/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.h @@ -37,19 +37,10 @@ using WebsocketDelegate = Delegate; using WebsocketMessageDelegate = Delegate; using WebsocketBinaryDelegate = Delegate; -/** - * @brief Current state of Websocket connection - */ -enum WsConnectionState { - eWSCS_Ready, - eWSCS_Open, - eWSCS_Closed, -}; - struct WsFrameInfo { - ws_frame_type_t type = WS_FRAME_TEXT; - char* payload = nullptr; - size_t payloadLength = 0; + ws_frame_type_t type{WS_FRAME_TEXT}; + char* payload{nullptr}; + size_t payloadLength{0}; WsFrameInfo() = default; @@ -62,6 +53,15 @@ struct WsFrameInfo { class WebsocketConnection { public: + /** + * @brief Current state of Websocket connection + */ + enum class State { + Ready, + Open, + Closed, + }; + /** * @brief Constructs a websocket connection on top of http client or server connection * @param connection the transport connection @@ -271,10 +271,10 @@ class WebsocketConnection this->isClientConnection = isClientConnection; } - /** @brief Gets the state of the websocket connection - * @retval WsConnectionState - */ - WsConnectionState getState() + /** + * @brief Gets the state of the websocket connection + */ + State getState() const { return state; } @@ -297,18 +297,18 @@ class WebsocketConnection bool processFrame(TcpClient& client, char* at, int size); protected: - WebsocketDelegate wsConnect = nullptr; - WebsocketMessageDelegate wsMessage = nullptr; - WebsocketBinaryDelegate wsBinary = nullptr; - WebsocketDelegate wsPong = nullptr; - WebsocketDelegate wsDisconnect = nullptr; + WebsocketDelegate wsConnect; + WebsocketMessageDelegate wsMessage; + WebsocketBinaryDelegate wsBinary; + WebsocketDelegate wsPong; + WebsocketDelegate wsDisconnect; - void* userData = nullptr; + void* userData{nullptr}; - WsConnectionState state = eWSCS_Ready; + State state{}; private: - ws_frame_type_t frameType = WS_FRAME_TEXT; + ws_frame_type_t frameType{WS_FRAME_TEXT}; WsFrameInfo controlFrame; ws_parser_t parser; @@ -316,10 +316,10 @@ class WebsocketConnection static WebsocketList websocketList; - bool isClientConnection = true; + bool isClientConnection{true}; - HttpConnection* connection = nullptr; - bool activated = false; + HttpConnection* connection{nullptr}; + bool activated{false}; }; /** @} */ diff --git a/Sming/Components/Network/src/Network/WebsocketClient.cpp b/Sming/Components/Network/src/Network/WebsocketClient.cpp index 5d38d48cdc..e67dbdba7f 100644 --- a/Sming/Components/Network/src/Network/WebsocketClient.cpp +++ b/Sming/Components/Network/src/Network/WebsocketClient.cpp @@ -20,7 +20,7 @@ HttpConnection* WebsocketClient::getHttpConnection() { auto connection = WebsocketConnection::getConnection(); - if(connection == nullptr && state == eWSCS_Closed) { + if(connection == nullptr && state == State::Closed) { connection = new HttpClientConnection(); setConnection(connection); } @@ -45,16 +45,11 @@ bool WebsocketClient::connect(const Url& url) httpConnection->setSslInitHandler(sslInitHandler); httpConnection->connect(uri.Host, uri.getPort(), useSsl); - state = eWSCS_Ready; + state = State::Ready; // Generate the key - unsigned char keyStart[17] = {0}; - char b64Key[25]; - memset(b64Key, 0, sizeof(b64Key)); - - for(int i = 0; i < 16; ++i) { - keyStart[i] = 1 + os_random() % 255; - } + uint8_t keyStart[16]; + os_get_random(keyStart, sizeof(keyStart)); key = base64_encode(keyStart, sizeof(keyStart)); HttpRequest* request = new HttpRequest(uri); @@ -78,7 +73,7 @@ int WebsocketClient::verifyKey(HttpConnection& connection, HttpResponse& respons { if(!response.headers.contains(HTTP_HEADER_SEC_WEBSOCKET_ACCEPT)) { debug_e("[WS] Websocket Accept missing from headers"); - state = eWSCS_Closed; + state = State::Closed; return -2; // we don't have response. } @@ -89,14 +84,14 @@ int WebsocketClient::verifyKey(HttpConnection& connection, HttpResponse& respons String base64hash = base64_encode(hash.data(), hash.size()); if(base64hash != serverHashedKey) { debug_e("wscli key mismatch: %s | %s", serverHashedKey.c_str(), base64hash.c_str()); - state = eWSCS_Closed; + state = State::Closed; WebsocketConnection::getConnection()->setTimeOut(1); return -3; } response.headers.clear(); - state = eWSCS_Open; + state = State::Open; connection.setTimeOut(USHRT_MAX); activate();