basic_stream_socket.hpp 28 KB

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