]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
1 | // Copyright (c) 2006, 2007 Julio M. Merino Vidal |
2 | // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling | |
3 | // Copyright (c) 2009 Boris Schaeling | |
4 | // Copyright (c) 2010 Felipe Tanus, Boris Schaeling | |
5 | // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling | |
6 | // | |
7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
9 | ||
10 | #ifndef BOOST_PROCESS_DETAIL_POSIX_ASYNC_OUT_HPP | |
11 | #define BOOST_PROCESS_DETAIL_POSIX_ASYNC_OUT_HPP | |
12 | ||
13 | ||
14 | #include <boost/process/detail/posix/handler.hpp> | |
15 | #include <boost/asio/posix/stream_descriptor.hpp> | |
16 | #include <boost/asio/read.hpp> | |
17 | #include <boost/process/async_pipe.hpp> | |
18 | #include <istream> | |
19 | #include <memory> | |
20 | #include <exception> | |
21 | #include <future> | |
22 | ||
23 | namespace boost { namespace process { namespace detail { namespace posix { | |
24 | ||
25 | ||
26 | inline int apply_out_handles(int handle, std::integral_constant<int, 1>, std::integral_constant<int, -1>) | |
27 | { | |
28 | return ::dup2(handle, STDOUT_FILENO); | |
29 | } | |
30 | ||
31 | inline int apply_out_handles(int handle, std::integral_constant<int, 2>, std::integral_constant<int, -1>) | |
32 | { | |
33 | return ::dup2(handle, STDERR_FILENO); | |
34 | } | |
35 | ||
36 | inline int apply_out_handles(int handle, std::integral_constant<int, 1>, std::integral_constant<int, 2>) | |
37 | { | |
38 | if (::dup2(handle, STDOUT_FILENO) == -1) | |
39 | return -1; | |
40 | if (::dup2(handle, STDERR_FILENO) == -1) | |
41 | return -1; | |
42 | ||
43 | return 0; | |
44 | } | |
45 | ||
46 | template<int p1, int p2, typename Buffer> | |
47 | struct async_out_buffer : ::boost::process::detail::posix::handler_base_ext, | |
48 | ::boost::process::detail::posix::require_io_context | |
49 | { | |
50 | Buffer & buf; | |
51 | ||
52 | std::shared_ptr<boost::process::async_pipe> pipe; | |
53 | ||
54 | ||
55 | async_out_buffer(Buffer & buf) : buf(buf) | |
56 | { | |
57 | } | |
58 | ||
59 | template <typename Executor> | |
60 | inline void on_success(Executor &exec) | |
61 | { | |
62 | auto pipe = this->pipe; | |
63 | boost::asio::async_read(*pipe, buf, | |
64 | [pipe](const boost::system::error_code&, std::size_t size){}); | |
65 | ||
66 | this->pipe = nullptr; | |
67 | std::move(*pipe).sink().close(); | |
68 | } | |
69 | ||
70 | template<typename Executor> | |
71 | void on_error(Executor &, const std::error_code &) const | |
72 | { | |
73 | std::move(*pipe).sink().close(); | |
74 | } | |
75 | ||
76 | template<typename Executor> | |
77 | void on_setup(Executor & exec) | |
78 | { | |
79 | pipe = std::make_shared<boost::process::async_pipe>(get_io_context(exec.seq)); | |
80 | } | |
81 | ||
82 | ||
83 | template <typename Executor> | |
84 | void on_exec_setup(Executor &exec) | |
85 | { | |
86 | int res = apply_out_handles(pipe->native_sink(), | |
87 | std::integral_constant<int, p1>(), std::integral_constant<int, p2>()); | |
88 | if (res == -1) | |
89 | exec.set_error(::boost::process::detail::get_last_error(), "dup2() failed"); | |
90 | ||
91 | ::close(pipe->native_sink()); | |
92 | } | |
93 | }; | |
94 | ||
95 | ||
96 | ||
97 | ||
98 | template<int p1, int p2, typename Type> | |
99 | struct async_out_future : ::boost::process::detail::posix::handler_base_ext, | |
100 | ::boost::process::detail::posix::require_io_context | |
101 | { | |
102 | std::shared_ptr<std::promise<Type>> promise = std::make_shared<std::promise<Type>>(); | |
103 | ||
104 | std::shared_ptr<boost::asio::streambuf> buffer = std::make_shared<boost::asio::streambuf>(); | |
105 | ||
106 | std::shared_ptr<boost::process::async_pipe> pipe; | |
107 | ||
108 | async_out_future(std::future<Type> & fut) | |
109 | { | |
110 | fut = promise->get_future(); | |
111 | } | |
112 | template <typename Executor> | |
113 | inline void on_success(Executor &exec) | |
114 | { | |
115 | auto pipe = this->pipe; | |
116 | ||
117 | auto buffer = this->buffer; | |
118 | auto promise = this->promise; | |
119 | ||
120 | boost::asio::async_read(*pipe, *buffer, | |
121 | [pipe, buffer, promise](const boost::system::error_code& ec, std::size_t size) | |
122 | { | |
123 | if (ec && (ec.value() != ENOENT)) | |
124 | { | |
125 | std::error_code e(ec.value(), std::system_category()); | |
126 | promise->set_exception(std::make_exception_ptr(process_error(e))); | |
127 | } | |
128 | else | |
129 | { | |
130 | std::istream is (buffer.get()); | |
131 | Type arg; | |
132 | arg.resize(buffer->size()); | |
133 | is.read(&*arg.begin(), buffer->size()); | |
134 | promise->set_value(std::move(arg)); | |
135 | } | |
136 | }); | |
137 | ||
138 | std::move(*pipe).sink().close(); | |
139 | this->pipe = nullptr; | |
140 | } | |
141 | ||
142 | template<typename Executor> | |
143 | void on_error(Executor &, const std::error_code &) const | |
144 | { | |
145 | std::move(*pipe).sink().close(); | |
146 | } | |
147 | ||
148 | template<typename Executor> | |
149 | void on_setup(Executor & exec) | |
150 | { | |
151 | pipe = std::make_shared<boost::process::async_pipe>(get_io_context(exec.seq)); | |
152 | } | |
153 | ||
154 | template <typename Executor> | |
155 | void on_exec_setup(Executor &exec) | |
156 | { | |
157 | ||
158 | int res = apply_out_handles(pipe->native_sink(), | |
159 | std::integral_constant<int, p1>(), std::integral_constant<int, p2>()); | |
160 | if (res == -1) | |
161 | exec.set_error(::boost::process::detail::get_last_error(), "dup2() failed"); | |
162 | ||
163 | ::close(pipe->native_sink()); | |
164 | } | |
165 | ||
166 | }; | |
167 | ||
168 | }}}} | |
169 | ||
170 | #endif |