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_DETAIL_POSIX_ASYNC_IN_HPP
  10. #define BOOST_PROCESS_DETAIL_POSIX_ASYNC_IN_HPP
  11. #include <boost/process/detail/handler_base.hpp>
  12. #include <boost/process/detail/posix/async_handler.hpp>
  13. #include <boost/asio/write.hpp>
  14. #include <boost/process/async_pipe.hpp>
  15. #include <memory>
  16. #include <future>
  17. #include <boost/process/detail/used_handles.hpp>
  18. #include <array>
  19. namespace boost { namespace process { namespace detail { namespace posix {
  20. template<typename Buffer>
  21. struct async_in_buffer : ::boost::process::detail::posix::handler_base_ext,
  22. ::boost::process::detail::posix::require_io_context,
  23. ::boost::process::detail::uses_handles
  24. {
  25. Buffer & buf;
  26. std::shared_ptr<std::promise<void>> promise;
  27. async_in_buffer operator>(std::future<void> & fut)
  28. {
  29. promise = std::make_shared<std::promise<void>>();
  30. fut = promise->get_future(); return std::move(*this);
  31. }
  32. std::shared_ptr<boost::process::async_pipe> pipe;
  33. async_in_buffer(Buffer & buf) : buf(buf)
  34. {
  35. }
  36. template <typename Executor>
  37. inline void on_success(Executor)
  38. {
  39. auto pipe_ = this->pipe;
  40. if (this->promise)
  41. {
  42. auto promise_ = this->promise;
  43. boost::asio::async_write(*pipe_, buf,
  44. [pipe_, promise_](const boost::system::error_code & ec, std::size_t)
  45. {
  46. if (ec && (ec.value() != EBADF) && (ec.value() != EPERM) && (ec.value() != ENOENT))
  47. {
  48. std::error_code e(ec.value(), std::system_category());
  49. promise_->set_exception(std::make_exception_ptr(process_error(e)));
  50. }
  51. else
  52. promise_->set_value();
  53. });
  54. }
  55. else
  56. boost::asio::async_write(*pipe_, buf,
  57. [pipe_](const boost::system::error_code&, std::size_t){});
  58. std::move(*pipe_).source().close();
  59. this->pipe = nullptr;
  60. }
  61. template<typename Executor>
  62. void on_error(Executor &, const std::error_code &) const
  63. {
  64. std::move(*pipe).source().close();
  65. }
  66. template<typename Executor>
  67. void on_setup(Executor & exec)
  68. {
  69. if (!pipe)
  70. pipe = std::make_shared<boost::process::async_pipe>(get_io_context(exec.seq));
  71. }
  72. std::array<int, 3> get_used_handles()
  73. {
  74. if (pipe)
  75. return {STDIN_FILENO, pipe->native_source(), pipe->native_sink()};
  76. else //if pipe is not constructed, limit_ds is invoked before -> this also means on_exec_setup gets invoked before.
  77. return {STDIN_FILENO, STDIN_FILENO, STDIN_FILENO};
  78. }
  79. template <typename Executor>
  80. void on_exec_setup(Executor &exec)
  81. {
  82. if (::dup2(pipe->native_source(), STDIN_FILENO) == -1)
  83. exec.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
  84. if (pipe->native_source() != STDIN_FILENO)
  85. ::close(pipe->native_source());
  86. ::close(pipe->native_sink());
  87. }
  88. };
  89. }}}}
  90. #endif