TorManager: pass parent to m_process

This commit is contained in:
tobtoht 2023-02-14 20:15:02 +01:00
parent e6cd81d8fe
commit 01db80c4a0
5 changed files with 28 additions and 26 deletions

View file

@ -15,6 +15,7 @@
TorManager::TorManager(QObject *parent) TorManager::TorManager(QObject *parent)
: QObject(parent) : QObject(parent)
, m_checkConnectionTimer(new QTimer(this)) , m_checkConnectionTimer(new QTimer(this))
, m_process(new ChildProcess(this))
{ {
connect(m_checkConnectionTimer, &QTimer::timeout, this, &TorManager::checkConnection); connect(m_checkConnectionTimer, &QTimer::timeout, this, &TorManager::checkConnection);
@ -26,11 +27,11 @@ TorManager::TorManager(QObject *parent)
this->torDataPath = Config::defaultConfigDir().filePath("tor/data"); 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::readyReadStandardOutput, this, &TorManager::handleProcessOutput);
connect(&m_process, &QProcess::errorOccurred, this, &TorManager::handleProcessError); connect(m_process, &QProcess::errorOccurred, this, &TorManager::handleProcessError);
connect(&m_process, &QProcess::stateChanged, this, &TorManager::stateChanged); connect(m_process, &QProcess::stateChanged, this, &TorManager::stateChanged);
} }
QPointer<TorManager> TorManager::m_instance(nullptr); QPointer<TorManager> TorManager::m_instance(nullptr);
@ -38,9 +39,9 @@ QPointer<TorManager> TorManager::m_instance(nullptr);
void TorManager::init() { void TorManager::init() {
m_localTor = !shouldStartTorDaemon(); m_localTor = !shouldStartTorDaemon();
auto state = m_process.state(); auto state = m_process->state();
if (m_localTor && (state == QProcess::ProcessState::Running || state == QProcess::ProcessState::Starting)) { if (m_localTor && (state == QProcess::ProcessState::Running || state == QProcess::ProcessState::Starting)) {
m_process.kill(); m_process->kill();
m_started = false; m_started = false;
} }
@ -48,7 +49,7 @@ void TorManager::init() {
} }
void TorManager::stop() { void TorManager::stop() {
m_process.kill(); m_process->kill();
m_started = false; m_started = false;
} }
@ -60,7 +61,7 @@ void TorManager::start() {
return; return;
} }
auto state = m_process.state(); auto state = m_process->state();
if (state == QProcess::ProcessState::Running || state == QProcess::ProcessState::Starting) { if (state == QProcess::ProcessState::Running || state == QProcess::ProcessState::Starting) {
this->setErrorMessage("Can't start Tor, already running or starting"); this->setErrorMessage("Can't start Tor, already running or starting");
return; return;
@ -90,7 +91,7 @@ void TorManager::start() {
qDebug() << QString("%1 %2").arg(this->torPath, arguments.join(" ")); qDebug() << QString("%1 %2").arg(this->torPath, arguments.join(" "));
m_process.start(this->torPath, arguments); m_process->start(this->torPath, arguments);
m_started = true; m_started = true;
} }
@ -149,7 +150,7 @@ void TorManager::stateChanged(QProcess::ProcessState state) {
} }
void TorManager::handleProcessOutput() { void TorManager::handleProcessOutput() {
QByteArray output = m_process.readAllStandardOutput(); QByteArray output = m_process->readAllStandardOutput();
this->torLogs.append(Utils::barrayToString(output)); this->torLogs.append(Utils::barrayToString(output));
emit logsUpdated(); emit logsUpdated();
if(output.contains(QByteArray("Bootstrapped 100%"))) { if(output.contains(QByteArray("Bootstrapped 100%"))) {

View file

@ -59,7 +59,7 @@ private:
static QPointer<TorManager> m_instance; static QPointer<TorManager> m_instance;
ChildProcess m_process; ChildProcess *m_process;
int m_restarts = 0; int m_restarts = 0;
bool m_stopRetries = false; bool m_stopRetries = false;
bool m_localTor; bool m_localTor;

View file

@ -14,7 +14,7 @@
class ChildProcess : public QProcess { class ChildProcess : public QProcess {
Q_OBJECT Q_OBJECT
public: public:
explicit ChildProcess(QObject* parent = nullptr); explicit ChildProcess(QObject *parent);
~ChildProcess() override; ~ChildProcess() override;
protected: protected:
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)

View file

@ -11,25 +11,26 @@
XmRig::XmRig(const QString &configDir, QObject *parent) XmRig::XmRig(const QString &configDir, QObject *parent)
: QObject(parent) : QObject(parent)
, m_process(new ChildProcess(this))
{ {
m_process.setProcessChannelMode(QProcess::MergedChannels); m_process->setProcessChannelMode(QProcess::MergedChannels);
connect(&m_process, &QProcess::readyReadStandardOutput, this, &XmRig::handleProcessOutput); connect(m_process, &QProcess::readyReadStandardOutput, this, &XmRig::handleProcessOutput);
connect(&m_process, &QProcess::errorOccurred, this, &XmRig::handleProcessError); connect(m_process, &QProcess::errorOccurred, this, &XmRig::handleProcessError);
connect(&m_process, &QProcess::stateChanged, this, &XmRig::onStateChanged); connect(m_process, &QProcess::stateChanged, this, &XmRig::onStateChanged);
} }
void XmRig::stop() { void XmRig::stop() {
qDebug() << m_process.processId(); qDebug() << m_process->processId();
if (m_process.state() == QProcess::Running) { if (m_process->state() == QProcess::Running) {
#if defined(Q_OS_WIN) #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) #elif defined(Q_OS_LINUX)
if (m_elevated) { 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; return;
} }
#endif #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; m_elevated = elevated;
auto state = m_process.state(); auto state = m_process->state();
if (state == QProcess::ProcessState::Running || state == QProcess::ProcessState::Starting) { if (state == QProcess::ProcessState::Running || state == QProcess::ProcessState::Starting) {
emit error("Can't start XMRig, already running or starting"); emit error("Can't start XMRig, already running or starting");
return; return;
@ -82,9 +83,9 @@ void XmRig::start(const QString &path, int threads, const QString &address, cons
emit output(cmd.toUtf8()); emit output(cmd.toUtf8());
if (m_elevated) { if (m_elevated) {
m_process.start("pkexec", arguments); m_process->start("pkexec", arguments);
} else { } else {
m_process.start(path, arguments); m_process->start(path, arguments);
} }
} }
@ -101,7 +102,7 @@ void XmRig::onStateChanged(QProcess::ProcessState state) {
} }
void XmRig::handleProcessOutput() { void XmRig::handleProcessOutput() {
QByteArray _output = m_process.readAllStandardOutput(); QByteArray _output = m_process->readAllStandardOutput();
if(_output.contains("miner") && _output.contains("speed")) { if(_output.contains("miner") && _output.contains("speed")) {
// detect hashrate // detect hashrate
auto str = Utils::barrayToString(_output); auto str = Utils::barrayToString(_output);

View file

@ -36,7 +36,7 @@ private slots:
void handleProcessError(QProcess::ProcessError error); void handleProcessError(QProcess::ProcessError error);
private: private:
ChildProcess m_process; ChildProcess *m_process;
QProcess m_killProcess; QProcess m_killProcess;
bool m_elevated; bool m_elevated;
}; };