async_in.hpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. namespace boost { namespace process { namespace detail { namespace posix {
  18. template<typename Buffer>
  19. struct async_in_buffer : ::boost::process::detail::posix::handler_base_ext,
  20. ::boost::process::detail::posix::require_io_context
  21. {
  22. Buffer & buf;
  23. std::shared_ptr<std::promise<void>> promise;
  24. async_in_buffer operator>(std::future<void> & fut)
  25. {
  26. promise = std::make_shared<std::promise<void>>();
  27. fut = promise->get_future(); return std::move(*this);
  28. }
  29. std::shared_ptr<boost::process::async_pipe> pipe;
  30. async_in_buffer(Buffer & buf) : buf(buf)
  31. {
  32. }
  33. template <typename Executor>
  34. inline void on_success(Executor)
  35. {
  36. auto pipe_ = this->pipe;
  37. if (this->promise)
  38. {
  39. auto promise_ = this->promise;
  40. boost::asio::async_write(*pipe_, buf,
  41. [pipe_, promise_](const boost::system::error_code & ec, std::size_t)
  42. {
  43. if (ec && (ec.value() != EBADF) && (ec.value() != EPERM) && (ec.value() != ENOENT))
  44. {
  45. std::error_code e(ec.value(), std::system_category());
  46. promise_->set_exception(std::make_exception_ptr(process_error(e)));
  47. }
  48. else
  49. promise_->set_value();
  50. });
  51. }
  52. else
  53. boost::asio::async_write(*pipe_, buf,
  54. [pipe_](const boost::system::error_code&, std::size_t){});
  55. std::move(*pipe_).source().close();
  56. this->pipe = nullptr;
  57. }
  58. template<typename Executor>
  59. void on_error(Executor &, const std::error_code &) const
  60. {
  61. std::move(*pipe).source().close();
  62. }
  63. template<typename Executor>
  64. void on_setup(Executor & exec)
  65. {
  66. pipe = std::make_shared<boost::process::async_pipe>(get_io_context(exec.seq));
  67. }
  68. template <typename Executor>
  69. void on_exec_setup(Executor &exec)
  70. {
  71. if (::dup2(pipe->native_source(), STDIN_FILENO) == -1)
  72. exec.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
  73. if (pipe->native_source() != STDIN_FILENO)
  74. ::close(pipe->native_source());
  75. ::close(pipe->native_sink());
  76. }
  77. };
  78. }}}}
  79. #endif