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