async_pipe.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. /** Construct a new async_pipe, does automatically open the pipe.
  41. * Initializes source and sink with the same io_context.
  42. * @note Windows creates a named pipe here, where the name is automatically generated.
  43. */
  44. inline async_pipe(boost::asio::io_context & ios);
  45. /** Construct a new async_pipe, does automatically open the pipe.
  46. * @note Windows creates a named pipe here, where the name is automatically generated.
  47. */
  48. inline async_pipe(boost::asio::io_context & ios_source,
  49. boost::asio::io_context & ios_sink);
  50. /** Construct a new async_pipe, does automatically open.
  51. * Initializes source and sink with the same io_context.
  52. *
  53. * @note Windows restricts possible names.
  54. */
  55. inline async_pipe(boost::asio::io_context & ios, const std::string & name);
  56. /** Construct a new async_pipe, does automatically open.
  57. *
  58. * @note Windows restricts possible names.
  59. */
  60. inline async_pipe(boost::asio::io_context & ios_source,
  61. boost::asio::io_context & ios_sink, const std::string & name);
  62. /** Copy-Constructor of the async pipe.
  63. * @note Windows requires a named pipe for this, if a the wrong type is used an exception is thrown.
  64. *
  65. */
  66. async_pipe(const async_pipe& lhs);
  67. /** Move-Constructor of the async pipe.
  68. */
  69. async_pipe(async_pipe&& lhs);
  70. /** Construct the async-pipe from a pipe.
  71. * @note Windows requires a named pipe for this, if a the wrong type is used an exception is thrown.
  72. *
  73. */
  74. template<class CharT, class Traits = std::char_traits<CharT>>
  75. explicit async_pipe(boost::asio::io_context & ios, const basic_pipe<CharT, Traits> & p);
  76. /** Construct the async-pipe from a pipe, with two different io_context objects.
  77. * @note Windows requires a named pipe for this, if a the wrong type is used an exception is thrown.
  78. *
  79. */
  80. template<class CharT, class Traits = std::char_traits<CharT>>
  81. explicit async_pipe(boost::asio::io_context & ios_source,
  82. boost::asio::io_context & ios_sink,
  83. const basic_pipe<CharT, Traits> & p);
  84. /** Assign a basic_pipe.
  85. * @note Windows requires a named pipe for this, if a the wrong type is used an exception is thrown.
  86. *
  87. */
  88. template<class CharT, class Traits = std::char_traits<CharT>>
  89. inline async_pipe& operator=(const basic_pipe<CharT, Traits>& p);
  90. /** Copy Assign a pipe.
  91. * @note Duplicates the handles.
  92. */
  93. async_pipe& operator=(const async_pipe& lhs);
  94. /** Move assign a pipe */
  95. async_pipe& operator=(async_pipe&& lhs);
  96. /** Destructor. Closes the pipe handles. */
  97. ~async_pipe();
  98. /** Explicit cast to basic_pipe. */
  99. template<class CharT, class Traits = std::char_traits<CharT>>
  100. inline explicit operator basic_pipe<CharT, Traits>() const;
  101. /** Cancel the current asynchronous operations. */
  102. void cancel();
  103. /** Close the pipe handles. */
  104. void close();
  105. /** Close the pipe handles. While passing an error_code
  106. *
  107. */
  108. void close(std::error_code & ec);
  109. /** Check if the pipes are open. */
  110. bool is_open() const;
  111. /** Async close, i.e. close after current operation is completed.
  112. *
  113. * \note There is no guarantee that this will indeed read the entire pipe-buffer
  114. */
  115. void async_close();
  116. /** Read some data from the handle.
  117. * See the boost.asio documentation for more details.
  118. */
  119. template<typename MutableBufferSequence>
  120. std::size_t read_some(const MutableBufferSequence & buffers);
  121. /** Write some data to the handle.
  122. * See the boost.asio documentation for more details.
  123. */
  124. template<typename MutableBufferSequence>
  125. std::size_t write_some(const MutableBufferSequence & buffers);
  126. /** Get the native handle of the source. */
  127. native_handle native_source() const {return const_cast<boost::asio::windows::stream_handle&>(_source).native();}
  128. /** Get the native handle of the sink. */
  129. native_handle native_sink () const {return const_cast<boost::asio::windows::stream_handle&>(_sink ).native();}
  130. /** Start an asynchronous read.
  131. *
  132. * See the [boost.asio documentation](http://www.boost.org/doc/libs/1_60_0/doc/html/boost_asio/reference/AsyncReadStream.html) for more details.
  133. */
  134. template<typename MutableBufferSequence,
  135. typename ReadHandler>
  136. detail::dummy async_read_some(
  137. const MutableBufferSequence & buffers,
  138. ReadHandler &&handler);
  139. /** Start an asynchronous write.
  140. * See the [boost.asio documentation](http://www.boost.org/doc/libs/1_60_0/doc/html/boost_asio/reference/AsyncWriteStream.html) for more details.
  141. */
  142. template<typename ConstBufferSequence,
  143. typename WriteHandler>
  144. detail::dummy async_write_some(
  145. const ConstBufferSequence & buffers,
  146. WriteHandler && handler);
  147. ///Get the asio handle of the pipe sink.
  148. const handle_type & sink () const &;
  149. ///Get the asio handle of the pipe source.
  150. const handle_type & source() const &;
  151. ///Get the asio handle of the pipe sink. Qualified as rvalue
  152. handle_type && sink () &&;
  153. ///Get the asio handle of the pipe source. Qualified as rvalue
  154. handle_type && source() &&;
  155. /// Move the source out of this class and change the io_context. Qualified as rvalue. \attention Will always move.
  156. handle_type source(::boost::asio::io_context& ios) &&;
  157. /// Move the sink out of this class and change the io_context. Qualified as rvalue. \attention Will always move
  158. handle_type sink (::boost::asio::io_context& ios) &&;
  159. /// Copy the source out of this class and change the io_context. \attention Will always copy.
  160. handle_type source(::boost::asio::io_context& ios) const &;
  161. /// Copy the sink out of this class and change the io_context. \attention Will always copy
  162. handle_type sink (::boost::asio::io_context& ios) const &;
  163. };
  164. #else
  165. using ::boost::process::detail::api::async_pipe;
  166. #endif
  167. }}
  168. #endif