basic_stream_descriptor.hpp 14 KB

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