1 // Copyright (c) 2006, 2007 Julio M. Merino Vidal
\r
2 // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
\r
3 // Copyright (c) 2009 Boris Schaeling
\r
4 // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
\r
5 // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
\r
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
\r
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
\r
10 #ifndef BOOST_PROCESS_DETAIL_POSIX_ASYNC_OUT_HPP
\r
11 #define BOOST_PROCESS_DETAIL_POSIX_ASYNC_OUT_HPP
\r
14 #include <boost/process/detail/posix/handler.hpp>
\r
15 #include <boost/asio/posix/stream_descriptor.hpp>
\r
16 #include <boost/asio/read.hpp>
\r
17 #include <boost/process/async_pipe.hpp>
\r
20 #include <exception>
\r
23 namespace boost { namespace process { namespace detail { namespace posix {
\r
26 inline int apply_out_handles(int handle, std::integral_constant<int, 1>, std::integral_constant<int, -1>)
\r
28 return ::dup2(handle, STDOUT_FILENO);
\r
31 inline int apply_out_handles(int handle, std::integral_constant<int, 2>, std::integral_constant<int, -1>)
\r
33 return ::dup2(handle, STDERR_FILENO);
\r
36 inline int apply_out_handles(int handle, std::integral_constant<int, 1>, std::integral_constant<int, 2>)
\r
38 if (::dup2(handle, STDOUT_FILENO) == -1)
\r
40 if (::dup2(handle, STDERR_FILENO) == -1)
\r
46 template<int p1, int p2, typename Buffer>
\r
47 struct async_out_buffer : ::boost::process::detail::posix::handler_base_ext,
\r
48 ::boost::process::detail::posix::require_io_service
\r
52 std::shared_ptr<boost::process::async_pipe> pipe;
\r
55 async_out_buffer(Buffer & buf) : buf(buf)
\r
59 template <typename Executor>
\r
60 inline void on_success(Executor &exec)
\r
62 auto pipe = this->pipe;
\r
63 boost::asio::async_read(*pipe, buf,
\r
64 [pipe](const boost::system::error_code&, std::size_t size){});
\r
66 this->pipe = nullptr;
\r
67 std::move(*pipe).sink().close();
\r
70 template<typename Executor>
\r
71 void on_error(Executor &, const std::error_code &) const
\r
73 std::move(*pipe).sink().close();
\r
76 template<typename Executor>
\r
77 void on_setup(Executor & exec)
\r
79 pipe = std::make_shared<boost::process::async_pipe>(get_io_service(exec.seq));
\r
83 template <typename Executor>
\r
84 void on_exec_setup(Executor &exec)
\r
86 int res = apply_out_handles(pipe->native_sink(),
\r
87 std::integral_constant<int, p1>(), std::integral_constant<int, p2>());
\r
89 exec.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
\r
91 ::close(pipe->native_sink());
\r
98 template<int p1, int p2, typename Type>
\r
99 struct async_out_future : ::boost::process::detail::posix::handler_base_ext,
\r
100 ::boost::process::detail::posix::require_io_service
\r
102 std::shared_ptr<std::promise<Type>> promise = std::make_shared<std::promise<Type>>();
\r
104 std::shared_ptr<boost::asio::streambuf> buffer = std::make_shared<boost::asio::streambuf>();
\r
106 std::shared_ptr<boost::process::async_pipe> pipe;
\r
108 async_out_future(std::future<Type> & fut)
\r
110 fut = promise->get_future();
\r
112 template <typename Executor>
\r
113 inline void on_success(Executor &exec)
\r
115 auto pipe = this->pipe;
\r
117 auto buffer = this->buffer;
\r
118 auto promise = this->promise;
\r
120 boost::asio::async_read(*pipe, *buffer,
\r
121 [pipe, buffer, promise](const boost::system::error_code& ec, std::size_t size)
\r
123 if (ec && (ec.value() != ENOENT))
\r
125 std::error_code e(ec.value(), std::system_category());
\r
126 promise->set_exception(std::make_exception_ptr(process_error(e)));
\r
130 std::istream is (buffer.get());
\r
132 arg.resize(buffer->size());
\r
133 is.read(&*arg.begin(), buffer->size());
\r
134 promise->set_value(std::move(arg));
\r
138 std::move(*pipe).sink().close();
\r
139 this->pipe = nullptr;
\r
142 template<typename Executor>
\r
143 void on_error(Executor &, const std::error_code &) const
\r
145 std::move(*pipe).sink().close();
\r
148 template<typename Executor>
\r
149 void on_setup(Executor & exec)
\r
151 pipe = std::make_shared<boost::process::async_pipe>(get_io_service(exec.seq));
\r
154 template <typename Executor>
\r
155 void on_exec_setup(Executor &exec)
\r
158 int res = apply_out_handles(pipe->native_sink(),
\r
159 std::integral_constant<int, p1>(), std::integral_constant<int, p2>());
\r
161 exec.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
\r
163 ::close(pipe->native_sink());
\r