Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

basic_raw_socket.hpp 36KB

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