async_pipe.hpp 15 KB

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