Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 authored and mikee47 committed Mar 25, 2024
1 parent 64642c5 commit a764621
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,10 @@ using WebsocketDelegate = Delegate<void(WebsocketConnection&)>;
using WebsocketMessageDelegate = Delegate<void(WebsocketConnection&, const String&)>;
using WebsocketBinaryDelegate = Delegate<void(WebsocketConnection&, uint8_t* data, size_t size)>;

/**
* @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;

Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -297,29 +297,29 @@ 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;
static const ws_parser_callbacks_t parserSettings;

static WebsocketList websocketList;

bool isClientConnection = true;
bool isClientConnection{true};

HttpConnection* connection = nullptr;
bool activated = false;
HttpConnection* connection{nullptr};
bool activated{false};
};

/** @} */
19 changes: 7 additions & 12 deletions Sming/Components/Network/src/Network/WebsocketClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand All @@ -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.
}

Expand All @@ -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();

Expand Down

0 comments on commit a764621

Please sign in to comment.