basic_pipe.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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_PIPE_HPP
  6. #define BOOST_PROCESS_DETAIL_WINDOWS_PIPE_HPP
  7. #include <boost/winapi/basic_types.hpp>
  8. #include <boost/winapi/error_codes.hpp>
  9. #include <boost/winapi/pipes.hpp>
  10. #include <boost/winapi/handles.hpp>
  11. #include <boost/winapi/file_management.hpp>
  12. #include <boost/winapi/get_last_error.hpp>
  13. #include <boost/winapi/access_rights.hpp>
  14. #include <boost/winapi/process.hpp>
  15. #include <boost/process/detail/windows/compare_handles.hpp>
  16. #include <system_error>
  17. #include <string>
  18. namespace boost { namespace process { namespace detail { namespace windows {
  19. template<class CharT, class Traits = std::char_traits<CharT>>
  20. class basic_pipe
  21. {
  22. ::boost::winapi::HANDLE_ _source = ::boost::winapi::INVALID_HANDLE_VALUE_;
  23. ::boost::winapi::HANDLE_ _sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
  24. public:
  25. typedef CharT char_type ;
  26. typedef Traits traits_type;
  27. typedef typename Traits::int_type int_type ;
  28. typedef typename Traits::pos_type pos_type ;
  29. typedef typename Traits::off_type off_type ;
  30. typedef ::boost::winapi::HANDLE_ native_handle_type;
  31. explicit basic_pipe(::boost::winapi::HANDLE_ source, ::boost::winapi::HANDLE_ sink)
  32. : _source(source), _sink(sink) {}
  33. inline explicit basic_pipe(const std::string & name);
  34. inline basic_pipe(const basic_pipe& p);
  35. basic_pipe(basic_pipe&& lhs) : _source(lhs._source), _sink(lhs._sink)
  36. {
  37. lhs._source = ::boost::winapi::INVALID_HANDLE_VALUE_;
  38. lhs._sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
  39. }
  40. inline basic_pipe& operator=(const basic_pipe& p);
  41. inline basic_pipe& operator=(basic_pipe&& lhs);
  42. ~basic_pipe()
  43. {
  44. if (_sink != ::boost::winapi::INVALID_HANDLE_VALUE_)
  45. ::boost::winapi::CloseHandle(_sink);
  46. if (_source != ::boost::winapi::INVALID_HANDLE_VALUE_)
  47. ::boost::winapi::CloseHandle(_source);
  48. }
  49. native_handle_type native_source() const {return _source;}
  50. native_handle_type native_sink () const {return _sink;}
  51. void assign_source(native_handle_type h) { _source = h;}
  52. void assign_sink (native_handle_type h) { _sink = h;}
  53. basic_pipe()
  54. {
  55. if (!::boost::winapi::CreatePipe(&_source, &_sink, nullptr, 0))
  56. throw_last_error("CreatePipe() failed");
  57. }
  58. int_type write(const char_type * data, int_type count)
  59. {
  60. ::boost::winapi::DWORD_ write_len;
  61. if (!::boost::winapi::WriteFile(
  62. _sink, data, count * sizeof(char_type), &write_len, nullptr
  63. ))
  64. {
  65. auto ec = ::boost::process::detail::get_last_error();
  66. if ((ec.value() == ::boost::winapi::ERROR_BROKEN_PIPE_) ||
  67. (ec.value() == ::boost::winapi::ERROR_NO_DATA_))
  68. return 0;
  69. else
  70. throw process_error(ec, "WriteFile failed");
  71. }
  72. return static_cast<int_type>(write_len);
  73. }
  74. int_type read(char_type * data, int_type count)
  75. {
  76. ::boost::winapi::DWORD_ read_len;
  77. if (!::boost::winapi::ReadFile(
  78. _source, data, count * sizeof(char_type), &read_len, nullptr
  79. ))
  80. {
  81. auto ec = ::boost::process::detail::get_last_error();
  82. if ((ec.value() == ::boost::winapi::ERROR_BROKEN_PIPE_) ||
  83. (ec.value() == ::boost::winapi::ERROR_NO_DATA_))
  84. return 0;
  85. else
  86. throw process_error(ec, "ReadFile failed");
  87. }
  88. return static_cast<int_type>(read_len);
  89. }
  90. bool is_open() const
  91. {
  92. return (_source != ::boost::winapi::INVALID_HANDLE_VALUE_) ||
  93. (_sink != ::boost::winapi::INVALID_HANDLE_VALUE_);
  94. }
  95. void close()
  96. {
  97. ::boost::winapi::CloseHandle(_source);
  98. ::boost::winapi::CloseHandle(_sink);
  99. _source = ::boost::winapi::INVALID_HANDLE_VALUE_;
  100. _sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
  101. }
  102. };
  103. template<class Char, class Traits>
  104. basic_pipe<Char, Traits>::basic_pipe(const basic_pipe & p)
  105. {
  106. auto proc = ::boost::winapi::GetCurrentProcess();
  107. if (p._source == ::boost::winapi::INVALID_HANDLE_VALUE_)
  108. _source = ::boost::winapi::INVALID_HANDLE_VALUE_;
  109. else if (!::boost::winapi::DuplicateHandle(
  110. proc, p._source, proc, &_source, 0,
  111. static_cast<::boost::winapi::BOOL_>(true),
  112. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  113. throw_last_error("Duplicate Pipe Failed");
  114. if (p._sink == ::boost::winapi::INVALID_HANDLE_VALUE_)
  115. _sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
  116. else if (!::boost::winapi::DuplicateHandle(
  117. proc, p._sink, proc, &_sink, 0,
  118. static_cast<::boost::winapi::BOOL_>(true),
  119. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  120. throw_last_error("Duplicate Pipe Failed");
  121. }
  122. template<class Char, class Traits>
  123. basic_pipe<Char, Traits>::basic_pipe(const std::string & name)
  124. {
  125. static constexpr int OPEN_EXISTING_ = 3; //temporary.
  126. static constexpr int FILE_FLAG_OVERLAPPED_ = 0x40000000; //temporary
  127. //static constexpr int FILE_ATTRIBUTE_NORMAL_ = 0x00000080; //temporary
  128. #if BOOST_NO_ANSI_APIS
  129. std::wstring name_ = boost::process::detail::convert(name);
  130. #else
  131. auto &name_ = name;
  132. #endif
  133. ::boost::winapi::HANDLE_ source = ::boost::winapi::create_named_pipe(
  134. name_.c_str(),
  135. ::boost::winapi::PIPE_ACCESS_INBOUND_
  136. | FILE_FLAG_OVERLAPPED_, //write flag
  137. 0, ::boost::winapi::PIPE_UNLIMITED_INSTANCES_, 8192, 8192, 0, nullptr);
  138. if (source == boost::winapi::INVALID_HANDLE_VALUE_)
  139. ::boost::process::detail::throw_last_error("create_named_pipe() failed");
  140. ::boost::winapi::HANDLE_ sink = boost::winapi::create_file(
  141. name.c_str(),
  142. ::boost::winapi::GENERIC_WRITE_, 0, nullptr,
  143. OPEN_EXISTING_,
  144. FILE_FLAG_OVERLAPPED_, //to allow read
  145. nullptr);
  146. if (sink == ::boost::winapi::INVALID_HANDLE_VALUE_)
  147. ::boost::process::detail::throw_last_error("create_file() failed");
  148. _source = source;
  149. _sink = sink;
  150. }
  151. template<class Char, class Traits>
  152. basic_pipe<Char, Traits>& basic_pipe<Char, Traits>::operator=(const basic_pipe & p)
  153. {
  154. auto proc = ::boost::winapi::GetCurrentProcess();
  155. if (p._source == ::boost::winapi::INVALID_HANDLE_VALUE_)
  156. _source = ::boost::winapi::INVALID_HANDLE_VALUE_;
  157. else if (!::boost::winapi::DuplicateHandle(
  158. proc, p._source, proc, &_source, 0,
  159. static_cast<::boost::winapi::BOOL_>(true),
  160. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  161. throw_last_error("Duplicate Pipe Failed");
  162. if (p._sink == ::boost::winapi::INVALID_HANDLE_VALUE_)
  163. _sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
  164. else if (!::boost::winapi::DuplicateHandle(
  165. proc, p._sink, proc, &_sink, 0,
  166. static_cast<::boost::winapi::BOOL_>(true),
  167. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  168. throw_last_error("Duplicate Pipe Failed");
  169. return *this;
  170. }
  171. template<class Char, class Traits>
  172. basic_pipe<Char, Traits>& basic_pipe<Char, Traits>::operator=(basic_pipe && lhs)
  173. {
  174. if (_source != ::boost::winapi::INVALID_HANDLE_VALUE_)
  175. ::boost::winapi::CloseHandle(_source);
  176. if (_sink != ::boost::winapi::INVALID_HANDLE_VALUE_)
  177. ::boost::winapi::CloseHandle(_sink);
  178. _source = lhs._source;
  179. _sink = lhs._sink;
  180. lhs._source = ::boost::winapi::INVALID_HANDLE_VALUE_;
  181. lhs._sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
  182. return *this;
  183. }
  184. template<class Char, class Traits>
  185. inline bool operator==(const basic_pipe<Char, Traits> & lhs, const basic_pipe<Char, Traits> & rhs)
  186. {
  187. return compare_handles(lhs.native_source(), rhs.native_source()) &&
  188. compare_handles(lhs.native_sink(), rhs.native_sink());
  189. }
  190. template<class Char, class Traits>
  191. inline bool operator!=(const basic_pipe<Char, Traits> & lhs, const basic_pipe<Char, Traits> & rhs)
  192. {
  193. return !compare_handles(lhs.native_source(), rhs.native_source()) ||
  194. !compare_handles(lhs.native_sink(), rhs.native_sink());
  195. }
  196. }}}}
  197. #endif