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/windows/async_handler.hpp>
21 #include <boost/process/detail/windows/asio_fwd.hpp>
22 #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
36 std::shared_ptr<std::promise<void>> promise;
37 async_in_buffer operator>(std::future<void> & fut)
39 promise = std::make_shared<std::promise<void>>();
40 fut = promise->get_future(); return std::move(*this);
43 std::shared_ptr<boost::process::async_pipe> pipe;
45 async_in_buffer(Buffer & buf) : buf(buf)
48 template <typename Executor>
49 inline void on_success(Executor&)
51 auto pipe = this->pipe;
55 auto promise = this->promise;
57 boost::asio::async_write(*pipe, buf,
58 [promise](const boost::system::error_code & ec, std::size_t)
60 if (ec && (ec.value() != ::boost::winapi::ERROR_BROKEN_PIPE_))
62 std::error_code e(ec.value(), std::system_category());
63 promise->set_exception(std::make_exception_ptr(process_error(e)));
69 boost::asio::async_write(*pipe, buf,
70 [pipe](const boost::system::error_code&, std::size_t){});
72 std::move(*pipe).source().close();
78 template<typename Executor>
79 void on_error(Executor &, const std::error_code &) const
81 ::boost::winapi::CloseHandle(pipe->native_source());
84 template <typename WindowsExecutor>
85 void on_setup(WindowsExecutor &exec)
88 pipe = std::make_shared<boost::process::async_pipe>(get_io_context(exec.seq));
90 ::boost::winapi::HANDLE_ source_handle = std::move(*pipe).source().native_handle();
92 boost::winapi::SetHandleInformation(source_handle,
93 boost::winapi::HANDLE_FLAG_INHERIT_,
94 boost::winapi::HANDLE_FLAG_INHERIT_);
96 exec.startup_info.hStdInput = source_handle;
97 exec.startup_info.dwFlags |= boost::winapi::STARTF_USESTDHANDLES_;
98 exec.inherit_handles = true;