From baf4fdc67d04213bbfa607af889cb64f5edf3301 Mon Sep 17 00:00:00 2001 From: tobtoht Date: Thu, 14 Oct 2021 14:42:43 +0200 Subject: [PATCH] Remove duplicate code --- src/appcontext.h | 1 - src/utils/wsclient.cpp | 96 ------------------------------------------ src/utils/wsclient.h | 42 ------------------ 3 files changed, 139 deletions(-) delete mode 100644 src/utils/wsclient.cpp delete mode 100644 src/utils/wsclient.h diff --git a/src/appcontext.h b/src/appcontext.h index 46fd215..ac4db6f 100644 --- a/src/appcontext.h +++ b/src/appcontext.h @@ -9,7 +9,6 @@ #include "utils/os/whonix.h" #include "utils/networking.h" -#include "utils/wsclient.h" #include "utils/FeatherSeed.h" #include "utils/daemonrpc.h" #include "utils/RestoreHeightLookup.h" diff --git a/src/utils/wsclient.cpp b/src/utils/wsclient.cpp deleted file mode 100644 index d9977f3..0000000 --- a/src/utils/wsclient.cpp +++ /dev/null @@ -1,96 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// Copyright (c) 2020-2021, The Monero Project. - -#include -#include -#include "wsclient.h" -#include "utils/Utils.h" - -WSClient::WSClient(QUrl url, QObject *parent) - : QObject(parent) - , m_url(std::move(url)) -{ - connect(&webSocket, &QWebSocket::binaryMessageReceived, this, &WSClient::onbinaryMessageReceived); - connect(&webSocket, &QWebSocket::connected, this, &WSClient::onConnected); - connect(&webSocket, &QWebSocket::disconnected, this, &WSClient::closed); - connect(&webSocket, QOverload::of(&QWebSocket::error), this, &WSClient::onError); - connect(&m_connectionTimer, &QTimer::timeout, this, &WSClient::checkConnection); - - // Keep websocket connection alive - connect(&m_pingTimer, &QTimer::timeout, [this]{ - if (webSocket.state() == QAbstractSocket::ConnectedState) - webSocket.ping(); - }); - m_pingTimer.setInterval(30 * 1000); - m_pingTimer.start(); -} - -void WSClient::sendMsg(const QByteArray &data) { - if (webSocket.state() == QAbstractSocket::ConnectedState) - webSocket.sendBinaryMessage(data); -} - -void WSClient::onToggleConnect(bool connect) { - m_connect = connect; - if (m_connect) - checkConnection(); -} - -void WSClient::start() { - // connect & reconnect on errors/close -#ifdef QT_DEBUG - qDebug() << "WebSocket connect:" << m_url.url(); -#endif - - if (m_connect) - webSocket.open(m_url); - - if (!m_connectionTimer.isActive()) { - m_connectionTimer.start(2000); - } -} - -void WSClient::checkConnection() { - if (!m_connect) - return; - - if (webSocket.state() == QAbstractSocket::UnconnectedState) { -#ifdef QT_DEBUG - qDebug() << "WebSocket reconnect"; -#endif - this->start(); - } -} - -void WSClient::onConnected() { -#ifdef QT_DEBUG - qDebug() << "WebSocket connected"; -#endif - emit connectionEstablished(); -} - -void WSClient::onError(QAbstractSocket::SocketError error) { - qCritical() << "WebSocket error: " << error; - auto state = webSocket.state(); - if (state == QAbstractSocket::ConnectedState || state == QAbstractSocket::ConnectingState) - webSocket.abort(); -} - -void WSClient::onbinaryMessageReceived(const QByteArray &message) { -#ifdef QT_DEBUG - qDebug() << "WebSocket received:" << message; -#endif - if (!Utils::validateJSON(message)) { - qCritical() << "Could not interpret WebSocket message as JSON"; - return; - } - - QJsonDocument doc = QJsonDocument::fromJson(message); - QJsonObject object = doc.object(); - if(!object.contains("cmd") || !object.contains("data")) { - qCritical() << "Invalid WebSocket message received"; - return; - } - - emit WSMessage(object); -} diff --git a/src/utils/wsclient.h b/src/utils/wsclient.h deleted file mode 100644 index 29a7955..0000000 --- a/src/utils/wsclient.h +++ /dev/null @@ -1,42 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// Copyright (c) 2020-2021, The Monero Project. - -#ifndef FEATHER_WSCLIENT_H -#define FEATHER_WSCLIENT_H - -#include -#include -#include - -class WSClient : public QObject -{ - Q_OBJECT - -public: - explicit WSClient(QUrl url, QObject *parent = nullptr); - void start(); - void sendMsg(const QByteArray &data); - QWebSocket webSocket; - -public slots: - void onToggleConnect(bool connect); - -signals: - void closed(); - void connectionEstablished(); - void WSMessage(QJsonObject message); - -private slots: - void onConnected(); - void onbinaryMessageReceived(const QByteArray &message); - void checkConnection(); - void onError(QAbstractSocket::SocketError error); - -private: - bool m_connect = false; - QUrl m_url; - QTimer m_connectionTimer; - QTimer m_pingTimer; -}; - -#endif // FEATHER_WSCLIENT_H \ No newline at end of file