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.

994 lines
38KB

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