basic_raw_socket.hpp 54 KB

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