async_in.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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_IN_HPP
  10. #define BOOST_PROCESS_WINDOWS_INITIALIZERS_ASYNC_IN_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/write.hpp>
  16. #include <boost/process/detail/handler_base.hpp>
  17. #include <boost/process/detail/used_handles.hpp>
  18. #include <boost/process/detail/windows/async_handler.hpp>
  19. #include <boost/process/detail/windows/asio_fwd.hpp>
  20. #include <boost/process/async_pipe.hpp>
  21. #include <memory>
  22. #include <future>
  23. namespace boost { namespace process { namespace detail { namespace windows {
  24. template<typename Buffer>
  25. struct async_in_buffer : ::boost::process::detail::windows::handler_base_ext,
  26. ::boost::process::detail::windows::require_io_context,
  27. ::boost::process::detail::uses_handles
  28. {
  29. Buffer & buf;
  30. std::shared_ptr<std::promise<void>> promise;
  31. async_in_buffer operator>(std::future<void> & fut)
  32. {
  33. promise = std::make_shared<std::promise<void>>();
  34. fut = promise->get_future(); return std::move(*this);
  35. }
  36. std::shared_ptr<boost::process::async_pipe> pipe;
  37. ::boost::winapi::HANDLE_ get_used_handles() const
  38. {
  39. return std::move(*pipe).source().native_handle();
  40. }
  41. async_in_buffer(Buffer & buf) : buf(buf)
  42. {
  43. }
  44. template <typename Executor>
  45. inline void on_success(Executor&)
  46. {
  47. auto pipe_ = this->pipe;
  48. if (this->promise)
  49. {
  50. auto promise_ = this->promise;
  51. boost::asio::async_write(*pipe_, buf,
  52. [promise_](const boost::system::error_code & ec, std::size_t)
  53. {
  54. if (ec && (ec.value() != ::boost::winapi::ERROR_BROKEN_PIPE_))
  55. {
  56. std::error_code e(ec.value(), std::system_category());
  57. promise_->set_exception(std::make_exception_ptr(process_error(e)));
  58. }
  59. promise_->set_value();
  60. });
  61. }
  62. else
  63. boost::asio::async_write(*pipe_, buf,
  64. [pipe_](const boost::system::error_code&, std::size_t){});
  65. std::move(*pipe_).source().close();
  66. this->pipe = nullptr;
  67. }
  68. template<typename Executor>
  69. void on_error(Executor &, const std::error_code &) const
  70. {
  71. ::boost::winapi::CloseHandle(pipe->native_source());
  72. }
  73. template <typename WindowsExecutor>
  74. void on_setup(WindowsExecutor &exec)
  75. {
  76. if (!pipe)
  77. pipe = std::make_shared<boost::process::async_pipe>(get_io_context(exec.seq));
  78. ::boost::winapi::HANDLE_ source_handle = std::move(*pipe).source().native_handle();
  79. boost::winapi::SetHandleInformation(source_handle,
  80. boost::winapi::HANDLE_FLAG_INHERIT_,
  81. boost::winapi::HANDLE_FLAG_INHERIT_);
  82. exec.startup_info.hStdInput = source_handle;
  83. exec.startup_info.dwFlags |= boost::winapi::STARTF_USESTDHANDLES_;
  84. exec.inherit_handles = true;
  85. }
  86. };
  87. }}}}
  88. #endif