basic_raw_socket.hpp 31 KB

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