async_out.hpp 5.0 KB

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