basic_stream_socket.hpp 33 KB

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