Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

basic_stream_handle.hpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. //
  2. // windows/basic_stream_handle.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_WINDOWS_BASIC_STREAM_HANDLE_HPP
  11. #define ASIO_WINDOWS_BASIC_STREAM_HANDLE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #if defined(ASIO_HAS_WINDOWS_STREAM_HANDLE) \
  17. || defined(GENERATING_DOCUMENTATION)
  18. #include <cstddef>
  19. #include "asio/detail/handler_type_requirements.hpp"
  20. #include "asio/detail/throw_error.hpp"
  21. #include "asio/error.hpp"
  22. #include "asio/windows/basic_handle.hpp"
  23. #include "asio/windows/stream_handle_service.hpp"
  24. #include "asio/detail/push_options.hpp"
  25. namespace asio {
  26. namespace windows {
  27. /// Provides stream-oriented handle functionality.
  28. /**
  29. * The windows::basic_stream_handle class template provides asynchronous and
  30. * blocking stream-oriented handle functionality.
  31. *
  32. * @par Thread Safety
  33. * @e Distinct @e objects: Safe.@n
  34. * @e Shared @e objects: Unsafe.
  35. *
  36. * @par Concepts:
  37. * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
  38. */
  39. template <typename StreamHandleService = stream_handle_service>
  40. class basic_stream_handle
  41. : public basic_handle<StreamHandleService>
  42. {
  43. public:
  44. /// The native representation of a handle.
  45. typedef typename StreamHandleService::native_handle_type native_handle_type;
  46. /// Construct a basic_stream_handle without opening it.
  47. /**
  48. * This constructor creates a stream handle without opening it. The handle
  49. * needs to be opened and then connected or accepted before data can be sent
  50. * or received on it.
  51. *
  52. * @param io_context The io_context object that the stream handle will use to
  53. * dispatch handlers for any asynchronous operations performed on the handle.
  54. */
  55. explicit basic_stream_handle(asio::io_context& io_context)
  56. : basic_handle<StreamHandleService>(io_context)
  57. {
  58. }
  59. /// Construct a basic_stream_handle on an existing native handle.
  60. /**
  61. * This constructor creates a stream handle object to hold an existing native
  62. * handle.
  63. *
  64. * @param io_context The io_context object that the stream handle will use to
  65. * dispatch handlers for any asynchronous operations performed on the handle.
  66. *
  67. * @param handle The new underlying handle implementation.
  68. *
  69. * @throws asio::system_error Thrown on failure.
  70. */
  71. basic_stream_handle(asio::io_context& io_context,
  72. const native_handle_type& handle)
  73. : basic_handle<StreamHandleService>(io_context, handle)
  74. {
  75. }
  76. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  77. /// Move-construct a basic_stream_handle from another.
  78. /**
  79. * This constructor moves a stream handle from one object to another.
  80. *
  81. * @param other The other basic_stream_handle object from which the move
  82. * will occur.
  83. *
  84. * @note Following the move, the moved-from object is in the same state as if
  85. * constructed using the @c basic_stream_handle(io_context&) constructor.
  86. */
  87. basic_stream_handle(basic_stream_handle&& other)
  88. : basic_handle<StreamHandleService>(
  89. ASIO_MOVE_CAST(basic_stream_handle)(other))
  90. {
  91. }
  92. /// Move-assign a basic_stream_handle from another.
  93. /**
  94. * This assignment operator moves a stream handle from one object to
  95. * another.
  96. *
  97. * @param other The other basic_stream_handle object from which the move
  98. * will occur.
  99. *
  100. * @note Following the move, the moved-from object is in the same state as if
  101. * constructed using the @c basic_stream_handle(io_context&) constructor.
  102. */
  103. basic_stream_handle& operator=(basic_stream_handle&& other)
  104. {
  105. basic_handle<StreamHandleService>::operator=(
  106. ASIO_MOVE_CAST(basic_stream_handle)(other));
  107. return *this;
  108. }
  109. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  110. /// Write some data to the handle.
  111. /**
  112. * This function is used to write data to the stream handle. The function call
  113. * will block until one or more bytes of the data has been written
  114. * successfully, or until an error occurs.
  115. *
  116. * @param buffers One or more data buffers to be written to the handle.
  117. *
  118. * @returns The number of bytes written.
  119. *
  120. * @throws asio::system_error Thrown on failure. An error code of
  121. * asio::error::eof indicates that the connection was closed by the
  122. * peer.
  123. *
  124. * @note The write_some operation may not transmit all of the data to the
  125. * peer. Consider using the @ref write function if you need to ensure that
  126. * all data is written before the blocking operation completes.
  127. *
  128. * @par Example
  129. * To write a single data buffer use the @ref buffer function as follows:
  130. * @code
  131. * handle.write_some(asio::buffer(data, size));
  132. * @endcode
  133. * See the @ref buffer documentation for information on writing multiple
  134. * buffers in one go, and how to use it with arrays, boost::array or
  135. * std::vector.
  136. */
  137. template <typename ConstBufferSequence>
  138. std::size_t write_some(const ConstBufferSequence& buffers)
  139. {
  140. asio::error_code ec;
  141. std::size_t s = this->get_service().write_some(
  142. this->get_implementation(), buffers, ec);
  143. asio::detail::throw_error(ec, "write_some");
  144. return s;
  145. }
  146. /// Write some data to the handle.
  147. /**
  148. * This function is used to write data to the stream handle. The function call
  149. * will block until one or more bytes of the data has been written
  150. * successfully, or until an error occurs.
  151. *
  152. * @param buffers One or more data buffers to be written to the handle.
  153. *
  154. * @param ec Set to indicate what error occurred, if any.
  155. *
  156. * @returns The number of bytes written. Returns 0 if an error occurred.
  157. *
  158. * @note The write_some operation may not transmit all of the data to the
  159. * peer. Consider using the @ref write function if you need to ensure that
  160. * all data is written before the blocking operation completes.
  161. */
  162. template <typename ConstBufferSequence>
  163. std::size_t write_some(const ConstBufferSequence& buffers,
  164. asio::error_code& ec)
  165. {
  166. return this->get_service().write_some(
  167. this->get_implementation(), buffers, ec);
  168. }
  169. /// Start an asynchronous write.
  170. /**
  171. * This function is used to asynchronously write data to the stream handle.
  172. * The function call always returns immediately.
  173. *
  174. * @param buffers One or more data buffers to be written to the handle.
  175. * Although the buffers object may be copied as necessary, ownership of the
  176. * underlying memory blocks is retained by the caller, which must guarantee
  177. * that they remain valid until the handler is called.
  178. *
  179. * @param handler The handler to be called when the write operation completes.
  180. * Copies will be made of the handler as required. The function signature of
  181. * the handler must be:
  182. * @code void handler(
  183. * const asio::error_code& error, // Result of operation.
  184. * std::size_t bytes_transferred // Number of bytes written.
  185. * ); @endcode
  186. * Regardless of whether the asynchronous operation completes immediately or
  187. * not, the handler will not be invoked from within this function. Invocation
  188. * of the handler will be performed in a manner equivalent to using
  189. * asio::io_context::post().
  190. *
  191. * @note The write operation may not transmit all of the data to the peer.
  192. * Consider using the @ref async_write function if you need to ensure that all
  193. * data is written before the asynchronous operation completes.
  194. *
  195. * @par Example
  196. * To write a single data buffer use the @ref buffer function as follows:
  197. * @code
  198. * handle.async_write_some(asio::buffer(data, size), handler);
  199. * @endcode
  200. * See the @ref buffer documentation for information on writing multiple
  201. * buffers in one go, and how to use it with arrays, boost::array or
  202. * std::vector.
  203. */
  204. template <typename ConstBufferSequence, typename WriteHandler>
  205. ASIO_INITFN_RESULT_TYPE(WriteHandler,
  206. void (asio::error_code, std::size_t))
  207. async_write_some(const ConstBufferSequence& buffers,
  208. ASIO_MOVE_ARG(WriteHandler) handler)
  209. {
  210. // If you get an error on the following line it means that your handler does
  211. // not meet the documented type requirements for a WriteHandler.
  212. ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  213. return this->get_service().async_write_some(this->get_implementation(),
  214. buffers, ASIO_MOVE_CAST(WriteHandler)(handler));
  215. }
  216. /// Read some data from the handle.
  217. /**
  218. * This function is used to read data from the stream handle. The function
  219. * call will block until one or more bytes of data has been read successfully,
  220. * or until an error occurs.
  221. *
  222. * @param buffers One or more buffers into which the data will be read.
  223. *
  224. * @returns The number of bytes read.
  225. *
  226. * @throws asio::system_error Thrown on failure. An error code of
  227. * asio::error::eof indicates that the connection was closed by the
  228. * peer.
  229. *
  230. * @note The read_some operation may not read all of the requested number of
  231. * bytes. Consider using the @ref read function if you need to ensure that
  232. * the requested amount of data is read before the blocking operation
  233. * completes.
  234. *
  235. * @par Example
  236. * To read into a single data buffer use the @ref buffer function as follows:
  237. * @code
  238. * handle.read_some(asio::buffer(data, size));
  239. * @endcode
  240. * See the @ref buffer documentation for information on reading into multiple
  241. * buffers in one go, and how to use it with arrays, boost::array or
  242. * std::vector.
  243. */
  244. template <typename MutableBufferSequence>
  245. std::size_t read_some(const MutableBufferSequence& buffers)
  246. {
  247. asio::error_code ec;
  248. std::size_t s = this->get_service().read_some(
  249. this->get_implementation(), buffers, ec);
  250. asio::detail::throw_error(ec, "read_some");
  251. return s;
  252. }
  253. /// Read some data from the handle.
  254. /**
  255. * This function is used to read data from the stream handle. The function
  256. * call will block until one or more bytes of data has been read successfully,
  257. * or until an error occurs.
  258. *
  259. * @param buffers One or more buffers into which the data will be read.
  260. *
  261. * @param ec Set to indicate what error occurred, if any.
  262. *
  263. * @returns The number of bytes read. Returns 0 if an error occurred.
  264. *
  265. * @note The read_some operation may not read all of the requested number of
  266. * bytes. Consider using the @ref read function if you need to ensure that
  267. * the requested amount of data is read before the blocking operation
  268. * completes.
  269. */
  270. template <typename MutableBufferSequence>
  271. std::size_t read_some(const MutableBufferSequence& buffers,
  272. asio::error_code& ec)
  273. {
  274. return this->get_service().read_some(
  275. this->get_implementation(), buffers, ec);
  276. }
  277. /// Start an asynchronous read.
  278. /**
  279. * This function is used to asynchronously read data from the stream handle.
  280. * The function call always returns immediately.
  281. *
  282. * @param buffers One or more buffers into which the data will be read.
  283. * Although the buffers object may be copied as necessary, ownership of the
  284. * underlying memory blocks is retained by the caller, which must guarantee
  285. * that they remain valid until the handler is called.
  286. *
  287. * @param handler The handler to be called when the read operation completes.
  288. * Copies will be made of the handler as required. The function signature of
  289. * the handler must be:
  290. * @code void handler(
  291. * const asio::error_code& error, // Result of operation.
  292. * std::size_t bytes_transferred // Number of bytes read.
  293. * ); @endcode
  294. * Regardless of whether the asynchronous operation completes immediately or
  295. * not, the handler will not be invoked from within this function. Invocation
  296. * of the handler will be performed in a manner equivalent to using
  297. * asio::io_context::post().
  298. *
  299. * @note The read operation may not read all of the requested number of bytes.
  300. * Consider using the @ref async_read function if you need to ensure that the
  301. * requested amount of data is read before the asynchronous operation
  302. * completes.
  303. *
  304. * @par Example
  305. * To read into a single data buffer use the @ref buffer function as follows:
  306. * @code
  307. * handle.async_read_some(asio::buffer(data, size), handler);
  308. * @endcode
  309. * See the @ref buffer documentation for information on reading into multiple
  310. * buffers in one go, and how to use it with arrays, boost::array or
  311. * std::vector.
  312. */
  313. template <typename MutableBufferSequence, typename ReadHandler>
  314. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  315. void (asio::error_code, std::size_t))
  316. async_read_some(const MutableBufferSequence& buffers,
  317. ASIO_MOVE_ARG(ReadHandler) handler)
  318. {
  319. // If you get an error on the following line it means that your handler does
  320. // not meet the documented type requirements for a ReadHandler.
  321. ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  322. return this->get_service().async_read_some(this->get_implementation(),
  323. buffers, ASIO_MOVE_CAST(ReadHandler)(handler));
  324. }
  325. };
  326. } // namespace windows
  327. } // namespace asio
  328. #include "asio/detail/pop_options.hpp"
  329. #endif // defined(ASIO_HAS_WINDOWS_STREAM_HANDLE)
  330. // || defined(GENERATING_DOCUMENTATION)
  331. #endif // ASIO_WINDOWS_BASIC_STREAM_HANDLE_HPP