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_socket.hpp 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. //
  2. // basic_stream_socket.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_BASIC_STREAM_SOCKET_HPP
  11. #define ASIO_BASIC_STREAM_SOCKET_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. #include <cstddef>
  17. #include "asio/async_result.hpp"
  18. #include "asio/basic_socket.hpp"
  19. #include "asio/detail/handler_type_requirements.hpp"
  20. #include "asio/detail/throw_error.hpp"
  21. #include "asio/error.hpp"
  22. #include "asio/stream_socket_service.hpp"
  23. #include "asio/detail/push_options.hpp"
  24. namespace asio {
  25. /// Provides stream-oriented socket functionality.
  26. /**
  27. * The basic_stream_socket class template provides asynchronous and blocking
  28. * stream-oriented socket functionality.
  29. *
  30. * @par Thread Safety
  31. * @e Distinct @e objects: Safe.@n
  32. * @e Shared @e objects: Unsafe.
  33. *
  34. * @par Concepts:
  35. * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
  36. */
  37. template <typename Protocol,
  38. typename StreamSocketService = stream_socket_service<Protocol> >
  39. class basic_stream_socket
  40. : public basic_socket<Protocol, StreamSocketService>
  41. {
  42. public:
  43. /// The native representation of a socket.
  44. typedef typename StreamSocketService::native_handle_type native_handle_type;
  45. /// The protocol type.
  46. typedef Protocol protocol_type;
  47. /// The endpoint type.
  48. typedef typename Protocol::endpoint endpoint_type;
  49. /// Construct a basic_stream_socket without opening it.
  50. /**
  51. * This constructor creates a stream socket without opening it. The socket
  52. * needs to be opened and then connected or accepted before data can be sent
  53. * or received on it.
  54. *
  55. * @param io_context The io_context object that the stream socket will use to
  56. * dispatch handlers for any asynchronous operations performed on the socket.
  57. */
  58. explicit basic_stream_socket(asio::io_context& io_context)
  59. : basic_socket<Protocol, StreamSocketService>(io_context)
  60. {
  61. }
  62. /// Construct and open a basic_stream_socket.
  63. /**
  64. * This constructor creates and opens a stream socket. The socket needs to be
  65. * connected or accepted before data can be sent or received on it.
  66. *
  67. * @param io_context The io_context object that the stream socket will use to
  68. * dispatch handlers for any asynchronous operations performed on the socket.
  69. *
  70. * @param protocol An object specifying protocol parameters to be used.
  71. *
  72. * @throws asio::system_error Thrown on failure.
  73. */
  74. basic_stream_socket(asio::io_context& io_context,
  75. const protocol_type& protocol)
  76. : basic_socket<Protocol, StreamSocketService>(io_context, protocol)
  77. {
  78. }
  79. /// Construct a basic_stream_socket, opening it and binding it to the given
  80. /// local endpoint.
  81. /**
  82. * This constructor creates a stream socket and automatically opens it bound
  83. * to the specified endpoint on the local machine. The protocol used is the
  84. * protocol associated with the given endpoint.
  85. *
  86. * @param io_context The io_context object that the stream socket will use to
  87. * dispatch handlers for any asynchronous operations performed on the socket.
  88. *
  89. * @param endpoint An endpoint on the local machine to which the stream
  90. * socket will be bound.
  91. *
  92. * @throws asio::system_error Thrown on failure.
  93. */
  94. basic_stream_socket(asio::io_context& io_context,
  95. const endpoint_type& endpoint)
  96. : basic_socket<Protocol, StreamSocketService>(io_context, endpoint)
  97. {
  98. }
  99. /// Construct a basic_stream_socket on an existing native socket.
  100. /**
  101. * This constructor creates a stream socket object to hold an existing native
  102. * socket.
  103. *
  104. * @param io_context The io_context object that the stream socket will use to
  105. * dispatch handlers for any asynchronous operations performed on the socket.
  106. *
  107. * @param protocol An object specifying protocol parameters to be used.
  108. *
  109. * @param native_socket The new underlying socket implementation.
  110. *
  111. * @throws asio::system_error Thrown on failure.
  112. */
  113. basic_stream_socket(asio::io_context& io_context,
  114. const protocol_type& protocol, const native_handle_type& native_socket)
  115. : basic_socket<Protocol, StreamSocketService>(
  116. io_context, protocol, native_socket)
  117. {
  118. }
  119. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  120. /// Move-construct a basic_stream_socket from another.
  121. /**
  122. * This constructor moves a stream socket from one object to another.
  123. *
  124. * @param other The other basic_stream_socket object from which the move
  125. * will occur.
  126. *
  127. * @note Following the move, the moved-from object is in the same state as if
  128. * constructed using the @c basic_stream_socket(io_context&) constructor.
  129. */
  130. basic_stream_socket(basic_stream_socket&& other)
  131. : basic_socket<Protocol, StreamSocketService>(
  132. ASIO_MOVE_CAST(basic_stream_socket)(other))
  133. {
  134. }
  135. /// Move-assign a basic_stream_socket from another.
  136. /**
  137. * This assignment operator moves a stream socket from one object to another.
  138. *
  139. * @param other The other basic_stream_socket object from which the move
  140. * will occur.
  141. *
  142. * @note Following the move, the moved-from object is in the same state as if
  143. * constructed using the @c basic_stream_socket(io_context&) constructor.
  144. */
  145. basic_stream_socket& operator=(basic_stream_socket&& other)
  146. {
  147. basic_socket<Protocol, StreamSocketService>::operator=(
  148. ASIO_MOVE_CAST(basic_stream_socket)(other));
  149. return *this;
  150. }
  151. /// Move-construct a basic_stream_socket from a socket of another protocol
  152. /// type.
  153. /**
  154. * This constructor moves a stream socket from one object to another.
  155. *
  156. * @param other The other basic_stream_socket object from which the move
  157. * will occur.
  158. *
  159. * @note Following the move, the moved-from object is in the same state as if
  160. * constructed using the @c basic_stream_socket(io_context&) constructor.
  161. */
  162. template <typename Protocol1, typename StreamSocketService1>
  163. basic_stream_socket(
  164. basic_stream_socket<Protocol1, StreamSocketService1>&& other,
  165. typename enable_if<is_convertible<Protocol1, Protocol>::value>::type* = 0)
  166. : basic_socket<Protocol, StreamSocketService>(
  167. ASIO_MOVE_CAST2(basic_stream_socket<
  168. Protocol1, StreamSocketService1>)(other))
  169. {
  170. }
  171. /// Move-assign a basic_stream_socket from a socket of another protocol type.
  172. /**
  173. * This assignment operator moves a stream socket from one object to another.
  174. *
  175. * @param other The other basic_stream_socket object from which the move
  176. * will occur.
  177. *
  178. * @note Following the move, the moved-from object is in the same state as if
  179. * constructed using the @c basic_stream_socket(io_context&) constructor.
  180. */
  181. template <typename Protocol1, typename StreamSocketService1>
  182. typename enable_if<is_convertible<Protocol1, Protocol>::value,
  183. basic_stream_socket>::type& operator=(
  184. basic_stream_socket<Protocol1, StreamSocketService1>&& other)
  185. {
  186. basic_socket<Protocol, StreamSocketService>::operator=(
  187. ASIO_MOVE_CAST2(basic_stream_socket<
  188. Protocol1, StreamSocketService1>)(other));
  189. return *this;
  190. }
  191. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  192. /// Send some data on the socket.
  193. /**
  194. * This function is used to send data on the stream socket. The function
  195. * call will block until one or more bytes of the data has been sent
  196. * successfully, or an until error occurs.
  197. *
  198. * @param buffers One or more data buffers to be sent on the socket.
  199. *
  200. * @returns The number of bytes sent.
  201. *
  202. * @throws asio::system_error Thrown on failure.
  203. *
  204. * @note The send operation may not transmit all of the data to the peer.
  205. * Consider using the @ref write function if you need to ensure that all data
  206. * is written before the blocking operation completes.
  207. *
  208. * @par Example
  209. * To send a single data buffer use the @ref buffer function as follows:
  210. * @code
  211. * socket.send(asio::buffer(data, size));
  212. * @endcode
  213. * See the @ref buffer documentation for information on sending multiple
  214. * buffers in one go, and how to use it with arrays, boost::array or
  215. * std::vector.
  216. */
  217. template <typename ConstBufferSequence>
  218. std::size_t send(const ConstBufferSequence& buffers)
  219. {
  220. asio::error_code ec;
  221. std::size_t s = this->get_service().send(
  222. this->get_implementation(), buffers, 0, ec);
  223. asio::detail::throw_error(ec, "send");
  224. return s;
  225. }
  226. /// Send some data on the socket.
  227. /**
  228. * This function is used to send data on the stream socket. The function
  229. * call will block until one or more bytes of the data has been sent
  230. * successfully, or an until error occurs.
  231. *
  232. * @param buffers One or more data buffers to be sent on the socket.
  233. *
  234. * @param flags Flags specifying how the send call is to be made.
  235. *
  236. * @returns The number of bytes sent.
  237. *
  238. * @throws asio::system_error Thrown on failure.
  239. *
  240. * @note The send operation may not transmit all of the data to the peer.
  241. * Consider using the @ref write function if you need to ensure that all data
  242. * is written before the blocking operation completes.
  243. *
  244. * @par Example
  245. * To send a single data buffer use the @ref buffer function as follows:
  246. * @code
  247. * socket.send(asio::buffer(data, size), 0);
  248. * @endcode
  249. * See the @ref buffer documentation for information on sending multiple
  250. * buffers in one go, and how to use it with arrays, boost::array or
  251. * std::vector.
  252. */
  253. template <typename ConstBufferSequence>
  254. std::size_t send(const ConstBufferSequence& buffers,
  255. socket_base::message_flags flags)
  256. {
  257. asio::error_code ec;
  258. std::size_t s = this->get_service().send(
  259. this->get_implementation(), buffers, flags, ec);
  260. asio::detail::throw_error(ec, "send");
  261. return s;
  262. }
  263. /// Send some data on the socket.
  264. /**
  265. * This function is used to send data on the stream socket. The function
  266. * call will block until one or more bytes of the data has been sent
  267. * successfully, or an until error occurs.
  268. *
  269. * @param buffers One or more data buffers to be sent on the socket.
  270. *
  271. * @param flags Flags specifying how the send call is to be made.
  272. *
  273. * @param ec Set to indicate what error occurred, if any.
  274. *
  275. * @returns The number of bytes sent. Returns 0 if an error occurred.
  276. *
  277. * @note The send operation may not transmit all of the data to the peer.
  278. * Consider using the @ref write function if you need to ensure that all data
  279. * is written before the blocking operation completes.
  280. */
  281. template <typename ConstBufferSequence>
  282. std::size_t send(const ConstBufferSequence& buffers,
  283. socket_base::message_flags flags, asio::error_code& ec)
  284. {
  285. return this->get_service().send(
  286. this->get_implementation(), buffers, flags, ec);
  287. }
  288. /// Start an asynchronous send.
  289. /**
  290. * This function is used to asynchronously send data on the stream socket.
  291. * The function call always returns immediately.
  292. *
  293. * @param buffers One or more data buffers to be sent on the socket. Although
  294. * the buffers object may be copied as necessary, ownership of the underlying
  295. * memory blocks is retained by the caller, which must guarantee that they
  296. * remain valid until the handler is called.
  297. *
  298. * @param handler The handler to be called when the send operation completes.
  299. * Copies will be made of the handler as required. The function signature of
  300. * the handler must be:
  301. * @code void handler(
  302. * const asio::error_code& error, // Result of operation.
  303. * std::size_t bytes_transferred // Number of bytes sent.
  304. * ); @endcode
  305. * Regardless of whether the asynchronous operation completes immediately or
  306. * not, the handler will not be invoked from within this function. Invocation
  307. * of the handler will be performed in a manner equivalent to using
  308. * asio::io_context::post().
  309. *
  310. * @note The send operation may not transmit all of the data to the peer.
  311. * Consider using the @ref async_write function if you need to ensure that all
  312. * data is written before the asynchronous operation completes.
  313. *
  314. * @par Example
  315. * To send a single data buffer use the @ref buffer function as follows:
  316. * @code
  317. * socket.async_send(asio::buffer(data, size), handler);
  318. * @endcode
  319. * See the @ref buffer documentation for information on sending multiple
  320. * buffers in one go, and how to use it with arrays, boost::array or
  321. * std::vector.
  322. */
  323. template <typename ConstBufferSequence, typename WriteHandler>
  324. ASIO_INITFN_RESULT_TYPE(WriteHandler,
  325. void (asio::error_code, std::size_t))
  326. async_send(const ConstBufferSequence& buffers,
  327. ASIO_MOVE_ARG(WriteHandler) handler)
  328. {
  329. // If you get an error on the following line it means that your handler does
  330. // not meet the documented type requirements for a WriteHandler.
  331. ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  332. return this->get_service().async_send(
  333. this->get_implementation(), buffers, 0,
  334. ASIO_MOVE_CAST(WriteHandler)(handler));
  335. }
  336. /// Start an asynchronous send.
  337. /**
  338. * This function is used to asynchronously send data on the stream socket.
  339. * The function call always returns immediately.
  340. *
  341. * @param buffers One or more data buffers to be sent on the socket. Although
  342. * the buffers object may be copied as necessary, ownership of the underlying
  343. * memory blocks is retained by the caller, which must guarantee that they
  344. * remain valid until the handler is called.
  345. *
  346. * @param flags Flags specifying how the send call is to be made.
  347. *
  348. * @param handler The handler to be called when the send operation completes.
  349. * Copies will be made of the handler as required. The function signature of
  350. * the handler must be:
  351. * @code void handler(
  352. * const asio::error_code& error, // Result of operation.
  353. * std::size_t bytes_transferred // Number of bytes sent.
  354. * ); @endcode
  355. * Regardless of whether the asynchronous operation completes immediately or
  356. * not, the handler will not be invoked from within this function. Invocation
  357. * of the handler will be performed in a manner equivalent to using
  358. * asio::io_context::post().
  359. *
  360. * @note The send operation may not transmit all of the data to the peer.
  361. * Consider using the @ref async_write function if you need to ensure that all
  362. * data is written before the asynchronous operation completes.
  363. *
  364. * @par Example
  365. * To send a single data buffer use the @ref buffer function as follows:
  366. * @code
  367. * socket.async_send(asio::buffer(data, size), 0, handler);
  368. * @endcode
  369. * See the @ref buffer documentation for information on sending multiple
  370. * buffers in one go, and how to use it with arrays, boost::array or
  371. * std::vector.
  372. */
  373. template <typename ConstBufferSequence, typename WriteHandler>
  374. ASIO_INITFN_RESULT_TYPE(WriteHandler,
  375. void (asio::error_code, std::size_t))
  376. async_send(const ConstBufferSequence& buffers,
  377. socket_base::message_flags flags,
  378. ASIO_MOVE_ARG(WriteHandler) handler)
  379. {
  380. // If you get an error on the following line it means that your handler does
  381. // not meet the documented type requirements for a WriteHandler.
  382. ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  383. return this->get_service().async_send(
  384. this->get_implementation(), buffers, flags,
  385. ASIO_MOVE_CAST(WriteHandler)(handler));
  386. }
  387. /// Receive some data on the socket.
  388. /**
  389. * This function is used to receive data on the stream socket. The function
  390. * call will block until one or more bytes of data has been received
  391. * successfully, or until an error occurs.
  392. *
  393. * @param buffers One or more buffers into which the data will be received.
  394. *
  395. * @returns The number of bytes received.
  396. *
  397. * @throws asio::system_error Thrown on failure. An error code of
  398. * asio::error::eof indicates that the connection was closed by the
  399. * peer.
  400. *
  401. * @note The receive operation may not receive all of the requested number of
  402. * bytes. Consider using the @ref read function if you need to ensure that the
  403. * requested amount of data is read before the blocking operation completes.
  404. *
  405. * @par Example
  406. * To receive into a single data buffer use the @ref buffer function as
  407. * follows:
  408. * @code
  409. * socket.receive(asio::buffer(data, size));
  410. * @endcode
  411. * See the @ref buffer documentation for information on receiving into
  412. * multiple buffers in one go, and how to use it with arrays, boost::array or
  413. * std::vector.
  414. */
  415. template <typename MutableBufferSequence>
  416. std::size_t receive(const MutableBufferSequence& buffers)
  417. {
  418. asio::error_code ec;
  419. std::size_t s = this->get_service().receive(
  420. this->get_implementation(), buffers, 0, ec);
  421. asio::detail::throw_error(ec, "receive");
  422. return s;
  423. }
  424. /// Receive some data on the socket.
  425. /**
  426. * This function is used to receive data on the stream socket. The function
  427. * call will block until one or more bytes of data has been received
  428. * successfully, or until an error occurs.
  429. *
  430. * @param buffers One or more buffers into which the data will be received.
  431. *
  432. * @param flags Flags specifying how the receive call is to be made.
  433. *
  434. * @returns The number of bytes received.
  435. *
  436. * @throws asio::system_error Thrown on failure. An error code of
  437. * asio::error::eof indicates that the connection was closed by the
  438. * peer.
  439. *
  440. * @note The receive operation may not receive all of the requested number of
  441. * bytes. Consider using the @ref read function if you need to ensure that the
  442. * requested amount of data is read before the blocking operation completes.
  443. *
  444. * @par Example
  445. * To receive into a single data buffer use the @ref buffer function as
  446. * follows:
  447. * @code
  448. * socket.receive(asio::buffer(data, size), 0);
  449. * @endcode
  450. * See the @ref buffer documentation for information on receiving into
  451. * multiple buffers in one go, and how to use it with arrays, boost::array or
  452. * std::vector.
  453. */
  454. template <typename MutableBufferSequence>
  455. std::size_t receive(const MutableBufferSequence& buffers,
  456. socket_base::message_flags flags)
  457. {
  458. asio::error_code ec;
  459. std::size_t s = this->get_service().receive(
  460. this->get_implementation(), buffers, flags, ec);
  461. asio::detail::throw_error(ec, "receive");
  462. return s;
  463. }
  464. /// Receive some data on a connected socket.
  465. /**
  466. * This function is used to receive data on the stream socket. The function
  467. * call will block until one or more bytes of data has been received
  468. * successfully, or until an error occurs.
  469. *
  470. * @param buffers One or more buffers into which the data will be received.
  471. *
  472. * @param flags Flags specifying how the receive call is to be made.
  473. *
  474. * @param ec Set to indicate what error occurred, if any.
  475. *
  476. * @returns The number of bytes received. Returns 0 if an error occurred.
  477. *
  478. * @note The receive operation may not receive all of the requested number of
  479. * bytes. Consider using the @ref read function if you need to ensure that the
  480. * requested amount of data is read before the blocking operation completes.
  481. */
  482. template <typename MutableBufferSequence>
  483. std::size_t receive(const MutableBufferSequence& buffers,
  484. socket_base::message_flags flags, asio::error_code& ec)
  485. {
  486. return this->get_service().receive(
  487. this->get_implementation(), buffers, flags, ec);
  488. }
  489. /// Start an asynchronous receive.
  490. /**
  491. * This function is used to asynchronously receive data from the stream
  492. * socket. The function call always returns immediately.
  493. *
  494. * @param buffers One or more buffers into which the data will be received.
  495. * Although the buffers object may be copied as necessary, ownership of the
  496. * underlying memory blocks is retained by the caller, which must guarantee
  497. * that they remain valid until the handler is called.
  498. *
  499. * @param handler The handler to be called when the receive operation
  500. * completes. Copies will be made of the handler as required. The function
  501. * signature of the handler must be:
  502. * @code void handler(
  503. * const asio::error_code& error, // Result of operation.
  504. * std::size_t bytes_transferred // Number of bytes received.
  505. * ); @endcode
  506. * Regardless of whether the asynchronous operation completes immediately or
  507. * not, the handler will not be invoked from within this function. Invocation
  508. * of the handler will be performed in a manner equivalent to using
  509. * asio::io_context::post().
  510. *
  511. * @note The receive operation may not receive all of the requested number of
  512. * bytes. Consider using the @ref async_read function if you need to ensure
  513. * that the requested amount of data is received before the asynchronous
  514. * operation completes.
  515. *
  516. * @par Example
  517. * To receive into a single data buffer use the @ref buffer function as
  518. * follows:
  519. * @code
  520. * socket.async_receive(asio::buffer(data, size), handler);
  521. * @endcode
  522. * See the @ref buffer documentation for information on receiving into
  523. * multiple buffers in one go, and how to use it with arrays, boost::array or
  524. * std::vector.
  525. */
  526. template <typename MutableBufferSequence, typename ReadHandler>
  527. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  528. void (asio::error_code, std::size_t))
  529. async_receive(const MutableBufferSequence& buffers,
  530. ASIO_MOVE_ARG(ReadHandler) handler)
  531. {
  532. // If you get an error on the following line it means that your handler does
  533. // not meet the documented type requirements for a ReadHandler.
  534. ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  535. return this->get_service().async_receive(this->get_implementation(),
  536. buffers, 0, ASIO_MOVE_CAST(ReadHandler)(handler));
  537. }
  538. /// Start an asynchronous receive.
  539. /**
  540. * This function is used to asynchronously receive data from the stream
  541. * socket. The function call always returns immediately.
  542. *
  543. * @param buffers One or more buffers into which the data will be received.
  544. * Although the buffers object may be copied as necessary, ownership of the
  545. * underlying memory blocks is retained by the caller, which must guarantee
  546. * that they remain valid until the handler is called.
  547. *
  548. * @param flags Flags specifying how the receive call is to be made.
  549. *
  550. * @param handler The handler to be called when the receive operation
  551. * completes. Copies will be made of the handler as required. The function
  552. * signature of the handler must be:
  553. * @code void handler(
  554. * const asio::error_code& error, // Result of operation.
  555. * std::size_t bytes_transferred // Number of bytes received.
  556. * ); @endcode
  557. * Regardless of whether the asynchronous operation completes immediately or
  558. * not, the handler will not be invoked from within this function. Invocation
  559. * of the handler will be performed in a manner equivalent to using
  560. * asio::io_context::post().
  561. *
  562. * @note The receive operation may not receive all of the requested number of
  563. * bytes. Consider using the @ref async_read function if you need to ensure
  564. * that the requested amount of data is received before the asynchronous
  565. * operation completes.
  566. *
  567. * @par Example
  568. * To receive into a single data buffer use the @ref buffer function as
  569. * follows:
  570. * @code
  571. * socket.async_receive(asio::buffer(data, size), 0, handler);
  572. * @endcode
  573. * See the @ref buffer documentation for information on receiving into
  574. * multiple buffers in one go, and how to use it with arrays, boost::array or
  575. * std::vector.
  576. */
  577. template <typename MutableBufferSequence, typename ReadHandler>
  578. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  579. void (asio::error_code, std::size_t))
  580. async_receive(const MutableBufferSequence& buffers,
  581. socket_base::message_flags flags,
  582. ASIO_MOVE_ARG(ReadHandler) handler)
  583. {
  584. // If you get an error on the following line it means that your handler does
  585. // not meet the documented type requirements for a ReadHandler.
  586. ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  587. return this->get_service().async_receive(this->get_implementation(),
  588. buffers, flags, ASIO_MOVE_CAST(ReadHandler)(handler));
  589. }
  590. /// Write some data to the socket.
  591. /**
  592. * This function is used to write data to the stream socket. The function call
  593. * will block until one or more bytes of the data has been written
  594. * successfully, or until an error occurs.
  595. *
  596. * @param buffers One or more data buffers to be written to the socket.
  597. *
  598. * @returns The number of bytes written.
  599. *
  600. * @throws asio::system_error Thrown on failure. An error code of
  601. * asio::error::eof indicates that the connection was closed by the
  602. * peer.
  603. *
  604. * @note The write_some operation may not transmit all of the data to the
  605. * peer. Consider using the @ref write function if you need to ensure that
  606. * all data is written before the blocking operation completes.
  607. *
  608. * @par Example
  609. * To write a single data buffer use the @ref buffer function as follows:
  610. * @code
  611. * socket.write_some(asio::buffer(data, size));
  612. * @endcode
  613. * See the @ref buffer documentation for information on writing multiple
  614. * buffers in one go, and how to use it with arrays, boost::array or
  615. * std::vector.
  616. */
  617. template <typename ConstBufferSequence>
  618. std::size_t write_some(const ConstBufferSequence& buffers)
  619. {
  620. asio::error_code ec;
  621. std::size_t s = this->get_service().send(
  622. this->get_implementation(), buffers, 0, ec);
  623. asio::detail::throw_error(ec, "write_some");
  624. return s;
  625. }
  626. /// Write some data to the socket.
  627. /**
  628. * This function is used to write data to the stream socket. The function call
  629. * will block until one or more bytes of the data has been written
  630. * successfully, or until an error occurs.
  631. *
  632. * @param buffers One or more data buffers to be written to the socket.
  633. *
  634. * @param ec Set to indicate what error occurred, if any.
  635. *
  636. * @returns The number of bytes written. Returns 0 if an error occurred.
  637. *
  638. * @note The write_some operation may not transmit all of the data to the
  639. * peer. Consider using the @ref write function if you need to ensure that
  640. * all data is written before the blocking operation completes.
  641. */
  642. template <typename ConstBufferSequence>
  643. std::size_t write_some(const ConstBufferSequence& buffers,
  644. asio::error_code& ec)
  645. {
  646. return this->get_service().send(this->get_implementation(), buffers, 0, ec);
  647. }
  648. /// Start an asynchronous write.
  649. /**
  650. * This function is used to asynchronously write data to the stream socket.
  651. * The function call always returns immediately.
  652. *
  653. * @param buffers One or more data buffers to be written to the socket.
  654. * Although the buffers object may be copied as necessary, ownership of the
  655. * underlying memory blocks is retained by the caller, which must guarantee
  656. * that they remain valid until the handler is called.
  657. *
  658. * @param handler The handler to be called when the write operation completes.
  659. * Copies will be made of the handler as required. The function signature of
  660. * the handler must be:
  661. * @code void handler(
  662. * const asio::error_code& error, // Result of operation.
  663. * std::size_t bytes_transferred // Number of bytes written.
  664. * ); @endcode
  665. * Regardless of whether the asynchronous operation completes immediately or
  666. * not, the handler will not be invoked from within this function. Invocation
  667. * of the handler will be performed in a manner equivalent to using
  668. * asio::io_context::post().
  669. *
  670. * @note The write operation may not transmit all of the data to the peer.
  671. * Consider using the @ref async_write function if you need to ensure that all
  672. * data is written before the asynchronous operation completes.
  673. *
  674. * @par Example
  675. * To write a single data buffer use the @ref buffer function as follows:
  676. * @code
  677. * socket.async_write_some(asio::buffer(data, size), handler);
  678. * @endcode
  679. * See the @ref buffer documentation for information on writing multiple
  680. * buffers in one go, and how to use it with arrays, boost::array or
  681. * std::vector.
  682. */
  683. template <typename ConstBufferSequence, typename WriteHandler>
  684. ASIO_INITFN_RESULT_TYPE(WriteHandler,
  685. void (asio::error_code, std::size_t))
  686. async_write_some(const ConstBufferSequence& buffers,
  687. ASIO_MOVE_ARG(WriteHandler) handler)
  688. {
  689. // If you get an error on the following line it means that your handler does
  690. // not meet the documented type requirements for a WriteHandler.
  691. ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  692. return this->get_service().async_send(this->get_implementation(),
  693. buffers, 0, ASIO_MOVE_CAST(WriteHandler)(handler));
  694. }
  695. /// Read some data from the socket.
  696. /**
  697. * This function is used to read data from the stream socket. The function
  698. * call will block until one or more bytes of data has been read successfully,
  699. * or until an error occurs.
  700. *
  701. * @param buffers One or more buffers into which the data will be read.
  702. *
  703. * @returns The number of bytes read.
  704. *
  705. * @throws asio::system_error Thrown on failure. An error code of
  706. * asio::error::eof indicates that the connection was closed by the
  707. * peer.
  708. *
  709. * @note The read_some operation may not read all of the requested number of
  710. * bytes. Consider using the @ref read function if you need to ensure that
  711. * the requested amount of data is read before the blocking operation
  712. * completes.
  713. *
  714. * @par Example
  715. * To read into a single data buffer use the @ref buffer function as follows:
  716. * @code
  717. * socket.read_some(asio::buffer(data, size));
  718. * @endcode
  719. * See the @ref buffer documentation for information on reading into multiple
  720. * buffers in one go, and how to use it with arrays, boost::array or
  721. * std::vector.
  722. */
  723. template <typename MutableBufferSequence>
  724. std::size_t read_some(const MutableBufferSequence& buffers)
  725. {
  726. asio::error_code ec;
  727. std::size_t s = this->get_service().receive(
  728. this->get_implementation(), buffers, 0, ec);
  729. asio::detail::throw_error(ec, "read_some");
  730. return s;
  731. }
  732. /// Read some data from the socket.
  733. /**
  734. * This function is used to read data from the stream socket. The function
  735. * call will block until one or more bytes of data has been read successfully,
  736. * or until an error occurs.
  737. *
  738. * @param buffers One or more buffers into which the data will be read.
  739. *
  740. * @param ec Set to indicate what error occurred, if any.
  741. *
  742. * @returns The number of bytes read. Returns 0 if an error occurred.
  743. *
  744. * @note The read_some operation may not read all of the requested number of
  745. * bytes. Consider using the @ref read function if you need to ensure that
  746. * the requested amount of data is read before the blocking operation
  747. * completes.
  748. */
  749. template <typename MutableBufferSequence>
  750. std::size_t read_some(const MutableBufferSequence& buffers,
  751. asio::error_code& ec)
  752. {
  753. return this->get_service().receive(
  754. this->get_implementation(), buffers, 0, ec);
  755. }
  756. /// Start an asynchronous read.
  757. /**
  758. * This function is used to asynchronously read data from the stream socket.
  759. * The function call always returns immediately.
  760. *
  761. * @param buffers One or more buffers into which the data will be read.
  762. * Although the buffers object may be copied as necessary, ownership of the
  763. * underlying memory blocks is retained by the caller, which must guarantee
  764. * that they remain valid until the handler is called.
  765. *
  766. * @param handler The handler to be called when the read operation completes.
  767. * Copies will be made of the handler as required. The function signature of
  768. * the handler must be:
  769. * @code void handler(
  770. * const asio::error_code& error, // Result of operation.
  771. * std::size_t bytes_transferred // Number of bytes read.
  772. * ); @endcode
  773. * Regardless of whether the asynchronous operation completes immediately or
  774. * not, the handler will not be invoked from within this function. Invocation
  775. * of the handler will be performed in a manner equivalent to using
  776. * asio::io_context::post().
  777. *
  778. * @note The read operation may not read all of the requested number of bytes.
  779. * Consider using the @ref async_read function if you need to ensure that the
  780. * requested amount of data is read before the asynchronous operation
  781. * completes.
  782. *
  783. * @par Example
  784. * To read into a single data buffer use the @ref buffer function as follows:
  785. * @code
  786. * socket.async_read_some(asio::buffer(data, size), handler);
  787. * @endcode
  788. * See the @ref buffer documentation for information on reading into multiple
  789. * buffers in one go, and how to use it with arrays, boost::array or
  790. * std::vector.
  791. */
  792. template <typename MutableBufferSequence, typename ReadHandler>
  793. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  794. void (asio::error_code, std::size_t))
  795. async_read_some(const MutableBufferSequence& buffers,
  796. ASIO_MOVE_ARG(ReadHandler) handler)
  797. {
  798. // If you get an error on the following line it means that your handler does
  799. // not meet the documented type requirements for a ReadHandler.
  800. ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  801. return this->get_service().async_receive(this->get_implementation(),
  802. buffers, 0, ASIO_MOVE_CAST(ReadHandler)(handler));
  803. }
  804. };
  805. } // namespace asio
  806. #include "asio/detail/pop_options.hpp"
  807. #endif // ASIO_BASIC_STREAM_SOCKET_HPP