basic_stream_descriptor.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. //
  2. // posix/basic_stream_descriptor.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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 BOOST_ASIO_POSIX_BASIC_STREAM_DESCRIPTOR_HPP
  11. #define BOOST_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 <boost/asio/detail/config.hpp>
  16. #include <boost/asio/posix/descriptor.hpp>
  17. #if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) \
  18. || defined(GENERATING_DOCUMENTATION)
  19. namespace boost {
  20. namespace asio {
  21. namespace posix {
  22. /// Provides stream-oriented descriptor functionality.
  23. /**
  24. * The posix::basic_stream_descriptor class template provides asynchronous and
  25. * blocking stream-oriented descriptor functionality.
  26. *
  27. * @par Thread Safety
  28. * @e Distinct @e objects: Safe.@n
  29. * @e Shared @e objects: Unsafe.
  30. *
  31. * @par Concepts:
  32. * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
  33. */
  34. template <typename Executor = executor>
  35. class basic_stream_descriptor
  36. : public basic_descriptor<Executor>
  37. {
  38. public:
  39. /// The type of the executor associated with the object.
  40. typedef Executor executor_type;
  41. /// Rebinds the descriptor type to another executor.
  42. template <typename Executor1>
  43. struct rebind_executor
  44. {
  45. /// The descriptor type when rebound to the specified executor.
  46. typedef basic_stream_descriptor<Executor1> other;
  47. };
  48. /// The native representation of a descriptor.
  49. typedef typename basic_descriptor<Executor>::native_handle_type
  50. native_handle_type;
  51. /// Construct a stream descriptor without opening it.
  52. /**
  53. * This constructor creates a stream descriptor without opening it. The
  54. * descriptor needs to be opened and then connected or accepted before data
  55. * can be sent or received on it.
  56. *
  57. * @param ex The I/O executor that the descriptor will use, by default, to
  58. * dispatch handlers for any asynchronous operations performed on the
  59. * descriptor.
  60. */
  61. explicit basic_stream_descriptor(const executor_type& ex)
  62. : basic_descriptor<Executor>(ex)
  63. {
  64. }
  65. /// Construct a stream descriptor without opening it.
  66. /**
  67. * This constructor creates a stream descriptor without opening it. The
  68. * descriptor needs to be opened and then connected or accepted before data
  69. * can be sent or received on it.
  70. *
  71. * @param context An execution context which provides the I/O executor that
  72. * the descriptor will use, by default, to dispatch handlers for any
  73. * asynchronous operations performed on the descriptor.
  74. */
  75. template <typename ExecutionContext>
  76. explicit basic_stream_descriptor(ExecutionContext& context,
  77. typename enable_if<
  78. is_convertible<ExecutionContext&, execution_context&>::value
  79. >::type* = 0)
  80. : basic_descriptor<Executor>(context)
  81. {
  82. }
  83. /// Construct a stream descriptor on an existing native descriptor.
  84. /**
  85. * This constructor creates a stream descriptor object to hold an existing
  86. * native descriptor.
  87. *
  88. * @param ex The I/O executor that the descriptor will use, by default, to
  89. * dispatch handlers for any asynchronous operations performed on the
  90. * descriptor.
  91. *
  92. * @param native_descriptor The new underlying descriptor implementation.
  93. *
  94. * @throws boost::system::system_error Thrown on failure.
  95. */
  96. basic_stream_descriptor(const executor_type& ex,
  97. const native_handle_type& native_descriptor)
  98. : basic_descriptor<Executor>(ex, native_descriptor)
  99. {
  100. }
  101. /// Construct a stream descriptor on an existing native descriptor.
  102. /**
  103. * This constructor creates a stream descriptor object to hold an existing
  104. * native descriptor.
  105. *
  106. * @param context An execution context which provides the I/O executor that
  107. * the descriptor will use, by default, to dispatch handlers for any
  108. * asynchronous operations performed on the descriptor.
  109. *
  110. * @param native_descriptor The new underlying descriptor implementation.
  111. *
  112. * @throws boost::system::system_error Thrown on failure.
  113. */
  114. template <typename ExecutionContext>
  115. basic_stream_descriptor(ExecutionContext& context,
  116. const native_handle_type& native_descriptor,
  117. typename enable_if<
  118. is_convertible<ExecutionContext&, execution_context&>::value
  119. >::type* = 0)
  120. : basic_descriptor<Executor>(context, native_descriptor)
  121. {
  122. }
  123. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  124. /// Move-construct a stream descriptor from another.
  125. /**
  126. * This constructor moves a stream descriptor from one object to another.
  127. *
  128. * @param other The other stream descriptor object from which the move
  129. * will occur.
  130. *
  131. * @note Following the move, the moved-from object is in the same state as if
  132. * constructed using the @c basic_stream_descriptor(const executor_type&)
  133. * constructor.
  134. */
  135. basic_stream_descriptor(basic_stream_descriptor&& other)
  136. : descriptor(std::move(other))
  137. {
  138. }
  139. /// Move-assign a stream descriptor from another.
  140. /**
  141. * This assignment operator moves a stream descriptor from one object to
  142. * another.
  143. *
  144. * @param other The other stream descriptor object from which the move
  145. * will occur.
  146. *
  147. * @note Following the move, the moved-from object is in the same state as if
  148. * constructed using the @c basic_stream_descriptor(const executor_type&)
  149. * constructor.
  150. */
  151. basic_stream_descriptor& operator=(basic_stream_descriptor&& other)
  152. {
  153. descriptor::operator=(std::move(other));
  154. return *this;
  155. }
  156. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  157. /// Write some data to the descriptor.
  158. /**
  159. * This function is used to write data to the stream descriptor. The function
  160. * call will block until one or more bytes of the data has been written
  161. * successfully, or until an error occurs.
  162. *
  163. * @param buffers One or more data buffers to be written to the descriptor.
  164. *
  165. * @returns The number of bytes written.
  166. *
  167. * @throws boost::system::system_error Thrown on failure. An error code of
  168. * boost::asio::error::eof indicates that the connection was closed by the
  169. * peer.
  170. *
  171. * @note The write_some operation may not transmit all of the data to the
  172. * peer. Consider using the @ref write function if you need to ensure that
  173. * all data is written before the blocking operation completes.
  174. *
  175. * @par Example
  176. * To write a single data buffer use the @ref buffer function as follows:
  177. * @code
  178. * descriptor.write_some(boost::asio::buffer(data, size));
  179. * @endcode
  180. * See the @ref buffer documentation for information on writing multiple
  181. * buffers in one go, and how to use it with arrays, boost::array or
  182. * std::vector.
  183. */
  184. template <typename ConstBufferSequence>
  185. std::size_t write_some(const ConstBufferSequence& buffers)
  186. {
  187. boost::system::error_code ec;
  188. std::size_t s = this->impl_.get_service().write_some(
  189. this->impl_.get_implementation(), buffers, ec);
  190. boost::asio::detail::throw_error(ec, "write_some");
  191. return s;
  192. }
  193. /// Write some data to the descriptor.
  194. /**
  195. * This function is used to write data to the stream descriptor. The function
  196. * call will block until one or more bytes of the data has been written
  197. * successfully, or until an error occurs.
  198. *
  199. * @param buffers One or more data buffers to be written to the descriptor.
  200. *
  201. * @param ec Set to indicate what error occurred, if any.
  202. *
  203. * @returns The number of bytes written. Returns 0 if an error occurred.
  204. *
  205. * @note The write_some operation may not transmit all of the data to the
  206. * peer. Consider using the @ref write function if you need to ensure that
  207. * all data is written before the blocking operation completes.
  208. */
  209. template <typename ConstBufferSequence>
  210. std::size_t write_some(const ConstBufferSequence& buffers,
  211. boost::system::error_code& ec)
  212. {
  213. return this->impl_.get_service().write_some(
  214. this->impl_.get_implementation(), buffers, ec);
  215. }
  216. /// Start an asynchronous write.
  217. /**
  218. * This function is used to asynchronously write data to the stream
  219. * descriptor. The function call always returns immediately.
  220. *
  221. * @param buffers One or more data buffers to be written to the descriptor.
  222. * Although the buffers object may be copied as necessary, ownership of the
  223. * underlying memory blocks is retained by the caller, which must guarantee
  224. * that they remain valid until the handler is called.
  225. *
  226. * @param handler The handler to be called when the write operation completes.
  227. * Copies will be made of the handler as required. The function signature of
  228. * the handler must be:
  229. * @code void handler(
  230. * const boost::system::error_code& error, // Result of operation.
  231. * std::size_t bytes_transferred // Number of bytes written.
  232. * ); @endcode
  233. * Regardless of whether the asynchronous operation completes immediately or
  234. * not, the handler will not be invoked from within this function. On
  235. * immediate completion, invocation of the handler will be performed in a
  236. * manner equivalent to using boost::asio::post().
  237. *
  238. * @note The write operation may not transmit all of the data to the peer.
  239. * Consider using the @ref async_write function if you need to ensure that all
  240. * data is written before the asynchronous operation completes.
  241. *
  242. * @par Example
  243. * To write a single data buffer use the @ref buffer function as follows:
  244. * @code
  245. * descriptor.async_write_some(boost::asio::buffer(data, size), handler);
  246. * @endcode
  247. * See the @ref buffer documentation for information on writing multiple
  248. * buffers in one go, and how to use it with arrays, boost::array or
  249. * std::vector.
  250. */
  251. template <typename ConstBufferSequence,
  252. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  253. std::size_t)) WriteHandler
  254. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  255. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
  256. void (boost::system::error_code, std::size_t))
  257. async_write_some(const ConstBufferSequence& buffers,
  258. BOOST_ASIO_MOVE_ARG(WriteHandler) handler
  259. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  260. {
  261. return async_initiate<WriteHandler,
  262. void (boost::system::error_code, std::size_t)>(
  263. initiate_async_write_some(this), handler, buffers);
  264. }
  265. /// Read some data from the descriptor.
  266. /**
  267. * This function is used to read data from the stream descriptor. The function
  268. * call will block until one or more bytes of data has been read successfully,
  269. * or until an error occurs.
  270. *
  271. * @param buffers One or more buffers into which the data will be read.
  272. *
  273. * @returns The number of bytes read.
  274. *
  275. * @throws boost::system::system_error Thrown on failure. An error code of
  276. * boost::asio::error::eof indicates that the connection was closed by the
  277. * peer.
  278. *
  279. * @note The read_some operation may not read all of the requested number of
  280. * bytes. Consider using the @ref read function if you need to ensure that
  281. * the requested amount of data is read before the blocking operation
  282. * completes.
  283. *
  284. * @par Example
  285. * To read into a single data buffer use the @ref buffer function as follows:
  286. * @code
  287. * descriptor.read_some(boost::asio::buffer(data, size));
  288. * @endcode
  289. * See the @ref buffer documentation for information on reading into multiple
  290. * buffers in one go, and how to use it with arrays, boost::array or
  291. * std::vector.
  292. */
  293. template <typename MutableBufferSequence>
  294. std::size_t read_some(const MutableBufferSequence& buffers)
  295. {
  296. boost::system::error_code ec;
  297. std::size_t s = this->impl_.get_service().read_some(
  298. this->impl_.get_implementation(), buffers, ec);
  299. boost::asio::detail::throw_error(ec, "read_some");
  300. return s;
  301. }
  302. /// Read some data from the descriptor.
  303. /**
  304. * This function is used to read data from the stream descriptor. The function
  305. * call will block until one or more bytes of data has been read successfully,
  306. * or until an error occurs.
  307. *
  308. * @param buffers One or more buffers into which the data will be read.
  309. *
  310. * @param ec Set to indicate what error occurred, if any.
  311. *
  312. * @returns The number of bytes read. Returns 0 if an error occurred.
  313. *
  314. * @note The read_some operation may not read all of the requested number of
  315. * bytes. Consider using the @ref read function if you need to ensure that
  316. * the requested amount of data is read before the blocking operation
  317. * completes.
  318. */
  319. template <typename MutableBufferSequence>
  320. std::size_t read_some(const MutableBufferSequence& buffers,
  321. boost::system::error_code& ec)
  322. {
  323. return this->impl_.get_service().read_some(
  324. this->impl_.get_implementation(), buffers, ec);
  325. }
  326. /// Start an asynchronous read.
  327. /**
  328. * This function is used to asynchronously read data from the stream
  329. * descriptor. The function call always returns immediately.
  330. *
  331. * @param buffers One or more buffers into which the data will be read.
  332. * Although the buffers object may be copied as necessary, ownership of the
  333. * underlying memory blocks is retained by the caller, which must guarantee
  334. * that they remain valid until the handler is called.
  335. *
  336. * @param handler The handler to be called when the read operation completes.
  337. * Copies will be made of the handler as required. The function signature of
  338. * the handler must be:
  339. * @code void handler(
  340. * const boost::system::error_code& error, // Result of operation.
  341. * std::size_t bytes_transferred // Number of bytes read.
  342. * ); @endcode
  343. * Regardless of whether the asynchronous operation completes immediately or
  344. * not, the handler will not be invoked from within this function. On
  345. * immediate completion, invocation of the handler will be performed in a
  346. * manner equivalent to using boost::asio::post().
  347. *
  348. * @note The read operation may not read all of the requested number of bytes.
  349. * Consider using the @ref async_read function if you need to ensure that the
  350. * requested amount of data is read before the asynchronous operation
  351. * completes.
  352. *
  353. * @par Example
  354. * To read into a single data buffer use the @ref buffer function as follows:
  355. * @code
  356. * descriptor.async_read_some(boost::asio::buffer(data, size), handler);
  357. * @endcode
  358. * See the @ref buffer documentation for information on reading into multiple
  359. * buffers in one go, and how to use it with arrays, boost::array or
  360. * std::vector.
  361. */
  362. template <typename MutableBufferSequence,
  363. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  364. std::size_t)) ReadHandler
  365. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  366. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  367. void (boost::system::error_code, std::size_t))
  368. async_read_some(const MutableBufferSequence& buffers,
  369. BOOST_ASIO_MOVE_ARG(ReadHandler) handler
  370. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  371. {
  372. return async_initiate<ReadHandler,
  373. void (boost::system::error_code, std::size_t)>(
  374. initiate_async_read_some(this), handler, buffers);
  375. }
  376. private:
  377. class initiate_async_write_some
  378. {
  379. public:
  380. typedef Executor executor_type;
  381. explicit initiate_async_write_some(basic_stream_descriptor* self)
  382. : self_(self)
  383. {
  384. }
  385. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  386. {
  387. return self_->get_executor();
  388. }
  389. template <typename WriteHandler, typename ConstBufferSequence>
  390. void operator()(BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
  391. const ConstBufferSequence& buffers) const
  392. {
  393. // If you get an error on the following line it means that your handler
  394. // does not meet the documented type requirements for a WriteHandler.
  395. BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  396. detail::non_const_lvalue<WriteHandler> handler2(handler);
  397. self_->impl_.get_service().async_write_some(
  398. self_->impl_.get_implementation(), buffers, handler2.value,
  399. self_->impl_.get_implementation_executor());
  400. }
  401. private:
  402. basic_stream_descriptor* self_;
  403. };
  404. class initiate_async_read_some
  405. {
  406. public:
  407. typedef Executor executor_type;
  408. explicit initiate_async_read_some(basic_stream_descriptor* self)
  409. : self_(self)
  410. {
  411. }
  412. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  413. {
  414. return self_->get_executor();
  415. }
  416. template <typename ReadHandler, typename MutableBufferSequence>
  417. void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  418. const MutableBufferSequence& buffers) const
  419. {
  420. // If you get an error on the following line it means that your handler
  421. // does not meet the documented type requirements for a ReadHandler.
  422. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  423. detail::non_const_lvalue<ReadHandler> handler2(handler);
  424. self_->impl_.get_service().async_read_some(
  425. self_->impl_.get_implementation(), buffers, handler2.value,
  426. self_->impl_.get_implementation_executor());
  427. }
  428. private:
  429. basic_stream_descriptor* self_;
  430. };
  431. };
  432. } // namespace posix
  433. } // namespace asio
  434. } // namespace boost
  435. #endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
  436. // || defined(GENERATING_DOCUMENTATION)
  437. #endif // BOOST_ASIO_POSIX_BASIC_STREAM_DESCRIPTOR_HPP