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
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)
10 #ifndef BOOST_PROCESS_WINDOWS_INITIALIZERS_ASYNC_IN_HPP
11 #define BOOST_PROCESS_WINDOWS_INITIALIZERS_ASYNC_IN_HPP
13 #include <boost/winapi/process.hpp>
14 #include <boost/winapi/handles.hpp>
15 #include <boost/winapi/handle_info.hpp>
16 #include <boost/winapi/error_codes.hpp>
18 #include <boost/asio/write.hpp>
19 #include <boost/process/detail/handler_base.hpp>
20 #include <boost/process/detail/used_handles.hpp>
21 #include <boost/process/detail/windows/async_handler.hpp>
22 #include <boost/process/detail/windows/asio_fwd.hpp>
23 #include <boost/process/async_pipe.hpp>
27 namespace boost { namespace process { namespace detail { namespace windows {
30 template<typename Buffer>
31 struct async_in_buffer : ::boost::process::detail::windows::handler_base_ext,
32 ::boost::process::detail::windows::require_io_context,
33 ::boost::process::detail::uses_handles
37 std::shared_ptr<std::promise<void>> promise;
38 async_in_buffer operator>(std::future<void> & fut)
40 promise = std::make_shared<std::promise<void>>();
41 fut = promise->get_future(); return std::move(*this);
44 std::shared_ptr<boost::process::async_pipe> pipe;
46 ::boost::winapi::HANDLE_ get_used_handles() const
48 return std::move(*pipe).source().native_handle();
51 async_in_buffer(Buffer & buf) : buf(buf)
54 template <typename Executor>
55 inline void on_success(Executor&)
57 auto pipe_ = this->pipe;
61 auto promise_ = this->promise;
63 boost::asio::async_write(*pipe_, buf,
64 [promise_](const boost::system::error_code & ec, std::size_t)
66 if (ec && (ec.value() != ::boost::winapi::ERROR_BROKEN_PIPE_))
68 std::error_code e(ec.value(), std::system_category());
69 promise_->set_exception(std::make_exception_ptr(process_error(e)));
71 promise_->set_value();
75 boost::asio::async_write(*pipe_, buf,
76 [pipe_](const boost::system::error_code&, std::size_t){});
78 std::move(*pipe_).source().close();
84 template<typename Executor>
85 void on_error(Executor &, const std::error_code &) const
87 ::boost::winapi::CloseHandle(pipe->native_source());
90 template <typename WindowsExecutor>
91 void on_setup(WindowsExecutor &exec)
94 pipe = std::make_shared<boost::process::async_pipe>(get_io_context(exec.seq));
96 ::boost::winapi::HANDLE_ source_handle = std::move(*pipe).source().native_handle();
98 boost::winapi::SetHandleInformation(source_handle,
99 boost::winapi::HANDLE_FLAG_INHERIT_,
100 boost::winapi::HANDLE_FLAG_INHERIT_);
102 exec.startup_info.hStdInput = source_handle;
103 exec.startup_info.dwFlags |= boost::winapi::STARTF_USESTDHANDLES_;
104 exec.inherit_handles = true;