websocket: remove explicit disconnect

This commit is contained in:
tobtoht 2023-03-01 13:28:01 +01:00
parent 103bc6408f
commit f416cd6e84
No known key found for this signature in database
GPG key ID: E45B10DD027D2472
11 changed files with 34 additions and 42 deletions

View file

@ -381,9 +381,9 @@ void Settings::setSelection(int index) {
void Settings::enableWebsocket(bool enabled) { void Settings::enableWebsocket(bool enabled) {
if (enabled && !config()->get(Config::offlineMode).toBool() && !config()->get(Config::disableWebsocket).toBool()) { if (enabled && !config()->get(Config::offlineMode).toBool() && !config()->get(Config::disableWebsocket).toBool()) {
websocketNotifier()->websocketClient.restart(); websocketNotifier()->websocketClient->restart();
} else { } else {
websocketNotifier()->websocketClient.stop(); websocketNotifier()->websocketClient->stop();
} }
ui->nodeWidget->onWebsocketStatusChanged(); ui->nodeWidget->onWebsocketStatusChanged();
emit websocketStatusChanged(enabled); emit websocketStatusChanged(enabled);

View file

@ -22,8 +22,8 @@ WindowManager::WindowManager(QObject *parent, EventFilter *eventFilter)
, eventFilter(eventFilter) , eventFilter(eventFilter)
{ {
m_walletManager = WalletManager::instance(); m_walletManager = WalletManager::instance();
m_splashDialog = new SplashDialog; m_splashDialog = new SplashDialog();
m_cleanupThread = new QThread(); m_cleanupThread = new QThread(this);
connect(m_walletManager, &WalletManager::walletOpened, this, &WindowManager::onWalletOpened); connect(m_walletManager, &WalletManager::walletOpened, this, &WindowManager::onWalletOpened);
connect(m_walletManager, &WalletManager::walletCreated, this, &WindowManager::onWalletCreated); connect(m_walletManager, &WalletManager::walletCreated, this, &WindowManager::onWalletCreated);
@ -79,9 +79,10 @@ void WindowManager::close() {
} }
m_wizard->deleteLater(); m_wizard->deleteLater();
m_splashDialog->deleteLater();
m_tray->deleteLater();
torManager()->stop(); torManager()->stop();
m_tray->hide();
QApplication::quit(); QApplication::quit();
} }
@ -612,10 +613,10 @@ void WindowManager::onProxySettingsChanged() {
qWarning() << "Proxy: " << proxy.hostName() << " " << proxy.port(); qWarning() << "Proxy: " << proxy.hostName() << " " << proxy.port();
// Switch websocket to new proxy and update URL // Switch websocket to new proxy and update URL
websocketNotifier()->websocketClient.stop(); websocketNotifier()->websocketClient->stop();
websocketNotifier()->websocketClient.webSocket.setProxy(proxy); websocketNotifier()->websocketClient->webSocket->setProxy(proxy);
websocketNotifier()->websocketClient.nextWebsocketUrl(); websocketNotifier()->websocketClient->nextWebsocketUrl();
websocketNotifier()->websocketClient.restart(); websocketNotifier()->websocketClient->restart();
emit proxySettingsChanged(); emit proxySettingsChanged();
} }

View file

@ -60,7 +60,7 @@ void DebugInfoDialog::updateInfo() {
auto node = m_nodes->connection(); auto node = m_nodes->connection();
ui->label_remoteNode->setText(node.toAddress()); ui->label_remoteNode->setText(node.toAddress());
ui->label_walletStatus->setText(this->statusToString(m_wallet->connectionStatus())); 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()) { if (config()->get(Config::disableWebsocket).toBool()) {
websocketStatus = "Disabled"; websocketStatus = "Disabled";
} }

View file

@ -444,8 +444,6 @@ void Wallet::onUpdated() {
} }
void Wallet::onRefreshed(bool success, const QString &message) { void Wallet::onRefreshed(bool success, const QString &message) {
qDebug() << "onRefreshed";
if (!success) { if (!success) {
setConnectionStatus(ConnectionStatus_Disconnected); setConnectionStatus(ConnectionStatus_Disconnected);
// Something went wrong during refresh, in some cases we need to notify the user // Something went wrong during refresh, in some cases we need to notify the user

View file

@ -13,10 +13,10 @@ AppData::AppData(QObject *parent)
auto genesis_timestamp = this->restoreHeights[NetworkType::Type::MAINNET]->data.firstKey(); auto genesis_timestamp = this->restoreHeights[NetworkType::Type::MAINNET]->data.firstKey();
this->txFiatHistory = new TxFiatHistory(genesis_timestamp, Config::defaultConfigDir().path(), this); 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){ connect(this->txFiatHistory, &TxFiatHistory::requestYear, [](int year){
QByteArray data = QString(R"({"cmd": "txFiatHistory", "data": {"year": %1}})").arg(year).toUtf8(); 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); connect(websocketNotifier(), &WebsocketNotifier::CryptoRatesReceived, &this->prices, &Prices::cryptoPricesReceived);

View file

@ -316,6 +316,4 @@ TorManager* TorManager::instance()
return m_instance; return m_instance;
} }
TorManager::~TorManager() { TorManager::~TorManager() = default;
qDebug() << "~TorManager";
}

View file

@ -10,18 +10,19 @@
WebsocketClient::WebsocketClient(QObject *parent) WebsocketClient::WebsocketClient(QObject *parent)
: QObject(parent) : QObject(parent)
, webSocket(new QWebSocket(QString(), QWebSocketProtocol::VersionLatest, this))
{ {
connect(&webSocket, &QWebSocket::stateChanged, this, &WebsocketClient::onStateChanged); connect(webSocket, &QWebSocket::stateChanged, this, &WebsocketClient::onStateChanged);
connect(&webSocket, &QWebSocket::connected, this, &WebsocketClient::onConnected); connect(webSocket, &QWebSocket::connected, this, &WebsocketClient::onConnected);
connect(&webSocket, &QWebSocket::disconnected, this, &WebsocketClient::onDisconnected); connect(webSocket, &QWebSocket::disconnected, this, &WebsocketClient::onDisconnected);
connect(&webSocket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error), this, &WebsocketClient::onError); connect(webSocket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error), this, &WebsocketClient::onError);
connect(&webSocket, &QWebSocket::binaryMessageReceived, this, &WebsocketClient::onbinaryMessageReceived); connect(webSocket, &QWebSocket::binaryMessageReceived, this, &WebsocketClient::onbinaryMessageReceived);
// Keep websocket connection alive // Keep websocket connection alive
connect(&m_pingTimer, &QTimer::timeout, [this]{ connect(&m_pingTimer, &QTimer::timeout, [this]{
if (webSocket.state() == QAbstractSocket::ConnectedState) { if (webSocket->state() == QAbstractSocket::ConnectedState) {
webSocket.ping(); webSocket->ping();
} }
}); });
m_pingTimer.setInterval(30 * 1000); m_pingTimer.setInterval(30 * 1000);
@ -34,8 +35,8 @@ WebsocketClient::WebsocketClient(QObject *parent)
} }
void WebsocketClient::sendMsg(const QByteArray &data) { void WebsocketClient::sendMsg(const QByteArray &data) {
if (webSocket.state() == QAbstractSocket::ConnectedState) { if (webSocket->state() == QAbstractSocket::ConnectedState) {
webSocket.sendBinaryMessage(data); webSocket->sendBinaryMessage(data);
} }
} }
@ -53,10 +54,10 @@ void WebsocketClient::start() {
} }
// connect & reconnect on errors/close // connect & reconnect on errors/close
auto state = webSocket.state(); auto state = webSocket->state();
if (state != QAbstractSocket::ConnectedState && state != QAbstractSocket::ConnectingState) { if (state != QAbstractSocket::ConnectedState && state != QAbstractSocket::ConnectingState) {
qDebug() << "WebSocket connect:" << m_url.url(); qDebug() << "WebSocket connect:" << m_url.url();
webSocket.open(m_url); webSocket->open(m_url);
} }
} }
@ -67,7 +68,7 @@ void WebsocketClient::restart() {
void WebsocketClient::stop() { void WebsocketClient::stop() {
m_stopped = true; m_stopped = true;
webSocket.close(); webSocket->close();
m_connectionTimeout.stop(); m_connectionTimeout.stop();
} }
@ -93,9 +94,9 @@ void WebsocketClient::onStateChanged(QAbstractSocket::SocketState state) {
void WebsocketClient::onError(QAbstractSocket::SocketError error) { void WebsocketClient::onError(QAbstractSocket::SocketError error) {
qCritical() << "WebSocket error: " << error; qCritical() << "WebSocket error: " << error;
auto state = webSocket.state(); auto state = webSocket->state();
if (state == QAbstractSocket::ConnectedState || state == QAbstractSocket::ConnectingState) { if (state == QAbstractSocket::ConnectedState || state == QAbstractSocket::ConnectingState) {
webSocket.abort(); webSocket->abort();
} }
} }
@ -143,8 +144,4 @@ void WebsocketClient::onbinaryMessageReceived(const QByteArray &message) {
emit WSMessage(object); emit WSMessage(object);
} }
WebsocketClient::~WebsocketClient() { WebsocketClient::~WebsocketClient() = default;
// webSocket may fire QWebSocket::disconnected after WebsocketClient is destroyed
// explicitly disconnect to prevent crash
webSocket.disconnect();
}

View file

@ -23,7 +23,7 @@ public:
void sendMsg(const QByteArray &data); void sendMsg(const QByteArray &data);
void nextWebsocketUrl(); void nextWebsocketUrl();
QWebSocket webSocket; QWebSocket *webSocket;
signals: signals:
void connectionEstablished(); void connectionEstablished();

View file

@ -12,7 +12,7 @@ WebsocketNotifier::WebsocketNotifier(QObject *parent)
: QObject(parent) : QObject(parent)
, websocketClient(new WebsocketClient(this)) , websocketClient(new WebsocketClient(this))
{ {
connect(&websocketClient, &WebsocketClient::WSMessage, this, &WebsocketNotifier::onWSMessage); connect(websocketClient, &WebsocketClient::WSMessage, this, &WebsocketNotifier::onWSMessage);
} }
QPointer<WebsocketNotifier> WebsocketNotifier::m_instance(nullptr); QPointer<WebsocketNotifier> WebsocketNotifier::m_instance(nullptr);

View file

@ -24,7 +24,7 @@ public:
explicit WebsocketNotifier(QObject *parent); explicit WebsocketNotifier(QObject *parent);
QMap<NetworkType::Type, int> heights; QMap<NetworkType::Type, int> heights;
WebsocketClient websocketClient; WebsocketClient *websocketClient;
static WebsocketNotifier* instance(); static WebsocketNotifier* instance();
void emitCache(); void emitCache();

View file

@ -595,6 +595,4 @@ void Nodes::allowConnection() {
m_allowConnection = true; m_allowConnection = true;
} }
Nodes::~Nodes() { Nodes::~Nodes() = default;
qDebug() << "~Nodes";
}