basic_pipe.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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()
  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. ::boost::winapi::HANDLE_ source = ::boost::winapi::create_named_pipe(
  129. name.c_str(),
  130. ::boost::winapi::PIPE_ACCESS_INBOUND_
  131. | FILE_FLAG_OVERLAPPED_, //write flag
  132. 0, 1, 8192, 8192, 0, nullptr);
  133. if (source == boost::winapi::INVALID_HANDLE_VALUE_)
  134. ::boost::process::detail::throw_last_error("create_named_pipe() failed");
  135. ::boost::winapi::HANDLE_ sink = boost::winapi::create_file(
  136. name.c_str(),
  137. ::boost::winapi::GENERIC_WRITE_, 0, nullptr,
  138. OPEN_EXISTING_,
  139. FILE_FLAG_OVERLAPPED_, //to allow read
  140. nullptr);
  141. if (sink == ::boost::winapi::INVALID_HANDLE_VALUE_)
  142. ::boost::process::detail::throw_last_error("create_file() failed");
  143. _source = source;
  144. _sink = sink;
  145. }
  146. template<class Char, class Traits>
  147. basic_pipe<Char, Traits>& basic_pipe<Char, Traits>::operator=(const basic_pipe & p)
  148. {
  149. auto proc = ::boost::winapi::GetCurrentProcess();
  150. if (p._source == ::boost::winapi::INVALID_HANDLE_VALUE_)
  151. _source = ::boost::winapi::INVALID_HANDLE_VALUE_;
  152. else if (!::boost::winapi::DuplicateHandle(
  153. proc, p._source, proc, &_source, 0,
  154. static_cast<::boost::winapi::BOOL_>(true),
  155. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  156. throw_last_error("Duplicate Pipe Failed");
  157. if (p._sink == ::boost::winapi::INVALID_HANDLE_VALUE_)
  158. _sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
  159. else if (!::boost::winapi::DuplicateHandle(
  160. proc, p._sink, proc, &_sink, 0,
  161. static_cast<::boost::winapi::BOOL_>(true),
  162. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  163. throw_last_error("Duplicate Pipe Failed");
  164. return *this;
  165. }
  166. template<class Char, class Traits>
  167. basic_pipe<Char, Traits>& basic_pipe<Char, Traits>::operator=(basic_pipe && lhs)
  168. {
  169. if (_source != ::boost::winapi::INVALID_HANDLE_VALUE_)
  170. ::boost::winapi::CloseHandle(_source);
  171. if (_sink != ::boost::winapi::INVALID_HANDLE_VALUE_)
  172. ::boost::winapi::CloseHandle(_sink);
  173. _source = lhs._source;
  174. _sink = lhs._sink;
  175. lhs._source = ::boost::winapi::INVALID_HANDLE_VALUE_;
  176. lhs._sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
  177. return *this;
  178. }
  179. template<class Char, class Traits>
  180. inline bool operator==(const basic_pipe<Char, Traits> & lhs, const basic_pipe<Char, Traits> & rhs)
  181. {
  182. return compare_handles(lhs.native_source(), rhs.native_source()) &&
  183. compare_handles(lhs.native_sink(), rhs.native_sink());
  184. }
  185. template<class Char, class Traits>
  186. inline bool operator!=(const basic_pipe<Char, Traits> & lhs, const basic_pipe<Char, Traits> & rhs)
  187. {
  188. return !compare_handles(lhs.native_source(), rhs.native_source()) ||
  189. !compare_handles(lhs.native_sink(), rhs.native_sink());
  190. }
  191. }}}}
  192. #endif