basic_raw_socket.hpp 41 KB

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