]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
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 | ||
10 | #ifndef BOOST_PROCESS_WINDOWS_INITIALIZERS_ASYNC_IN_HPP | |
11 | #define BOOST_PROCESS_WINDOWS_INITIALIZERS_ASYNC_IN_HPP | |
12 | ||
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> | |
17 | ||
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> | |
23 | #include <memory> | |
24 | #include <future> | |
25 | ||
26 | ||
27 | namespace boost { namespace process { namespace detail { namespace windows { | |
28 | ||
29 | ||
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 | { | |
34 | Buffer & buf; | |
35 | ||
36 | std::shared_ptr<std::promise<void>> promise; | |
37 | async_in_buffer operator>(std::future<void> & fut) | |
38 | { | |
39 | promise = std::make_shared<std::promise<void>>(); | |
40 | fut = promise->get_future(); return std::move(*this); | |
41 | } | |
42 | ||
43 | std::shared_ptr<boost::process::async_pipe> pipe; | |
44 | ||
45 | async_in_buffer(Buffer & buf) : buf(buf) | |
46 | { | |
47 | } | |
48 | template <typename Executor> | |
49 | inline void on_success(Executor&) | |
50 | { | |
51 | auto pipe = this->pipe; | |
52 | ||
53 | if (this->promise) | |
54 | { | |
55 | auto promise = this->promise; | |
56 | ||
57 | boost::asio::async_write(*pipe, buf, | |
58 | [promise](const boost::system::error_code & ec, std::size_t) | |
59 | { | |
60 | if (ec && (ec.value() != ::boost::winapi::ERROR_BROKEN_PIPE_)) | |
61 | { | |
62 | std::error_code e(ec.value(), std::system_category()); | |
63 | promise->set_exception(std::make_exception_ptr(process_error(e))); | |
64 | } | |
65 | promise->set_value(); | |
66 | }); | |
67 | } | |
68 | else | |
69 | boost::asio::async_write(*pipe, buf, | |
70 | [pipe](const boost::system::error_code&, std::size_t){}); | |
71 | ||
72 | std::move(*pipe).source().close(); | |
73 | ||
74 | ||
75 | this->pipe = nullptr; | |
76 | } | |
77 | ||
78 | template<typename Executor> | |
79 | void on_error(Executor &, const std::error_code &) const | |
80 | { | |
81 | ::boost::winapi::CloseHandle(pipe->native_source()); | |
82 | } | |
83 | ||
84 | template <typename WindowsExecutor> | |
85 | void on_setup(WindowsExecutor &exec) | |
86 | { | |
87 | if (!pipe) | |
88 | pipe = std::make_shared<boost::process::async_pipe>(get_io_context(exec.seq)); | |
89 | ||
90 | ::boost::winapi::HANDLE_ source_handle = std::move(*pipe).source().native_handle(); | |
91 | ||
92 | boost::winapi::SetHandleInformation(source_handle, | |
93 | boost::winapi::HANDLE_FLAG_INHERIT_, | |
94 | boost::winapi::HANDLE_FLAG_INHERIT_); | |
95 | ||
96 | exec.startup_info.hStdInput = source_handle; | |
97 | exec.startup_info.dwFlags |= boost::winapi::STARTF_USESTDHANDLES_; | |
98 | exec.inherit_handles = true; | |
99 | } | |
100 | }; | |
101 | ||
102 | ||
103 | }}}} | |
104 | ||
105 | #endif |