From f416cd6e847e28d7867614d912072a65e67e7cbe Mon Sep 17 00:00:00 2001 From: tobtoht Date: Wed, 1 Mar 2023 13:28:01 +0100 Subject: [PATCH] websocket: remove explicit disconnect --- src/SettingsDialog.cpp | 4 ++-- src/WindowManager.cpp | 15 +++++++------- src/dialog/DebugInfoDialog.cpp | 2 +- src/libwalletqt/Wallet.cpp | 2 -- src/utils/AppData.cpp | 4 ++-- src/utils/TorManager.cpp | 4 +--- src/utils/WebsocketClient.cpp | 35 +++++++++++++++------------------ src/utils/WebsocketClient.h | 2 +- src/utils/WebsocketNotifier.cpp | 2 +- src/utils/WebsocketNotifier.h | 2 +- src/utils/nodes.cpp | 4 +--- 11 files changed, 34 insertions(+), 42 deletions(-) diff --git a/src/SettingsDialog.cpp b/src/SettingsDialog.cpp index d97635c..227ad3b 100644 --- a/src/SettingsDialog.cpp +++ b/src/SettingsDialog.cpp @@ -381,9 +381,9 @@ void Settings::setSelection(int index) { void Settings::enableWebsocket(bool enabled) { if (enabled && !config()->get(Config::offlineMode).toBool() && !config()->get(Config::disableWebsocket).toBool()) { - websocketNotifier()->websocketClient.restart(); + websocketNotifier()->websocketClient->restart(); } else { - websocketNotifier()->websocketClient.stop(); + websocketNotifier()->websocketClient->stop(); } ui->nodeWidget->onWebsocketStatusChanged(); emit websocketStatusChanged(enabled); diff --git a/src/WindowManager.cpp b/src/WindowManager.cpp index 131f55d..610e9ae 100644 --- a/src/WindowManager.cpp +++ b/src/WindowManager.cpp @@ -22,8 +22,8 @@ WindowManager::WindowManager(QObject *parent, EventFilter *eventFilter) , eventFilter(eventFilter) { m_walletManager = WalletManager::instance(); - m_splashDialog = new SplashDialog; - m_cleanupThread = new QThread(); + m_splashDialog = new SplashDialog(); + m_cleanupThread = new QThread(this); connect(m_walletManager, &WalletManager::walletOpened, this, &WindowManager::onWalletOpened); connect(m_walletManager, &WalletManager::walletCreated, this, &WindowManager::onWalletCreated); @@ -79,9 +79,10 @@ void WindowManager::close() { } m_wizard->deleteLater(); + m_splashDialog->deleteLater(); + m_tray->deleteLater(); torManager()->stop(); - m_tray->hide(); QApplication::quit(); } @@ -612,10 +613,10 @@ void WindowManager::onProxySettingsChanged() { qWarning() << "Proxy: " << proxy.hostName() << " " << proxy.port(); // Switch websocket to new proxy and update URL - websocketNotifier()->websocketClient.stop(); - websocketNotifier()->websocketClient.webSocket.setProxy(proxy); - websocketNotifier()->websocketClient.nextWebsocketUrl(); - websocketNotifier()->websocketClient.restart(); + websocketNotifier()->websocketClient->stop(); + websocketNotifier()->websocketClient->webSocket->setProxy(proxy); + websocketNotifier()->websocketClient->nextWebsocketUrl(); + websocketNotifier()->websocketClient->restart(); emit proxySettingsChanged(); } diff --git a/src/dialog/DebugInfoDialog.cpp b/src/dialog/DebugInfoDialog.cpp index 05e8382..00c164b 100644 --- a/src/dialog/DebugInfoDialog.cpp +++ b/src/dialog/DebugInfoDialog.cpp @@ -60,7 +60,7 @@ void DebugInfoDialog::updateInfo() { auto node = m_nodes->connection(); ui->label_remoteNode->setText(node.toAddress()); ui->label_walletStatus->setText(this->statusToString(m_wallet->connectionStatus())); - QString websocketStatus = Utils::QtEnumToString(websocketNotifier()->websocketClient.webSocket.state()).remove("State"); + QString websocketStatus = Utils::QtEnumToString(websocketNotifier()->websocketClient->webSocket->state()).remove("State"); if (config()->get(Config::disableWebsocket).toBool()) { websocketStatus = "Disabled"; } diff --git a/src/libwalletqt/Wallet.cpp b/src/libwalletqt/Wallet.cpp index 2ed4e36..751d14e 100644 --- a/src/libwalletqt/Wallet.cpp +++ b/src/libwalletqt/Wallet.cpp @@ -444,8 +444,6 @@ void Wallet::onUpdated() { } void Wallet::onRefreshed(bool success, const QString &message) { - qDebug() << "onRefreshed"; - if (!success) { setConnectionStatus(ConnectionStatus_Disconnected); // Something went wrong during refresh, in some cases we need to notify the user diff --git a/src/utils/AppData.cpp b/src/utils/AppData.cpp index 8434e2f..44511e2 100644 --- a/src/utils/AppData.cpp +++ b/src/utils/AppData.cpp @@ -13,10 +13,10 @@ AppData::AppData(QObject *parent) auto genesis_timestamp = this->restoreHeights[NetworkType::Type::MAINNET]->data.firstKey(); this->txFiatHistory = new TxFiatHistory(genesis_timestamp, Config::defaultConfigDir().path(), this); - connect(&websocketNotifier()->websocketClient, &WebsocketClient::connectionEstablished, this->txFiatHistory, &TxFiatHistory::onUpdateDatabase); + connect(websocketNotifier()->websocketClient, &WebsocketClient::connectionEstablished, this->txFiatHistory, &TxFiatHistory::onUpdateDatabase); connect(this->txFiatHistory, &TxFiatHistory::requestYear, [](int year){ QByteArray data = QString(R"({"cmd": "txFiatHistory", "data": {"year": %1}})").arg(year).toUtf8(); - websocketNotifier()->websocketClient.sendMsg(data); + websocketNotifier()->websocketClient->sendMsg(data); }); connect(websocketNotifier(), &WebsocketNotifier::CryptoRatesReceived, &this->prices, &Prices::cryptoPricesReceived); diff --git a/src/utils/TorManager.cpp b/src/utils/TorManager.cpp index fe64009..75de7ea 100644 --- a/src/utils/TorManager.cpp +++ b/src/utils/TorManager.cpp @@ -316,6 +316,4 @@ TorManager* TorManager::instance() return m_instance; } -TorManager::~TorManager() { - qDebug() << "~TorManager"; -} +TorManager::~TorManager() = default; diff --git a/src/utils/WebsocketClient.cpp b/src/utils/WebsocketClient.cpp index 39142ed..d734806 100644 --- a/src/utils/WebsocketClient.cpp +++ b/src/utils/WebsocketClient.cpp @@ -10,18 +10,19 @@ WebsocketClient::WebsocketClient(QObject *parent) : QObject(parent) + , webSocket(new QWebSocket(QString(), QWebSocketProtocol::VersionLatest, this)) { - connect(&webSocket, &QWebSocket::stateChanged, this, &WebsocketClient::onStateChanged); - connect(&webSocket, &QWebSocket::connected, this, &WebsocketClient::onConnected); - connect(&webSocket, &QWebSocket::disconnected, this, &WebsocketClient::onDisconnected); - connect(&webSocket, QOverload::of(&QWebSocket::error), this, &WebsocketClient::onError); + connect(webSocket, &QWebSocket::stateChanged, this, &WebsocketClient::onStateChanged); + connect(webSocket, &QWebSocket::connected, this, &WebsocketClient::onConnected); + connect(webSocket, &QWebSocket::disconnected, this, &WebsocketClient::onDisconnected); + connect(webSocket, QOverload::of(&QWebSocket::error), this, &WebsocketClient::onError); - connect(&webSocket, &QWebSocket::binaryMessageReceived, this, &WebsocketClient::onbinaryMessageReceived); + connect(webSocket, &QWebSocket::binaryMessageReceived, this, &WebsocketClient::onbinaryMessageReceived); // Keep websocket connection alive connect(&m_pingTimer, &QTimer::timeout, [this]{ - if (webSocket.state() == QAbstractSocket::ConnectedState) { - webSocket.ping(); + if (webSocket->state() == QAbstractSocket::ConnectedState) { + webSocket->ping(); } }); m_pingTimer.setInterval(30 * 1000); @@ -34,8 +35,8 @@ WebsocketClient::WebsocketClient(QObject *parent) } void WebsocketClient::sendMsg(const QByteArray &data) { - if (webSocket.state() == QAbstractSocket::ConnectedState) { - webSocket.sendBinaryMessage(data); + if (webSocket->state() == QAbstractSocket::ConnectedState) { + webSocket->sendBinaryMessage(data); } } @@ -53,10 +54,10 @@ void WebsocketClient::start() { } // connect & reconnect on errors/close - auto state = webSocket.state(); + auto state = webSocket->state(); if (state != QAbstractSocket::ConnectedState && state != QAbstractSocket::ConnectingState) { qDebug() << "WebSocket connect:" << m_url.url(); - webSocket.open(m_url); + webSocket->open(m_url); } } @@ -67,7 +68,7 @@ void WebsocketClient::restart() { void WebsocketClient::stop() { m_stopped = true; - webSocket.close(); + webSocket->close(); m_connectionTimeout.stop(); } @@ -93,9 +94,9 @@ void WebsocketClient::onStateChanged(QAbstractSocket::SocketState state) { void WebsocketClient::onError(QAbstractSocket::SocketError error) { qCritical() << "WebSocket error: " << error; - auto state = webSocket.state(); + auto state = webSocket->state(); if (state == QAbstractSocket::ConnectedState || state == QAbstractSocket::ConnectingState) { - webSocket.abort(); + webSocket->abort(); } } @@ -143,8 +144,4 @@ void WebsocketClient::onbinaryMessageReceived(const QByteArray &message) { emit WSMessage(object); } -WebsocketClient::~WebsocketClient() { - // webSocket may fire QWebSocket::disconnected after WebsocketClient is destroyed - // explicitly disconnect to prevent crash - webSocket.disconnect(); -} \ No newline at end of file +WebsocketClient::~WebsocketClient() = default; \ No newline at end of file diff --git a/src/utils/WebsocketClient.h b/src/utils/WebsocketClient.h index 2368253..0f0833e 100644 --- a/src/utils/WebsocketClient.h +++ b/src/utils/WebsocketClient.h @@ -23,7 +23,7 @@ public: void sendMsg(const QByteArray &data); void nextWebsocketUrl(); - QWebSocket webSocket; + QWebSocket *webSocket; signals: void connectionEstablished(); diff --git a/src/utils/WebsocketNotifier.cpp b/src/utils/WebsocketNotifier.cpp index 750fc76..4cf4c0d 100644 --- a/src/utils/WebsocketNotifier.cpp +++ b/src/utils/WebsocketNotifier.cpp @@ -12,7 +12,7 @@ WebsocketNotifier::WebsocketNotifier(QObject *parent) : QObject(parent) , websocketClient(new WebsocketClient(this)) { - connect(&websocketClient, &WebsocketClient::WSMessage, this, &WebsocketNotifier::onWSMessage); + connect(websocketClient, &WebsocketClient::WSMessage, this, &WebsocketNotifier::onWSMessage); } QPointer WebsocketNotifier::m_instance(nullptr); diff --git a/src/utils/WebsocketNotifier.h b/src/utils/WebsocketNotifier.h index a1d6937..228f3f2 100644 --- a/src/utils/WebsocketNotifier.h +++ b/src/utils/WebsocketNotifier.h @@ -24,7 +24,7 @@ public: explicit WebsocketNotifier(QObject *parent); QMap heights; - WebsocketClient websocketClient; + WebsocketClient *websocketClient; static WebsocketNotifier* instance(); void emitCache(); diff --git a/src/utils/nodes.cpp b/src/utils/nodes.cpp index 3f9d7af..2ca9ea2 100644 --- a/src/utils/nodes.cpp +++ b/src/utils/nodes.cpp @@ -595,6 +595,4 @@ void Nodes::allowConnection() { m_allowConnection = true; } -Nodes::~Nodes() { - qDebug() << "~Nodes"; -} +Nodes::~Nodes() = default;