async_out.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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_WINDOWS_INITIALIZERS_ASYNC_OUT_HPP
  10. #define BOOST_PROCESS_WINDOWS_INITIALIZERS_ASYNC_OUT_HPP
  11. #include <boost/winapi/process.hpp>
  12. #include <boost/winapi/handles.hpp>
  13. #include <boost/winapi/handle_info.hpp>
  14. #include <boost/winapi/error_codes.hpp>
  15. #include <boost/asio/read.hpp>
  16. #include <boost/process/detail/handler_base.hpp>
  17. #include <boost/process/detail/used_handles.hpp>
  18. #include <boost/process/detail/windows/asio_fwd.hpp>
  19. #include <istream>
  20. #include <memory>
  21. #include <exception>
  22. #include <future>
  23. namespace boost { namespace process { namespace detail { namespace windows {
  24. template <typename Executor>
  25. inline void apply_out_handles(Executor &e, void* handle, std::integral_constant<int, 1>, std::integral_constant<int, -1>)
  26. {
  27. boost::winapi::SetHandleInformation(handle,
  28. boost::winapi::HANDLE_FLAG_INHERIT_,
  29. boost::winapi::HANDLE_FLAG_INHERIT_);
  30. e.startup_info.hStdOutput = handle;
  31. e.startup_info.dwFlags |= ::boost::winapi::STARTF_USESTDHANDLES_;
  32. e.inherit_handles = true;
  33. }
  34. template <typename Executor>
  35. inline void apply_out_handles(Executor &e, void* handle, std::integral_constant<int, 2>, std::integral_constant<int, -1>)
  36. {
  37. boost::winapi::SetHandleInformation(handle,
  38. boost::winapi::HANDLE_FLAG_INHERIT_,
  39. boost::winapi::HANDLE_FLAG_INHERIT_);
  40. e.startup_info.hStdError = handle;
  41. e.startup_info.dwFlags |= ::boost::winapi::STARTF_USESTDHANDLES_;
  42. e.inherit_handles = true;
  43. }
  44. template <typename Executor>
  45. inline void apply_out_handles(Executor &e, void* handle, std::integral_constant<int, 1>, std::integral_constant<int, 2>)
  46. {
  47. boost::winapi::SetHandleInformation(handle,
  48. boost::winapi::HANDLE_FLAG_INHERIT_,
  49. boost::winapi::HANDLE_FLAG_INHERIT_);
  50. e.startup_info.hStdOutput = handle;
  51. e.startup_info.hStdError = handle;
  52. e.startup_info.dwFlags |= ::boost::winapi::STARTF_USESTDHANDLES_;
  53. e.inherit_handles = true;
  54. }
  55. template<int p1, int p2, typename Buffer>
  56. struct async_out_buffer : ::boost::process::detail::windows::handler_base_ext,
  57. ::boost::process::detail::windows::require_io_context
  58. {
  59. Buffer & buf;
  60. std::shared_ptr<boost::process::async_pipe> pipe;
  61. async_out_buffer(Buffer & buf) : buf(buf)
  62. {
  63. }
  64. template <typename Executor>
  65. inline void on_success(Executor&)
  66. {
  67. auto pipe_ = this->pipe;
  68. boost::asio::async_read(*pipe_, buf,
  69. [pipe_](const boost::system::error_code&, std::size_t){});
  70. std::move(*pipe_).sink().close();
  71. this->pipe = nullptr;
  72. }
  73. template<typename Executor>
  74. void on_error(Executor &, const std::error_code &) const
  75. {
  76. std::move(*pipe).sink().close();
  77. }
  78. template <typename WindowsExecutor>
  79. void on_setup(WindowsExecutor &exec)
  80. {
  81. if (!pipe)
  82. pipe = std::make_shared<boost::process::async_pipe>(get_io_context(exec.seq));
  83. apply_out_handles(exec, std::move(*pipe).sink().native_handle(),
  84. std::integral_constant<int, p1>(), std::integral_constant<int, p2>());
  85. }
  86. };
  87. template<int p1, int p2, typename Type>
  88. struct async_out_future : ::boost::process::detail::windows::handler_base_ext,
  89. ::boost::process::detail::windows::require_io_context,
  90. ::boost::process::detail::uses_handles
  91. {
  92. std::shared_ptr<boost::process::async_pipe> pipe;
  93. std::shared_ptr<std::promise<Type>> promise = std::make_shared<std::promise<Type>>();
  94. std::shared_ptr<boost::asio::streambuf> buffer = std::make_shared<boost::asio::streambuf>();
  95. ::boost::winapi::HANDLE_ get_used_handles() const
  96. {
  97. return std::move(*pipe).sink().native_handle();
  98. }
  99. async_out_future(std::future<Type> & fut)
  100. {
  101. fut = promise->get_future();
  102. }
  103. template <typename Executor>
  104. inline void on_success(Executor&)
  105. {
  106. auto pipe_ = this->pipe;
  107. auto buffer_ = this->buffer;
  108. auto promise_ = this->promise;
  109. std::move(*pipe_).sink().close();
  110. boost::asio::async_read(*pipe_, *buffer_,
  111. [pipe_, buffer_, promise_](const boost::system::error_code& ec, std::size_t)
  112. {
  113. if (ec && (ec.value() != ::boost::winapi::ERROR_BROKEN_PIPE_))
  114. {
  115. std::error_code e(ec.value(), std::system_category());
  116. promise_->set_exception(std::make_exception_ptr(process_error(e)));
  117. }
  118. else
  119. {
  120. std::istream is (buffer_.get());
  121. Type arg;
  122. if (buffer_->size() > 0)
  123. {
  124. arg.resize(buffer_->size());
  125. is.read(&*arg.begin(), buffer_->size());
  126. }
  127. promise_->set_value(std::move(arg));
  128. }
  129. });
  130. this->pipe = nullptr;
  131. this->buffer = nullptr;
  132. this->promise = nullptr;
  133. }
  134. template<typename Executor>
  135. void on_error(Executor &, const std::error_code &) const
  136. {
  137. std::move(*pipe).sink().close();
  138. }
  139. template <typename WindowsExecutor>
  140. void on_setup(WindowsExecutor &exec)
  141. {
  142. if (!pipe)
  143. pipe = std::make_shared<boost::process::async_pipe>(get_io_context(exec.seq));
  144. apply_out_handles(exec, std::move(*pipe).sink().native_handle(),
  145. std::integral_constant<int, p1>(), std::integral_constant<int, p2>());
  146. }
  147. };
  148. }}}}
  149. #endif