mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-13 00:56:44 +02:00
Switch to boost::process
This commit is contained in:
parent
87d4c00d21
commit
4a802d73ba
2 changed files with 24 additions and 12 deletions
|
@ -35,8 +35,7 @@ namespace streamreader
|
||||||
static constexpr auto LOG_TAG = "ProcessStream";
|
static constexpr auto LOG_TAG = "ProcessStream";
|
||||||
|
|
||||||
|
|
||||||
ProcessStream::ProcessStream(PcmListener* pcmListener, boost::asio::io_context& ioc, const StreamUri& uri)
|
ProcessStream::ProcessStream(PcmListener* pcmListener, boost::asio::io_context& ioc, const StreamUri& uri) : PosixStream(pcmListener, ioc, uri)
|
||||||
: PosixStream(pcmListener, ioc, uri), path_(""), process_(nullptr)
|
|
||||||
{
|
{
|
||||||
params_ = uri_.getQuery("params");
|
params_ = uri_.getQuery("params");
|
||||||
wd_timeout_sec_ = cpt::stoul(uri_.getQuery("wd_timeout", "0"));
|
wd_timeout_sec_ = cpt::stoul(uri_.getQuery("wd_timeout", "0"));
|
||||||
|
@ -103,11 +102,18 @@ void ProcessStream::do_connect()
|
||||||
return;
|
return;
|
||||||
initExeAndPath(uri_.path);
|
initExeAndPath(uri_.path);
|
||||||
LOG(DEBUG, LOG_TAG) << "Launching: '" << path_ + exe_ << "', with params: '" << params_ << "', in path: '" << path_ << "'\n";
|
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);
|
pipe_stdout_ = bp::pipe();
|
||||||
fcntl(process_->getStdout(), F_SETFL, flags | O_NONBLOCK);
|
// could use bp::async_pipe, but this is broken in boost 1.72:
|
||||||
stream_ = make_unique<stream_descriptor>(ioc_, process_->getStdout());
|
// https://github.com/boostorg/process/issues/116
|
||||||
stream_stderr_ = make_unique<stream_descriptor>(ioc_, process_->getStderr());
|
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();
|
on_connect();
|
||||||
if (wd_timeout_sec_ > 0)
|
if (wd_timeout_sec_ > 0)
|
||||||
{
|
{
|
||||||
|
@ -124,8 +130,8 @@ void ProcessStream::do_connect()
|
||||||
|
|
||||||
void ProcessStream::do_disconnect()
|
void ProcessStream::do_disconnect()
|
||||||
{
|
{
|
||||||
if (process_)
|
if (process_.running())
|
||||||
process_->kill();
|
::kill(-process_.native_handle(), SIGINT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -171,7 +177,7 @@ void ProcessStream::onTimeout(const Watchdog& /*watchdog*/, std::chrono::millise
|
||||||
{
|
{
|
||||||
LOG(ERROR, LOG_TAG) << "Watchdog timeout: " << ms.count() / 1000 << "s\n";
|
LOG(ERROR, LOG_TAG) << "Watchdog timeout: " << ms.count() / 1000 << "s\n";
|
||||||
if (process_)
|
if (process_)
|
||||||
process_->kill();
|
::kill(-process_.native_handle(), SIGINT);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace streamreader
|
} // namespace streamreader
|
||||||
|
|
|
@ -19,13 +19,17 @@
|
||||||
#ifndef PROCESS_STREAM_HPP
|
#ifndef PROCESS_STREAM_HPP
|
||||||
#define PROCESS_STREAM_HPP
|
#define PROCESS_STREAM_HPP
|
||||||
|
|
||||||
|
#include <boost/process.hpp>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "posix_stream.hpp"
|
#include "posix_stream.hpp"
|
||||||
#include "process.hpp"
|
|
||||||
#include "watchdog.hpp"
|
#include "watchdog.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
namespace bp = boost::process;
|
||||||
|
|
||||||
|
|
||||||
namespace streamreader
|
namespace streamreader
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -49,7 +53,9 @@ protected:
|
||||||
std::string exe_;
|
std::string exe_;
|
||||||
std::string path_;
|
std::string path_;
|
||||||
std::string params_;
|
std::string params_;
|
||||||
std::unique_ptr<Process> process_;
|
bp::pipe pipe_stdout_;
|
||||||
|
bp::pipe pipe_stderr_;
|
||||||
|
bp::child process_;
|
||||||
|
|
||||||
bool logStderr_;
|
bool logStderr_;
|
||||||
boost::asio::streambuf streambuf_stderr_;
|
boost::asio::streambuf streambuf_stderr_;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue