async_pipe.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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/v1/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 { BOOST_PROCESS_V1_INLINE namespace v1 { 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(intptr_t(&cnt)); // to unclash Boost instances in plug-in DLLs
  29. name += "_";
  30. name += std::to_string(cnt++);
  31. return name;
  32. }
  33. class async_pipe
  34. {
  35. ::boost::asio::windows::stream_handle _source;
  36. ::boost::asio::windows::stream_handle _sink ;
  37. inline async_pipe(boost::asio::io_context & ios_source,
  38. boost::asio::io_context & ios_sink,
  39. const std::string & name, bool private_);
  40. public:
  41. typedef ::boost::winapi::HANDLE_ native_handle_type;
  42. typedef ::boost::asio::windows::stream_handle handle_type;
  43. typedef typename handle_type::executor_type executor_type;
  44. async_pipe(boost::asio::io_context & ios) : async_pipe(ios, ios, make_pipe_name(), true) {}
  45. async_pipe(boost::asio::io_context & ios_source, boost::asio::io_context & ios_sink)
  46. : async_pipe(ios_source, ios_sink, make_pipe_name(), true) {}
  47. async_pipe(boost::asio::io_context & ios, const std::string & name)
  48. : async_pipe(ios, ios, name, false) {}
  49. async_pipe(boost::asio::io_context & ios_source, boost::asio::io_context & ios_sink, const std::string & name)
  50. : async_pipe(ios_source, ios_sink, name, false) {}
  51. inline async_pipe(const async_pipe& rhs);
  52. async_pipe(async_pipe&& rhs) : _source(std::move(rhs._source)), _sink(std::move(rhs._sink))
  53. {
  54. }
  55. template<class CharT, class Traits = std::char_traits<CharT>>
  56. explicit async_pipe(::boost::asio::io_context & ios_source,
  57. ::boost::asio::io_context & ios_sink,
  58. const basic_pipe<CharT, Traits> & p)
  59. : _source(ios_source, p.native_source()), _sink(ios_sink, p.native_sink())
  60. {
  61. }
  62. template<class CharT, class Traits = std::char_traits<CharT>>
  63. explicit async_pipe(boost::asio::io_context & ios, const basic_pipe<CharT, Traits> & p)
  64. : async_pipe(ios, ios, p)
  65. {
  66. }
  67. template<class CharT, class Traits = std::char_traits<CharT>>
  68. inline async_pipe& operator=(const basic_pipe<CharT, Traits>& p);
  69. inline async_pipe& operator=(const async_pipe& rhs);
  70. inline async_pipe& operator=(async_pipe&& rhs);
  71. ~async_pipe()
  72. {
  73. boost::system::error_code ec;
  74. close(ec);
  75. }
  76. template<class CharT, class Traits = std::char_traits<CharT>>
  77. inline explicit operator basic_pipe<CharT, Traits>() const;
  78. void cancel()
  79. {
  80. if (_sink.is_open())
  81. _sink.cancel();
  82. if (_source.is_open())
  83. _source.cancel();
  84. }
  85. void close()
  86. {
  87. if (_sink.is_open())
  88. {
  89. _sink.close();
  90. _sink = handle_type(_sink.get_executor());
  91. }
  92. if (_source.is_open())
  93. {
  94. _source.close();
  95. _source = handle_type(_source.get_executor());
  96. }
  97. }
  98. void close(boost::system::error_code & ec)
  99. {
  100. if (_sink.is_open())
  101. {
  102. _sink.close(ec);
  103. _sink = handle_type(_sink.get_executor());
  104. }
  105. if (_source.is_open())
  106. {
  107. _source.close(ec);
  108. _source = handle_type(_source.get_executor());
  109. }
  110. }
  111. bool is_open() const
  112. {
  113. return _sink.is_open() || _source.is_open();
  114. }
  115. void async_close()
  116. {
  117. if (_sink.is_open())
  118. boost::asio::post(_sink.get_executor(), [this]{_sink.close();});
  119. if (_source.is_open())
  120. boost::asio::post(_source.get_executor(), [this]{_source.close();});
  121. }
  122. template<typename MutableBufferSequence>
  123. std::size_t read_some(const MutableBufferSequence & buffers)
  124. {
  125. return _source.read_some(buffers);
  126. }
  127. template<typename MutableBufferSequence>
  128. std::size_t write_some(const MutableBufferSequence & buffers)
  129. {
  130. return _sink.write_some(buffers);
  131. }
  132. template<typename MutableBufferSequence>
  133. std::size_t read_some(const MutableBufferSequence & buffers, boost::system::error_code & ec) noexcept
  134. {
  135. return _source.read_some(buffers, ec);
  136. }
  137. template<typename MutableBufferSequence>
  138. std::size_t write_some(const MutableBufferSequence & buffers, boost::system::error_code & ec) noexcept
  139. {
  140. return _sink.write_some(buffers, ec);
  141. }
  142. native_handle_type native_source() const {return const_cast<boost::asio::windows::stream_handle&>(_source).native_handle();}
  143. native_handle_type native_sink () const {return const_cast<boost::asio::windows::stream_handle&>(_sink ).native_handle();}
  144. template<typename MutableBufferSequence,
  145. typename ReadHandler>
  146. BOOST_ASIO_INITFN_RESULT_TYPE(
  147. ReadHandler, void(boost::system::error_code, std::size_t))
  148. async_read_some(
  149. const MutableBufferSequence & buffers,
  150. ReadHandler &&handler)
  151. {
  152. return _source.async_read_some(buffers, std::forward<ReadHandler>(handler));
  153. }
  154. template<typename ConstBufferSequence,
  155. typename WriteHandler>
  156. BOOST_ASIO_INITFN_RESULT_TYPE(
  157. WriteHandler, void(boost::system::error_code, std::size_t))
  158. async_write_some(
  159. const ConstBufferSequence & buffers,
  160. WriteHandler && handler)
  161. {
  162. return _sink.async_write_some(buffers, std::forward<WriteHandler>(handler));
  163. }
  164. const handle_type & sink () const & {return _sink;}
  165. const handle_type & source() const & {return _source;}
  166. handle_type && source() && { return std::move(_source); }
  167. handle_type && sink() && { return std::move(_sink); }
  168. handle_type source(::boost::asio::io_context& ios) &&
  169. {
  170. ::boost::asio::windows::stream_handle stolen(ios.get_executor(), _source.native_handle());
  171. boost::system::error_code ec;
  172. _source.assign(::boost::winapi::INVALID_HANDLE_VALUE_, ec);
  173. return stolen;
  174. }
  175. handle_type sink (::boost::asio::io_context& ios) &&
  176. {
  177. ::boost::asio::windows::stream_handle stolen(ios.get_executor(), _sink.native_handle());
  178. boost::system::error_code ec;
  179. _sink.assign(::boost::winapi::INVALID_HANDLE_VALUE_, ec);
  180. return stolen;
  181. }
  182. handle_type source(::boost::asio::io_context& ios) const &
  183. {
  184. auto proc = ::boost::winapi::GetCurrentProcess();
  185. ::boost::winapi::HANDLE_ source;
  186. auto source_in = const_cast<handle_type&>(_source).native_handle();
  187. if (source_in == ::boost::winapi::INVALID_HANDLE_VALUE_)
  188. source = ::boost::winapi::INVALID_HANDLE_VALUE_;
  189. else if (!::boost::winapi::DuplicateHandle(
  190. proc, source_in, proc, &source, 0,
  191. static_cast<::boost::winapi::BOOL_>(true),
  192. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  193. throw_last_error("Duplicate Pipe Failed");
  194. return ::boost::asio::windows::stream_handle(ios.get_executor(), source);
  195. }
  196. handle_type sink (::boost::asio::io_context& ios) const &
  197. {
  198. auto proc = ::boost::winapi::GetCurrentProcess();
  199. ::boost::winapi::HANDLE_ sink;
  200. auto sink_in = const_cast<handle_type&>(_sink).native_handle();
  201. if (sink_in == ::boost::winapi::INVALID_HANDLE_VALUE_)
  202. sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
  203. else if (!::boost::winapi::DuplicateHandle(
  204. proc, sink_in, proc, &sink, 0,
  205. static_cast<::boost::winapi::BOOL_>(true),
  206. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  207. throw_last_error("Duplicate Pipe Failed");
  208. return ::boost::asio::windows::stream_handle(ios.get_executor(), sink);
  209. }
  210. };
  211. async_pipe::async_pipe(const async_pipe& p) :
  212. _source(const_cast<handle_type&>(p._source).get_executor()),
  213. _sink (const_cast<handle_type&>(p._sink).get_executor())
  214. {
  215. auto proc = ::boost::winapi::GetCurrentProcess();
  216. ::boost::winapi::HANDLE_ source;
  217. ::boost::winapi::HANDLE_ sink;
  218. //cannot get the handle from a const object.
  219. auto source_in = const_cast<handle_type&>(p._source).native_handle();
  220. auto sink_in = const_cast<handle_type&>(p._sink).native_handle();
  221. if (source_in == ::boost::winapi::INVALID_HANDLE_VALUE_)
  222. source = ::boost::winapi::INVALID_HANDLE_VALUE_;
  223. else if (!::boost::winapi::DuplicateHandle(
  224. proc, source_in, proc, &source, 0,
  225. static_cast<::boost::winapi::BOOL_>(true),
  226. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  227. throw_last_error("Duplicate Pipe Failed");
  228. if (sink_in == ::boost::winapi::INVALID_HANDLE_VALUE_)
  229. sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
  230. else if (!::boost::winapi::DuplicateHandle(
  231. proc, sink_in, proc, &sink, 0,
  232. static_cast<::boost::winapi::BOOL_>(true),
  233. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  234. throw_last_error("Duplicate Pipe Failed");
  235. if (source != ::boost::winapi::INVALID_HANDLE_VALUE_)
  236. _source.assign(source);
  237. if (sink != ::boost::winapi::INVALID_HANDLE_VALUE_)
  238. _sink. assign(sink);
  239. }
  240. async_pipe::async_pipe(boost::asio::io_context & ios_source,
  241. boost::asio::io_context & ios_sink,
  242. const std::string & name, bool private_) : _source(ios_source), _sink(ios_sink)
  243. {
  244. static constexpr int FILE_FLAG_OVERLAPPED_ = 0x40000000; //temporary
  245. ::boost::winapi::HANDLE_ source = ::boost::winapi::create_named_pipe(
  246. #if defined(BOOST_NO_ANSI_APIS)
  247. ::boost::process::v1::detail::convert(name).c_str(),
  248. #else
  249. name.c_str(),
  250. #endif
  251. ::boost::winapi::PIPE_ACCESS_INBOUND_
  252. | FILE_FLAG_OVERLAPPED_, //write flag
  253. 0, private_ ? 1 : ::boost::winapi::PIPE_UNLIMITED_INSTANCES_, 8192, 8192, 0, nullptr);
  254. if (source == boost::winapi::INVALID_HANDLE_VALUE_)
  255. ::boost::process::v1::detail::throw_last_error("create_named_pipe(" + name + ") failed");
  256. _source.assign(source);
  257. ::boost::winapi::HANDLE_ sink = boost::winapi::create_file(
  258. #if defined(BOOST_NO_ANSI_APIS)
  259. ::boost::process::v1::detail::convert(name).c_str(),
  260. #else
  261. name.c_str(),
  262. #endif
  263. ::boost::winapi::GENERIC_WRITE_, 0, nullptr,
  264. ::boost::winapi::OPEN_EXISTING_,
  265. FILE_FLAG_OVERLAPPED_, //to allow read
  266. nullptr);
  267. if (sink == ::boost::winapi::INVALID_HANDLE_VALUE_)
  268. ::boost::process::v1::detail::throw_last_error("create_file() failed");
  269. _sink.assign(sink);
  270. }
  271. template<class CharT, class Traits>
  272. async_pipe& async_pipe::operator=(const basic_pipe<CharT, Traits> & p)
  273. {
  274. auto proc = ::boost::winapi::GetCurrentProcess();
  275. ::boost::winapi::HANDLE_ source;
  276. ::boost::winapi::HANDLE_ sink;
  277. //cannot get the handle from a const object.
  278. auto source_in = p.native_source();
  279. auto sink_in = p.native_sink();
  280. if (source_in == ::boost::winapi::INVALID_HANDLE_VALUE_)
  281. source = ::boost::winapi::INVALID_HANDLE_VALUE_;
  282. else if (!::boost::winapi::DuplicateHandle(
  283. proc, source_in.native_handle(), proc, &source, 0,
  284. static_cast<::boost::winapi::BOOL_>(true),
  285. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  286. throw_last_error("Duplicate Pipe Failed");
  287. if (sink_in == ::boost::winapi::INVALID_HANDLE_VALUE_)
  288. sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
  289. else if (!::boost::winapi::DuplicateHandle(
  290. proc, sink_in.native_handle(), proc, &sink, 0,
  291. static_cast<::boost::winapi::BOOL_>(true),
  292. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  293. throw_last_error("Duplicate Pipe Failed");
  294. //so we also assign the io_context
  295. if (source != ::boost::winapi::INVALID_HANDLE_VALUE_)
  296. _source.assign(source);
  297. if (sink != ::boost::winapi::INVALID_HANDLE_VALUE_)
  298. _sink.assign(sink);
  299. return *this;
  300. }
  301. async_pipe& async_pipe::operator=(const async_pipe & p)
  302. {
  303. auto proc = ::boost::winapi::GetCurrentProcess();
  304. ::boost::winapi::HANDLE_ source;
  305. ::boost::winapi::HANDLE_ sink;
  306. //cannot get the handle from a const object.
  307. auto &source_in = const_cast<::boost::asio::windows::stream_handle &>(p._source);
  308. auto &sink_in = const_cast<::boost::asio::windows::stream_handle &>(p._sink);
  309. source_in.get_executor();
  310. if (source_in.native_handle() == ::boost::winapi::INVALID_HANDLE_VALUE_)
  311. source = ::boost::winapi::INVALID_HANDLE_VALUE_;
  312. else if (!::boost::winapi::DuplicateHandle(
  313. proc, source_in.native_handle(), 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.native_handle() == ::boost::winapi::INVALID_HANDLE_VALUE_)
  318. sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
  319. else if (!::boost::winapi::DuplicateHandle(
  320. proc, sink_in.native_handle(), proc, &sink, 0,
  321. static_cast<::boost::winapi::BOOL_>(true),
  322. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  323. throw_last_error("Duplicate Pipe Failed");
  324. //so we also assign the io_context
  325. if (source != ::boost::winapi::INVALID_HANDLE_VALUE_)
  326. _source = ::boost::asio::windows::stream_handle(source_in.get_executor(), source);
  327. else
  328. _source = ::boost::asio::windows::stream_handle(source_in.get_executor());
  329. if (sink != ::boost::winapi::INVALID_HANDLE_VALUE_)
  330. _sink = ::boost::asio::windows::stream_handle(source_in.get_executor(), sink);
  331. else
  332. _sink = ::boost::asio::windows::stream_handle(source_in.get_executor());
  333. return *this;
  334. }
  335. async_pipe& async_pipe::operator=(async_pipe && rhs)
  336. {
  337. _source = std::move(rhs._source);
  338. _sink = std::move(rhs._sink);
  339. return *this;
  340. }
  341. template<class CharT, class Traits>
  342. async_pipe::operator basic_pipe<CharT, Traits>() const
  343. {
  344. auto proc = ::boost::winapi::GetCurrentProcess();
  345. ::boost::winapi::HANDLE_ source;
  346. ::boost::winapi::HANDLE_ sink;
  347. //cannot get the handle from a const object.
  348. auto source_in = const_cast<::boost::asio::windows::stream_handle &>(_source).native_handle();
  349. auto sink_in = const_cast<::boost::asio::windows::stream_handle &>(_sink).native_handle();
  350. if (source_in == ::boost::winapi::INVALID_HANDLE_VALUE_)
  351. source = ::boost::winapi::INVALID_HANDLE_VALUE_;
  352. else if (!::boost::winapi::DuplicateHandle(
  353. proc, source_in, proc, &source, 0,
  354. static_cast<::boost::winapi::BOOL_>(true),
  355. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  356. throw_last_error("Duplicate Pipe Failed");
  357. if (sink_in == ::boost::winapi::INVALID_HANDLE_VALUE_)
  358. sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
  359. else if (!::boost::winapi::DuplicateHandle(
  360. proc, sink_in, proc, &sink, 0,
  361. static_cast<::boost::winapi::BOOL_>(true),
  362. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  363. throw_last_error("Duplicate Pipe Failed");
  364. return basic_pipe<CharT, Traits>{source, sink};
  365. }
  366. inline bool operator==(const async_pipe & lhs, const async_pipe & rhs)
  367. {
  368. return compare_handles(lhs.native_source(), rhs.native_source()) &&
  369. compare_handles(lhs.native_sink(), rhs.native_sink());
  370. }
  371. inline bool operator!=(const async_pipe & lhs, const async_pipe & rhs)
  372. {
  373. return !compare_handles(lhs.native_source(), rhs.native_source()) ||
  374. !compare_handles(lhs.native_sink(), rhs.native_sink());
  375. }
  376. template<class Char, class Traits>
  377. inline bool operator==(const async_pipe & lhs, const basic_pipe<Char, Traits> & rhs)
  378. {
  379. return compare_handles(lhs.native_source(), rhs.native_source()) &&
  380. compare_handles(lhs.native_sink(), rhs.native_sink());
  381. }
  382. template<class Char, class Traits>
  383. inline bool operator!=(const async_pipe & lhs, const basic_pipe<Char, Traits> & rhs)
  384. {
  385. return !compare_handles(lhs.native_source(), rhs.native_source()) ||
  386. !compare_handles(lhs.native_sink(), rhs.native_sink());
  387. }
  388. template<class Char, class Traits>
  389. inline bool operator==(const basic_pipe<Char, Traits> & lhs, const async_pipe & rhs)
  390. {
  391. return compare_handles(lhs.native_source(), rhs.native_source()) &&
  392. compare_handles(lhs.native_sink(), rhs.native_sink());
  393. }
  394. template<class Char, class Traits>
  395. inline bool operator!=(const basic_pipe<Char, Traits> & lhs, const async_pipe & rhs)
  396. {
  397. return !compare_handles(lhs.native_source(), rhs.native_source()) ||
  398. !compare_handles(lhs.native_sink(), rhs.native_sink());
  399. }
  400. }}}}}
  401. #endif /* INCLUDE_BOOST_PIPE_DETAIL_WINDOWS_ASYNC_PIPE_HPP_ */