Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1108 lines
42KB

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