async_out.hpp 5.7 KB

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