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.

basic_seq_packet_socket.hpp 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. //
  2. // basic_seq_packet_socket.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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_SEQ_PACKET_SOCKET_HPP
  11. #define ASIO_BASIC_SEQ_PACKET_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/throw_error.hpp"
  20. #include "asio/error.hpp"
  21. #include "asio/seq_packet_socket_service.hpp"
  22. #include "asio/detail/push_options.hpp"
  23. namespace asio {
  24. /// Provides sequenced packet socket functionality.
  25. /**
  26. * The basic_seq_packet_socket class template provides asynchronous and blocking
  27. * sequenced packet socket functionality.
  28. *
  29. * @par Thread Safety
  30. * @e Distinct @e objects: Safe.@n
  31. * @e Shared @e objects: Unsafe.
  32. */
  33. template <typename Protocol,
  34. typename SeqPacketSocketService = seq_packet_socket_service<Protocol> >
  35. class basic_seq_packet_socket
  36. : public basic_socket<Protocol, SeqPacketSocketService>
  37. {
  38. public:
  39. /// The native representation of a socket.
  40. typedef typename SeqPacketSocketService::native_handle_type
  41. native_handle_type;
  42. /// The protocol type.
  43. typedef Protocol protocol_type;
  44. /// The endpoint type.
  45. typedef typename Protocol::endpoint endpoint_type;
  46. /// Construct a basic_seq_packet_socket without opening it.
  47. /**
  48. * This constructor creates a sequenced packet socket without opening it. The
  49. * socket needs to be opened and then connected or accepted before data can
  50. * be sent or received on it.
  51. *
  52. * @param io_context The io_context object that the sequenced packet socket
  53. * will use to dispatch handlers for any asynchronous operations performed on
  54. * the socket.
  55. */
  56. explicit basic_seq_packet_socket(asio::io_context& io_context)
  57. : basic_socket<Protocol, SeqPacketSocketService>(io_context)
  58. {
  59. }
  60. /// Construct and open a basic_seq_packet_socket.
  61. /**
  62. * This constructor creates and opens a sequenced_packet socket. The socket
  63. * needs to be connected or accepted before data can be sent or received on
  64. * it.
  65. *
  66. * @param io_context The io_context object that the sequenced packet socket
  67. * will use to dispatch handlers for any asynchronous operations performed on
  68. * the socket.
  69. *
  70. * @param protocol An object specifying protocol parameters to be used.
  71. *
  72. * @throws asio::system_error Thrown on failure.
  73. */
  74. basic_seq_packet_socket(asio::io_context& io_context,
  75. const protocol_type& protocol)
  76. : basic_socket<Protocol, SeqPacketSocketService>(io_context, protocol)
  77. {
  78. }
  79. /// Construct a basic_seq_packet_socket, opening it and binding it to the
  80. /// given local endpoint.
  81. /**
  82. * This constructor creates a sequenced packet socket and automatically opens
  83. * it bound to the specified endpoint on the local machine. The protocol used
  84. * is the protocol associated with the given endpoint.
  85. *
  86. * @param io_context The io_context object that the sequenced packet socket
  87. * will use to dispatch handlers for any asynchronous operations performed on
  88. * the socket.
  89. *
  90. * @param endpoint An endpoint on the local machine to which the sequenced
  91. * packet socket will be bound.
  92. *
  93. * @throws asio::system_error Thrown on failure.
  94. */
  95. basic_seq_packet_socket(asio::io_context& io_context,
  96. const endpoint_type& endpoint)
  97. : basic_socket<Protocol, SeqPacketSocketService>(io_context, endpoint)
  98. {
  99. }
  100. /// Construct a basic_seq_packet_socket on an existing native socket.
  101. /**
  102. * This constructor creates a sequenced packet socket object to hold an
  103. * existing native socket.
  104. *
  105. * @param io_context The io_context object that the sequenced packet socket
  106. * will use to dispatch handlers for any asynchronous operations performed on
  107. * the socket.
  108. *
  109. * @param protocol An object specifying protocol parameters to be used.
  110. *
  111. * @param native_socket The new underlying socket implementation.
  112. *
  113. * @throws asio::system_error Thrown on failure.
  114. */
  115. basic_seq_packet_socket(asio::io_context& io_context,
  116. const protocol_type& protocol, const native_handle_type& native_socket)
  117. : basic_socket<Protocol, SeqPacketSocketService>(
  118. io_context, protocol, native_socket)
  119. {
  120. }
  121. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  122. /// Move-construct a basic_seq_packet_socket from another.
  123. /**
  124. * This constructor moves a sequenced packet socket from one object to
  125. * another.
  126. *
  127. * @param other The other basic_seq_packet_socket object from which the move
  128. * will occur.
  129. *
  130. * @note Following the move, the moved-from object is in the same state as if
  131. * constructed using the @c basic_seq_packet_socket(io_context&) constructor.
  132. */
  133. basic_seq_packet_socket(basic_seq_packet_socket&& other)
  134. : basic_socket<Protocol, SeqPacketSocketService>(
  135. ASIO_MOVE_CAST(basic_seq_packet_socket)(other))
  136. {
  137. }
  138. /// Move-assign a basic_seq_packet_socket from another.
  139. /**
  140. * This assignment operator moves a sequenced packet socket from one object to
  141. * another.
  142. *
  143. * @param other The other basic_seq_packet_socket object from which the move
  144. * will occur.
  145. *
  146. * @note Following the move, the moved-from object is in the same state as if
  147. * constructed using the @c basic_seq_packet_socket(io_context&) constructor.
  148. */
  149. basic_seq_packet_socket& operator=(basic_seq_packet_socket&& other)
  150. {
  151. basic_socket<Protocol, SeqPacketSocketService>::operator=(
  152. ASIO_MOVE_CAST(basic_seq_packet_socket)(other));
  153. return *this;
  154. }
  155. /// Move-construct a basic_seq_packet_socket from a socket of another protocol
  156. /// type.
  157. /**
  158. * This constructor moves a sequenced packet socket from one object to
  159. * another.
  160. *
  161. * @param other The other basic_seq_packet_socket object from which the move
  162. * will occur.
  163. *
  164. * @note Following the move, the moved-from object is in the same state as if
  165. * constructed using the @c basic_seq_packet_socket(io_context&) constructor.
  166. */
  167. template <typename Protocol1, typename SeqPacketSocketService1>
  168. basic_seq_packet_socket(
  169. basic_seq_packet_socket<Protocol1, SeqPacketSocketService1>&& other,
  170. typename enable_if<is_convertible<Protocol1, Protocol>::value>::type* = 0)
  171. : basic_socket<Protocol, SeqPacketSocketService>(
  172. ASIO_MOVE_CAST2(basic_seq_packet_socket<
  173. Protocol1, SeqPacketSocketService1>)(other))
  174. {
  175. }
  176. /// Move-assign a basic_seq_packet_socket from a socket of another protocol
  177. /// type.
  178. /**
  179. * This assignment operator moves a sequenced packet socket from one object to
  180. * another.
  181. *
  182. * @param other The other basic_seq_packet_socket object from which the move
  183. * will occur.
  184. *
  185. * @note Following the move, the moved-from object is in the same state as if
  186. * constructed using the @c basic_seq_packet_socket(io_context&) constructor.
  187. */
  188. template <typename Protocol1, typename SeqPacketSocketService1>
  189. typename enable_if<is_convertible<Protocol1, Protocol>::value,
  190. basic_seq_packet_socket>::type& operator=(
  191. basic_seq_packet_socket<Protocol1, SeqPacketSocketService1>&& other)
  192. {
  193. basic_socket<Protocol, SeqPacketSocketService>::operator=(
  194. ASIO_MOVE_CAST2(basic_seq_packet_socket<
  195. Protocol1, SeqPacketSocketService1>)(other));
  196. return *this;
  197. }
  198. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  199. /// Send some data on the socket.
  200. /**
  201. * This function is used to send data on the sequenced packet socket. The
  202. * function call will block until the data has been sent successfully, or an
  203. * until error occurs.
  204. *
  205. * @param buffers One or more data buffers to be sent on the socket.
  206. *
  207. * @param flags Flags specifying how the send call is to be made.
  208. *
  209. * @returns The number of bytes sent.
  210. *
  211. * @throws asio::system_error Thrown on failure.
  212. *
  213. * @par Example
  214. * To send a single data buffer use the @ref buffer function as follows:
  215. * @code
  216. * socket.send(asio::buffer(data, size), 0);
  217. * @endcode
  218. * See the @ref buffer documentation for information on sending multiple
  219. * buffers in one go, and how to use it with arrays, boost::array or
  220. * std::vector.
  221. */
  222. template <typename ConstBufferSequence>
  223. std::size_t send(const ConstBufferSequence& buffers,
  224. socket_base::message_flags flags)
  225. {
  226. asio::error_code ec;
  227. std::size_t s = this->get_service().send(
  228. this->get_implementation(), buffers, flags, ec);
  229. asio::detail::throw_error(ec, "send");
  230. return s;
  231. }
  232. /// Send some data on the socket.
  233. /**
  234. * This function is used to send data on the sequenced packet socket. The
  235. * function call will block the data has been sent successfully, or an until
  236. * error occurs.
  237. *
  238. * @param buffers One or more data buffers to be sent on the socket.
  239. *
  240. * @param flags Flags specifying how the send call is to be made.
  241. *
  242. * @param ec Set to indicate what error occurred, if any.
  243. *
  244. * @returns The number of bytes sent. Returns 0 if an error occurred.
  245. *
  246. * @note The send operation may not transmit all of the data to the peer.
  247. * Consider using the @ref write function if you need to ensure that all data
  248. * is written before the blocking operation completes.
  249. */
  250. template <typename ConstBufferSequence>
  251. std::size_t send(const ConstBufferSequence& buffers,
  252. socket_base::message_flags flags, asio::error_code& ec)
  253. {
  254. return this->get_service().send(
  255. this->get_implementation(), buffers, flags, ec);
  256. }
  257. /// Start an asynchronous send.
  258. /**
  259. * This function is used to asynchronously send data on the sequenced packet
  260. * socket. The function call always returns immediately.
  261. *
  262. * @param buffers One or more data buffers to be sent on the socket. Although
  263. * the buffers object may be copied as necessary, ownership of the underlying
  264. * memory blocks is retained by the caller, which must guarantee that they
  265. * remain valid until the handler is called.
  266. *
  267. * @param flags Flags specifying how the send call is to be made.
  268. *
  269. * @param handler The handler to be called when the send operation completes.
  270. * Copies will be made of the handler as required. The function signature of
  271. * the handler must be:
  272. * @code void handler(
  273. * const asio::error_code& error, // Result of operation.
  274. * std::size_t bytes_transferred // Number of bytes sent.
  275. * ); @endcode
  276. * Regardless of whether the asynchronous operation completes immediately or
  277. * not, the handler will not be invoked from within this function. Invocation
  278. * of the handler will be performed in a manner equivalent to using
  279. * asio::io_context::post().
  280. *
  281. * @par Example
  282. * To send a single data buffer use the @ref buffer function as follows:
  283. * @code
  284. * socket.async_send(asio::buffer(data, size), 0, handler);
  285. * @endcode
  286. * See the @ref buffer documentation for information on sending multiple
  287. * buffers in one go, and how to use it with arrays, boost::array or
  288. * std::vector.
  289. */
  290. template <typename ConstBufferSequence, typename WriteHandler>
  291. ASIO_INITFN_RESULT_TYPE(WriteHandler,
  292. void (asio::error_code, std::size_t))
  293. async_send(const ConstBufferSequence& buffers,
  294. socket_base::message_flags flags,
  295. ASIO_MOVE_ARG(WriteHandler) handler)
  296. {
  297. // If you get an error on the following line it means that your handler does
  298. // not meet the documented type requirements for a WriteHandler.
  299. ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  300. return this->get_service().async_send(this->get_implementation(),
  301. buffers, flags, ASIO_MOVE_CAST(WriteHandler)(handler));
  302. }
  303. /// Receive some data on the socket.
  304. /**
  305. * This function is used to receive data on the sequenced packet socket. The
  306. * function call will block until data has been received successfully, or
  307. * until an error occurs.
  308. *
  309. * @param buffers One or more buffers into which the data will be received.
  310. *
  311. * @param out_flags After the receive call completes, contains flags
  312. * associated with the received data. For example, if the
  313. * socket_base::message_end_of_record bit is set then the received data marks
  314. * the end of a record.
  315. *
  316. * @returns The number of bytes received.
  317. *
  318. * @throws asio::system_error Thrown on failure. An error code of
  319. * asio::error::eof indicates that the connection was closed by the
  320. * peer.
  321. *
  322. * @par Example
  323. * To receive into a single data buffer use the @ref buffer function as
  324. * follows:
  325. * @code
  326. * socket.receive(asio::buffer(data, size), out_flags);
  327. * @endcode
  328. * See the @ref buffer documentation for information on receiving into
  329. * multiple buffers in one go, and how to use it with arrays, boost::array or
  330. * std::vector.
  331. */
  332. template <typename MutableBufferSequence>
  333. std::size_t receive(const MutableBufferSequence& buffers,
  334. socket_base::message_flags& out_flags)
  335. {
  336. asio::error_code ec;
  337. std::size_t s = this->get_service().receive(
  338. this->get_implementation(), buffers, 0, out_flags, ec);
  339. asio::detail::throw_error(ec, "receive");
  340. return s;
  341. }
  342. /// Receive some data on the socket.
  343. /**
  344. * This function is used to receive data on the sequenced packet socket. The
  345. * function call will block until data has been received successfully, or
  346. * until an error occurs.
  347. *
  348. * @param buffers One or more buffers into which the data will be received.
  349. *
  350. * @param in_flags Flags specifying how the receive call is to be made.
  351. *
  352. * @param out_flags After the receive call completes, contains flags
  353. * associated with the received data. For example, if the
  354. * socket_base::message_end_of_record bit is set then the received data marks
  355. * the end of a record.
  356. *
  357. * @returns The number of bytes received.
  358. *
  359. * @throws asio::system_error Thrown on failure. An error code of
  360. * asio::error::eof indicates that the connection was closed by the
  361. * peer.
  362. *
  363. * @note The receive operation may not receive all of the requested number of
  364. * bytes. Consider using the @ref read function if you need to ensure that the
  365. * requested amount of data is read before the blocking operation completes.
  366. *
  367. * @par Example
  368. * To receive into a single data buffer use the @ref buffer function as
  369. * follows:
  370. * @code
  371. * socket.receive(asio::buffer(data, size), 0, out_flags);
  372. * @endcode
  373. * See the @ref buffer documentation for information on receiving into
  374. * multiple buffers in one go, and how to use it with arrays, boost::array or
  375. * std::vector.
  376. */
  377. template <typename MutableBufferSequence>
  378. std::size_t receive(const MutableBufferSequence& buffers,
  379. socket_base::message_flags in_flags,
  380. socket_base::message_flags& out_flags)
  381. {
  382. asio::error_code ec;
  383. std::size_t s = this->get_service().receive(
  384. this->get_implementation(), buffers, in_flags, out_flags, ec);
  385. asio::detail::throw_error(ec, "receive");
  386. return s;
  387. }
  388. /// Receive some data on a connected socket.
  389. /**
  390. * This function is used to receive data on the sequenced packet socket. The
  391. * function call will block until data has been received successfully, or
  392. * until an error occurs.
  393. *
  394. * @param buffers One or more buffers into which the data will be received.
  395. *
  396. * @param in_flags Flags specifying how the receive call is to be made.
  397. *
  398. * @param out_flags After the receive call completes, contains flags
  399. * associated with the received data. For example, if the
  400. * socket_base::message_end_of_record bit is set then the received data marks
  401. * the end of a record.
  402. *
  403. * @param ec Set to indicate what error occurred, if any.
  404. *
  405. * @returns The number of bytes received. Returns 0 if an error occurred.
  406. *
  407. * @note The receive operation may not receive all of the requested number of
  408. * bytes. Consider using the @ref read function if you need to ensure that the
  409. * requested amount of data is read before the blocking operation completes.
  410. */
  411. template <typename MutableBufferSequence>
  412. std::size_t receive(const MutableBufferSequence& buffers,
  413. socket_base::message_flags in_flags,
  414. socket_base::message_flags& out_flags, asio::error_code& ec)
  415. {
  416. return this->get_service().receive(this->get_implementation(),
  417. buffers, in_flags, out_flags, ec);
  418. }
  419. /// Start an asynchronous receive.
  420. /**
  421. * This function is used to asynchronously receive data from the sequenced
  422. * packet socket. The function call always returns immediately.
  423. *
  424. * @param buffers One or more buffers into which the data will be received.
  425. * Although the buffers object may be copied as necessary, ownership of the
  426. * underlying memory blocks is retained by the caller, which must guarantee
  427. * that they remain valid until the handler is called.
  428. *
  429. * @param out_flags Once the asynchronous operation completes, contains flags
  430. * associated with the received data. For example, if the
  431. * socket_base::message_end_of_record bit is set then the received data marks
  432. * the end of a record. The caller must guarantee that the referenced
  433. * variable remains valid until the handler is called.
  434. *
  435. * @param handler The handler to be called when the receive operation
  436. * completes. Copies will be made of the handler as required. The function
  437. * signature of 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 received.
  441. * ); @endcode
  442. * Regardless of whether the asynchronous operation completes immediately or
  443. * not, the handler will not be invoked from within this function. Invocation
  444. * of the handler will be performed in a manner equivalent to using
  445. * asio::io_context::post().
  446. *
  447. * @par Example
  448. * To receive into a single data buffer use the @ref buffer function as
  449. * follows:
  450. * @code
  451. * socket.async_receive(asio::buffer(data, size), out_flags, handler);
  452. * @endcode
  453. * See the @ref buffer documentation for information on receiving into
  454. * multiple buffers in one go, and how to use it with arrays, boost::array or
  455. * std::vector.
  456. */
  457. template <typename MutableBufferSequence, typename ReadHandler>
  458. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  459. void (asio::error_code, std::size_t))
  460. async_receive(const MutableBufferSequence& buffers,
  461. socket_base::message_flags& out_flags,
  462. ASIO_MOVE_ARG(ReadHandler) handler)
  463. {
  464. // If you get an error on the following line it means that your handler does
  465. // not meet the documented type requirements for a ReadHandler.
  466. ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  467. return this->get_service().async_receive(
  468. this->get_implementation(), buffers, 0, out_flags,
  469. ASIO_MOVE_CAST(ReadHandler)(handler));
  470. }
  471. /// Start an asynchronous receive.
  472. /**
  473. * This function is used to asynchronously receive data from the sequenced
  474. * data socket. The function call always returns immediately.
  475. *
  476. * @param buffers One or more buffers into which the data will be received.
  477. * Although the buffers object may be copied as necessary, ownership of the
  478. * underlying memory blocks is retained by the caller, which must guarantee
  479. * that they remain valid until the handler is called.
  480. *
  481. * @param in_flags Flags specifying how the receive call is to be made.
  482. *
  483. * @param out_flags Once the asynchronous operation completes, contains flags
  484. * associated with the received data. For example, if the
  485. * socket_base::message_end_of_record bit is set then the received data marks
  486. * the end of a record. The caller must guarantee that the referenced
  487. * variable remains valid until the handler is called.
  488. *
  489. * @param handler The handler to be called when the receive operation
  490. * completes. Copies will be made of the handler as required. The function
  491. * signature of the handler must be:
  492. * @code void handler(
  493. * const asio::error_code& error, // Result of operation.
  494. * std::size_t bytes_transferred // Number of bytes received.
  495. * ); @endcode
  496. * Regardless of whether the asynchronous operation completes immediately or
  497. * not, the handler will not be invoked from within this function. Invocation
  498. * of the handler will be performed in a manner equivalent to using
  499. * asio::io_context::post().
  500. *
  501. * @par Example
  502. * To receive into a single data buffer use the @ref buffer function as
  503. * follows:
  504. * @code
  505. * socket.async_receive(
  506. * asio::buffer(data, size),
  507. * 0, out_flags, handler);
  508. * @endcode
  509. * See the @ref buffer documentation for information on receiving into
  510. * multiple buffers in one go, and how to use it with arrays, boost::array or
  511. * std::vector.
  512. */
  513. template <typename MutableBufferSequence, typename ReadHandler>
  514. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  515. void (asio::error_code, std::size_t))
  516. async_receive(const MutableBufferSequence& buffers,
  517. socket_base::message_flags in_flags,
  518. socket_base::message_flags& out_flags,
  519. ASIO_MOVE_ARG(ReadHandler) handler)
  520. {
  521. // If you get an error on the following line it means that your handler does
  522. // not meet the documented type requirements for a ReadHandler.
  523. ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  524. return this->get_service().async_receive(
  525. this->get_implementation(), buffers, in_flags, out_flags,
  526. ASIO_MOVE_CAST(ReadHandler)(handler));
  527. }
  528. };
  529. } // namespace asio
  530. #include "asio/detail/pop_options.hpp"
  531. #endif // ASIO_BASIC_SEQ_PACKET_SOCKET_HPP