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) {
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);

View file

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

View file

@ -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";
}

View file

@ -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

View file

@ -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);

View file

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

View file

@ -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<QAbstractSocket::SocketError>::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<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
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();
}
WebsocketClient::~WebsocketClient() = default;

View file

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

View file

@ -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> WebsocketNotifier::m_instance(nullptr);

View file

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

View file

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