Preperations for websocket
This commit is contained in:
1 parent
aa073c2385
commit
dd07aaede6
12 files changed
+468
-20
No files matched your search
@@ -64,6 +64,14 @@ void ClientConnection::responseFinished(std::error_code ec)
|
||||
m_socket.close();
|
||||
}
|
||||
|
||||
void ClientConnection::upgradeWebsocket()
|
||||
{
|
||||
// ESP_LOGD(TAG, "state changed to RequestLine");
|
||||
m_state = State::WebSocket;
|
||||
|
||||
doReadWebSocket();
|
||||
}
|
||||
|
||||
void ClientConnection::doRead()
|
||||
{
|
||||
m_socket.async_read_some(asio::buffer(m_receiveBuffer, max_length),
|
||||
@@ -71,6 +79,13 @@ void ClientConnection::doRead()
|
||||
{ readyRead(ec, length); });
|
||||
}
|
||||
|
||||
void ClientConnection::doReadWebSocket()
|
||||
{
|
||||
m_socket.async_read_some(asio::buffer(m_receiveBuffer, max_length),
|
||||
[this, self=shared_from_this()](std::error_code ec, std::size_t length)
|
||||
{ readyReadWebSocket(ec, length); });
|
||||
}
|
||||
|
||||
void ClientConnection::readyRead(std::error_code ec, std::size_t length)
|
||||
{
|
||||
if (ec)
|
||||
@@ -119,7 +134,7 @@ requestFinished:
|
||||
}
|
||||
}
|
||||
|
||||
// ESP_LOGV(TAG, "received: %zd \"%.*s\"", length, length, m_data);
|
||||
// ESP_LOGV(TAG, "received: %zd \"%.*s\"", length, length, m_receiveBuffer);
|
||||
m_parsingBuffer.append(m_receiveBuffer, length);
|
||||
|
||||
bool shouldDoRead{true};
|
||||
@@ -145,6 +160,20 @@ requestFinished:
|
||||
doRead();
|
||||
}
|
||||
|
||||
void ClientConnection::readyReadWebSocket(std::error_code ec, std::size_t length)
|
||||
{
|
||||
if (ec)
|
||||
{
|
||||
ESP_LOGI(TAG, "error: %i %s (%s:%hi)", ec.value(), ec.message().c_str(),
|
||||
m_remote_endpoint.address().to_string().c_str(), m_remote_endpoint.port());
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGV(TAG, "received: %zd \"%.*s\"", length, length, m_receiveBuffer);
|
||||
|
||||
doReadWebSocket();
|
||||
}
|
||||
|
||||
bool ClientConnection::readyReadLine(std::string_view line)
|
||||
{
|
||||
switch (m_state)
|
||||
|
||||
@@ -27,10 +27,13 @@ public:
|
||||
|
||||
void start();
|
||||
void responseFinished(std::error_code ec);
|
||||
void upgradeWebsocket();
|
||||
|
||||
private:
|
||||
void doRead();
|
||||
void doReadWebSocket();
|
||||
void readyRead(std::error_code ec, std::size_t length);
|
||||
void readyReadWebSocket(std::error_code ec, std::size_t length);
|
||||
bool parseRequestLine(std::string_view line);
|
||||
bool readyReadLine(std::string_view line);
|
||||
bool parseRequestHeader(std::string_view line);
|
||||
@@ -39,12 +42,12 @@ private:
|
||||
asio::ip::tcp::socket m_socket;
|
||||
const asio::ip::tcp::endpoint m_remote_endpoint;
|
||||
|
||||
static constexpr const std::size_t max_length = 128;
|
||||
static constexpr const std::size_t max_length = 1024;
|
||||
char m_receiveBuffer[max_length];
|
||||
|
||||
std::string m_parsingBuffer;
|
||||
|
||||
enum class State { RequestLine, RequestHeaders, RequestBody, Response };
|
||||
enum class State { RequestLine, RequestHeaders, RequestBody, Response, WebSocket };
|
||||
State m_state { State::RequestLine };
|
||||
|
||||
std::size_t m_requestBodySize{};
|
||||
|
||||
Reference in new issue
Block a user