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
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)
11 #ifndef BOOST_PROCESS_ASYNC_PIPE_HPP
12 #define BOOST_PROCESS_ASYNC_PIPE_HPP
14 #include <boost/config.hpp>
15 #include <boost/process/detail/config.hpp>
17 #if defined(BOOST_POSIX_API)
18 #include <boost/process/detail/posix/async_pipe.hpp>
19 #elif defined(BOOST_WINDOWS_API)
20 #include <boost/process/detail/windows/async_pipe.hpp>
23 namespace boost { namespace process {
26 #if defined(BOOST_PROCESS_DOXYGEN)
29 /** Class implementing and asnychronous I/O-Object for use with boost.asio.
30 * It is based on the corresponding I/O Object, that is either boost::asio::windows::stream_handle or
31 * boost::asio::posix::stream_descriptor.
33 * It can be used directly with boost::asio::async_read or async_write.
35 * \note The object is copyable, but that does invoke a handle duplicate.
40 /** Typedef for the native handle representation.
41 * \note This is the handle on the system, not the boost.asio class.
44 typedef platform_specific native_handle_type;
45 /** Typedef for the handle representation of boost.asio.
48 typedef platform_specific handle_type;
50 /** Construct a new async_pipe, does automatically open the pipe.
51 * Initializes source and sink with the same io_context.
52 * @note Windows creates a named pipe here, where the name is automatically generated.
54 inline async_pipe(boost::asio::io_context & ios);
56 /** Construct a new async_pipe, does automatically open the pipe.
57 * @note Windows creates a named pipe here, where the name is automatically generated.
59 inline async_pipe(boost::asio::io_context & ios_source,
60 boost::asio::io_context & ios_sink);
62 /** Construct a new async_pipe, does automatically open.
63 * Initializes source and sink with the same io_context.
65 * @note Windows restricts possible names.
67 inline async_pipe(boost::asio::io_context & ios, const std::string & name);
70 /** Construct a new async_pipe, does automatically open.
72 * @note Windows restricts possible names.
74 inline async_pipe(boost::asio::io_context & ios_source,
75 boost::asio::io_context & ios_sink, const std::string & name);
77 /** Copy-Constructor of the async pipe.
78 * @note Windows requires a named pipe for this, if a the wrong type is used an exception is thrown.
81 async_pipe(const async_pipe& lhs);
83 /** Move-Constructor of the async pipe.
85 async_pipe(async_pipe&& lhs);
87 /** Construct the async-pipe from a pipe.
88 * @note Windows requires a named pipe for this, if a the wrong type is used an exception is thrown.
91 template<class CharT, class Traits = std::char_traits<CharT>>
92 explicit async_pipe(boost::asio::io_context & ios, const basic_pipe<CharT, Traits> & p);
94 /** Construct the async-pipe from a pipe, with two different io_context objects.
95 * @note Windows requires a named pipe for this, if a the wrong type is used an exception is thrown.
98 template<class CharT, class Traits = std::char_traits<CharT>>
99 explicit async_pipe(boost::asio::io_context & ios_source,
100 boost::asio::io_context & ios_sink,
101 const basic_pipe<CharT, Traits> & p);
104 /** Assign a basic_pipe.
105 * @note Windows requires a named pipe for this, if a the wrong type is used an exception is thrown.
108 template<class CharT, class Traits = std::char_traits<CharT>>
109 inline async_pipe& operator=(const basic_pipe<CharT, Traits>& p);
111 /** Copy Assign a pipe.
112 * @note Duplicates the handles.
114 async_pipe& operator=(const async_pipe& lhs);
115 /** Move assign a pipe */
116 async_pipe& operator=(async_pipe&& lhs);
118 /** Destructor. Closes the pipe handles. */
121 /** Explicit cast to basic_pipe. */
122 template<class CharT, class Traits = std::char_traits<CharT>>
123 inline explicit operator basic_pipe<CharT, Traits>() const;
125 /** Cancel the current asynchronous operations. */
127 /** Close the pipe handles. */
129 /** Close the pipe handles. While passing an error_code
132 void close(std::error_code & ec);
134 /** Check if the pipes are open. */
135 bool is_open() const;
137 /** Async close, i.e. close after current operation is completed.
139 * \note There is no guarantee that this will indeed read the entire pipe-buffer
143 /** Read some data from the handle.
145 * See the boost.asio documentation for more details.
147 template<typename MutableBufferSequence>
148 std::size_t read_some(const MutableBufferSequence & buffers);
150 /** Write some data to the handle.
152 * See the boost.asio documentation for more details.
154 template<typename MutableBufferSequence>
155 std::size_t write_some(const MutableBufferSequence & buffers);
157 /** Get the native handle of the source. */
158 native_handle native_source() const {return const_cast<boost::asio::windows::stream_handle&>(_source).native();}
159 /** Get the native handle of the sink. */
160 native_handle native_sink () const {return const_cast<boost::asio::windows::stream_handle&>(_sink ).native();}
162 /** Start an asynchronous read.
164 * See the [boost.asio documentation](http://www.boost.org/doc/libs/1_60_0/doc/html/boost_asio/reference/AsyncReadStream.html) for more details.
166 template<typename MutableBufferSequence,
167 typename ReadHandler>
168 detail::dummy async_read_some(
169 const MutableBufferSequence & buffers,
170 ReadHandler &&handler);
172 /** Start an asynchronous write.
174 * See the [boost.asio documentation](http://www.boost.org/doc/libs/1_60_0/doc/html/boost_asio/reference/AsyncWriteStream.html) for more details.
176 template<typename ConstBufferSequence,
177 typename WriteHandler>
178 detail::dummy async_write_some(
179 const ConstBufferSequence & buffers,
180 WriteHandler && handler);
182 ///Get the asio handle of the pipe sink.
183 const handle_type & sink () const &;
184 ///Get the asio handle of the pipe source.
185 const handle_type & source() const &;
187 ///Get the asio handle of the pipe sink. Qualified as rvalue
188 handle_type && sink () &&;
189 ///Get the asio handle of the pipe source. Qualified as rvalue
190 handle_type && source() &&;
192 /// Move the source out of this class and change the io_context. Qualified as rvalue. \attention Will always move.
193 handle_type source(::boost::asio::io_context& ios) &&;
194 /// Move the sink out of this class and change the io_context. Qualified as rvalue. \attention Will always move
195 handle_type sink (::boost::asio::io_context& ios) &&;
197 /// Copy the source out of this class and change the io_context. \attention Will always copy.
198 handle_type source(::boost::asio::io_context& ios) const &;
199 /// Copy the sink out of this class and change the io_context. \attention Will always copy
200 handle_type sink (::boost::asio::io_context& ios) const &;
207 using ::boost::process::detail::api::async_pipe;