basic_raw_socket.hpp 30 KB

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