basic_raw_socket.hpp 30 KB

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