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/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> 24 #include <memory> 25 #include <future> 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 ::boost::process::detail::uses_handles 34 { 35 Buffer & buf; 36 37 std::shared_ptr<std::promise<void>> promise; operator >boost::process::detail::windows::async_in_buffer38 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 get_used_handlesboost::process::detail::windows::async_in_buffer46 ::boost::winapi::HANDLE_ get_used_handles() const 47 { 48 return std::move(*pipe).source().native_handle(); 49 } 50 async_in_bufferboost::process::detail::windows::async_in_buffer51 async_in_buffer(Buffer & buf) : buf(buf) 52 { 53 } 54 template <typename Executor> on_successboost::process::detail::windows::async_in_buffer55 inline void on_success(Executor&) 56 { 57 auto pipe_ = this->pipe; 58 59 if (this->promise) 60 { 61 auto promise_ = this->promise; 62 63 boost::asio::async_write(*pipe_, buf, 64 [promise_](const boost::system::error_code & ec, std::size_t) 65 { 66 if (ec && (ec.value() != ::boost::winapi::ERROR_BROKEN_PIPE_)) 67 { 68 std::error_code e(ec.value(), std::system_category()); 69 promise_->set_exception(std::make_exception_ptr(process_error(e))); 70 } 71 promise_->set_value(); 72 }); 73 } 74 else 75 boost::asio::async_write(*pipe_, buf, 76 [pipe_](const boost::system::error_code&, std::size_t){}); 77 78 std::move(*pipe_).source().close(); 79 80 81 this->pipe = nullptr; 82 } 83 84 template<typename Executor> on_errorboost::process::detail::windows::async_in_buffer85 void on_error(Executor &, const std::error_code &) const 86 { 87 ::boost::winapi::CloseHandle(pipe->native_source()); 88 } 89 90 template <typename WindowsExecutor> on_setupboost::process::detail::windows::async_in_buffer91 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 112