Replace process class with boost::process

This commit is contained in:
badaix 2020-01-05 22:00:35 +01:00
parent d80c87351c
commit ad05e87f6d
3 changed files with 23 additions and 260 deletions

View file

@ -35,8 +35,7 @@ namespace streamreader
static constexpr auto LOG_TAG = "ProcessStream";
ProcessStream::ProcessStream(PcmListener* pcmListener, boost::asio::io_context& ioc, const StreamUri& uri)
: PosixStream(pcmListener, ioc, uri), path_(""), process_(nullptr)
ProcessStream::ProcessStream(PcmListener* pcmListener, boost::asio::io_context& ioc, const StreamUri& uri) : PosixStream(pcmListener, ioc, uri)
{
params_ = uri_.getQuery("params");
wd_timeout_sec_ = cpt::stoul(uri_.getQuery("wd_timeout", "0"));
@ -103,11 +102,16 @@ void ProcessStream::do_connect()
return;
initExeAndPath(uri_.path);
LOG(DEBUG, LOG_TAG) << "Launching: '" << path_ + exe_ << "', with params: '" << params_ << "', in path: '" << path_ << "'\n";
process_.reset(new Process(path_ + exe_ + " " + params_, path_));
int flags = fcntl(process_->getStdout(), F_GETFL, 0);
fcntl(process_->getStdout(), F_SETFL, flags | O_NONBLOCK);
stream_ = make_unique<stream_descriptor>(ioc_, process_->getStdout());
stream_stderr_ = make_unique<stream_descriptor>(ioc_, process_->getStderr());
pipe_stdout_ = bp::pipe();
pipe_stderr_ = bp::pipe();
// stdout pipe should not block
int flags = fcntl(pipe_stdout_.native_source(), F_GETFL, 0);
fcntl(pipe_stdout_.native_source(), F_SETFL, flags | O_NONBLOCK);
process_ = bp::child(path_ + exe_ + " " + params_, bp::std_out > pipe_stdout_, bp::std_err > pipe_stderr_, bp::start_dir = path_);
stream_ = make_unique<stream_descriptor>(ioc_, pipe_stdout_.native_source());
stream_stderr_ = make_unique<stream_descriptor>(ioc_, pipe_stderr_.native_source());
on_connect();
if (wd_timeout_sec_ > 0)
{
@ -124,8 +128,8 @@ void ProcessStream::do_connect()
void ProcessStream::do_disconnect()
{
if (process_)
process_->kill();
if (process_.running())
::kill(-process_.native_handle(), SIGINT);
}
@ -146,7 +150,7 @@ void ProcessStream::stderrReadLine()
*stream_stderr_, streambuf_stderr_, delimiter, [this, self, delimiter](const std::error_code& ec, std::size_t bytes_transferred) {
if (ec)
{
LOG(ERROR, LOG_TAG) << "Error while reading from control socket: " << ec.message() << "\n";
LOG(ERROR, LOG_TAG) << "Error while reading from stderr: " << ec.message() << "\n";
return;
}
@ -171,7 +175,7 @@ void ProcessStream::onTimeout(const Watchdog& /*watchdog*/, std::chrono::millise
{
LOG(ERROR, LOG_TAG) << "Watchdog timeout: " << ms.count() / 1000 << "s\n";
if (process_)
process_->kill();
::kill(-process_.native_handle(), SIGINT);
}
} // namespace streamreader