feat(process): add ways to close stdio channels

This commit is contained in:
outfoxxed 2024-03-03 15:23:41 -08:00
parent 3f0bd20852
commit ffa9d02d48
Signed by untrusted user: outfoxxed
GPG key ID: 4C88A185FB89301E
2 changed files with 42 additions and 3 deletions

View file

@ -41,6 +41,12 @@ void Process::setStdoutParser(DataStreamParser* parser) {
if (this->mStdoutParser != nullptr) {
QObject::disconnect(this->mStdoutParser, nullptr, this, nullptr);
if (this->process != nullptr) {
this->process->closeReadChannel(QProcess::StandardOutput);
this->process->readAllStandardOutput(); // discard
this->stdoutBuffer.clear();
}
}
this->mStdoutParser = parser;
@ -68,6 +74,12 @@ void Process::setStderrParser(DataStreamParser* parser) {
if (this->mStderrParser != nullptr) {
QObject::disconnect(this->mStderrParser, nullptr, this, nullptr);
if (this->process != nullptr) {
this->process->closeReadChannel(QProcess::StandardError);
this->process->readAllStandardError(); // discard
this->stderrBuffer.clear();
}
}
this->mStderrParser = parser;
@ -88,6 +100,19 @@ void Process::onStderrParserDestroyed() {
emit this->stderrParserChanged();
}
bool Process::stdinEnabled() const { return this->mStdinEnabled; }
void Process::setStdinEnabled(bool enabled) {
if (enabled == this->mStdinEnabled) return;
this->mStdinEnabled = enabled;
if (!enabled && this->process != nullptr) {
this->process->closeWriteChannel();
}
emit this->stdinEnabledChanged();
}
void Process::startProcessIfReady() {
if (this->process != nullptr || !this->targetRunning || this->mCommand.isEmpty()) return;
this->targetRunning = false;
@ -108,6 +133,10 @@ void Process::startProcessIfReady() {
this->stdoutBuffer.clear();
this->stderrBuffer.clear();
if (this->mStdoutParser == nullptr) this->process->closeReadChannel(QProcess::StandardOutput);
if (this->mStderrParser == nullptr) this->process->closeReadChannel(QProcess::StandardError);
if (!this->mStdinEnabled) this->process->closeWriteChannel();
this->process->start(cmd, args);
}