basic_raw_socket.hpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. //
  2. // basic_raw_socket.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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/handler_type_requirements.hpp"
  19. #include "asio/detail/throw_error.hpp"
  20. #include "asio/detail/type_traits.hpp"
  21. #include "asio/error.hpp"
  22. #include "asio/raw_socket_service.hpp"
  23. #include "asio/detail/push_options.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. /// (Deprecated: Use native_handle_type.) The native representation of a
  41. /// socket.
  42. typedef typename RawSocketService::native_handle_type native_type;
  43. /// The native representation of a socket.
  44. typedef typename RawSocketService::native_handle_type native_handle_type;
  45. /// The protocol type.
  46. typedef Protocol protocol_type;
  47. /// The endpoint type.
  48. typedef typename Protocol::endpoint endpoint_type;
  49. /// Construct a basic_raw_socket without opening it.
  50. /**
  51. * This constructor creates a raw socket without opening it. The open()
  52. * function must be called before data can be sent or received on the socket.
  53. *
  54. * @param io_service The io_service object that the raw socket will use
  55. * to dispatch handlers for any asynchronous operations performed on the
  56. * socket.
  57. */
  58. explicit basic_raw_socket(asio::io_service& io_service)
  59. : basic_socket<Protocol, RawSocketService>(io_service)
  60. {
  61. }
  62. /// Construct and open a basic_raw_socket.
  63. /**
  64. * This constructor creates and opens a raw socket.
  65. *
  66. * @param io_service The io_service object that the raw socket will use
  67. * to dispatch handlers for any asynchronous operations performed on the
  68. * socket.
  69. *
  70. * @param protocol An object specifying protocol parameters to be used.
  71. *
  72. * @throws asio::system_error Thrown on failure.
  73. */
  74. basic_raw_socket(asio::io_service& io_service,
  75. const protocol_type& protocol)
  76. : basic_socket<Protocol, RawSocketService>(io_service, protocol)
  77. {
  78. }
  79. /// Construct a basic_raw_socket, opening it and binding it to the given
  80. /// local endpoint.
  81. /**
  82. * This constructor creates a raw socket and automatically opens it bound
  83. * to the specified endpoint on the local machine. The protocol used is the
  84. * protocol associated with the given endpoint.
  85. *
  86. * @param io_service The io_service object that the raw socket will use
  87. * to dispatch handlers for any asynchronous operations performed on the
  88. * socket.
  89. *
  90. * @param endpoint An endpoint on the local machine to which the raw
  91. * socket will be bound.
  92. *
  93. * @throws asio::system_error Thrown on failure.
  94. */
  95. basic_raw_socket(asio::io_service& io_service,
  96. const endpoint_type& endpoint)
  97. : basic_socket<Protocol, RawSocketService>(io_service, endpoint)
  98. {
  99. }
  100. /// Construct a basic_raw_socket on an existing native socket.
  101. /**
  102. * This constructor creates a raw socket object to hold an existing
  103. * native socket.
  104. *
  105. * @param io_service The io_service object that the raw socket will use
  106. * to dispatch handlers for any asynchronous operations performed on the
  107. * socket.
  108. *
  109. * @param protocol An object specifying protocol parameters to be used.
  110. *
  111. * @param native_socket The new underlying socket implementation.
  112. *
  113. * @throws asio::system_error Thrown on failure.
  114. */
  115. basic_raw_socket(asio::io_service& io_service,
  116. const protocol_type& protocol, const native_handle_type& native_socket)
  117. : basic_socket<Protocol, RawSocketService>(
  118. io_service, protocol, native_socket)
  119. {
  120. }
  121. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  122. /// Move-construct a basic_raw_socket from another.
  123. /**
  124. * This constructor moves a raw socket from one object to another.
  125. *
  126. * @param other The other basic_raw_socket object from which the move
  127. * will occur.
  128. *
  129. * @note Following the move, the moved-from object is in the same state as if
  130. * constructed using the @c basic_raw_socket(io_service&) constructor.
  131. */
  132. basic_raw_socket(basic_raw_socket&& other)
  133. : basic_socket<Protocol, RawSocketService>(
  134. ASIO_MOVE_CAST(basic_raw_socket)(other))
  135. {
  136. }
  137. /// Move-assign a basic_raw_socket from another.
  138. /**
  139. * This assignment operator moves a raw socket from one object to another.
  140. *
  141. * @param other The other basic_raw_socket object from which the move
  142. * will occur.
  143. *
  144. * @note Following the move, the moved-from object is in the same state as if
  145. * constructed using the @c basic_raw_socket(io_service&) constructor.
  146. */
  147. basic_raw_socket& operator=(basic_raw_socket&& other)
  148. {
  149. basic_socket<Protocol, RawSocketService>::operator=(
  150. ASIO_MOVE_CAST(basic_raw_socket)(other));
  151. return *this;
  152. }
  153. /// Move-construct a basic_raw_socket from a socket of another protocol type.
  154. /**
  155. * This constructor moves a raw socket from one object to another.
  156. *
  157. * @param other The other basic_raw_socket object from which the move will
  158. * occur.
  159. *
  160. * @note Following the move, the moved-from object is in the same state as if
  161. * constructed using the @c basic_raw_socket(io_service&) constructor.
  162. */
  163. template <typename Protocol1, typename RawSocketService1>
  164. basic_raw_socket(basic_raw_socket<Protocol1, RawSocketService1>&& other,
  165. typename enable_if<is_convertible<Protocol1, Protocol>::value>::type* = 0)
  166. : basic_socket<Protocol, RawSocketService>(
  167. ASIO_MOVE_CAST2(basic_raw_socket<
  168. Protocol1, RawSocketService1>)(other))
  169. {
  170. }
  171. /// Move-assign a basic_raw_socket from a socket of another protocol type.
  172. /**
  173. * This assignment operator moves a raw socket from one object to another.
  174. *
  175. * @param other The other basic_raw_socket object from which the move
  176. * will occur.
  177. *
  178. * @note Following the move, the moved-from object is in the same state as if
  179. * constructed using the @c basic_raw_socket(io_service&) constructor.
  180. */
  181. template <typename Protocol1, typename RawSocketService1>
  182. typename enable_if<is_convertible<Protocol1, Protocol>::value,
  183. basic_raw_socket>::type& operator=(
  184. basic_raw_socket<Protocol1, RawSocketService1>&& other)
  185. {
  186. basic_socket<Protocol, RawSocketService>::operator=(
  187. ASIO_MOVE_CAST2(basic_raw_socket<
  188. Protocol1, RawSocketService1>)(other));
  189. return *this;
  190. }
  191. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  192. /// Send some data on a connected socket.
  193. /**
  194. * This function is used to send data on the raw socket. The function call
  195. * will block until the data has been sent successfully or an error occurs.
  196. *
  197. * @param buffers One ore more data buffers to be sent on the socket.
  198. *
  199. * @returns The number of bytes sent.
  200. *
  201. * @throws asio::system_error Thrown on failure.
  202. *
  203. * @note The send operation can only be used with a connected socket. Use
  204. * the send_to function to send data on an unconnected raw socket.
  205. *
  206. * @par Example
  207. * To send a single data buffer use the @ref buffer function as follows:
  208. * @code socket.send(asio::buffer(data, size)); @endcode
  209. * See the @ref buffer documentation for information on sending multiple
  210. * buffers in one go, and how to use it with arrays, boost::array or
  211. * std::vector.
  212. */
  213. template <typename ConstBufferSequence>
  214. std::size_t send(const ConstBufferSequence& buffers)
  215. {
  216. asio::error_code ec;
  217. std::size_t s = this->get_service().send(
  218. this->get_implementation(), buffers, 0, ec);
  219. asio::detail::throw_error(ec, "send");
  220. return s;
  221. }
  222. /// Send some data on a connected socket.
  223. /**
  224. * This function is used to send data on the raw socket. The function call
  225. * will block until the data has been sent successfully or an error occurs.
  226. *
  227. * @param buffers One ore more data buffers to be sent on the socket.
  228. *
  229. * @param flags Flags specifying how the send call is to be made.
  230. *
  231. * @returns The number of bytes sent.
  232. *
  233. * @throws asio::system_error Thrown on failure.
  234. *
  235. * @note The send operation can only be used with a connected socket. Use
  236. * the send_to function to send data on an unconnected raw socket.
  237. */
  238. template <typename ConstBufferSequence>
  239. std::size_t send(const ConstBufferSequence& buffers,
  240. socket_base::message_flags flags)
  241. {
  242. asio::error_code ec;
  243. std::size_t s = this->get_service().send(
  244. this->get_implementation(), buffers, flags, ec);
  245. asio::detail::throw_error(ec, "send");
  246. return s;
  247. }
  248. /// Send some data on a connected socket.
  249. /**
  250. * This function is used to send data on the raw socket. The function call
  251. * will block until the data has been sent successfully or an error occurs.
  252. *
  253. * @param buffers One or more data buffers to be sent on the socket.
  254. *
  255. * @param flags Flags specifying how the send call is to be made.
  256. *
  257. * @param ec Set to indicate what error occurred, if any.
  258. *
  259. * @returns The number of bytes sent.
  260. *
  261. * @note The send operation can only be used with a connected socket. Use
  262. * the send_to function to send data on an unconnected raw socket.
  263. */
  264. template <typename ConstBufferSequence>
  265. std::size_t send(const ConstBufferSequence& buffers,
  266. socket_base::message_flags flags, asio::error_code& ec)
  267. {
  268. return this->get_service().send(
  269. this->get_implementation(), buffers, flags, ec);
  270. }
  271. /// Start an asynchronous send on a connected socket.
  272. /**
  273. * This function is used to send data on the raw socket. The function call
  274. * will block until the data has been sent successfully or an error occurs.
  275. *
  276. * @param buffers One or more data buffers to be sent on the socket. Although
  277. * the buffers object may be copied as necessary, ownership of the underlying
  278. * memory blocks is retained by the caller, which must guarantee that they
  279. * remain valid until the handler is called.
  280. *
  281. * @param handler The handler to be called when the send operation completes.
  282. * Copies will be made of the handler as required. The function signature of
  283. * the handler must be:
  284. * @code void handler(
  285. * const asio::error_code& error, // Result of operation.
  286. * std::size_t bytes_transferred // Number of bytes sent.
  287. * ); @endcode
  288. * Regardless of whether the asynchronous operation completes immediately or
  289. * not, the handler will not be invoked from within this function. Invocation
  290. * of the handler will be performed in a manner equivalent to using
  291. * asio::io_service::post().
  292. *
  293. * @note The async_send operation can only be used with a connected socket.
  294. * Use the async_send_to function to send data on an unconnected raw
  295. * socket.
  296. *
  297. * @par Example
  298. * To send a single data buffer use the @ref buffer function as follows:
  299. * @code
  300. * socket.async_send(asio::buffer(data, size), handler);
  301. * @endcode
  302. * See the @ref buffer documentation for information on sending multiple
  303. * buffers in one go, and how to use it with arrays, boost::array or
  304. * std::vector.
  305. */
  306. template <typename ConstBufferSequence, typename WriteHandler>
  307. ASIO_INITFN_RESULT_TYPE(WriteHandler,
  308. void (asio::error_code, std::size_t))
  309. async_send(const ConstBufferSequence& buffers,
  310. ASIO_MOVE_ARG(WriteHandler) handler)
  311. {
  312. // If you get an error on the following line it means that your handler does
  313. // not meet the documented type requirements for a WriteHandler.
  314. ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  315. return this->get_service().async_send(this->get_implementation(),
  316. buffers, 0, ASIO_MOVE_CAST(WriteHandler)(handler));
  317. }
  318. /// Start an asynchronous send on a connected socket.
  319. /**
  320. * This function is used to send data on the raw socket. The function call
  321. * will block until the data has been sent successfully or an error occurs.
  322. *
  323. * @param buffers One or more data buffers to be sent on the socket. Although
  324. * the buffers object may be copied as necessary, ownership of the underlying
  325. * memory blocks is retained by the caller, which must guarantee that they
  326. * remain valid until the handler is called.
  327. *
  328. * @param flags Flags specifying how the send call is to be made.
  329. *
  330. * @param handler The handler to be called when the send operation completes.
  331. * Copies will be made of the handler as required. The function signature of
  332. * the handler must be:
  333. * @code void handler(
  334. * const asio::error_code& error, // Result of operation.
  335. * std::size_t bytes_transferred // Number of bytes sent.
  336. * ); @endcode
  337. * Regardless of whether the asynchronous operation completes immediately or
  338. * not, the handler will not be invoked from within this function. Invocation
  339. * of the handler will be performed in a manner equivalent to using
  340. * asio::io_service::post().
  341. *
  342. * @note The async_send operation can only be used with a connected socket.
  343. * Use the async_send_to function to send data on an unconnected raw
  344. * socket.
  345. */
  346. template <typename ConstBufferSequence, typename WriteHandler>
  347. ASIO_INITFN_RESULT_TYPE(WriteHandler,
  348. void (asio::error_code, std::size_t))
  349. async_send(const ConstBufferSequence& buffers,
  350. socket_base::message_flags flags,
  351. ASIO_MOVE_ARG(WriteHandler) handler)
  352. {
  353. // If you get an error on the following line it means that your handler does
  354. // not meet the documented type requirements for a WriteHandler.
  355. ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  356. return this->get_service().async_send(this->get_implementation(),
  357. buffers, flags, ASIO_MOVE_CAST(WriteHandler)(handler));
  358. }
  359. /// Send raw data to the specified endpoint.
  360. /**
  361. * This function is used to send raw data to the specified remote endpoint.
  362. * The function call will block until the data has been sent successfully or
  363. * an error occurs.
  364. *
  365. * @param buffers One or more data buffers to be sent to the remote endpoint.
  366. *
  367. * @param destination The remote endpoint to which the data will be sent.
  368. *
  369. * @returns The number of bytes sent.
  370. *
  371. * @throws asio::system_error Thrown on failure.
  372. *
  373. * @par Example
  374. * To send a single data buffer use the @ref buffer function as follows:
  375. * @code
  376. * asio::ip::udp::endpoint destination(
  377. * asio::ip::address::from_string("1.2.3.4"), 12345);
  378. * socket.send_to(asio::buffer(data, size), destination);
  379. * @endcode
  380. * See the @ref buffer documentation for information on sending multiple
  381. * buffers in one go, and how to use it with arrays, boost::array or
  382. * std::vector.
  383. */
  384. template <typename ConstBufferSequence>
  385. std::size_t send_to(const ConstBufferSequence& buffers,
  386. const endpoint_type& destination)
  387. {
  388. asio::error_code ec;
  389. std::size_t s = this->get_service().send_to(
  390. this->get_implementation(), buffers, destination, 0, ec);
  391. asio::detail::throw_error(ec, "send_to");
  392. return s;
  393. }
  394. /// Send raw data to the specified endpoint.
  395. /**
  396. * This function is used to send raw data to the specified remote endpoint.
  397. * The function call will block until the data has been sent successfully or
  398. * an error occurs.
  399. *
  400. * @param buffers One or more data buffers to be sent to the remote endpoint.
  401. *
  402. * @param destination The remote endpoint to which the data will be sent.
  403. *
  404. * @param flags Flags specifying how the send call is to be made.
  405. *
  406. * @returns The number of bytes sent.
  407. *
  408. * @throws asio::system_error Thrown on failure.
  409. */
  410. template <typename ConstBufferSequence>
  411. std::size_t send_to(const ConstBufferSequence& buffers,
  412. const endpoint_type& destination, socket_base::message_flags flags)
  413. {
  414. asio::error_code ec;
  415. std::size_t s = this->get_service().send_to(
  416. this->get_implementation(), buffers, destination, flags, ec);
  417. asio::detail::throw_error(ec, "send_to");
  418. return s;
  419. }
  420. /// Send raw data to the specified endpoint.
  421. /**
  422. * This function is used to send raw data to the specified remote endpoint.
  423. * The function call will block until the data has been sent successfully or
  424. * an error occurs.
  425. *
  426. * @param buffers One or more data buffers to be sent to the remote endpoint.
  427. *
  428. * @param destination The remote endpoint to which the data will be sent.
  429. *
  430. * @param flags Flags specifying how the send call is to be made.
  431. *
  432. * @param ec Set to indicate what error occurred, if any.
  433. *
  434. * @returns The number of bytes sent.
  435. */
  436. template <typename ConstBufferSequence>
  437. std::size_t send_to(const ConstBufferSequence& buffers,
  438. const endpoint_type& destination, socket_base::message_flags flags,
  439. asio::error_code& ec)
  440. {
  441. return this->get_service().send_to(this->get_implementation(),
  442. buffers, destination, flags, ec);
  443. }
  444. /// Start an asynchronous send.
  445. /**
  446. * This function is used to asynchronously send raw data to the specified
  447. * remote endpoint. The function call always returns immediately.
  448. *
  449. * @param buffers One or more data buffers to be sent to the remote endpoint.
  450. * Although the buffers object may be copied as necessary, ownership of the
  451. * underlying memory blocks is retained by the caller, which must guarantee
  452. * that they remain valid until the handler is called.
  453. *
  454. * @param destination The remote endpoint to which the data will be sent.
  455. * Copies will be made of the endpoint as required.
  456. *
  457. * @param handler The handler to be called when the send operation completes.
  458. * Copies will be made of the handler as required. The function signature of
  459. * the handler must be:
  460. * @code void handler(
  461. * const asio::error_code& error, // Result of operation.
  462. * std::size_t bytes_transferred // Number of bytes sent.
  463. * ); @endcode
  464. * Regardless of whether the asynchronous operation completes immediately or
  465. * not, the handler will not be invoked from within this function. Invocation
  466. * of the handler will be performed in a manner equivalent to using
  467. * asio::io_service::post().
  468. *
  469. * @par Example
  470. * To send a single data buffer use the @ref buffer function as follows:
  471. * @code
  472. * asio::ip::udp::endpoint destination(
  473. * asio::ip::address::from_string("1.2.3.4"), 12345);
  474. * socket.async_send_to(
  475. * asio::buffer(data, size), destination, handler);
  476. * @endcode
  477. * See the @ref buffer documentation for information on sending multiple
  478. * buffers in one go, and how to use it with arrays, boost::array or
  479. * std::vector.
  480. */
  481. template <typename ConstBufferSequence, typename WriteHandler>
  482. ASIO_INITFN_RESULT_TYPE(WriteHandler,
  483. void (asio::error_code, std::size_t))
  484. async_send_to(const ConstBufferSequence& buffers,
  485. const endpoint_type& destination,
  486. ASIO_MOVE_ARG(WriteHandler) handler)
  487. {
  488. // If you get an error on the following line it means that your handler does
  489. // not meet the documented type requirements for a WriteHandler.
  490. ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  491. return this->get_service().async_send_to(this->get_implementation(),
  492. buffers, destination, 0, ASIO_MOVE_CAST(WriteHandler)(handler));
  493. }
  494. /// Start an asynchronous send.
  495. /**
  496. * This function is used to asynchronously send raw data to the specified
  497. * remote endpoint. The function call always returns immediately.
  498. *
  499. * @param buffers One or more data buffers to be sent to the remote endpoint.
  500. * Although the buffers object may be copied as necessary, ownership of the
  501. * underlying memory blocks is retained by the caller, which must guarantee
  502. * that they remain valid until the handler is called.
  503. *
  504. * @param flags Flags specifying how the send call is to be made.
  505. *
  506. * @param destination The remote endpoint to which the data will be sent.
  507. * Copies will be made of the endpoint as required.
  508. *
  509. * @param handler The handler to be called when the send operation completes.
  510. * Copies will be made of the handler as required. The function signature of
  511. * the handler must be:
  512. * @code void handler(
  513. * const asio::error_code& error, // Result of operation.
  514. * std::size_t bytes_transferred // Number of bytes sent.
  515. * ); @endcode
  516. * Regardless of whether the asynchronous operation completes immediately or
  517. * not, the handler will not be invoked from within this function. Invocation
  518. * of the handler will be performed in a manner equivalent to using
  519. * asio::io_service::post().
  520. */
  521. template <typename ConstBufferSequence, typename WriteHandler>
  522. ASIO_INITFN_RESULT_TYPE(WriteHandler,
  523. void (asio::error_code, std::size_t))
  524. async_send_to(const ConstBufferSequence& buffers,
  525. const endpoint_type& destination, socket_base::message_flags flags,
  526. ASIO_MOVE_ARG(WriteHandler) handler)
  527. {
  528. // If you get an error on the following line it means that your handler does
  529. // not meet the documented type requirements for a WriteHandler.
  530. ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  531. return this->get_service().async_send_to(
  532. this->get_implementation(), buffers, destination, flags,
  533. ASIO_MOVE_CAST(WriteHandler)(handler));
  534. }
  535. /// Receive some data on a connected socket.
  536. /**
  537. * This function is used to receive data on the raw socket. The function
  538. * call will block until data has been received successfully or an error
  539. * occurs.
  540. *
  541. * @param buffers One or more buffers into which the data will be received.
  542. *
  543. * @returns The number of bytes received.
  544. *
  545. * @throws asio::system_error Thrown on failure.
  546. *
  547. * @note The receive operation can only be used with a connected socket. Use
  548. * the receive_from function to receive data on an unconnected raw
  549. * socket.
  550. *
  551. * @par Example
  552. * To receive into a single data buffer use the @ref buffer function as
  553. * follows:
  554. * @code socket.receive(asio::buffer(data, size)); @endcode
  555. * See the @ref buffer documentation for information on receiving into
  556. * multiple buffers in one go, and how to use it with arrays, boost::array or
  557. * std::vector.
  558. */
  559. template <typename MutableBufferSequence>
  560. std::size_t receive(const MutableBufferSequence& buffers)
  561. {
  562. asio::error_code ec;
  563. std::size_t s = this->get_service().receive(
  564. this->get_implementation(), buffers, 0, ec);
  565. asio::detail::throw_error(ec, "receive");
  566. return s;
  567. }
  568. /// Receive some data on a connected socket.
  569. /**
  570. * This function is used to receive data on the raw socket. The function
  571. * call will block until data has been received successfully or an error
  572. * occurs.
  573. *
  574. * @param buffers One or more buffers into which the data will be received.
  575. *
  576. * @param flags Flags specifying how the receive call is to be made.
  577. *
  578. * @returns The number of bytes received.
  579. *
  580. * @throws asio::system_error Thrown on failure.
  581. *
  582. * @note The receive operation can only be used with a connected socket. Use
  583. * the receive_from function to receive data on an unconnected raw
  584. * socket.
  585. */
  586. template <typename MutableBufferSequence>
  587. std::size_t receive(const MutableBufferSequence& buffers,
  588. socket_base::message_flags flags)
  589. {
  590. asio::error_code ec;
  591. std::size_t s = this->get_service().receive(
  592. this->get_implementation(), buffers, flags, ec);
  593. asio::detail::throw_error(ec, "receive");
  594. return s;
  595. }
  596. /// Receive some data on a connected socket.
  597. /**
  598. * This function is used to receive data on the raw socket. The function
  599. * call will block until data has been received successfully or an error
  600. * occurs.
  601. *
  602. * @param buffers One or more buffers into which the data will be received.
  603. *
  604. * @param flags Flags specifying how the receive call is to be made.
  605. *
  606. * @param ec Set to indicate what error occurred, if any.
  607. *
  608. * @returns The number of bytes received.
  609. *
  610. * @note The receive operation can only be used with a connected socket. Use
  611. * the receive_from function to receive data on an unconnected raw
  612. * socket.
  613. */
  614. template <typename MutableBufferSequence>
  615. std::size_t receive(const MutableBufferSequence& buffers,
  616. socket_base::message_flags flags, asio::error_code& ec)
  617. {
  618. return this->get_service().receive(
  619. this->get_implementation(), buffers, flags, ec);
  620. }
  621. /// Start an asynchronous receive on a connected socket.
  622. /**
  623. * This function is used to asynchronously receive data from the raw
  624. * socket. The function call always returns immediately.
  625. *
  626. * @param buffers One or more buffers into which the data will be received.
  627. * Although the buffers object may be copied as necessary, ownership of the
  628. * underlying memory blocks is retained by the caller, which must guarantee
  629. * that they remain valid until the handler is called.
  630. *
  631. * @param handler The handler to be called when the receive operation
  632. * completes. Copies will be made of the handler as required. The function
  633. * signature of the handler must be:
  634. * @code void handler(
  635. * const asio::error_code& error, // Result of operation.
  636. * std::size_t bytes_transferred // Number of bytes received.
  637. * ); @endcode
  638. * Regardless of whether the asynchronous operation completes immediately or
  639. * not, the handler will not be invoked from within this function. Invocation
  640. * of the handler will be performed in a manner equivalent to using
  641. * asio::io_service::post().
  642. *
  643. * @note The async_receive operation can only be used with a connected socket.
  644. * Use the async_receive_from function to receive data on an unconnected
  645. * raw socket.
  646. *
  647. * @par Example
  648. * To receive into a single data buffer use the @ref buffer function as
  649. * follows:
  650. * @code
  651. * socket.async_receive(asio::buffer(data, size), handler);
  652. * @endcode
  653. * See the @ref buffer documentation for information on receiving into
  654. * multiple buffers in one go, and how to use it with arrays, boost::array or
  655. * std::vector.
  656. */
  657. template <typename MutableBufferSequence, typename ReadHandler>
  658. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  659. void (asio::error_code, std::size_t))
  660. async_receive(const MutableBufferSequence& buffers,
  661. ASIO_MOVE_ARG(ReadHandler) handler)
  662. {
  663. // If you get an error on the following line it means that your handler does
  664. // not meet the documented type requirements for a ReadHandler.
  665. ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  666. return this->get_service().async_receive(this->get_implementation(),
  667. buffers, 0, ASIO_MOVE_CAST(ReadHandler)(handler));
  668. }
  669. /// Start an asynchronous receive on a connected socket.
  670. /**
  671. * This function is used to asynchronously receive data from the raw
  672. * socket. The function call always returns immediately.
  673. *
  674. * @param buffers One or more buffers into which the data will be received.
  675. * Although the buffers object may be copied as necessary, ownership of the
  676. * underlying memory blocks is retained by the caller, which must guarantee
  677. * that they remain valid until the handler is called.
  678. *
  679. * @param flags Flags specifying how the receive call is to be made.
  680. *
  681. * @param handler The handler to be called when the receive operation
  682. * completes. Copies will be made of the handler as required. The function
  683. * signature of the handler must be:
  684. * @code void handler(
  685. * const asio::error_code& error, // Result of operation.
  686. * std::size_t bytes_transferred // Number of bytes received.
  687. * ); @endcode
  688. * Regardless of whether the asynchronous operation completes immediately or
  689. * not, the handler will not be invoked from within this function. Invocation
  690. * of the handler will be performed in a manner equivalent to using
  691. * asio::io_service::post().
  692. *
  693. * @note The async_receive operation can only be used with a connected socket.
  694. * Use the async_receive_from function to receive data on an unconnected
  695. * raw socket.
  696. */
  697. template <typename MutableBufferSequence, typename ReadHandler>
  698. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  699. void (asio::error_code, std::size_t))
  700. async_receive(const MutableBufferSequence& buffers,
  701. socket_base::message_flags flags,
  702. ASIO_MOVE_ARG(ReadHandler) handler)
  703. {
  704. // If you get an error on the following line it means that your handler does
  705. // not meet the documented type requirements for a ReadHandler.
  706. ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  707. return this->get_service().async_receive(this->get_implementation(),
  708. buffers, flags, ASIO_MOVE_CAST(ReadHandler)(handler));
  709. }
  710. /// Receive raw data with the endpoint of the sender.
  711. /**
  712. * This function is used to receive raw data. The function call will block
  713. * until data has been received successfully or an error occurs.
  714. *
  715. * @param buffers One or more buffers into which the data will be received.
  716. *
  717. * @param sender_endpoint An endpoint object that receives the endpoint of
  718. * the remote sender of the data.
  719. *
  720. * @returns The number of bytes received.
  721. *
  722. * @throws asio::system_error Thrown on failure.
  723. *
  724. * @par Example
  725. * To receive into a single data buffer use the @ref buffer function as
  726. * follows:
  727. * @code
  728. * asio::ip::udp::endpoint sender_endpoint;
  729. * socket.receive_from(
  730. * asio::buffer(data, size), sender_endpoint);
  731. * @endcode
  732. * See the @ref buffer documentation for information on receiving into
  733. * multiple buffers in one go, and how to use it with arrays, boost::array or
  734. * std::vector.
  735. */
  736. template <typename MutableBufferSequence>
  737. std::size_t receive_from(const MutableBufferSequence& buffers,
  738. endpoint_type& sender_endpoint)
  739. {
  740. asio::error_code ec;
  741. std::size_t s = this->get_service().receive_from(
  742. this->get_implementation(), buffers, sender_endpoint, 0, ec);
  743. asio::detail::throw_error(ec, "receive_from");
  744. return s;
  745. }
  746. /// Receive raw data with the endpoint of the sender.
  747. /**
  748. * This function is used to receive raw data. The function call will block
  749. * until data has been received successfully or an error occurs.
  750. *
  751. * @param buffers One or more buffers into which the data will be received.
  752. *
  753. * @param sender_endpoint An endpoint object that receives the endpoint of
  754. * the remote sender of the data.
  755. *
  756. * @param flags Flags specifying how the receive call is to be made.
  757. *
  758. * @returns The number of bytes received.
  759. *
  760. * @throws asio::system_error Thrown on failure.
  761. */
  762. template <typename MutableBufferSequence>
  763. std::size_t receive_from(const MutableBufferSequence& buffers,
  764. endpoint_type& sender_endpoint, socket_base::message_flags flags)
  765. {
  766. asio::error_code ec;
  767. std::size_t s = this->get_service().receive_from(
  768. this->get_implementation(), buffers, sender_endpoint, flags, ec);
  769. asio::detail::throw_error(ec, "receive_from");
  770. return s;
  771. }
  772. /// Receive raw data with the endpoint of the sender.
  773. /**
  774. * This function is used to receive raw data. The function call will block
  775. * until data has been received successfully or an error occurs.
  776. *
  777. * @param buffers One or more buffers into which the data will be received.
  778. *
  779. * @param sender_endpoint An endpoint object that receives the endpoint of
  780. * the remote sender of the data.
  781. *
  782. * @param flags Flags specifying how the receive call is to be made.
  783. *
  784. * @param ec Set to indicate what error occurred, if any.
  785. *
  786. * @returns The number of bytes received.
  787. */
  788. template <typename MutableBufferSequence>
  789. std::size_t receive_from(const MutableBufferSequence& buffers,
  790. endpoint_type& sender_endpoint, socket_base::message_flags flags,
  791. asio::error_code& ec)
  792. {
  793. return this->get_service().receive_from(this->get_implementation(),
  794. buffers, sender_endpoint, flags, ec);
  795. }
  796. /// Start an asynchronous receive.
  797. /**
  798. * This function is used to asynchronously receive raw data. The function
  799. * call always returns immediately.
  800. *
  801. * @param buffers One or more buffers into which the data will be received.
  802. * Although the buffers object may be copied as necessary, ownership of the
  803. * underlying memory blocks is retained by the caller, which must guarantee
  804. * that they remain valid until the handler is called.
  805. *
  806. * @param sender_endpoint An endpoint object that receives the endpoint of
  807. * the remote sender of the data. Ownership of the sender_endpoint object
  808. * is retained by the caller, which must guarantee that it is valid until the
  809. * handler is called.
  810. *
  811. * @param handler The handler to be called when the receive operation
  812. * completes. Copies will be made of the handler as required. The function
  813. * signature of the handler must be:
  814. * @code void handler(
  815. * const asio::error_code& error, // Result of operation.
  816. * std::size_t bytes_transferred // Number of bytes received.
  817. * ); @endcode
  818. * Regardless of whether the asynchronous operation completes immediately or
  819. * not, the handler will not be invoked from within this function. Invocation
  820. * of the handler will be performed in a manner equivalent to using
  821. * asio::io_service::post().
  822. *
  823. * @par Example
  824. * To receive into a single data buffer use the @ref buffer function as
  825. * follows:
  826. * @code socket.async_receive_from(
  827. * asio::buffer(data, size), 0, sender_endpoint, handler); @endcode
  828. * See the @ref buffer documentation for information on receiving into
  829. * multiple buffers in one go, and how to use it with arrays, boost::array or
  830. * std::vector.
  831. */
  832. template <typename MutableBufferSequence, typename ReadHandler>
  833. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  834. void (asio::error_code, std::size_t))
  835. async_receive_from(const MutableBufferSequence& buffers,
  836. endpoint_type& sender_endpoint,
  837. ASIO_MOVE_ARG(ReadHandler) handler)
  838. {
  839. // If you get an error on the following line it means that your handler does
  840. // not meet the documented type requirements for a ReadHandler.
  841. ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  842. return this->get_service().async_receive_from(
  843. this->get_implementation(), buffers, sender_endpoint, 0,
  844. ASIO_MOVE_CAST(ReadHandler)(handler));
  845. }
  846. /// Start an asynchronous receive.
  847. /**
  848. * This function is used to asynchronously receive raw data. The function
  849. * call always returns immediately.
  850. *
  851. * @param buffers One or more buffers into which the data will be received.
  852. * Although the buffers object may be copied as necessary, ownership of the
  853. * underlying memory blocks is retained by the caller, which must guarantee
  854. * that they remain valid until the handler is called.
  855. *
  856. * @param sender_endpoint An endpoint object that receives the endpoint of
  857. * the remote sender of the data. Ownership of the sender_endpoint object
  858. * is retained by the caller, which must guarantee that it is valid until the
  859. * handler is called.
  860. *
  861. * @param flags Flags specifying how the receive call is to be made.
  862. *
  863. * @param handler The handler to be called when the receive operation
  864. * completes. Copies will be made of the handler as required. The function
  865. * signature of the handler must be:
  866. * @code void handler(
  867. * const asio::error_code& error, // Result of operation.
  868. * std::size_t bytes_transferred // Number of bytes received.
  869. * ); @endcode
  870. * Regardless of whether the asynchronous operation completes immediately or
  871. * not, the handler will not be invoked from within this function. Invocation
  872. * of the handler will be performed in a manner equivalent to using
  873. * asio::io_service::post().
  874. */
  875. template <typename MutableBufferSequence, typename ReadHandler>
  876. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  877. void (asio::error_code, std::size_t))
  878. async_receive_from(const MutableBufferSequence& buffers,
  879. endpoint_type& sender_endpoint, socket_base::message_flags flags,
  880. ASIO_MOVE_ARG(ReadHandler) handler)
  881. {
  882. // If you get an error on the following line it means that your handler does
  883. // not meet the documented type requirements for a ReadHandler.
  884. ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  885. return this->get_service().async_receive_from(
  886. this->get_implementation(), buffers, sender_endpoint, flags,
  887. ASIO_MOVE_CAST(ReadHandler)(handler));
  888. }
  889. };
  890. } // namespace asio
  891. #include "asio/detail/pop_options.hpp"
  892. #endif // ASIO_BASIC_RAW_SOCKET_HPP