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)
: 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> TorManager::m_instance(nullptr);
@ -38,9 +39,9 @@ QPointer<TorManager> 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%"))) {

View file

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

View file

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

View file

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

View file

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