async_pipe.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #ifndef BOOST_PROCESS_ASYNC_PIPE_HPP
  10. #define BOOST_PROCESS_ASYNC_PIPE_HPP
  11. #include <boost/config.hpp>
  12. #include <boost/process/detail/config.hpp>
  13. #if defined(BOOST_POSIX_API)
  14. #include <boost/process/detail/posix/async_pipe.hpp>
  15. #elif defined(BOOST_WINDOWS_API)
  16. #include <boost/process/detail/windows/async_pipe.hpp>
  17. #endif
  18. namespace boost { namespace process {
  19. #if defined(BOOST_PROCESS_DOXYGEN)
  20. /** Class implementing and asnychronous I/O-Object for use with boost.asio.
  21. * It is based on the corresponding I/O Object, that is either boost::asio::windows::stream_handle or
  22. * boost::asio::posix::stream_descriptor.
  23. *
  24. * It can be used directly with boost::asio::async_read or async_write.
  25. *
  26. * \note The object is copyable, but that does invoke a handle duplicate.
  27. */
  28. class async_pipe
  29. {
  30. public:
  31. /** Typedef for the native handle representation.
  32. * \note This is the handle on the system, not the boost.asio class.
  33. *
  34. */
  35. typedef platform_specific native_handle_type;
  36. /** Typedef for the handle representation of boost.asio.
  37. *
  38. */
  39. typedef platform_specific handle_type;
  40. typedef typename handle_type::executor_type executor_type;
  41. /** Construct a new async_pipe, does automatically open the pipe.
  42. * Initializes source and sink with the same io_context.
  43. * @note Windows creates a named pipe here, where the name is automatically generated.
  44. */
  45. inline async_pipe(boost::asio::io_context & ios);
  46. /** Construct a new async_pipe, does automatically open the pipe.
  47. * @note Windows creates a named pipe here, where the name is automatically generated.
  48. */
  49. inline async_pipe(boost::asio::io_context & ios_source,
  50. boost::asio::io_context & ios_sink);
  51. /** Construct a new async_pipe, does automatically open.
  52. * Initializes source and sink with the same io_context.
  53. *
  54. * @note Windows restricts possible names.
  55. */
  56. inline async_pipe(boost::asio::io_context & ios, const std::string & name);
  57. /** Construct a new async_pipe, does automatically open.
  58. *
  59. * @note Windows restricts possible names.
  60. */
  61. inline async_pipe(boost::asio::io_context & ios_source,
  62. boost::asio::io_context & ios_sink, const std::string & name);
  63. /** Copy-Constructor of the async pipe.
  64. * @note Windows requires a named pipe for this, if a the wrong type is used an exception is thrown.
  65. *
  66. */
  67. async_pipe(const async_pipe& lhs);
  68. /** Move-Constructor of the async pipe.
  69. */
  70. async_pipe(async_pipe&& lhs);
  71. /** Construct the async-pipe from a pipe.
  72. * @note Windows requires a named pipe for this, if a the wrong type is used an exception is thrown.
  73. *
  74. */
  75. template<class CharT, class Traits = std::char_traits<CharT>>
  76. explicit async_pipe(boost::asio::io_context & ios, const basic_pipe<CharT, Traits> & p);
  77. /** Construct the async-pipe from a pipe, with two different io_context objects.
  78. * @note Windows requires a named pipe for this, if a the wrong type is used an exception is thrown.
  79. *
  80. */
  81. template<class CharT, class Traits = std::char_traits<CharT>>
  82. explicit async_pipe(boost::asio::io_context & ios_source,
  83. boost::asio::io_context & ios_sink,
  84. const basic_pipe<CharT, Traits> & p);
  85. /** Assign a basic_pipe.
  86. * @note Windows requires a named pipe for this, if a the wrong type is used an exception is thrown.
  87. *
  88. */
  89. template<class CharT, class Traits = std::char_traits<CharT>>
  90. inline async_pipe& operator=(const basic_pipe<CharT, Traits>& p);
  91. /** Copy Assign a pipe.
  92. * @note Duplicates the handles.
  93. */
  94. async_pipe& operator=(const async_pipe& lhs);
  95. /** Move assign a pipe */
  96. async_pipe& operator=(async_pipe&& lhs);
  97. /** Destructor. Closes the pipe handles. */
  98. ~async_pipe();
  99. /** Explicit cast to basic_pipe. */
  100. template<class CharT, class Traits = std::char_traits<CharT>>
  101. inline explicit operator basic_pipe<CharT, Traits>() const;
  102. /** Cancel the current asynchronous operations. */
  103. void cancel();
  104. /** Close the pipe handles. */
  105. void close();
  106. /** Close the pipe handles. While passing an error_code
  107. *
  108. */
  109. void close(std::error_code & ec);
  110. /** Check if the pipes are open. */
  111. bool is_open() const;
  112. /** Async close, i.e. close after current operation is completed.
  113. *
  114. * \note There is no guarantee that this will indeed read the entire pipe-buffer
  115. */
  116. void async_close();
  117. /** Read some data from the handle.
  118. * See the boost.asio documentation for more details.
  119. */
  120. template<typename MutableBufferSequence>
  121. std::size_t read_some(const MutableBufferSequence & buffers);
  122. /** Write some data to the handle.
  123. * See the boost.asio documentation for more details.
  124. */
  125. template<typename MutableBufferSequence>
  126. std::size_t write_some(const MutableBufferSequence & buffers);
  127. /** Get the native handle of the source. */
  128. native_handle native_source() const {return const_cast<boost::asio::windows::stream_handle&>(_source).native();}
  129. /** Get the native handle of the sink. */
  130. native_handle native_sink () const {return const_cast<boost::asio::windows::stream_handle&>(_sink ).native();}
  131. /** Start an asynchronous read.
  132. *
  133. * See the [boost.asio documentation](http://www.boost.org/doc/libs/1_60_0/doc/html/boost_asio/reference/AsyncReadStream.html) for more details.
  134. */
  135. template<typename MutableBufferSequence,
  136. typename ReadHandler>
  137. detail::dummy async_read_some(
  138. const MutableBufferSequence & buffers,
  139. ReadHandler &&handler);
  140. /** Start an asynchronous write.
  141. * See the [boost.asio documentation](http://www.boost.org/doc/libs/1_60_0/doc/html/boost_asio/reference/AsyncWriteStream.html) for more details.
  142. */
  143. template<typename ConstBufferSequence,
  144. typename WriteHandler>
  145. detail::dummy async_write_some(
  146. const ConstBufferSequence & buffers,
  147. WriteHandler && handler);
  148. ///Get the asio handle of the pipe sink.
  149. const handle_type & sink () const &;
  150. ///Get the asio handle of the pipe source.
  151. const handle_type & source() const &;
  152. ///Get the asio handle of the pipe sink. Qualified as rvalue
  153. handle_type && sink () &&;
  154. ///Get the asio handle of the pipe source. Qualified as rvalue
  155. handle_type && source() &&;
  156. /// Move the source out of this class and change the io_context. Qualified as rvalue. \attention Will always move.
  157. handle_type source(::boost::asio::io_context& ios) &&;
  158. /// Move the sink out of this class and change the io_context. Qualified as rvalue. \attention Will always move
  159. handle_type sink (::boost::asio::io_context& ios) &&;
  160. /// Copy the source out of this class and change the io_context. \attention Will always copy.
  161. handle_type source(::boost::asio::io_context& ios) const &;
  162. /// Copy the sink out of this class and change the io_context. \attention Will always copy
  163. handle_type sink (::boost::asio::io_context& ios) const &;
  164. };
  165. #else
  166. using ::boost::process::detail::api::async_pipe;
  167. #endif
  168. }}
  169. #endif