Skip to content

Commit

Permalink
fix(QWebsocketpp): Fix crash caused by invalid state
Browse files Browse the repository at this point in the history
We not properly handled the websocketpp connected state. Add running
indicator and lock indicate that current status is connected.

Signed-off-by: Yibai Zhang <xm1994@gmail.com>
  • Loading branch information
summershrimp committed Jan 8, 2024
1 parent 3e8e12f commit b7a59c1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 13 deletions.
70 changes: 57 additions & 13 deletions src/util/QWebsocketpp/QWebsocketpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,11 @@ QWebsocketpp::QWebsocketpp(QObject *parent) : QObject(parent)

qint64 QWebsocketpp::sendBinaryMessage(const QByteArray &data)
{
std::lock_guard<std::mutex> lock(running_lck);
if (!running) {
return 0;
}

if (client_is_tls) {
wss_client->send(hdl, data.toStdString(),
websocketpp::frame::opcode::binary);
Expand All @@ -260,6 +262,7 @@ qint64 QWebsocketpp::sendBinaryMessage(const QByteArray &data)

qint64 QWebsocketpp::sendTextMessage(const QString &message)
{
std::lock_guard<std::mutex> lock(running_lck);
if (!running) {
return 0;
}
Expand All @@ -280,6 +283,7 @@ QString QWebsocketpp::errorString() const

void QWebsocketpp::close(unsigned short closeCode, const QString &reason)
{
std::lock_guard<std::mutex> lock(running_lck);
try {
if (client_is_tls) {
wss_client->close(hdl, closeCode, reason.toStdString());
Expand All @@ -289,6 +293,7 @@ void QWebsocketpp::close(unsigned short closeCode, const QString &reason)
} catch (websocketpp::exception const &e) {
qDebug() << e.what();
}
running = false;
}

void QWebsocketpp::ignoreSslErrors()
Expand All @@ -306,6 +311,7 @@ void QWebsocketpp::onMessage(websocketpp::connection_hdl thdl,
core_client::message_type::ptr msg)
{
(void)thdl;
std::lock_guard<std::mutex> lock(running_lck);
if (!running) {
return;
}
Expand All @@ -321,6 +327,13 @@ void QWebsocketpp::onMessage(websocketpp::connection_hdl thdl,
void QWebsocketpp::open(const QNetworkRequest &request)
{

{
std::lock_guard<std::mutex> lock(running_lck);
if (running) {
return;
}
}

if (request.url().toString().startsWith("ws://")) {
client_is_tls = 0;
} else if (request.url().toString().startsWith("wss://")) {
Expand All @@ -342,22 +355,39 @@ void QWebsocketpp::open(const QNetworkRequest &request)
wss_client->set_open_handler(
[this](websocketpp::connection_hdl hdl) {
(void)hdl;
{
std::lock_guard<std::mutex> lock(
running_lck);
running = true;
}
emit connected();
});

wss_client->set_fail_handler(
wss_client->set_fail_handler([this](websocketpp::connection_hdl
hdl) {
(void)hdl;
tls_client::connection_ptr con =
wss_client->get_con_from_hdl(hdl);
auto m_server = con->get_response_header("Server");
auto m_error_reason = con->get_ec().message();
qDebug() << m_server.c_str() << m_error_reason.c_str();
{
std::lock_guard<std::mutex> lock(running_lck);
running = false;
}
emit errorOccurred(QAbstractSocket::SocketError::
ConnectionRefusedError);
});

wss_client->set_close_handler(
[this](websocketpp::connection_hdl hdl) {
(void)hdl;
tls_client::connection_ptr con =
wss_client->get_con_from_hdl(hdl);
auto m_server =
con->get_response_header("Server");
auto m_error_reason = con->get_ec().message();
qDebug() << m_server.c_str()
<< m_error_reason.c_str();
emit errorOccurred(
QAbstractSocket::SocketError::
ConnectionRefusedError);
{
std::lock_guard<std::mutex> lock(
running_lck);
running = false;
}
emit disconnected();
});

websocketpp::lib::error_code ec;
Expand All @@ -383,6 +413,11 @@ void QWebsocketpp::open(const QNetworkRequest &request)
ws_client->set_open_handler(
[this](websocketpp::connection_hdl hdl) {
(void)hdl;
{
std::lock_guard<std::mutex> lock(
running_lck);
running = true;
}
emit connected();
});

Expand All @@ -401,6 +436,17 @@ void QWebsocketpp::open(const QNetworkRequest &request)
ConnectionRefusedError);
});

ws_client->set_close_handler(
[this](websocketpp::connection_hdl hdl) {
(void)hdl;
{
std::lock_guard<std::mutex> lock(
running_lck);
running = false;
}
emit disconnected();
});

websocketpp::lib::error_code ec;
no_tls_client::connection_ptr con = ws_client->get_connection(
request.url().toString().toStdString(), ec);
Expand All @@ -420,12 +466,10 @@ void QWebsocketpp::open(const QNetworkRequest &request)

run_thread = std::thread([this]() {
try {
running = true;
client_is_tls ? wss_client->run() : ws_client->run();
} catch (asio::system_error &e) {
qDebug() << "asio system_error:" << e.what();
}
running = false;
});
run_thread.detach();
}
1 change: 1 addition & 0 deletions src/util/QWebsocketpp/QWebsocketpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public slots:
websocketpp::connection_hdl hdl;
QString cacert_path;
bool running = false;
std::mutex running_lck;
};

#endif

0 comments on commit b7a59c1

Please sign in to comment.