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>
15 namespace boost { namespace process { namespace detail { namespace posix {
19 ::boost::asio::posix::stream_descriptor _source;
20 ::boost::asio::posix::stream_descriptor _sink ;
22 typedef int native_handle_type;
23 typedef ::boost::asio::posix::stream_descriptor handle_type;
25 inline async_pipe(boost::asio::io_service & ios) : async_pipe(ios, ios) {}
27 inline async_pipe(boost::asio::io_service & ios_source,
28 boost::asio::io_service & ios_sink) : _source(ios_source), _sink(ios_sink)
31 if (::pipe(fds) == -1)
32 boost::process::detail::throw_last_error("pipe(2) failed");
34 _source.assign(fds[0]);
35 _sink .assign(fds[1]);
37 inline async_pipe(boost::asio::io_service & ios, const std::string & name)
38 : async_pipe(ios, ios, name) {}
40 inline async_pipe(boost::asio::io_service & ios_source,
41 boost::asio::io_service & io_sink, const std::string & name);
42 inline async_pipe(const async_pipe& lhs);
43 async_pipe(async_pipe&& lhs) : _source(std::move(lhs._source)), _sink(std::move(lhs._sink))
45 lhs._source.assign (-1);
46 lhs._sink .assign (-1);
49 template<class CharT, class Traits = std::char_traits<CharT>>
50 explicit async_pipe(::boost::asio::io_service & ios_source,
51 ::boost::asio::io_service & ios_sink,
52 const basic_pipe<CharT, Traits> & p)
53 : _source(ios_source, p.native_source()), _sink(ios_sink, p.native_sink())
57 template<class CharT, class Traits = std::char_traits<CharT>>
58 explicit async_pipe(boost::asio::io_service & ios, const basic_pipe<CharT, Traits> & p)
59 : async_pipe(ios, ios, p)
63 template<class CharT, class Traits = std::char_traits<CharT>>
64 inline async_pipe& operator=(const basic_pipe<CharT, Traits>& p);
65 inline async_pipe& operator=(const async_pipe& rhs);
67 inline async_pipe& operator=(async_pipe&& lhs);
71 if (_sink .native() != -1)
72 ::close(_sink.native());
73 if (_source.native() != -1)
74 ::close(_source.native());
77 template<class CharT, class Traits = std::char_traits<CharT>>
78 inline explicit operator basic_pipe<CharT, Traits>() const;
84 if (_source.is_open())
92 if (_source.is_open())
95 void close(boost::system::error_code & ec)
99 if (_source.is_open())
106 return _sink.is_open() || _source.is_open();
111 _sink.get_io_service(). post([this]{_sink.close();});
112 if (_source.is_open())
113 _source.get_io_service().post([this]{_source.close();});
116 template<typename MutableBufferSequence>
117 std::size_t read_some(const MutableBufferSequence & buffers)
119 return _source.read_some(buffers);
121 template<typename MutableBufferSequence>
122 std::size_t write_some(const MutableBufferSequence & buffers)
124 return _sink.write_some(buffers);
127 native_handle_type native_source() const {return const_cast<boost::asio::posix::stream_descriptor&>(_source).native();}
128 native_handle_type native_sink () const {return const_cast<boost::asio::posix::stream_descriptor&>(_sink ).native();}
130 template<typename MutableBufferSequence,
131 typename ReadHandler>
132 BOOST_ASIO_INITFN_RESULT_TYPE(
133 ReadHandler, void(boost::system::error_code, std::size_t))
135 const MutableBufferSequence & buffers,
136 ReadHandler &&handler)
138 _source.async_read_some(buffers, std::forward<ReadHandler>(handler));
141 template<typename ConstBufferSequence,
142 typename WriteHandler>
143 BOOST_ASIO_INITFN_RESULT_TYPE(
144 WriteHandler, void(boost::system::error_code, std::size_t))
146 const ConstBufferSequence & buffers,
147 WriteHandler&& handler)
149 _sink.async_write_some(buffers, std::forward<WriteHandler>(handler));
153 const handle_type & sink () const & {return _sink;}
154 const handle_type & source() const & {return _source;}
156 handle_type && sink() && { return std::move(_sink); }
157 handle_type && source()&& { return std::move(_source); }
159 handle_type source(::boost::asio::io_service& ios) &&
161 ::boost::asio::posix::stream_descriptor stolen(ios, _source.release());
164 handle_type sink (::boost::asio::io_service& ios) &&
166 ::boost::asio::posix::stream_descriptor stolen(ios, _sink.release());
170 handle_type source(::boost::asio::io_service& ios) const &
172 auto source_in = const_cast<::boost::asio::posix::stream_descriptor &>(_source).native();
173 return ::boost::asio::posix::stream_descriptor(ios, ::dup(source_in));
175 handle_type sink (::boost::asio::io_service& ios) const &
177 auto sink_in = const_cast<::boost::asio::posix::stream_descriptor &>(_sink).native();
178 return ::boost::asio::posix::stream_descriptor(ios, ::dup(sink_in));
183 async_pipe::async_pipe(boost::asio::io_service & ios_source,
184 boost::asio::io_service & ios_sink,
185 const std::string & name) : _source(ios_source), _sink(ios_sink)
187 auto fifo = mkfifo(name.c_str(), 0666 );
190 boost::process::detail::throw_last_error("mkfifo() failed");
193 int read_fd = open(name.c_str(), O_RDWR);
196 boost::process::detail::throw_last_error();
198 int write_fd = dup(read_fd);
201 boost::process::detail::throw_last_error();
203 _source.assign(read_fd);
204 _sink .assign(write_fd);
207 async_pipe::async_pipe(const async_pipe & p) :
208 _source(const_cast<async_pipe&>(p)._source.get_io_service()),
209 _sink( const_cast<async_pipe&>(p)._sink.get_io_service())
212 //cannot get the handle from a const object.
213 auto source_in = const_cast<::boost::asio::posix::stream_descriptor &>(_source).native();
214 auto sink_in = const_cast<::boost::asio::posix::stream_descriptor &>(_sink).native();
219 _source.assign(::dup(source_in));
220 if (_source.native()== -1)
221 ::boost::process::detail::throw_last_error("dup()");
228 _sink.assign(::dup(sink_in));
229 if (_sink.native() == -1)
230 ::boost::process::detail::throw_last_error("dup()");
234 async_pipe& async_pipe::operator=(const async_pipe & p)
239 //cannot get the handle from a const object.
240 auto source_in = const_cast<::boost::asio::posix::stream_descriptor &>(_source).native();
241 auto sink_in = const_cast<::boost::asio::posix::stream_descriptor &>(_sink).native();
246 source = ::dup(source_in);
248 ::boost::process::detail::throw_last_error("dup()");
255 sink = ::dup(sink_in);
257 ::boost::process::detail::throw_last_error("dup()");
259 _source.assign(source);
265 async_pipe& async_pipe::operator=(async_pipe && lhs)
267 if (_source.native_handle() == -1)
268 ::close(_source.native());
270 if (_sink.native_handle() == -1)
271 ::close(_sink.native());
273 _source.assign(lhs._source.native_handle());
274 _sink .assign(lhs._sink .native_handle());
275 lhs._source.assign(-1);
276 lhs._sink .assign(-1);
280 template<class CharT, class Traits>
281 async_pipe::operator basic_pipe<CharT, Traits>() const
286 //cannot get the handle from a const object.
287 auto source_in = const_cast<::boost::asio::posix::stream_descriptor &>(_source).native();
288 auto sink_in = const_cast<::boost::asio::posix::stream_descriptor &>(_sink).native();
295 source = ::dup(source_in);
297 ::boost::process::detail::throw_last_error("dup()");
304 sink = ::dup(sink_in);
306 ::boost::process::detail::throw_last_error("dup()");
309 return basic_pipe<CharT, Traits>{source, sink};
313 inline bool operator==(const async_pipe & lhs, const async_pipe & rhs)
315 return compare_handles(lhs.native_source(), rhs.native_source()) &&
316 compare_handles(lhs.native_sink(), rhs.native_sink());
319 inline bool operator!=(const async_pipe & lhs, const async_pipe & 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 async_pipe & lhs, const basic_pipe<Char, Traits> & 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());
346 template<class Char, class Traits>
347 inline bool operator!=(const basic_pipe<Char, Traits> & lhs, const async_pipe & rhs)
349 return !compare_handles(lhs.native_source(), rhs.native_source()) ||
350 !compare_handles(lhs.native_sink(), rhs.native_sink());
355 #endif /* INCLUDE_BOOST_PIPE_DETAIL_WINDOWS_ASYNC_PIPE_HPP_ */