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 <system_error>
16 namespace boost { namespace process { namespace detail { namespace posix {
20 ::boost::asio::posix::stream_descriptor _source;
21 ::boost::asio::posix::stream_descriptor _sink ;
23 typedef int native_handle_type;
24 typedef ::boost::asio::posix::stream_descriptor handle_type;
26 inline async_pipe(boost::asio::io_context & ios) : async_pipe(ios, ios) {}
28 inline async_pipe(boost::asio::io_context & ios_source,
29 boost::asio::io_context & ios_sink) : _source(ios_source), _sink(ios_sink)
32 if (::pipe(fds) == -1)
33 boost::process::detail::throw_last_error("pipe(2) failed");
35 _source.assign(fds[0]);
36 _sink .assign(fds[1]);
38 inline async_pipe(boost::asio::io_context & ios, const std::string & name)
39 : async_pipe(ios, ios, name) {}
41 inline async_pipe(boost::asio::io_context & ios_source,
42 boost::asio::io_context & io_sink, const std::string & name);
43 inline async_pipe(const async_pipe& lhs);
44 async_pipe(async_pipe&& lhs) : _source(std::move(lhs._source)), _sink(std::move(lhs._sink))
46 lhs._source.assign (-1);
47 lhs._sink .assign (-1);
50 template<class CharT, class Traits = std::char_traits<CharT>>
51 explicit async_pipe(::boost::asio::io_context & ios_source,
52 ::boost::asio::io_context & ios_sink,
53 const basic_pipe<CharT, Traits> & p)
54 : _source(ios_source, p.native_source()), _sink(ios_sink, p.native_sink())
58 template<class CharT, class Traits = std::char_traits<CharT>>
59 explicit async_pipe(boost::asio::io_context & ios, const basic_pipe<CharT, Traits> & p)
60 : async_pipe(ios, ios, p)
64 template<class CharT, class Traits = std::char_traits<CharT>>
65 inline async_pipe& operator=(const basic_pipe<CharT, Traits>& p);
66 inline async_pipe& operator=(const async_pipe& rhs);
68 inline async_pipe& operator=(async_pipe&& lhs);
72 if (_sink .native_handle() != -1)
73 ::close(_sink.native_handle());
74 if (_source.native_handle() != -1)
75 ::close(_source.native_handle());
78 template<class CharT, class Traits = std::char_traits<CharT>>
79 inline explicit operator basic_pipe<CharT, Traits>() const;
85 if (_source.is_open())
93 if (_source.is_open())
96 void close(boost::system::error_code & ec)
100 if (_source.is_open())
107 return _sink.is_open() || _source.is_open();
112 _sink.get_io_context(). post([this]{_sink.close();});
113 if (_source.is_open())
114 _source.get_io_context().post([this]{_source.close();});
117 template<typename MutableBufferSequence>
118 std::size_t read_some(const MutableBufferSequence & buffers)
120 return _source.read_some(buffers);
122 template<typename MutableBufferSequence>
123 std::size_t write_some(const MutableBufferSequence & buffers)
125 return _sink.write_some(buffers);
128 native_handle_type native_source() const {return const_cast<boost::asio::posix::stream_descriptor&>(_source).native_handle();}
129 native_handle_type native_sink () const {return const_cast<boost::asio::posix::stream_descriptor&>(_sink ).native_handle();}
131 template<typename MutableBufferSequence,
132 typename ReadHandler>
133 BOOST_ASIO_INITFN_RESULT_TYPE(
134 ReadHandler, void(boost::system::error_code, std::size_t))
136 const MutableBufferSequence & buffers,
137 ReadHandler &&handler)
139 _source.async_read_some(buffers, std::forward<ReadHandler>(handler));
142 template<typename ConstBufferSequence,
143 typename WriteHandler>
144 BOOST_ASIO_INITFN_RESULT_TYPE(
145 WriteHandler, void(boost::system::error_code, std::size_t))
147 const ConstBufferSequence & buffers,
148 WriteHandler&& handler)
150 _sink.async_write_some(buffers, std::forward<WriteHandler>(handler));
154 const handle_type & sink () const & {return _sink;}
155 const handle_type & source() const & {return _source;}
157 handle_type && sink() && { return std::move(_sink); }
158 handle_type && source()&& { return std::move(_source); }
160 handle_type source(::boost::asio::io_context& ios) &&
162 ::boost::asio::posix::stream_descriptor stolen(ios, _source.release());
165 handle_type sink (::boost::asio::io_context& ios) &&
167 ::boost::asio::posix::stream_descriptor stolen(ios, _sink.release());
171 handle_type source(::boost::asio::io_context& ios) const &
173 auto source_in = const_cast<::boost::asio::posix::stream_descriptor &>(_source).native_handle();
174 return ::boost::asio::posix::stream_descriptor(ios, ::dup(source_in));
176 handle_type sink (::boost::asio::io_context& ios) const &
178 auto sink_in = const_cast<::boost::asio::posix::stream_descriptor &>(_sink).native_handle();
179 return ::boost::asio::posix::stream_descriptor(ios, ::dup(sink_in));
184 async_pipe::async_pipe(boost::asio::io_context & ios_source,
185 boost::asio::io_context & ios_sink,
186 const std::string & name) : _source(ios_source), _sink(ios_sink)
188 auto fifo = mkfifo(name.c_str(), 0666 );
191 boost::process::detail::throw_last_error("mkfifo() failed");
194 int read_fd = open(name.c_str(), O_RDWR);
197 boost::process::detail::throw_last_error();
199 int write_fd = dup(read_fd);
202 boost::process::detail::throw_last_error();
204 _source.assign(read_fd);
205 _sink .assign(write_fd);
208 async_pipe::async_pipe(const async_pipe & p) :
209 _source(const_cast<async_pipe&>(p)._source.get_io_context()),
210 _sink( const_cast<async_pipe&>(p)._sink.get_io_context())
213 //cannot get the handle from a const object.
214 auto source_in = const_cast<::boost::asio::posix::stream_descriptor &>(_source).native_handle();
215 auto sink_in = const_cast<::boost::asio::posix::stream_descriptor &>(_sink).native_handle();
220 _source.assign(::dup(source_in));
221 if (_source.native_handle()== -1)
222 ::boost::process::detail::throw_last_error("dup()");
229 _sink.assign(::dup(sink_in));
230 if (_sink.native_handle() == -1)
231 ::boost::process::detail::throw_last_error("dup()");
235 async_pipe& async_pipe::operator=(const async_pipe & p)
240 //cannot get the handle from a const object.
241 auto source_in = const_cast<::boost::asio::posix::stream_descriptor &>(_source).native_handle();
242 auto sink_in = const_cast<::boost::asio::posix::stream_descriptor &>(_sink).native_handle();
247 source = ::dup(source_in);
249 ::boost::process::detail::throw_last_error("dup()");
256 sink = ::dup(sink_in);
258 ::boost::process::detail::throw_last_error("dup()");
260 _source.assign(source);
266 async_pipe& async_pipe::operator=(async_pipe && lhs)
268 std::swap(_source, lhs._source);
269 std::swap(_sink, lhs._sink);
273 template<class CharT, class Traits>
274 async_pipe::operator basic_pipe<CharT, Traits>() const
279 //cannot get the handle from a const object.
280 auto source_in = const_cast<::boost::asio::posix::stream_descriptor &>(_source).native_handle();
281 auto sink_in = const_cast<::boost::asio::posix::stream_descriptor &>(_sink).native_handle();
288 source = ::dup(source_in);
290 ::boost::process::detail::throw_last_error("dup()");
297 sink = ::dup(sink_in);
299 ::boost::process::detail::throw_last_error("dup()");
302 return basic_pipe<CharT, Traits>{source, sink};
306 inline bool operator==(const async_pipe & lhs, const async_pipe & rhs)
308 return compare_handles(lhs.native_source(), rhs.native_source()) &&
309 compare_handles(lhs.native_sink(), rhs.native_sink());
312 inline bool operator!=(const async_pipe & lhs, const async_pipe & rhs)
314 return !compare_handles(lhs.native_source(), rhs.native_source()) ||
315 !compare_handles(lhs.native_sink(), rhs.native_sink());
318 template<class Char, class Traits>
319 inline bool operator==(const async_pipe & lhs, const basic_pipe<Char, Traits> & rhs)
321 return compare_handles(lhs.native_source(), rhs.native_source()) &&
322 compare_handles(lhs.native_sink(), rhs.native_sink());
325 template<class Char, class Traits>
326 inline bool operator!=(const async_pipe & lhs, const basic_pipe<Char, Traits> & rhs)
328 return !compare_handles(lhs.native_source(), rhs.native_source()) ||
329 !compare_handles(lhs.native_sink(), rhs.native_sink());
332 template<class Char, class Traits>
333 inline bool operator==(const basic_pipe<Char, Traits> & lhs, const async_pipe & rhs)
335 return compare_handles(lhs.native_source(), rhs.native_source()) &&
336 compare_handles(lhs.native_sink(), rhs.native_sink());
339 template<class Char, class Traits>
340 inline bool operator!=(const basic_pipe<Char, Traits> & lhs, const async_pipe & rhs)
342 return !compare_handles(lhs.native_source(), rhs.native_source()) ||
343 !compare_handles(lhs.native_sink(), rhs.native_sink());
348 #endif /* INCLUDE_BOOST_PIPE_DETAIL_WINDOWS_ASYNC_PIPE_HPP_ */