async_pipe.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. // Copyright (c) 2016 Klemens D. Morgenstern
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_PROCESS_DETAIL_WINDOWS_ASYNC_PIPE_HPP_
  6. #define BOOST_PROCESS_DETAIL_WINDOWS_ASYNC_PIPE_HPP_
  7. #include <boost/winapi/basic_types.hpp>
  8. #include <boost/winapi/pipes.hpp>
  9. #include <boost/winapi/handles.hpp>
  10. #include <boost/winapi/file_management.hpp>
  11. #include <boost/winapi/get_last_error.hpp>
  12. #include <boost/winapi/access_rights.hpp>
  13. #include <boost/winapi/process.hpp>
  14. #include <boost/process/detail/windows/basic_pipe.hpp>
  15. #include <boost/asio/post.hpp>
  16. #include <boost/asio/windows/stream_handle.hpp>
  17. #include <atomic>
  18. #include <system_error>
  19. #include <string>
  20. namespace boost { namespace process { namespace detail { namespace windows {
  21. inline std::string make_pipe_name()
  22. {
  23. std::string name = "\\\\.\\pipe\\boost_process_auto_pipe_";
  24. auto pid = ::boost::winapi::GetCurrentProcessId();
  25. static std::atomic_size_t cnt{0};
  26. name += std::to_string(pid);
  27. name += "_";
  28. name += std::to_string(cnt++);
  29. return name;
  30. }
  31. class async_pipe
  32. {
  33. ::boost::asio::windows::stream_handle _source;
  34. ::boost::asio::windows::stream_handle _sink ;
  35. public:
  36. typedef ::boost::winapi::HANDLE_ native_handle_type;
  37. typedef ::boost::asio::windows::stream_handle handle_type;
  38. inline async_pipe(boost::asio::io_context & ios,
  39. const std::string & name = make_pipe_name())
  40. : async_pipe(ios, ios, name) {}
  41. inline async_pipe(boost::asio::io_context & ios_source,
  42. boost::asio::io_context & ios_sink,
  43. const std::string & name = make_pipe_name());
  44. inline async_pipe(const async_pipe& rhs);
  45. async_pipe(async_pipe&& rhs) : _source(std::move(rhs._source)), _sink(std::move(rhs._sink))
  46. {
  47. rhs._source.assign (::boost::winapi::INVALID_HANDLE_VALUE_);
  48. rhs._sink .assign (::boost::winapi::INVALID_HANDLE_VALUE_);
  49. }
  50. template<class CharT, class Traits = std::char_traits<CharT>>
  51. explicit async_pipe(::boost::asio::io_context & ios_source,
  52. ::boost::asio::io_context & ios_sink,
  53. const basic_pipe<CharT, Traits> & p)
  54. : _source(ios_source, p.native_source()), _sink(ios_sink, p.native_sink())
  55. {
  56. }
  57. template<class CharT, class Traits = std::char_traits<CharT>>
  58. explicit async_pipe(boost::asio::io_context & ios, const basic_pipe<CharT, Traits> & p)
  59. : async_pipe(ios, ios, p)
  60. {
  61. }
  62. template<class CharT, class Traits = std::char_traits<CharT>>
  63. inline async_pipe& operator=(const basic_pipe<CharT, Traits>& p);
  64. inline async_pipe& operator=(const async_pipe& rhs);
  65. inline async_pipe& operator=(async_pipe&& rhs);
  66. ~async_pipe()
  67. {
  68. boost::system::error_code ec;
  69. close(ec);
  70. }
  71. template<class CharT, class Traits = std::char_traits<CharT>>
  72. inline explicit operator basic_pipe<CharT, Traits>() const;
  73. void cancel()
  74. {
  75. if (_sink.is_open())
  76. _sink.cancel();
  77. if (_source.is_open())
  78. _source.cancel();
  79. }
  80. void close()
  81. {
  82. if (_sink.is_open())
  83. {
  84. _sink.close();
  85. _sink = handle_type(_sink.get_executor());
  86. }
  87. if (_source.is_open())
  88. {
  89. _source.close();
  90. _source = handle_type(_source.get_executor());
  91. }
  92. }
  93. void close(boost::system::error_code & ec)
  94. {
  95. if (_sink.is_open())
  96. {
  97. _sink.close(ec);
  98. _sink = handle_type(_sink.get_executor());
  99. }
  100. if (_source.is_open())
  101. {
  102. _source.close(ec);
  103. _source = handle_type(_source.get_executor());
  104. }
  105. }
  106. bool is_open() const
  107. {
  108. return _sink.is_open() || _source.is_open();
  109. }
  110. void async_close()
  111. {
  112. if (_sink.is_open())
  113. boost::asio::post(_sink.get_executor(), [this]{_sink.close();});
  114. if (_source.is_open())
  115. boost::asio::post(_source.get_executor(), [this]{_source.close();});
  116. }
  117. template<typename MutableBufferSequence>
  118. std::size_t read_some(const MutableBufferSequence & buffers)
  119. {
  120. return _source.read_some(buffers);
  121. }
  122. template<typename MutableBufferSequence>
  123. std::size_t write_some(const MutableBufferSequence & buffers)
  124. {
  125. return _sink.write_some(buffers);
  126. }
  127. template<typename MutableBufferSequence>
  128. std::size_t read_some(const MutableBufferSequence & buffers, boost::system::error_code & ec) noexcept
  129. {
  130. return _source.read_some(buffers, ec);
  131. }
  132. template<typename MutableBufferSequence>
  133. std::size_t write_some(const MutableBufferSequence & buffers, boost::system::error_code & ec) noexcept
  134. {
  135. return _sink.write_some(buffers, ec);
  136. }
  137. native_handle_type native_source() const {return const_cast<boost::asio::windows::stream_handle&>(_source).native_handle();}
  138. native_handle_type native_sink () const {return const_cast<boost::asio::windows::stream_handle&>(_sink ).native_handle();}
  139. template<typename MutableBufferSequence,
  140. typename ReadHandler>
  141. BOOST_ASIO_INITFN_RESULT_TYPE(
  142. ReadHandler, void(boost::system::error_code, std::size_t))
  143. async_read_some(
  144. const MutableBufferSequence & buffers,
  145. ReadHandler &&handler)
  146. {
  147. return _source.async_read_some(buffers, std::forward<ReadHandler>(handler));
  148. }
  149. template<typename ConstBufferSequence,
  150. typename WriteHandler>
  151. BOOST_ASIO_INITFN_RESULT_TYPE(
  152. WriteHandler, void(boost::system::error_code, std::size_t))
  153. async_write_some(
  154. const ConstBufferSequence & buffers,
  155. WriteHandler && handler)
  156. {
  157. return _sink.async_write_some(buffers, std::forward<WriteHandler>(handler));
  158. }
  159. const handle_type & sink () const & {return _sink;}
  160. const handle_type & source() const & {return _source;}
  161. handle_type && source() && { return std::move(_source); }
  162. handle_type && sink() && { return std::move(_sink); }
  163. handle_type source(::boost::asio::io_context& ios) &&
  164. {
  165. ::boost::asio::windows::stream_handle stolen(ios.get_executor(), _source.native_handle());
  166. _source.assign(::boost::winapi::INVALID_HANDLE_VALUE_);
  167. return stolen;
  168. }
  169. handle_type sink (::boost::asio::io_context& ios) &&
  170. {
  171. ::boost::asio::windows::stream_handle stolen(ios.get_executor(), _sink.native_handle());
  172. _sink.assign(::boost::winapi::INVALID_HANDLE_VALUE_);
  173. return stolen;
  174. }
  175. handle_type source(::boost::asio::io_context& ios) const &
  176. {
  177. auto proc = ::boost::winapi::GetCurrentProcess();
  178. ::boost::winapi::HANDLE_ source;
  179. auto source_in = const_cast<handle_type&>(_source).native_handle();
  180. if (source_in == ::boost::winapi::INVALID_HANDLE_VALUE_)
  181. source = ::boost::winapi::INVALID_HANDLE_VALUE_;
  182. else if (!::boost::winapi::DuplicateHandle(
  183. proc, source_in, proc, &source, 0,
  184. static_cast<::boost::winapi::BOOL_>(true),
  185. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  186. throw_last_error("Duplicate Pipe Failed");
  187. return ::boost::asio::windows::stream_handle(ios.get_executor(), source);
  188. }
  189. handle_type sink (::boost::asio::io_context& ios) const &
  190. {
  191. auto proc = ::boost::winapi::GetCurrentProcess();
  192. ::boost::winapi::HANDLE_ sink;
  193. auto sink_in = const_cast<handle_type&>(_sink).native_handle();
  194. if (sink_in == ::boost::winapi::INVALID_HANDLE_VALUE_)
  195. sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
  196. else if (!::boost::winapi::DuplicateHandle(
  197. proc, sink_in, proc, &sink, 0,
  198. static_cast<::boost::winapi::BOOL_>(true),
  199. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  200. throw_last_error("Duplicate Pipe Failed");
  201. return ::boost::asio::windows::stream_handle(ios.get_executor(), sink);
  202. }
  203. };
  204. async_pipe::async_pipe(const async_pipe& p) :
  205. _source(const_cast<handle_type&>(p._source).get_executor()),
  206. _sink (const_cast<handle_type&>(p._sink).get_executor())
  207. {
  208. auto proc = ::boost::winapi::GetCurrentProcess();
  209. ::boost::winapi::HANDLE_ source;
  210. ::boost::winapi::HANDLE_ sink;
  211. //cannot get the handle from a const object.
  212. auto source_in = const_cast<handle_type&>(p._source).native_handle();
  213. auto sink_in = const_cast<handle_type&>(p._sink).native_handle();
  214. if (source_in == ::boost::winapi::INVALID_HANDLE_VALUE_)
  215. source = ::boost::winapi::INVALID_HANDLE_VALUE_;
  216. else if (!::boost::winapi::DuplicateHandle(
  217. proc, source_in, proc, &source, 0,
  218. static_cast<::boost::winapi::BOOL_>(true),
  219. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  220. throw_last_error("Duplicate Pipe Failed");
  221. if (sink_in == ::boost::winapi::INVALID_HANDLE_VALUE_)
  222. sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
  223. else if (!::boost::winapi::DuplicateHandle(
  224. proc, sink_in, proc, &sink, 0,
  225. static_cast<::boost::winapi::BOOL_>(true),
  226. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  227. throw_last_error("Duplicate Pipe Failed");
  228. _source.assign(source);
  229. _sink. assign(sink);
  230. }
  231. async_pipe::async_pipe(boost::asio::io_context & ios_source,
  232. boost::asio::io_context & ios_sink,
  233. const std::string & name) : _source(ios_source), _sink(ios_sink)
  234. {
  235. static constexpr int FILE_FLAG_OVERLAPPED_ = 0x40000000; //temporary
  236. ::boost::winapi::HANDLE_ source = ::boost::winapi::create_named_pipe(
  237. #if defined(BOOST_NO_ANSI_APIS)
  238. ::boost::process::detail::convert(name).c_str(),
  239. #else
  240. name.c_str(),
  241. #endif
  242. ::boost::winapi::PIPE_ACCESS_INBOUND_
  243. | FILE_FLAG_OVERLAPPED_, //write flag
  244. 0, 1, 8192, 8192, 0, nullptr);
  245. if (source == boost::winapi::INVALID_HANDLE_VALUE_)
  246. ::boost::process::detail::throw_last_error("create_named_pipe(" + name + ") failed");
  247. _source.assign(source);
  248. ::boost::winapi::HANDLE_ sink = boost::winapi::create_file(
  249. #if defined(BOOST_NO_ANSI_APIS)
  250. ::boost::process::detail::convert(name).c_str(),
  251. #else
  252. name.c_str(),
  253. #endif
  254. ::boost::winapi::GENERIC_WRITE_, 0, nullptr,
  255. ::boost::winapi::OPEN_EXISTING_,
  256. FILE_FLAG_OVERLAPPED_, //to allow read
  257. nullptr);
  258. if (sink == ::boost::winapi::INVALID_HANDLE_VALUE_)
  259. ::boost::process::detail::throw_last_error("create_file() failed");
  260. _sink.assign(sink);
  261. }
  262. async_pipe& async_pipe::operator=(const async_pipe & p)
  263. {
  264. auto proc = ::boost::winapi::GetCurrentProcess();
  265. ::boost::winapi::HANDLE_ source;
  266. ::boost::winapi::HANDLE_ sink;
  267. //cannot get the handle from a const object.
  268. auto &source_in = const_cast<::boost::asio::windows::stream_handle &>(p._source);
  269. auto &sink_in = const_cast<::boost::asio::windows::stream_handle &>(p._sink);
  270. if (source_in.native_handle() == ::boost::winapi::INVALID_HANDLE_VALUE_)
  271. source = ::boost::winapi::INVALID_HANDLE_VALUE_;
  272. else if (!::boost::winapi::DuplicateHandle(
  273. proc, source_in.native_handle(), proc, &source, 0,
  274. static_cast<::boost::winapi::BOOL_>(true),
  275. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  276. throw_last_error("Duplicate Pipe Failed");
  277. if (sink_in.native_handle() == ::boost::winapi::INVALID_HANDLE_VALUE_)
  278. sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
  279. else if (!::boost::winapi::DuplicateHandle(
  280. proc, sink_in.native_handle(), proc, &sink, 0,
  281. static_cast<::boost::winapi::BOOL_>(true),
  282. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  283. throw_last_error("Duplicate Pipe Failed");
  284. //so we also assign the io_context
  285. _source = ::boost::asio::windows::stream_handle(source_in.get_executor(), source);
  286. _sink = ::boost::asio::windows::stream_handle(source_in.get_executor(), sink);
  287. return *this;
  288. }
  289. async_pipe& async_pipe::operator=(async_pipe && rhs)
  290. {
  291. if (_source.native_handle() != ::boost::winapi::INVALID_HANDLE_VALUE_)
  292. ::boost::winapi::CloseHandle(_source.native_handle());
  293. if (_sink.native_handle() != ::boost::winapi::INVALID_HANDLE_VALUE_)
  294. ::boost::winapi::CloseHandle(_sink.native_handle());
  295. _source.assign(rhs._source.native_handle());
  296. _sink .assign(rhs._sink .native_handle());
  297. rhs._source.assign(::boost::winapi::INVALID_HANDLE_VALUE_);
  298. rhs._sink .assign(::boost::winapi::INVALID_HANDLE_VALUE_);
  299. return *this;
  300. }
  301. template<class CharT, class Traits>
  302. async_pipe::operator basic_pipe<CharT, Traits>() const
  303. {
  304. auto proc = ::boost::winapi::GetCurrentProcess();
  305. ::boost::winapi::HANDLE_ source;
  306. ::boost::winapi::HANDLE_ sink;
  307. //cannot get the handle from a const object.
  308. auto source_in = const_cast<::boost::asio::windows::stream_handle &>(_source).native_handle();
  309. auto sink_in = const_cast<::boost::asio::windows::stream_handle &>(_sink).native_handle();
  310. if (source_in == ::boost::winapi::INVALID_HANDLE_VALUE_)
  311. source = ::boost::winapi::INVALID_HANDLE_VALUE_;
  312. else if (!::boost::winapi::DuplicateHandle(
  313. proc, source_in, proc, &source, 0,
  314. static_cast<::boost::winapi::BOOL_>(true),
  315. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  316. throw_last_error("Duplicate Pipe Failed");
  317. if (sink_in == ::boost::winapi::INVALID_HANDLE_VALUE_)
  318. sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
  319. else if (!::boost::winapi::DuplicateHandle(
  320. proc, sink_in, proc, &sink, 0,
  321. static_cast<::boost::winapi::BOOL_>(true),
  322. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  323. throw_last_error("Duplicate Pipe Failed");
  324. return basic_pipe<CharT, Traits>{source, sink};
  325. }
  326. inline bool operator==(const async_pipe & lhs, const async_pipe & rhs)
  327. {
  328. return compare_handles(lhs.native_source(), rhs.native_source()) &&
  329. compare_handles(lhs.native_sink(), rhs.native_sink());
  330. }
  331. inline bool operator!=(const async_pipe & lhs, const async_pipe & rhs)
  332. {
  333. return !compare_handles(lhs.native_source(), rhs.native_source()) ||
  334. !compare_handles(lhs.native_sink(), rhs.native_sink());
  335. }
  336. template<class Char, class Traits>
  337. inline bool operator==(const async_pipe & lhs, const basic_pipe<Char, Traits> & rhs)
  338. {
  339. return compare_handles(lhs.native_source(), rhs.native_source()) &&
  340. compare_handles(lhs.native_sink(), rhs.native_sink());
  341. }
  342. template<class Char, class Traits>
  343. inline bool operator!=(const async_pipe & lhs, const basic_pipe<Char, Traits> & rhs)
  344. {
  345. return !compare_handles(lhs.native_source(), rhs.native_source()) ||
  346. !compare_handles(lhs.native_sink(), rhs.native_sink());
  347. }
  348. template<class Char, class Traits>
  349. inline bool operator==(const basic_pipe<Char, Traits> & lhs, const async_pipe & rhs)
  350. {
  351. return compare_handles(lhs.native_source(), rhs.native_source()) &&
  352. compare_handles(lhs.native_sink(), rhs.native_sink());
  353. }
  354. template<class Char, class Traits>
  355. inline bool operator!=(const basic_pipe<Char, Traits> & lhs, const async_pipe & rhs)
  356. {
  357. return !compare_handles(lhs.native_source(), rhs.native_source()) ||
  358. !compare_handles(lhs.native_sink(), rhs.native_sink());
  359. }
  360. }}}}
  361. #endif /* INCLUDE_BOOST_PIPE_DETAIL_WINDOWS_ASYNC_PIPE_HPP_ */