basic_stream_socket.hpp 28 KB

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