basic_stream_socket.hpp 28 KB

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