async_in.hpp 3.2 KB

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