basic_raw_socket.hpp 52 KB

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