basic_raw_socket.hpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  1. //
  2. // basic_raw_socket.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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/non_const_lvalue.hpp"
  20. #include "asio/detail/throw_error.hpp"
  21. #include "asio/detail/type_traits.hpp"
  22. #include "asio/error.hpp"
  23. #include "asio/detail/push_options.hpp"
  24. namespace asio {
  25. #if !defined(ASIO_BASIC_RAW_SOCKET_FWD_DECL)
  26. #define ASIO_BASIC_RAW_SOCKET_FWD_DECL
  27. // Forward declaration with defaulted arguments.
  28. template <typename Protocol, typename Executor = any_io_executor>
  29. class basic_raw_socket;
  30. #endif // !defined(ASIO_BASIC_RAW_SOCKET_FWD_DECL)
  31. /// Provides raw-oriented socket functionality.
  32. /**
  33. * The basic_raw_socket class template provides asynchronous and blocking
  34. * raw-oriented socket functionality.
  35. *
  36. * @par Thread Safety
  37. * @e Distinct @e objects: Safe.@n
  38. * @e Shared @e objects: Unsafe.
  39. *
  40. * Synchronous @c send, @c send_to, @c receive, @c receive_from, @c connect,
  41. * and @c shutdown operations are thread safe with respect to each other, if
  42. * the underlying operating system calls are also thread safe. This means that
  43. * it is permitted to perform concurrent calls to these synchronous operations
  44. * on a single socket object. Other synchronous operations, such as @c open or
  45. * @c close, are not thread safe.
  46. */
  47. template <typename Protocol, typename Executor>
  48. class basic_raw_socket
  49. : public basic_socket<Protocol, Executor>
  50. {
  51. private:
  52. class initiate_async_send;
  53. class initiate_async_send_to;
  54. class initiate_async_receive;
  55. class initiate_async_receive_from;
  56. public:
  57. /// The type of the executor associated with the object.
  58. typedef Executor executor_type;
  59. /// Rebinds the socket type to another executor.
  60. template <typename Executor1>
  61. struct rebind_executor
  62. {
  63. /// The socket type when rebound to the specified executor.
  64. typedef basic_raw_socket<Protocol, Executor1> other;
  65. };
  66. /// The native representation of a socket.
  67. #if defined(GENERATING_DOCUMENTATION)
  68. typedef implementation_defined native_handle_type;
  69. #else
  70. typedef typename basic_socket<Protocol,
  71. Executor>::native_handle_type native_handle_type;
  72. #endif
  73. /// The protocol type.
  74. typedef Protocol protocol_type;
  75. /// The endpoint type.
  76. typedef typename Protocol::endpoint endpoint_type;
  77. /// Construct a basic_raw_socket without opening it.
  78. /**
  79. * This constructor creates a raw socket without opening it. The open()
  80. * function must be called before data can be sent or received on the socket.
  81. *
  82. * @param ex The I/O executor that the socket will use, by default, to
  83. * dispatch handlers for any asynchronous operations performed on the socket.
  84. */
  85. explicit basic_raw_socket(const executor_type& ex)
  86. : basic_socket<Protocol, Executor>(ex)
  87. {
  88. }
  89. /// Construct a basic_raw_socket without opening it.
  90. /**
  91. * This constructor creates a raw socket without opening it. The open()
  92. * function must be called before data can be sent or received on the socket.
  93. *
  94. * @param context An execution context which provides the I/O executor that
  95. * the socket will use, by default, to dispatch handlers for any asynchronous
  96. * operations performed on the socket.
  97. */
  98. template <typename ExecutionContext>
  99. explicit basic_raw_socket(ExecutionContext& context,
  100. constraint_t<
  101. is_convertible<ExecutionContext&, execution_context&>::value
  102. > = 0)
  103. : basic_socket<Protocol, Executor>(context)
  104. {
  105. }
  106. /// Construct and open a basic_raw_socket.
  107. /**
  108. * This constructor creates and opens a raw socket.
  109. *
  110. * @param ex The I/O executor that the socket will use, by default, to
  111. * dispatch handlers for any asynchronous operations performed on the socket.
  112. *
  113. * @param protocol An object specifying protocol parameters to be used.
  114. *
  115. * @throws asio::system_error Thrown on failure.
  116. */
  117. basic_raw_socket(const executor_type& ex, const protocol_type& protocol)
  118. : basic_socket<Protocol, Executor>(ex, protocol)
  119. {
  120. }
  121. /// Construct and open a basic_raw_socket.
  122. /**
  123. * This constructor creates and opens a raw socket.
  124. *
  125. * @param context An execution context which provides the I/O executor that
  126. * the socket will use, by default, to dispatch handlers for any asynchronous
  127. * operations performed on the socket.
  128. *
  129. * @param protocol An object specifying protocol parameters to be used.
  130. *
  131. * @throws asio::system_error Thrown on failure.
  132. */
  133. template <typename ExecutionContext>
  134. basic_raw_socket(ExecutionContext& context, const protocol_type& protocol,
  135. constraint_t<
  136. is_convertible<ExecutionContext&, execution_context&>::value,
  137. defaulted_constraint
  138. > = defaulted_constraint())
  139. : basic_socket<Protocol, Executor>(context, protocol)
  140. {
  141. }
  142. /// Construct a basic_raw_socket, opening it and binding it to the given
  143. /// local endpoint.
  144. /**
  145. * This constructor creates a raw socket and automatically opens it bound
  146. * to the specified endpoint on the local machine. The protocol used is the
  147. * protocol associated with the given endpoint.
  148. *
  149. * @param ex The I/O executor that the socket will use, by default, to
  150. * dispatch handlers for any asynchronous operations performed on the socket.
  151. *
  152. * @param endpoint An endpoint on the local machine to which the raw
  153. * socket will be bound.
  154. *
  155. * @throws asio::system_error Thrown on failure.
  156. */
  157. basic_raw_socket(const executor_type& ex, const endpoint_type& endpoint)
  158. : basic_socket<Protocol, Executor>(ex, endpoint)
  159. {
  160. }
  161. /// Construct a basic_raw_socket, opening it and binding it to the given
  162. /// local endpoint.
  163. /**
  164. * This constructor creates a raw socket and automatically opens it bound
  165. * to the specified endpoint on the local machine. The protocol used is the
  166. * protocol associated with the given endpoint.
  167. *
  168. * @param context An execution context which provides the I/O executor that
  169. * the socket will use, by default, to dispatch handlers for any asynchronous
  170. * operations performed on the socket.
  171. *
  172. * @param endpoint An endpoint on the local machine to which the raw
  173. * socket will be bound.
  174. *
  175. * @throws asio::system_error Thrown on failure.
  176. */
  177. template <typename ExecutionContext>
  178. basic_raw_socket(ExecutionContext& context, const endpoint_type& endpoint,
  179. constraint_t<
  180. is_convertible<ExecutionContext&, execution_context&>::value
  181. > = 0)
  182. : basic_socket<Protocol, Executor>(context, endpoint)
  183. {
  184. }
  185. /// Construct a basic_raw_socket on an existing native socket.
  186. /**
  187. * This constructor creates a raw socket object to hold an existing
  188. * native socket.
  189. *
  190. * @param ex The I/O executor that the socket will use, by default, to
  191. * dispatch handlers for any asynchronous operations performed on the socket.
  192. *
  193. * @param protocol An object specifying protocol parameters to be used.
  194. *
  195. * @param native_socket The new underlying socket implementation.
  196. *
  197. * @throws asio::system_error Thrown on failure.
  198. */
  199. basic_raw_socket(const executor_type& ex,
  200. const protocol_type& protocol, const native_handle_type& native_socket)
  201. : basic_socket<Protocol, Executor>(ex, protocol, native_socket)
  202. {
  203. }
  204. /// Construct a basic_raw_socket on an existing native socket.
  205. /**
  206. * This constructor creates a raw socket object to hold an existing
  207. * native socket.
  208. *
  209. * @param context An execution context which provides the I/O executor that
  210. * the socket will use, by default, to dispatch handlers for any asynchronous
  211. * operations performed on the socket.
  212. *
  213. * @param protocol An object specifying protocol parameters to be used.
  214. *
  215. * @param native_socket The new underlying socket implementation.
  216. *
  217. * @throws asio::system_error Thrown on failure.
  218. */
  219. template <typename ExecutionContext>
  220. basic_raw_socket(ExecutionContext& context,
  221. const protocol_type& protocol, const native_handle_type& native_socket,
  222. constraint_t<
  223. is_convertible<ExecutionContext&, execution_context&>::value
  224. > = 0)
  225. : basic_socket<Protocol, Executor>(context, protocol, native_socket)
  226. {
  227. }
  228. /// Move-construct a basic_raw_socket from another.
  229. /**
  230. * This constructor moves a raw socket from one object to another.
  231. *
  232. * @param other The other basic_raw_socket object from which the move
  233. * will occur.
  234. *
  235. * @note Following the move, the moved-from object is in the same state as if
  236. * constructed using the @c basic_raw_socket(const executor_type&)
  237. * constructor.
  238. */
  239. basic_raw_socket(basic_raw_socket&& other) noexcept
  240. : basic_socket<Protocol, Executor>(std::move(other))
  241. {
  242. }
  243. /// Move-assign a basic_raw_socket from another.
  244. /**
  245. * This assignment operator moves a raw socket from one object to another.
  246. *
  247. * @param other The other basic_raw_socket object from which the move
  248. * will occur.
  249. *
  250. * @note Following the move, the moved-from object is in the same state as if
  251. * constructed using the @c basic_raw_socket(const executor_type&)
  252. * constructor.
  253. */
  254. basic_raw_socket& operator=(basic_raw_socket&& other)
  255. {
  256. basic_socket<Protocol, Executor>::operator=(std::move(other));
  257. return *this;
  258. }
  259. /// Move-construct a basic_raw_socket from a socket of another protocol
  260. /// type.
  261. /**
  262. * This constructor moves a raw socket from one object to another.
  263. *
  264. * @param other The other basic_raw_socket object from which the move
  265. * will occur.
  266. *
  267. * @note Following the move, the moved-from object is in the same state as if
  268. * constructed using the @c basic_raw_socket(const executor_type&)
  269. * constructor.
  270. */
  271. template <typename Protocol1, typename Executor1>
  272. basic_raw_socket(basic_raw_socket<Protocol1, Executor1>&& other,
  273. constraint_t<
  274. is_convertible<Protocol1, Protocol>::value
  275. && is_convertible<Executor1, Executor>::value
  276. > = 0)
  277. : basic_socket<Protocol, Executor>(std::move(other))
  278. {
  279. }
  280. /// Move-assign a basic_raw_socket from a socket of another protocol type.
  281. /**
  282. * This assignment operator moves a raw socket from one object to another.
  283. *
  284. * @param other The other basic_raw_socket object from which the move
  285. * will occur.
  286. *
  287. * @note Following the move, the moved-from object is in the same state as if
  288. * constructed using the @c basic_raw_socket(const executor_type&)
  289. * constructor.
  290. */
  291. template <typename Protocol1, typename Executor1>
  292. constraint_t<
  293. is_convertible<Protocol1, Protocol>::value
  294. && is_convertible<Executor1, Executor>::value,
  295. basic_raw_socket&
  296. > operator=(basic_raw_socket<Protocol1, Executor1>&& other)
  297. {
  298. basic_socket<Protocol, Executor>::operator=(std::move(other));
  299. return *this;
  300. }
  301. /// Destroys the socket.
  302. /**
  303. * This function destroys the socket, cancelling any outstanding asynchronous
  304. * operations associated with the socket as if by calling @c cancel.
  305. */
  306. ~basic_raw_socket()
  307. {
  308. }
  309. /// Send some data on a connected socket.
  310. /**
  311. * This function is used to send data on the raw socket. The function call
  312. * will block until the data has been sent successfully or an error occurs.
  313. *
  314. * @param buffers One ore more data buffers to be sent on the socket.
  315. *
  316. * @returns The number of bytes sent.
  317. *
  318. * @throws asio::system_error Thrown on failure.
  319. *
  320. * @note The send operation can only be used with a connected socket. Use
  321. * the send_to function to send data on an unconnected raw socket.
  322. *
  323. * @par Example
  324. * To send a single data buffer use the @ref buffer function as follows:
  325. * @code socket.send(asio::buffer(data, size)); @endcode
  326. * See the @ref buffer documentation for information on sending multiple
  327. * buffers in one go, and how to use it with arrays, boost::array or
  328. * std::vector.
  329. */
  330. template <typename ConstBufferSequence>
  331. std::size_t send(const ConstBufferSequence& buffers)
  332. {
  333. asio::error_code ec;
  334. std::size_t s = this->impl_.get_service().send(
  335. this->impl_.get_implementation(), buffers, 0, ec);
  336. asio::detail::throw_error(ec, "send");
  337. return s;
  338. }
  339. /// Send some data on a connected socket.
  340. /**
  341. * This function is used to send data on the raw socket. The function call
  342. * will block until the data has been sent successfully or an error occurs.
  343. *
  344. * @param buffers One ore more data buffers to be sent on the socket.
  345. *
  346. * @param flags Flags specifying how the send call is to be made.
  347. *
  348. * @returns The number of bytes sent.
  349. *
  350. * @throws asio::system_error Thrown on failure.
  351. *
  352. * @note The send operation can only be used with a connected socket. Use
  353. * the send_to function to send data on an unconnected raw socket.
  354. */
  355. template <typename ConstBufferSequence>
  356. std::size_t send(const ConstBufferSequence& buffers,
  357. socket_base::message_flags flags)
  358. {
  359. asio::error_code ec;
  360. std::size_t s = this->impl_.get_service().send(
  361. this->impl_.get_implementation(), buffers, flags, ec);
  362. asio::detail::throw_error(ec, "send");
  363. return s;
  364. }
  365. /// Send some data on a connected socket.
  366. /**
  367. * This function is used to send data on the raw socket. The function call
  368. * will block until the data has been sent successfully or an error occurs.
  369. *
  370. * @param buffers One or more data buffers to be sent on the socket.
  371. *
  372. * @param flags Flags specifying how the send call is to be made.
  373. *
  374. * @param ec Set to indicate what error occurred, if any.
  375. *
  376. * @returns The number of bytes sent.
  377. *
  378. * @note The send operation can only be used with a connected socket. Use
  379. * the send_to function to send data on an unconnected raw socket.
  380. */
  381. template <typename ConstBufferSequence>
  382. std::size_t send(const ConstBufferSequence& buffers,
  383. socket_base::message_flags flags, asio::error_code& ec)
  384. {
  385. return this->impl_.get_service().send(
  386. this->impl_.get_implementation(), buffers, flags, ec);
  387. }
  388. /// Start an asynchronous send on a connected socket.
  389. /**
  390. * This function is used to asynchronously send data on the raw socket. It is
  391. * an initiating function for an @ref asynchronous_operation, and always
  392. * returns immediately.
  393. *
  394. * @param buffers One or more data buffers to be sent on the socket. Although
  395. * the buffers object may be copied as necessary, ownership of the underlying
  396. * memory blocks is retained by the caller, which must guarantee that they
  397. * remain valid until the completion handler is called.
  398. *
  399. * @param token The @ref completion_token that will be used to produce a
  400. * completion handler, which will be called when the send completes.
  401. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  402. * @ref yield_context, or a function object with the correct completion
  403. * signature. The function signature of the completion handler must be:
  404. * @code void handler(
  405. * const asio::error_code& error, // Result of operation.
  406. * std::size_t bytes_transferred // Number of bytes sent.
  407. * ); @endcode
  408. * Regardless of whether the asynchronous operation completes immediately or
  409. * not, the completion handler will not be invoked from within this function.
  410. * On immediate completion, invocation of the handler will be performed in a
  411. * manner equivalent to using asio::post().
  412. *
  413. * @par Completion Signature
  414. * @code void(asio::error_code, std::size_t) @endcode
  415. *
  416. * @note The async_send operation can only be used with a connected socket.
  417. * Use the async_send_to function to send data on an unconnected raw
  418. * socket.
  419. *
  420. * @par Example
  421. * To send a single data buffer use the @ref buffer function as follows:
  422. * @code
  423. * socket.async_send(asio::buffer(data, size), handler);
  424. * @endcode
  425. * See the @ref buffer documentation for information on sending multiple
  426. * buffers in one go, and how to use it with arrays, boost::array or
  427. * std::vector.
  428. *
  429. * @par Per-Operation Cancellation
  430. * On POSIX or Windows operating systems, this asynchronous operation supports
  431. * cancellation for the following asio::cancellation_type values:
  432. *
  433. * @li @c cancellation_type::terminal
  434. *
  435. * @li @c cancellation_type::partial
  436. *
  437. * @li @c cancellation_type::total
  438. */
  439. template <typename ConstBufferSequence,
  440. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  441. std::size_t)) WriteToken = default_completion_token_t<executor_type>>
  442. auto async_send(const ConstBufferSequence& buffers,
  443. WriteToken&& token = default_completion_token_t<executor_type>())
  444. -> decltype(
  445. async_initiate<WriteToken,
  446. void (asio::error_code, std::size_t)>(
  447. declval<initiate_async_send>(), token,
  448. buffers, socket_base::message_flags(0)))
  449. {
  450. return async_initiate<WriteToken,
  451. void (asio::error_code, std::size_t)>(
  452. initiate_async_send(this), token,
  453. buffers, socket_base::message_flags(0));
  454. }
  455. /// Start an asynchronous send on a connected socket.
  456. /**
  457. * This function is used to asynchronously send data on the raw socket. It is
  458. * an initiating function for an @ref asynchronous_operation, and always
  459. * returns immediately.
  460. *
  461. * @param buffers One or more data buffers to be sent on the socket. Although
  462. * the buffers object may be copied as necessary, ownership of the underlying
  463. * memory blocks is retained by the caller, which must guarantee that they
  464. * remain valid until the completion handler is called.
  465. *
  466. * @param flags Flags specifying how the send call is to be made.
  467. *
  468. * @param token The @ref completion_token that will be used to produce a
  469. * completion handler, which will be called when the send completes.
  470. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  471. * @ref yield_context, or a function object with the correct completion
  472. * signature. The function signature of the completion handler must be:
  473. * @code void handler(
  474. * const asio::error_code& error, // Result of operation.
  475. * std::size_t bytes_transferred // Number of bytes sent.
  476. * ); @endcode
  477. * Regardless of whether the asynchronous operation completes immediately or
  478. * not, the completion handler will not be invoked from within this function.
  479. * On immediate completion, invocation of the handler will be performed in a
  480. * manner equivalent to using asio::post().
  481. *
  482. * @par Completion Signature
  483. * @code void(asio::error_code, std::size_t) @endcode
  484. *
  485. * @note The async_send operation can only be used with a connected socket.
  486. * Use the async_send_to function to send data on an unconnected raw
  487. * socket.
  488. *
  489. * @par Per-Operation Cancellation
  490. * On POSIX or Windows operating systems, this asynchronous operation supports
  491. * cancellation for the following asio::cancellation_type values:
  492. *
  493. * @li @c cancellation_type::terminal
  494. *
  495. * @li @c cancellation_type::partial
  496. *
  497. * @li @c cancellation_type::total
  498. */
  499. template <typename ConstBufferSequence,
  500. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  501. std::size_t)) WriteToken = default_completion_token_t<executor_type>>
  502. auto async_send(const ConstBufferSequence& buffers,
  503. socket_base::message_flags flags,
  504. WriteToken&& token = default_completion_token_t<executor_type>())
  505. -> decltype(
  506. async_initiate<WriteToken,
  507. void (asio::error_code, std::size_t)>(
  508. declval<initiate_async_send>(), token, buffers, flags))
  509. {
  510. return async_initiate<WriteToken,
  511. void (asio::error_code, std::size_t)>(
  512. initiate_async_send(this), token, buffers, flags);
  513. }
  514. /// Send raw data to the specified endpoint.
  515. /**
  516. * This function is used to send raw data to the specified remote endpoint.
  517. * The function call will block until the data has been sent successfully or
  518. * an error occurs.
  519. *
  520. * @param buffers One or more data buffers to be sent to the remote endpoint.
  521. *
  522. * @param destination The remote endpoint to which the data will be sent.
  523. *
  524. * @returns The number of bytes sent.
  525. *
  526. * @throws asio::system_error Thrown on failure.
  527. *
  528. * @par Example
  529. * To send a single data buffer use the @ref buffer function as follows:
  530. * @code
  531. * asio::ip::udp::endpoint destination(
  532. * asio::ip::address::from_string("1.2.3.4"), 12345);
  533. * socket.send_to(asio::buffer(data, size), destination);
  534. * @endcode
  535. * See the @ref buffer documentation for information on sending multiple
  536. * buffers in one go, and how to use it with arrays, boost::array or
  537. * std::vector.
  538. */
  539. template <typename ConstBufferSequence>
  540. std::size_t send_to(const ConstBufferSequence& buffers,
  541. const endpoint_type& destination)
  542. {
  543. asio::error_code ec;
  544. std::size_t s = this->impl_.get_service().send_to(
  545. this->impl_.get_implementation(), buffers, destination, 0, ec);
  546. asio::detail::throw_error(ec, "send_to");
  547. return s;
  548. }
  549. /// Send raw data to the specified endpoint.
  550. /**
  551. * This function is used to send raw data to the specified remote endpoint.
  552. * The function call will block until the data has been sent successfully or
  553. * an error occurs.
  554. *
  555. * @param buffers One or more data buffers to be sent to the remote endpoint.
  556. *
  557. * @param destination The remote endpoint to which the data will be sent.
  558. *
  559. * @param flags Flags specifying how the send call is to be made.
  560. *
  561. * @returns The number of bytes sent.
  562. *
  563. * @throws asio::system_error Thrown on failure.
  564. */
  565. template <typename ConstBufferSequence>
  566. std::size_t send_to(const ConstBufferSequence& buffers,
  567. const endpoint_type& destination, socket_base::message_flags flags)
  568. {
  569. asio::error_code ec;
  570. std::size_t s = this->impl_.get_service().send_to(
  571. this->impl_.get_implementation(), buffers, destination, flags, ec);
  572. asio::detail::throw_error(ec, "send_to");
  573. return s;
  574. }
  575. /// Send raw data to the specified endpoint.
  576. /**
  577. * This function is used to send raw data to the specified remote endpoint.
  578. * The function call will block until the data has been sent successfully or
  579. * an error occurs.
  580. *
  581. * @param buffers One or more data buffers to be sent to the remote endpoint.
  582. *
  583. * @param destination The remote endpoint to which the data will be sent.
  584. *
  585. * @param flags Flags specifying how the send call is to be made.
  586. *
  587. * @param ec Set to indicate what error occurred, if any.
  588. *
  589. * @returns The number of bytes sent.
  590. */
  591. template <typename ConstBufferSequence>
  592. std::size_t send_to(const ConstBufferSequence& buffers,
  593. const endpoint_type& destination, socket_base::message_flags flags,
  594. asio::error_code& ec)
  595. {
  596. return this->impl_.get_service().send_to(this->impl_.get_implementation(),
  597. buffers, destination, flags, ec);
  598. }
  599. /// Start an asynchronous send.
  600. /**
  601. * This function is used to asynchronously send raw data to the specified
  602. * remote endpoint. It is an initiating function for an @ref
  603. * asynchronous_operation, and always returns immediately.
  604. *
  605. * @param buffers One or more data buffers to be sent to the remote endpoint.
  606. * Although the buffers object may be copied as necessary, ownership of the
  607. * underlying memory blocks is retained by the caller, which must guarantee
  608. * that they remain valid until the completion handler is called.
  609. *
  610. * @param destination The remote endpoint to which the data will be sent.
  611. * Copies will be made of the endpoint as required.
  612. *
  613. * @param token The @ref completion_token that will be used to produce a
  614. * completion handler, which will be called when the send completes.
  615. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  616. * @ref yield_context, or a function object with the correct completion
  617. * signature. The function signature of the completion handler must be:
  618. * @code void handler(
  619. * const asio::error_code& error, // Result of operation.
  620. * std::size_t bytes_transferred // Number of bytes sent.
  621. * ); @endcode
  622. * Regardless of whether the asynchronous operation completes immediately or
  623. * not, the completion handler will not be invoked from within this function.
  624. * On immediate completion, invocation of the handler will be performed in a
  625. * manner equivalent to using asio::post().
  626. *
  627. * @par Completion Signature
  628. * @code void(asio::error_code, std::size_t) @endcode
  629. *
  630. * @par Example
  631. * To send a single data buffer use the @ref buffer function as follows:
  632. * @code
  633. * asio::ip::udp::endpoint destination(
  634. * asio::ip::address::from_string("1.2.3.4"), 12345);
  635. * socket.async_send_to(
  636. * asio::buffer(data, size), destination, handler);
  637. * @endcode
  638. * See the @ref buffer documentation for information on sending multiple
  639. * buffers in one go, and how to use it with arrays, boost::array or
  640. * std::vector.
  641. *
  642. * @par Per-Operation Cancellation
  643. * On POSIX or Windows operating systems, this asynchronous operation supports
  644. * cancellation for the following asio::cancellation_type values:
  645. *
  646. * @li @c cancellation_type::terminal
  647. *
  648. * @li @c cancellation_type::partial
  649. *
  650. * @li @c cancellation_type::total
  651. */
  652. template <typename ConstBufferSequence,
  653. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  654. std::size_t)) WriteToken = default_completion_token_t<executor_type>>
  655. auto async_send_to(const ConstBufferSequence& buffers,
  656. const endpoint_type& destination,
  657. WriteToken&& token = default_completion_token_t<executor_type>())
  658. -> decltype(
  659. async_initiate<WriteToken,
  660. void (asio::error_code, std::size_t)>(
  661. declval<initiate_async_send_to>(), token, buffers,
  662. destination, socket_base::message_flags(0)))
  663. {
  664. return async_initiate<WriteToken,
  665. void (asio::error_code, std::size_t)>(
  666. initiate_async_send_to(this), token, buffers,
  667. destination, socket_base::message_flags(0));
  668. }
  669. /// Start an asynchronous send.
  670. /**
  671. * This function is used to asynchronously send raw data to the specified
  672. * remote endpoint. It is an initiating function for an @ref
  673. * asynchronous_operation, and always returns immediately.
  674. *
  675. * @param buffers One or more data buffers to be sent to the remote endpoint.
  676. * Although the buffers object may be copied as necessary, ownership of the
  677. * underlying memory blocks is retained by the caller, which must guarantee
  678. * that they remain valid until the completion handler is called.
  679. *
  680. * @param flags Flags specifying how the send call is to be made.
  681. *
  682. * @param destination The remote endpoint to which the data will be sent.
  683. * Copies will be made of the endpoint as required.
  684. *
  685. * @param token The @ref completion_token that will be used to produce a
  686. * completion handler, which will be called when the send completes.
  687. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  688. * @ref yield_context, or a function object with the correct completion
  689. * signature. The function signature of the completion handler must be:
  690. * @code void handler(
  691. * const asio::error_code& error, // Result of operation.
  692. * std::size_t bytes_transferred // Number of bytes sent.
  693. * ); @endcode
  694. * Regardless of whether the asynchronous operation completes immediately or
  695. * not, the completion handler will not be invoked from within this function.
  696. * On immediate completion, invocation of the handler will be performed in a
  697. * manner equivalent to using asio::post().
  698. *
  699. * @par Completion Signature
  700. * @code void(asio::error_code, std::size_t) @endcode
  701. *
  702. * @par Per-Operation Cancellation
  703. * On POSIX or Windows operating systems, this asynchronous operation supports
  704. * cancellation for the following asio::cancellation_type values:
  705. *
  706. * @li @c cancellation_type::terminal
  707. *
  708. * @li @c cancellation_type::partial
  709. *
  710. * @li @c cancellation_type::total
  711. */
  712. template <typename ConstBufferSequence,
  713. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  714. std::size_t)) WriteToken = default_completion_token_t<executor_type>>
  715. auto async_send_to(const ConstBufferSequence& buffers,
  716. const endpoint_type& destination, socket_base::message_flags flags,
  717. WriteToken&& token = default_completion_token_t<executor_type>())
  718. -> decltype(
  719. async_initiate<WriteToken,
  720. void (asio::error_code, std::size_t)>(
  721. declval<initiate_async_send_to>(), token,
  722. buffers, destination, flags))
  723. {
  724. return async_initiate<WriteToken,
  725. void (asio::error_code, std::size_t)>(
  726. initiate_async_send_to(this), token,
  727. buffers, destination, flags);
  728. }
  729. /// Receive some data on a connected socket.
  730. /**
  731. * This function is used to receive data on the raw socket. The function
  732. * call will block until data has been received successfully or an error
  733. * occurs.
  734. *
  735. * @param buffers One or more buffers into which the data will be received.
  736. *
  737. * @returns The number of bytes received.
  738. *
  739. * @throws asio::system_error Thrown on failure.
  740. *
  741. * @note The receive operation can only be used with a connected socket. Use
  742. * the receive_from function to receive data on an unconnected raw
  743. * socket.
  744. *
  745. * @par Example
  746. * To receive into a single data buffer use the @ref buffer function as
  747. * follows:
  748. * @code socket.receive(asio::buffer(data, size)); @endcode
  749. * See the @ref buffer documentation for information on receiving into
  750. * multiple buffers in one go, and how to use it with arrays, boost::array or
  751. * std::vector.
  752. */
  753. template <typename MutableBufferSequence>
  754. std::size_t receive(const MutableBufferSequence& buffers)
  755. {
  756. asio::error_code ec;
  757. std::size_t s = this->impl_.get_service().receive(
  758. this->impl_.get_implementation(), buffers, 0, ec);
  759. asio::detail::throw_error(ec, "receive");
  760. return s;
  761. }
  762. /// Receive some data on a connected socket.
  763. /**
  764. * This function is used to receive data on the raw socket. The function
  765. * call will block until data has been received successfully or an error
  766. * occurs.
  767. *
  768. * @param buffers One or more buffers into which the data will be received.
  769. *
  770. * @param flags Flags specifying how the receive call is to be made.
  771. *
  772. * @returns The number of bytes received.
  773. *
  774. * @throws asio::system_error Thrown on failure.
  775. *
  776. * @note The receive operation can only be used with a connected socket. Use
  777. * the receive_from function to receive data on an unconnected raw
  778. * socket.
  779. */
  780. template <typename MutableBufferSequence>
  781. std::size_t receive(const MutableBufferSequence& buffers,
  782. socket_base::message_flags flags)
  783. {
  784. asio::error_code ec;
  785. std::size_t s = this->impl_.get_service().receive(
  786. this->impl_.get_implementation(), buffers, flags, ec);
  787. asio::detail::throw_error(ec, "receive");
  788. return s;
  789. }
  790. /// Receive some data on a connected socket.
  791. /**
  792. * This function is used to receive data on the raw socket. The function
  793. * call will block until data has been received successfully or an error
  794. * occurs.
  795. *
  796. * @param buffers One or more buffers into which the data will be received.
  797. *
  798. * @param flags Flags specifying how the receive call is to be made.
  799. *
  800. * @param ec Set to indicate what error occurred, if any.
  801. *
  802. * @returns The number of bytes received.
  803. *
  804. * @note The receive operation can only be used with a connected socket. Use
  805. * the receive_from function to receive data on an unconnected raw
  806. * socket.
  807. */
  808. template <typename MutableBufferSequence>
  809. std::size_t receive(const MutableBufferSequence& buffers,
  810. socket_base::message_flags flags, asio::error_code& ec)
  811. {
  812. return this->impl_.get_service().receive(
  813. this->impl_.get_implementation(), buffers, flags, ec);
  814. }
  815. /// Start an asynchronous receive on a connected socket.
  816. /**
  817. * This function is used to asynchronously receive data from the raw
  818. * socket. It is an initiating function for an @ref asynchronous_operation,
  819. * and always returns immediately.
  820. *
  821. * @param buffers One or more buffers into which the data will be received.
  822. * Although the buffers object may be copied as necessary, ownership of the
  823. * underlying memory blocks is retained by the caller, which must guarantee
  824. * that they remain valid until the completion handler is called.
  825. *
  826. * @param token The @ref completion_token that will be used to produce a
  827. * completion handler, which will be called when the receive completes.
  828. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  829. * @ref yield_context, or a function object with the correct completion
  830. * signature. The function signature of the completion handler must be:
  831. * @code void handler(
  832. * const asio::error_code& error, // Result of operation.
  833. * std::size_t bytes_transferred // Number of bytes received.
  834. * ); @endcode
  835. * Regardless of whether the asynchronous operation completes immediately or
  836. * not, the completion handler will not be invoked from within this function.
  837. * On immediate completion, invocation of the handler will be performed in a
  838. * manner equivalent to using asio::post().
  839. *
  840. * @par Completion Signature
  841. * @code void(asio::error_code, std::size_t) @endcode
  842. *
  843. * @note The async_receive operation can only be used with a connected socket.
  844. * Use the async_receive_from function to receive data on an unconnected
  845. * raw socket.
  846. *
  847. * @par Example
  848. * To receive into a single data buffer use the @ref buffer function as
  849. * follows:
  850. * @code
  851. * socket.async_receive(asio::buffer(data, size), handler);
  852. * @endcode
  853. * See the @ref buffer documentation for information on receiving into
  854. * multiple buffers in one go, and how to use it with arrays, boost::array or
  855. * std::vector.
  856. *
  857. * @par Per-Operation Cancellation
  858. * On POSIX or Windows operating systems, this asynchronous operation supports
  859. * cancellation for the following asio::cancellation_type values:
  860. *
  861. * @li @c cancellation_type::terminal
  862. *
  863. * @li @c cancellation_type::partial
  864. *
  865. * @li @c cancellation_type::total
  866. */
  867. template <typename MutableBufferSequence,
  868. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  869. std::size_t)) ReadToken = default_completion_token_t<executor_type>>
  870. auto async_receive(const MutableBufferSequence& buffers,
  871. ReadToken&& token = default_completion_token_t<executor_type>())
  872. -> decltype(
  873. async_initiate<ReadToken,
  874. void (asio::error_code, std::size_t)>(
  875. declval<initiate_async_receive>(), token,
  876. buffers, socket_base::message_flags(0)))
  877. {
  878. return async_initiate<ReadToken,
  879. void (asio::error_code, std::size_t)>(
  880. initiate_async_receive(this), token,
  881. buffers, socket_base::message_flags(0));
  882. }
  883. /// Start an asynchronous receive on a connected socket.
  884. /**
  885. * This function is used to asynchronously receive data from the raw
  886. * socket. It is an initiating function for an @ref asynchronous_operation,
  887. * and always returns immediately.
  888. *
  889. * @param buffers One or more buffers into which the data will be received.
  890. * Although the buffers object may be copied as necessary, ownership of the
  891. * underlying memory blocks is retained by the caller, which must guarantee
  892. * that they remain valid until the completion handler is called.
  893. *
  894. * @param flags Flags specifying how the receive call is to be made.
  895. *
  896. * @param token The @ref completion_token that will be used to produce a
  897. * completion handler, which will be called when the receive completes.
  898. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  899. * @ref yield_context, or a function object with the correct completion
  900. * signature. The function signature of the completion handler must be:
  901. * @code void handler(
  902. * const asio::error_code& error, // Result of operation.
  903. * std::size_t bytes_transferred // Number of bytes received.
  904. * ); @endcode
  905. * Regardless of whether the asynchronous operation completes immediately or
  906. * not, the completion handler will not be invoked from within this function.
  907. * On immediate completion, invocation of the handler will be performed in a
  908. * manner equivalent to using asio::post().
  909. *
  910. * @par Completion Signature
  911. * @code void(asio::error_code, std::size_t) @endcode
  912. *
  913. * @note The async_receive operation can only be used with a connected socket.
  914. * Use the async_receive_from function to receive data on an unconnected
  915. * raw socket.
  916. *
  917. * @par Per-Operation Cancellation
  918. * On POSIX or Windows operating systems, this asynchronous operation supports
  919. * cancellation for the following asio::cancellation_type values:
  920. *
  921. * @li @c cancellation_type::terminal
  922. *
  923. * @li @c cancellation_type::partial
  924. *
  925. * @li @c cancellation_type::total
  926. */
  927. template <typename MutableBufferSequence,
  928. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  929. std::size_t)) ReadToken
  930. = default_completion_token_t<executor_type>>
  931. auto async_receive(const MutableBufferSequence& buffers,
  932. socket_base::message_flags flags,
  933. ReadToken&& token = default_completion_token_t<executor_type>())
  934. -> decltype(
  935. async_initiate<ReadToken,
  936. void (asio::error_code, std::size_t)>(
  937. declval<initiate_async_receive>(), token, buffers, flags))
  938. {
  939. return async_initiate<ReadToken,
  940. void (asio::error_code, std::size_t)>(
  941. initiate_async_receive(this), token, buffers, flags);
  942. }
  943. /// Receive raw data with the endpoint of the sender.
  944. /**
  945. * This function is used to receive raw data. The function call will block
  946. * until data has been received successfully or an error occurs.
  947. *
  948. * @param buffers One or more buffers into which the data will be received.
  949. *
  950. * @param sender_endpoint An endpoint object that receives the endpoint of
  951. * the remote sender of the data.
  952. *
  953. * @returns The number of bytes received.
  954. *
  955. * @throws asio::system_error Thrown on failure.
  956. *
  957. * @par Example
  958. * To receive into a single data buffer use the @ref buffer function as
  959. * follows:
  960. * @code
  961. * asio::ip::udp::endpoint sender_endpoint;
  962. * socket.receive_from(
  963. * asio::buffer(data, size), sender_endpoint);
  964. * @endcode
  965. * See the @ref buffer documentation for information on receiving into
  966. * multiple buffers in one go, and how to use it with arrays, boost::array or
  967. * std::vector.
  968. */
  969. template <typename MutableBufferSequence>
  970. std::size_t receive_from(const MutableBufferSequence& buffers,
  971. endpoint_type& sender_endpoint)
  972. {
  973. asio::error_code ec;
  974. std::size_t s = this->impl_.get_service().receive_from(
  975. this->impl_.get_implementation(), buffers, sender_endpoint, 0, ec);
  976. asio::detail::throw_error(ec, "receive_from");
  977. return s;
  978. }
  979. /// Receive raw data with the endpoint of the sender.
  980. /**
  981. * This function is used to receive raw data. The function call will block
  982. * until data has been received successfully or an error occurs.
  983. *
  984. * @param buffers One or more buffers into which the data will be received.
  985. *
  986. * @param sender_endpoint An endpoint object that receives the endpoint of
  987. * the remote sender of the data.
  988. *
  989. * @param flags Flags specifying how the receive call is to be made.
  990. *
  991. * @returns The number of bytes received.
  992. *
  993. * @throws asio::system_error Thrown on failure.
  994. */
  995. template <typename MutableBufferSequence>
  996. std::size_t receive_from(const MutableBufferSequence& buffers,
  997. endpoint_type& sender_endpoint, socket_base::message_flags flags)
  998. {
  999. asio::error_code ec;
  1000. std::size_t s = this->impl_.get_service().receive_from(
  1001. this->impl_.get_implementation(), buffers, sender_endpoint, flags, ec);
  1002. asio::detail::throw_error(ec, "receive_from");
  1003. return s;
  1004. }
  1005. /// Receive raw data with the endpoint of the sender.
  1006. /**
  1007. * This function is used to receive raw data. The function call will block
  1008. * until data has been received successfully or an error occurs.
  1009. *
  1010. * @param buffers One or more buffers into which the data will be received.
  1011. *
  1012. * @param sender_endpoint An endpoint object that receives the endpoint of
  1013. * the remote sender of the data.
  1014. *
  1015. * @param flags Flags specifying how the receive call is to be made.
  1016. *
  1017. * @param ec Set to indicate what error occurred, if any.
  1018. *
  1019. * @returns The number of bytes received.
  1020. */
  1021. template <typename MutableBufferSequence>
  1022. std::size_t receive_from(const MutableBufferSequence& buffers,
  1023. endpoint_type& sender_endpoint, socket_base::message_flags flags,
  1024. asio::error_code& ec)
  1025. {
  1026. return this->impl_.get_service().receive_from(
  1027. this->impl_.get_implementation(), buffers, sender_endpoint, flags, ec);
  1028. }
  1029. /// Start an asynchronous receive.
  1030. /**
  1031. * This function is used to asynchronously receive raw data. It is an
  1032. * initiating function for an @ref asynchronous_operation, and always returns
  1033. * immediately.
  1034. *
  1035. * @param buffers One or more buffers into which the data will be received.
  1036. * Although the buffers object may be copied as necessary, ownership of the
  1037. * underlying memory blocks is retained by the caller, which must guarantee
  1038. * that they remain valid until the completion handler is called.
  1039. *
  1040. * @param sender_endpoint An endpoint object that receives the endpoint of
  1041. * the remote sender of the data. Ownership of the sender_endpoint object
  1042. * is retained by the caller, which must guarantee that it is valid until the
  1043. * completion handler is called.
  1044. *
  1045. * @param token The @ref completion_token that will be used to produce a
  1046. * completion handler, which will be called when the receive completes.
  1047. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  1048. * @ref yield_context, or a function object with the correct completion
  1049. * signature. The function signature of the completion handler must be:
  1050. * @code void handler(
  1051. * const asio::error_code& error, // Result of operation.
  1052. * std::size_t bytes_transferred // Number of bytes received.
  1053. * ); @endcode
  1054. * Regardless of whether the asynchronous operation completes immediately or
  1055. * not, the completion handler will not be invoked from within this function.
  1056. * On immediate completion, invocation of the handler will be performed in a
  1057. * manner equivalent to using asio::post().
  1058. *
  1059. * @par Completion Signature
  1060. * @code void(asio::error_code, std::size_t) @endcode
  1061. *
  1062. * @par Example
  1063. * To receive into a single data buffer use the @ref buffer function as
  1064. * follows:
  1065. * @code socket.async_receive_from(
  1066. * asio::buffer(data, size), 0, sender_endpoint, handler); @endcode
  1067. * See the @ref buffer documentation for information on receiving into
  1068. * multiple buffers in one go, and how to use it with arrays, boost::array or
  1069. * std::vector.
  1070. *
  1071. * @par Per-Operation Cancellation
  1072. * On POSIX or Windows operating systems, this asynchronous operation supports
  1073. * cancellation for the following asio::cancellation_type values:
  1074. *
  1075. * @li @c cancellation_type::terminal
  1076. *
  1077. * @li @c cancellation_type::partial
  1078. *
  1079. * @li @c cancellation_type::total
  1080. */
  1081. template <typename MutableBufferSequence,
  1082. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  1083. std::size_t)) ReadToken = default_completion_token_t<executor_type>>
  1084. auto async_receive_from(const MutableBufferSequence& buffers,
  1085. endpoint_type& sender_endpoint,
  1086. ReadToken&& token
  1087. = default_completion_token_t<executor_type>())
  1088. -> decltype(
  1089. async_initiate<ReadToken,
  1090. void (asio::error_code, std::size_t)>(
  1091. declval<initiate_async_receive_from>(), token, buffers,
  1092. &sender_endpoint, socket_base::message_flags(0)))
  1093. {
  1094. return async_initiate<ReadToken,
  1095. void (asio::error_code, std::size_t)>(
  1096. initiate_async_receive_from(this), token, buffers,
  1097. &sender_endpoint, socket_base::message_flags(0));
  1098. }
  1099. /// Start an asynchronous receive.
  1100. /**
  1101. * This function is used to asynchronously receive raw data. It is an
  1102. * initiating function for an @ref asynchronous_operation, and always returns
  1103. * immediately.
  1104. *
  1105. * @param buffers One or more buffers into which the data will be received.
  1106. * Although the buffers object may be copied as necessary, ownership of the
  1107. * underlying memory blocks is retained by the caller, which must guarantee
  1108. * that they remain valid until the completion handler is called.
  1109. *
  1110. * @param sender_endpoint An endpoint object that receives the endpoint of
  1111. * the remote sender of the data. Ownership of the sender_endpoint object
  1112. * is retained by the caller, which must guarantee that it is valid until the
  1113. * completion handler is called.
  1114. *
  1115. * @param flags Flags specifying how the receive call is to be made.
  1116. *
  1117. * @param token The @ref completion_token that will be used to produce a
  1118. * completion handler, which will be called when the receive completes.
  1119. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  1120. * @ref yield_context, or a function object with the correct completion
  1121. * signature. The function signature of the completion handler must be:
  1122. * @code void handler(
  1123. * const asio::error_code& error, // Result of operation.
  1124. * std::size_t bytes_transferred // Number of bytes received.
  1125. * ); @endcode
  1126. * Regardless of whether the asynchronous operation completes immediately or
  1127. * not, the completion handler will not be invoked from within this function.
  1128. * On immediate completion, invocation of the handler will be performed in a
  1129. * manner equivalent to using asio::post().
  1130. *
  1131. * @par Completion Signature
  1132. * @code void(asio::error_code, std::size_t) @endcode
  1133. *
  1134. * @par Per-Operation Cancellation
  1135. * On POSIX or Windows operating systems, this asynchronous operation supports
  1136. * cancellation for the following asio::cancellation_type values:
  1137. *
  1138. * @li @c cancellation_type::terminal
  1139. *
  1140. * @li @c cancellation_type::partial
  1141. *
  1142. * @li @c cancellation_type::total
  1143. */
  1144. template <typename MutableBufferSequence,
  1145. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  1146. std::size_t)) ReadToken
  1147. = default_completion_token_t<executor_type>>
  1148. auto async_receive_from(const MutableBufferSequence& buffers,
  1149. endpoint_type& sender_endpoint, socket_base::message_flags flags,
  1150. ReadToken&& token = default_completion_token_t<executor_type>())
  1151. -> decltype(
  1152. async_initiate<ReadToken,
  1153. void (asio::error_code, std::size_t)>(
  1154. declval<initiate_async_receive_from>(), token,
  1155. buffers, &sender_endpoint, flags))
  1156. {
  1157. return async_initiate<ReadToken,
  1158. void (asio::error_code, std::size_t)>(
  1159. initiate_async_receive_from(this), token,
  1160. buffers, &sender_endpoint, flags);
  1161. }
  1162. private:
  1163. // Disallow copying and assignment.
  1164. basic_raw_socket(const basic_raw_socket&) = delete;
  1165. basic_raw_socket& operator=(const basic_raw_socket&) = delete;
  1166. class initiate_async_send
  1167. {
  1168. public:
  1169. typedef Executor executor_type;
  1170. explicit initiate_async_send(basic_raw_socket* self)
  1171. : self_(self)
  1172. {
  1173. }
  1174. const executor_type& get_executor() const noexcept
  1175. {
  1176. return self_->get_executor();
  1177. }
  1178. template <typename WriteHandler, typename ConstBufferSequence>
  1179. void operator()(WriteHandler&& handler,
  1180. const ConstBufferSequence& buffers,
  1181. socket_base::message_flags flags) const
  1182. {
  1183. // If you get an error on the following line it means that your handler
  1184. // does not meet the documented type requirements for a WriteHandler.
  1185. ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  1186. detail::non_const_lvalue<WriteHandler> handler2(handler);
  1187. self_->impl_.get_service().async_send(
  1188. self_->impl_.get_implementation(), buffers, flags,
  1189. handler2.value, self_->impl_.get_executor());
  1190. }
  1191. private:
  1192. basic_raw_socket* self_;
  1193. };
  1194. class initiate_async_send_to
  1195. {
  1196. public:
  1197. typedef Executor executor_type;
  1198. explicit initiate_async_send_to(basic_raw_socket* self)
  1199. : self_(self)
  1200. {
  1201. }
  1202. const executor_type& get_executor() const noexcept
  1203. {
  1204. return self_->get_executor();
  1205. }
  1206. template <typename WriteHandler, typename ConstBufferSequence>
  1207. void operator()(WriteHandler&& handler,
  1208. const ConstBufferSequence& buffers, const endpoint_type& destination,
  1209. socket_base::message_flags flags) const
  1210. {
  1211. // If you get an error on the following line it means that your handler
  1212. // does not meet the documented type requirements for a WriteHandler.
  1213. ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  1214. detail::non_const_lvalue<WriteHandler> handler2(handler);
  1215. self_->impl_.get_service().async_send_to(
  1216. self_->impl_.get_implementation(), buffers, destination,
  1217. flags, handler2.value, self_->impl_.get_executor());
  1218. }
  1219. private:
  1220. basic_raw_socket* self_;
  1221. };
  1222. class initiate_async_receive
  1223. {
  1224. public:
  1225. typedef Executor executor_type;
  1226. explicit initiate_async_receive(basic_raw_socket* self)
  1227. : self_(self)
  1228. {
  1229. }
  1230. const executor_type& get_executor() const noexcept
  1231. {
  1232. return self_->get_executor();
  1233. }
  1234. template <typename ReadHandler, typename MutableBufferSequence>
  1235. void operator()(ReadHandler&& handler,
  1236. const MutableBufferSequence& buffers,
  1237. socket_base::message_flags flags) const
  1238. {
  1239. // If you get an error on the following line it means that your handler
  1240. // does not meet the documented type requirements for a ReadHandler.
  1241. ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  1242. detail::non_const_lvalue<ReadHandler> handler2(handler);
  1243. self_->impl_.get_service().async_receive(
  1244. self_->impl_.get_implementation(), buffers, flags,
  1245. handler2.value, self_->impl_.get_executor());
  1246. }
  1247. private:
  1248. basic_raw_socket* self_;
  1249. };
  1250. class initiate_async_receive_from
  1251. {
  1252. public:
  1253. typedef Executor executor_type;
  1254. explicit initiate_async_receive_from(basic_raw_socket* self)
  1255. : self_(self)
  1256. {
  1257. }
  1258. const executor_type& get_executor() const noexcept
  1259. {
  1260. return self_->get_executor();
  1261. }
  1262. template <typename ReadHandler, typename MutableBufferSequence>
  1263. void operator()(ReadHandler&& handler,
  1264. const MutableBufferSequence& buffers, endpoint_type* sender_endpoint,
  1265. socket_base::message_flags flags) const
  1266. {
  1267. // If you get an error on the following line it means that your handler
  1268. // does not meet the documented type requirements for a ReadHandler.
  1269. ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  1270. detail::non_const_lvalue<ReadHandler> handler2(handler);
  1271. self_->impl_.get_service().async_receive_from(
  1272. self_->impl_.get_implementation(), buffers, *sender_endpoint,
  1273. flags, handler2.value, self_->impl_.get_executor());
  1274. }
  1275. private:
  1276. basic_raw_socket* self_;
  1277. };
  1278. };
  1279. } // namespace asio
  1280. #include "asio/detail/pop_options.hpp"
  1281. #endif // ASIO_BASIC_RAW_SOCKET_HPP