From 01db80c4a08a482855a0f1b269970abbc3b7866f Mon Sep 17 00:00:00 2001 From: tobtoht Date: Tue, 14 Feb 2023 20:15:02 +0100 Subject: [PATCH] TorManager: pass parent to m_process --- src/utils/TorManager.cpp | 21 +++++++++++---------- src/utils/TorManager.h | 2 +- src/utils/childproc.h | 2 +- src/utils/xmrig.cpp | 27 ++++++++++++++------------- src/utils/xmrig.h | 2 +- 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/utils/TorManager.cpp b/src/utils/TorManager.cpp index a445654..6645051 100644 --- a/src/utils/TorManager.cpp +++ b/src/utils/TorManager.cpp @@ -15,6 +15,7 @@ TorManager::TorManager(QObject *parent) : QObject(parent) , m_checkConnectionTimer(new QTimer(this)) + , m_process(new ChildProcess(this)) { connect(m_checkConnectionTimer, &QTimer::timeout, this, &TorManager::checkConnection); @@ -26,11 +27,11 @@ TorManager::TorManager(QObject *parent) this->torDataPath = Config::defaultConfigDir().filePath("tor/data"); - m_process.setProcessChannelMode(QProcess::MergedChannels); + m_process->setProcessChannelMode(QProcess::MergedChannels); - connect(&m_process, &QProcess::readyReadStandardOutput, this, &TorManager::handleProcessOutput); - connect(&m_process, &QProcess::errorOccurred, this, &TorManager::handleProcessError); - connect(&m_process, &QProcess::stateChanged, this, &TorManager::stateChanged); + connect(m_process, &QProcess::readyReadStandardOutput, this, &TorManager::handleProcessOutput); + connect(m_process, &QProcess::errorOccurred, this, &TorManager::handleProcessError); + connect(m_process, &QProcess::stateChanged, this, &TorManager::stateChanged); } QPointer TorManager::m_instance(nullptr); @@ -38,9 +39,9 @@ QPointer TorManager::m_instance(nullptr); void TorManager::init() { m_localTor = !shouldStartTorDaemon(); - auto state = m_process.state(); + auto state = m_process->state(); if (m_localTor && (state == QProcess::ProcessState::Running || state == QProcess::ProcessState::Starting)) { - m_process.kill(); + m_process->kill(); m_started = false; } @@ -48,7 +49,7 @@ void TorManager::init() { } void TorManager::stop() { - m_process.kill(); + m_process->kill(); m_started = false; } @@ -60,7 +61,7 @@ void TorManager::start() { return; } - auto state = m_process.state(); + auto state = m_process->state(); if (state == QProcess::ProcessState::Running || state == QProcess::ProcessState::Starting) { this->setErrorMessage("Can't start Tor, already running or starting"); return; @@ -90,7 +91,7 @@ void TorManager::start() { qDebug() << QString("%1 %2").arg(this->torPath, arguments.join(" ")); - m_process.start(this->torPath, arguments); + m_process->start(this->torPath, arguments); m_started = true; } @@ -149,7 +150,7 @@ void TorManager::stateChanged(QProcess::ProcessState state) { } void TorManager::handleProcessOutput() { - QByteArray output = m_process.readAllStandardOutput(); + QByteArray output = m_process->readAllStandardOutput(); this->torLogs.append(Utils::barrayToString(output)); emit logsUpdated(); if(output.contains(QByteArray("Bootstrapped 100%"))) { diff --git a/src/utils/TorManager.h b/src/utils/TorManager.h index a09b710..31c30be 100644 --- a/src/utils/TorManager.h +++ b/src/utils/TorManager.h @@ -59,7 +59,7 @@ private: static QPointer m_instance; - ChildProcess m_process; + ChildProcess *m_process; int m_restarts = 0; bool m_stopRetries = false; bool m_localTor; diff --git a/src/utils/childproc.h b/src/utils/childproc.h index c6f5f80..5c63587 100644 --- a/src/utils/childproc.h +++ b/src/utils/childproc.h @@ -14,7 +14,7 @@ class ChildProcess : public QProcess { Q_OBJECT public: - explicit ChildProcess(QObject* parent = nullptr); + explicit ChildProcess(QObject *parent); ~ChildProcess() override; protected: #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) diff --git a/src/utils/xmrig.cpp b/src/utils/xmrig.cpp index 2c37fe5..b2e354c 100644 --- a/src/utils/xmrig.cpp +++ b/src/utils/xmrig.cpp @@ -11,25 +11,26 @@ XmRig::XmRig(const QString &configDir, QObject *parent) : QObject(parent) + , m_process(new ChildProcess(this)) { - m_process.setProcessChannelMode(QProcess::MergedChannels); - connect(&m_process, &QProcess::readyReadStandardOutput, this, &XmRig::handleProcessOutput); - connect(&m_process, &QProcess::errorOccurred, this, &XmRig::handleProcessError); - connect(&m_process, &QProcess::stateChanged, this, &XmRig::onStateChanged); + m_process->setProcessChannelMode(QProcess::MergedChannels); + connect(m_process, &QProcess::readyReadStandardOutput, this, &XmRig::handleProcessOutput); + connect(m_process, &QProcess::errorOccurred, this, &XmRig::handleProcessError); + connect(m_process, &QProcess::stateChanged, this, &XmRig::onStateChanged); } void XmRig::stop() { - qDebug() << m_process.processId(); - if (m_process.state() == QProcess::Running) { + qDebug() << m_process->processId(); + if (m_process->state() == QProcess::Running) { #if defined(Q_OS_WIN) - m_process.kill(); // https://doc.qt.io/qt-5/qprocess.html#terminate + m_process->kill(); // https://doc.qt.io/qt-5/qprocess.html#terminate #elif defined(Q_OS_LINUX) if (m_elevated) { - m_killProcess.start("pkexec", QStringList() << "kill" << QString::number(m_process.processId())); + m_killProcess.start("pkexec", QStringList() << "kill" << QString::number(m_process->processId())); return; } #endif - m_process.terminate(); + m_process->terminate(); } } @@ -38,7 +39,7 @@ void XmRig::start(const QString &path, int threads, const QString &address, cons { m_elevated = elevated; - auto state = m_process.state(); + auto state = m_process->state(); if (state == QProcess::ProcessState::Running || state == QProcess::ProcessState::Starting) { emit error("Can't start XMRig, already running or starting"); return; @@ -82,9 +83,9 @@ void XmRig::start(const QString &path, int threads, const QString &address, cons emit output(cmd.toUtf8()); if (m_elevated) { - m_process.start("pkexec", arguments); + m_process->start("pkexec", arguments); } else { - m_process.start(path, arguments); + m_process->start(path, arguments); } } @@ -101,7 +102,7 @@ void XmRig::onStateChanged(QProcess::ProcessState state) { } void XmRig::handleProcessOutput() { - QByteArray _output = m_process.readAllStandardOutput(); + QByteArray _output = m_process->readAllStandardOutput(); if(_output.contains("miner") && _output.contains("speed")) { // detect hashrate auto str = Utils::barrayToString(_output); diff --git a/src/utils/xmrig.h b/src/utils/xmrig.h index fd55eee..9f7b54b 100644 --- a/src/utils/xmrig.h +++ b/src/utils/xmrig.h @@ -36,7 +36,7 @@ private slots: void handleProcessError(QProcess::ProcessError error); private: - ChildProcess m_process; + ChildProcess *m_process; QProcess m_killProcess; bool m_elevated; };