basic_raw_socket.hpp 50 KB

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