1 // Copyright (c) 2016 Klemens D. Morgenstern
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef BOOST_PROCESS_DETAIL_POSIX_ASYNC_PIPE_HPP_
7 #define BOOST_PROCESS_DETAIL_POSIX_ASYNC_PIPE_HPP_
10 #include <boost/process/detail/posix/basic_pipe.hpp>
11 #include <boost/asio/posix/stream_descriptor.hpp>
12 #include <boost/asio/post.hpp>
13 #include <system_error>
17 namespace boost { namespace process { namespace detail { namespace posix {
21 ::boost::asio::posix::stream_descriptor _source;
22 ::boost::asio::posix::stream_descriptor _sink ;
24 typedef int native_handle_type;
25 typedef ::boost::asio::posix::stream_descriptor handle_type;
26 typedef typename handle_type::executor_type executor_type;
28 executor_type get_executor()
30 return _source.get_executor();
33 inline async_pipe(boost::asio::io_context & ios) : async_pipe(ios, ios) {}
35 inline async_pipe(boost::asio::io_context & ios_source,
36 boost::asio::io_context & ios_sink) : _source(ios_source), _sink(ios_sink)
39 if (::pipe(fds) == -1)
40 boost::process::detail::throw_last_error("pipe(2) failed");
42 _source.assign(fds[0]);
43 _sink .assign(fds[1]);
45 inline async_pipe(boost::asio::io_context & ios, const std::string & name)
46 : async_pipe(ios, ios, name) {}
48 inline async_pipe(boost::asio::io_context & ios_source,
49 boost::asio::io_context & io_sink, const std::string & name);
50 inline async_pipe(const async_pipe& lhs);
51 async_pipe(async_pipe&& lhs) : _source(std::move(lhs._source)), _sink(std::move(lhs._sink))
53 lhs._source = ::boost::asio::posix::stream_descriptor{lhs._source.get_executor()};
54 lhs._sink = ::boost::asio::posix::stream_descriptor{lhs._sink. get_executor()};
57 template<class CharT, class Traits = std::char_traits<CharT>>
58 explicit async_pipe(::boost::asio::io_context & ios_source,
59 ::boost::asio::io_context & ios_sink,
60 const basic_pipe<CharT, Traits> & p)
61 : _source(ios_source, p.native_source()), _sink(ios_sink, p.native_sink())
65 template<class CharT, class Traits = std::char_traits<CharT>>
66 explicit async_pipe(boost::asio::io_context & ios, const basic_pipe<CharT, Traits> & p)
67 : async_pipe(ios, ios, p)
71 template<class CharT, class Traits = std::char_traits<CharT>>
72 inline async_pipe& operator=(const basic_pipe<CharT, Traits>& p);
73 inline async_pipe& operator=(const async_pipe& rhs);
75 inline async_pipe& operator=(async_pipe&& lhs);
79 boost::system::error_code ec;
83 template<class CharT, class Traits = std::char_traits<CharT>>
84 inline explicit operator basic_pipe<CharT, Traits>() const;
90 if (_source.is_open())
98 if (_source.is_open())
101 void close(boost::system::error_code & ec)
105 if (_source.is_open())
112 return _sink.is_open() || _source.is_open();
117 boost::asio::post(_sink.get_executor(), [this]{_sink.close();});
118 if (_source.is_open())
119 boost::asio::post(_source.get_executor(), [this]{_source.close();});
122 template<typename MutableBufferSequence>
123 std::size_t read_some(const MutableBufferSequence & buffers)
125 return _source.read_some(buffers);
127 template<typename MutableBufferSequence>
128 std::size_t write_some(const MutableBufferSequence & buffers)
130 return _sink.write_some(buffers);
133 template<typename MutableBufferSequence>
134 std::size_t read_some(const MutableBufferSequence & buffers, boost::system::error_code & ec) noexcept
136 return _source.read_some(buffers, ec);
138 template<typename MutableBufferSequence>
139 std::size_t write_some(const MutableBufferSequence & buffers, boost::system::error_code & ec) noexcept
141 return _sink.write_some(buffers, ec);
145 native_handle_type native_source() const {return const_cast<boost::asio::posix::stream_descriptor&>(_source).native_handle();}
146 native_handle_type native_sink () const {return const_cast<boost::asio::posix::stream_descriptor&>(_sink ).native_handle();}
148 template<typename MutableBufferSequence,
149 typename ReadHandler>
150 BOOST_ASIO_INITFN_RESULT_TYPE(
151 ReadHandler, void(boost::system::error_code, std::size_t))
153 const MutableBufferSequence & buffers,
154 ReadHandler &&handler)
156 return _source.async_read_some(buffers, std::forward<ReadHandler>(handler));
159 template<typename ConstBufferSequence,
160 typename WriteHandler>
161 BOOST_ASIO_INITFN_RESULT_TYPE(
162 WriteHandler, void(boost::system::error_code, std::size_t))
164 const ConstBufferSequence & buffers,
165 WriteHandler&& handler)
167 return _sink.async_write_some(buffers, std::forward<WriteHandler>(handler));
171 const handle_type & sink () const & {return _sink;}
172 const handle_type & source() const & {return _source;}
174 handle_type && sink() && { return std::move(_sink); }
175 handle_type && source()&& { return std::move(_source); }
177 handle_type source(::boost::asio::io_context& ios) &&
179 ::boost::asio::posix::stream_descriptor stolen(ios, _source.release());
182 handle_type sink (::boost::asio::io_context& ios) &&
184 ::boost::asio::posix::stream_descriptor stolen(ios, _sink.release());
188 handle_type source(::boost::asio::io_context& ios) const &
190 auto source_in = const_cast<::boost::asio::posix::stream_descriptor &>(_source).native_handle();
191 return ::boost::asio::posix::stream_descriptor(ios, ::dup(source_in));
193 handle_type sink (::boost::asio::io_context& ios) const &
195 auto sink_in = const_cast<::boost::asio::posix::stream_descriptor &>(_sink).native_handle();
196 return ::boost::asio::posix::stream_descriptor(ios, ::dup(sink_in));
201 async_pipe::async_pipe(boost::asio::io_context & ios_source,
202 boost::asio::io_context & ios_sink,
203 const std::string & name) : _source(ios_source), _sink(ios_sink)
205 auto fifo = mkfifo(name.c_str(), 0666 );
208 boost::process::detail::throw_last_error("mkfifo() failed");
211 int read_fd = open(name.c_str(), O_RDWR);
214 boost::process::detail::throw_last_error();
216 int write_fd = dup(read_fd);
219 boost::process::detail::throw_last_error();
221 _source.assign(read_fd);
222 _sink .assign(write_fd);
225 async_pipe::async_pipe(const async_pipe & p) :
226 _source(const_cast<async_pipe&>(p)._source.get_executor()),
227 _sink( const_cast<async_pipe&>(p)._sink.get_executor())
230 //cannot get the handle from a const object.
231 auto source_in = const_cast<::boost::asio::posix::stream_descriptor &>(_source).native_handle();
232 auto sink_in = const_cast<::boost::asio::posix::stream_descriptor &>(_sink).native_handle();
237 _source.assign(::dup(source_in));
238 if (_source.native_handle()== -1)
239 ::boost::process::detail::throw_last_error("dup()");
246 _sink.assign(::dup(sink_in));
247 if (_sink.native_handle() == -1)
248 ::boost::process::detail::throw_last_error("dup()");
252 async_pipe& async_pipe::operator=(const async_pipe & p)
257 //cannot get the handle from a const object.
258 auto source_in = const_cast<::boost::asio::posix::stream_descriptor &>(p._source).native_handle();
259 auto sink_in = const_cast<::boost::asio::posix::stream_descriptor &>(p._sink).native_handle();
264 source = ::dup(source_in);
266 ::boost::process::detail::throw_last_error("dup()");
273 sink = ::dup(sink_in);
275 ::boost::process::detail::throw_last_error("dup()");
277 _source.assign(source);
283 async_pipe& async_pipe::operator=(async_pipe && lhs)
285 std::swap(_source, lhs._source);
286 std::swap(_sink, lhs._sink);
290 template<class CharT, class Traits>
291 async_pipe::operator basic_pipe<CharT, Traits>() const
296 //cannot get the handle from a const object.
297 auto source_in = const_cast<::boost::asio::posix::stream_descriptor &>(_source).native_handle();
298 auto sink_in = const_cast<::boost::asio::posix::stream_descriptor &>(_sink).native_handle();
305 source = ::dup(source_in);
307 ::boost::process::detail::throw_last_error("dup()");
314 sink = ::dup(sink_in);
316 ::boost::process::detail::throw_last_error("dup()");
319 return basic_pipe<CharT, Traits>{source, sink};
323 inline bool operator==(const async_pipe & lhs, const async_pipe & rhs)
325 return compare_handles(lhs.native_source(), rhs.native_source()) &&
326 compare_handles(lhs.native_sink(), rhs.native_sink());
329 inline bool operator!=(const async_pipe & lhs, const async_pipe & rhs)
331 return !compare_handles(lhs.native_source(), rhs.native_source()) ||
332 !compare_handles(lhs.native_sink(), rhs.native_sink());
335 template<class Char, class Traits>
336 inline bool operator==(const async_pipe & lhs, const basic_pipe<Char, Traits> & rhs)
338 return compare_handles(lhs.native_source(), rhs.native_source()) &&
339 compare_handles(lhs.native_sink(), rhs.native_sink());
342 template<class Char, class Traits>
343 inline bool operator!=(const async_pipe & lhs, const basic_pipe<Char, Traits> & rhs)
345 return !compare_handles(lhs.native_source(), rhs.native_source()) ||
346 !compare_handles(lhs.native_sink(), rhs.native_sink());
349 template<class Char, class Traits>
350 inline bool operator==(const basic_pipe<Char, Traits> & lhs, const async_pipe & rhs)
352 return compare_handles(lhs.native_source(), rhs.native_source()) &&
353 compare_handles(lhs.native_sink(), rhs.native_sink());
356 template<class Char, class Traits>
357 inline bool operator!=(const basic_pipe<Char, Traits> & lhs, const async_pipe & rhs)
359 return !compare_handles(lhs.native_source(), rhs.native_source()) ||
360 !compare_handles(lhs.native_sink(), rhs.native_sink());
365 #endif /* INCLUDE_BOOST_PIPE_DETAIL_WINDOWS_ASYNC_PIPE_HPP_ */