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.

1600 lines
52KB

  1. //
  2. // basic_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_SOCKET_HPP
  11. #define ASIO_BASIC_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 "asio/async_result.hpp"
  17. #include "asio/basic_io_object.hpp"
  18. #include "asio/detail/handler_type_requirements.hpp"
  19. #include "asio/detail/throw_error.hpp"
  20. #include "asio/detail/type_traits.hpp"
  21. #include "asio/error.hpp"
  22. #include "asio/post.hpp"
  23. #include "asio/socket_base.hpp"
  24. #include "asio/detail/push_options.hpp"
  25. namespace asio {
  26. /// Provides socket functionality.
  27. /**
  28. * The basic_socket class template provides functionality that is common to both
  29. * stream-oriented and datagram-oriented sockets.
  30. *
  31. * @par Thread Safety
  32. * @e Distinct @e objects: Safe.@n
  33. * @e Shared @e objects: Unsafe.
  34. */
  35. template <typename Protocol, typename SocketService>
  36. class basic_socket
  37. : public basic_io_object<SocketService>,
  38. public socket_base
  39. {
  40. public:
  41. /// The native representation of a socket.
  42. typedef typename SocketService::native_handle_type native_handle_type;
  43. /// The protocol type.
  44. typedef Protocol protocol_type;
  45. /// The endpoint type.
  46. typedef typename Protocol::endpoint endpoint_type;
  47. /// A basic_socket is always the lowest layer.
  48. typedef basic_socket<Protocol, SocketService> lowest_layer_type;
  49. /// Construct a basic_socket without opening it.
  50. /**
  51. * This constructor creates a socket without opening it.
  52. *
  53. * @param io_context The io_context object that the socket will use to
  54. * dispatch handlers for any asynchronous operations performed on the socket.
  55. */
  56. explicit basic_socket(asio::io_context& io_context)
  57. : basic_io_object<SocketService>(io_context)
  58. {
  59. }
  60. /// Construct and open a basic_socket.
  61. /**
  62. * This constructor creates and opens a socket.
  63. *
  64. * @param io_context The io_context object that the socket will use to
  65. * dispatch handlers for any asynchronous operations performed on the socket.
  66. *
  67. * @param protocol An object specifying protocol parameters to be used.
  68. *
  69. * @throws asio::system_error Thrown on failure.
  70. */
  71. basic_socket(asio::io_context& io_context,
  72. const protocol_type& protocol)
  73. : basic_io_object<SocketService>(io_context)
  74. {
  75. asio::error_code ec;
  76. this->get_service().open(this->get_implementation(), protocol, ec);
  77. asio::detail::throw_error(ec, "open");
  78. }
  79. /// Construct a basic_socket, opening it and binding it to the given local
  80. /// endpoint.
  81. /**
  82. * This constructor creates a socket and automatically opens it bound to the
  83. * specified endpoint on the local machine. The protocol used is the protocol
  84. * associated with the given endpoint.
  85. *
  86. * @param io_context The io_context object that the socket will use to
  87. * dispatch handlers for any asynchronous operations performed on the socket.
  88. *
  89. * @param endpoint An endpoint on the local machine to which the socket will
  90. * be bound.
  91. *
  92. * @throws asio::system_error Thrown on failure.
  93. */
  94. basic_socket(asio::io_context& io_context,
  95. const endpoint_type& endpoint)
  96. : basic_io_object<SocketService>(io_context)
  97. {
  98. asio::error_code ec;
  99. const protocol_type protocol = endpoint.protocol();
  100. this->get_service().open(this->get_implementation(), protocol, ec);
  101. asio::detail::throw_error(ec, "open");
  102. this->get_service().bind(this->get_implementation(), endpoint, ec);
  103. asio::detail::throw_error(ec, "bind");
  104. }
  105. /// Construct a basic_socket on an existing native socket.
  106. /**
  107. * This constructor creates a socket object to hold an existing native socket.
  108. *
  109. * @param io_context The io_context object that the socket will use to
  110. * dispatch handlers for any asynchronous operations performed on the socket.
  111. *
  112. * @param protocol An object specifying protocol parameters to be used.
  113. *
  114. * @param native_socket A native socket.
  115. *
  116. * @throws asio::system_error Thrown on failure.
  117. */
  118. basic_socket(asio::io_context& io_context,
  119. const protocol_type& protocol, const native_handle_type& native_socket)
  120. : basic_io_object<SocketService>(io_context)
  121. {
  122. asio::error_code ec;
  123. this->get_service().assign(this->get_implementation(),
  124. protocol, native_socket, ec);
  125. asio::detail::throw_error(ec, "assign");
  126. }
  127. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  128. /// Move-construct a basic_socket from another.
  129. /**
  130. * This constructor moves a socket from one object to another.
  131. *
  132. * @param other The other basic_socket object from which the move will
  133. * occur.
  134. *
  135. * @note Following the move, the moved-from object is in the same state as if
  136. * constructed using the @c basic_socket(io_context&) constructor.
  137. */
  138. basic_socket(basic_socket&& other)
  139. : basic_io_object<SocketService>(
  140. ASIO_MOVE_CAST(basic_socket)(other))
  141. {
  142. }
  143. /// Move-assign a basic_socket from another.
  144. /**
  145. * This assignment operator moves a socket from one object to another.
  146. *
  147. * @param other The other basic_socket object from which the move will
  148. * occur.
  149. *
  150. * @note Following the move, the moved-from object is in the same state as if
  151. * constructed using the @c basic_socket(io_context&) constructor.
  152. */
  153. basic_socket& operator=(basic_socket&& other)
  154. {
  155. basic_io_object<SocketService>::operator=(
  156. ASIO_MOVE_CAST(basic_socket)(other));
  157. return *this;
  158. }
  159. // All sockets have access to each other's implementations.
  160. template <typename Protocol1, typename SocketService1>
  161. friend class basic_socket;
  162. /// Move-construct a basic_socket from a socket of another protocol type.
  163. /**
  164. * This constructor moves a socket from one object to another.
  165. *
  166. * @param other The other basic_socket object from which the move will
  167. * occur.
  168. *
  169. * @note Following the move, the moved-from object is in the same state as if
  170. * constructed using the @c basic_socket(io_context&) constructor.
  171. */
  172. template <typename Protocol1, typename SocketService1>
  173. basic_socket(basic_socket<Protocol1, SocketService1>&& other,
  174. typename enable_if<is_convertible<Protocol1, Protocol>::value>::type* = 0)
  175. : basic_io_object<SocketService>(other.get_service().get_io_context())
  176. {
  177. this->get_service().template converting_move_construct<Protocol1>(
  178. this->get_implementation(), other.get_implementation());
  179. }
  180. /// Move-assign a basic_socket from a socket of another protocol type.
  181. /**
  182. * This assignment operator moves a socket from one object to another.
  183. *
  184. * @param other The other basic_socket object from which the move will
  185. * occur.
  186. *
  187. * @note Following the move, the moved-from object is in the same state as if
  188. * constructed using the @c basic_socket(io_context&) constructor.
  189. */
  190. template <typename Protocol1, typename SocketService1>
  191. typename enable_if<is_convertible<Protocol1, Protocol>::value,
  192. basic_socket>::type& operator=(
  193. basic_socket<Protocol1, SocketService1>&& other)
  194. {
  195. basic_socket tmp(ASIO_MOVE_CAST2(basic_socket<
  196. Protocol1, SocketService1>)(other));
  197. basic_io_object<SocketService>::operator=(
  198. ASIO_MOVE_CAST(basic_socket)(tmp));
  199. return *this;
  200. }
  201. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  202. /// Get a reference to the lowest layer.
  203. /**
  204. * This function returns a reference to the lowest layer in a stack of
  205. * layers. Since a basic_socket cannot contain any further layers, it simply
  206. * returns a reference to itself.
  207. *
  208. * @return A reference to the lowest layer in the stack of layers. Ownership
  209. * is not transferred to the caller.
  210. */
  211. lowest_layer_type& lowest_layer()
  212. {
  213. return *this;
  214. }
  215. /// Get a const reference to the lowest layer.
  216. /**
  217. * This function returns a const reference to the lowest layer in a stack of
  218. * layers. Since a basic_socket cannot contain any further layers, it simply
  219. * returns a reference to itself.
  220. *
  221. * @return A const reference to the lowest layer in the stack of layers.
  222. * Ownership is not transferred to the caller.
  223. */
  224. const lowest_layer_type& lowest_layer() const
  225. {
  226. return *this;
  227. }
  228. /// Open the socket using the specified protocol.
  229. /**
  230. * This function opens the socket so that it will use the specified protocol.
  231. *
  232. * @param protocol An object specifying protocol parameters to be used.
  233. *
  234. * @throws asio::system_error Thrown on failure.
  235. *
  236. * @par Example
  237. * @code
  238. * asio::ip::tcp::socket socket(io_context);
  239. * socket.open(asio::ip::tcp::v4());
  240. * @endcode
  241. */
  242. void open(const protocol_type& protocol = protocol_type())
  243. {
  244. asio::error_code ec;
  245. this->get_service().open(this->get_implementation(), protocol, ec);
  246. asio::detail::throw_error(ec, "open");
  247. }
  248. /// Open the socket using the specified protocol.
  249. /**
  250. * This function opens the socket so that it will use the specified protocol.
  251. *
  252. * @param protocol An object specifying which protocol is to be used.
  253. *
  254. * @param ec Set to indicate what error occurred, if any.
  255. *
  256. * @par Example
  257. * @code
  258. * asio::ip::tcp::socket socket(io_context);
  259. * asio::error_code ec;
  260. * socket.open(asio::ip::tcp::v4(), ec);
  261. * if (ec)
  262. * {
  263. * // An error occurred.
  264. * }
  265. * @endcode
  266. */
  267. asio::error_code open(const protocol_type& protocol,
  268. asio::error_code& ec)
  269. {
  270. return this->get_service().open(this->get_implementation(), protocol, ec);
  271. }
  272. /// Assign an existing native socket to the socket.
  273. /*
  274. * This function opens the socket to hold an existing native socket.
  275. *
  276. * @param protocol An object specifying which protocol is to be used.
  277. *
  278. * @param native_socket A native socket.
  279. *
  280. * @throws asio::system_error Thrown on failure.
  281. */
  282. void assign(const protocol_type& protocol,
  283. const native_handle_type& native_socket)
  284. {
  285. asio::error_code ec;
  286. this->get_service().assign(this->get_implementation(),
  287. protocol, native_socket, ec);
  288. asio::detail::throw_error(ec, "assign");
  289. }
  290. /// Assign an existing native socket to the socket.
  291. /*
  292. * This function opens the socket to hold an existing native socket.
  293. *
  294. * @param protocol An object specifying which protocol is to be used.
  295. *
  296. * @param native_socket A native socket.
  297. *
  298. * @param ec Set to indicate what error occurred, if any.
  299. */
  300. asio::error_code assign(const protocol_type& protocol,
  301. const native_handle_type& native_socket, asio::error_code& ec)
  302. {
  303. return this->get_service().assign(this->get_implementation(),
  304. protocol, native_socket, ec);
  305. }
  306. /// Determine whether the socket is open.
  307. bool is_open() const
  308. {
  309. return this->get_service().is_open(this->get_implementation());
  310. }
  311. /// Close the socket.
  312. /**
  313. * This function is used to close the socket. Any asynchronous send, receive
  314. * or connect operations will be cancelled immediately, and will complete
  315. * with the asio::error::operation_aborted error.
  316. *
  317. * @throws asio::system_error Thrown on failure. Note that, even if
  318. * the function indicates an error, the underlying descriptor is closed.
  319. *
  320. * @note For portable behaviour with respect to graceful closure of a
  321. * connected socket, call shutdown() before closing the socket.
  322. */
  323. void close()
  324. {
  325. asio::error_code ec;
  326. this->get_service().close(this->get_implementation(), ec);
  327. asio::detail::throw_error(ec, "close");
  328. }
  329. /// Close the socket.
  330. /**
  331. * This function is used to close the socket. Any asynchronous send, receive
  332. * or connect operations will be cancelled immediately, and will complete
  333. * with the asio::error::operation_aborted error.
  334. *
  335. * @param ec Set to indicate what error occurred, if any. Note that, even if
  336. * the function indicates an error, the underlying descriptor is closed.
  337. *
  338. * @par Example
  339. * @code
  340. * asio::ip::tcp::socket socket(io_context);
  341. * ...
  342. * asio::error_code ec;
  343. * socket.close(ec);
  344. * if (ec)
  345. * {
  346. * // An error occurred.
  347. * }
  348. * @endcode
  349. *
  350. * @note For portable behaviour with respect to graceful closure of a
  351. * connected socket, call shutdown() before closing the socket.
  352. */
  353. asio::error_code close(asio::error_code& ec)
  354. {
  355. return this->get_service().close(this->get_implementation(), ec);
  356. }
  357. /// Get the native socket representation.
  358. /**
  359. * This function may be used to obtain the underlying representation of the
  360. * socket. This is intended to allow access to native socket functionality
  361. * that is not otherwise provided.
  362. */
  363. native_handle_type native_handle()
  364. {
  365. return this->get_service().native_handle(this->get_implementation());
  366. }
  367. /// Cancel all asynchronous operations associated with the socket.
  368. /**
  369. * This function causes all outstanding asynchronous connect, send and receive
  370. * operations to finish immediately, and the handlers for cancelled operations
  371. * will be passed the asio::error::operation_aborted error.
  372. *
  373. * @throws asio::system_error Thrown on failure.
  374. *
  375. * @note Calls to cancel() will always fail with
  376. * asio::error::operation_not_supported when run on Windows XP, Windows
  377. * Server 2003, and earlier versions of Windows, unless
  378. * ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
  379. * two issues that should be considered before enabling its use:
  380. *
  381. * @li It will only cancel asynchronous operations that were initiated in the
  382. * current thread.
  383. *
  384. * @li It can appear to complete without error, but the request to cancel the
  385. * unfinished operations may be silently ignored by the operating system.
  386. * Whether it works or not seems to depend on the drivers that are installed.
  387. *
  388. * For portable cancellation, consider using one of the following
  389. * alternatives:
  390. *
  391. * @li Disable asio's I/O completion port backend by defining
  392. * ASIO_DISABLE_IOCP.
  393. *
  394. * @li Use the close() function to simultaneously cancel the outstanding
  395. * operations and close the socket.
  396. *
  397. * When running on Windows Vista, Windows Server 2008, and later, the
  398. * CancelIoEx function is always used. This function does not have the
  399. * problems described above.
  400. */
  401. #if defined(ASIO_MSVC) && (ASIO_MSVC >= 1400) \
  402. && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
  403. && !defined(ASIO_ENABLE_CANCELIO)
  404. __declspec(deprecated("By default, this function always fails with "
  405. "operation_not_supported when used on Windows XP, Windows Server 2003, "
  406. "or earlier. Consult documentation for details."))
  407. #endif
  408. void cancel()
  409. {
  410. asio::error_code ec;
  411. this->get_service().cancel(this->get_implementation(), ec);
  412. asio::detail::throw_error(ec, "cancel");
  413. }
  414. /// Cancel all asynchronous operations associated with the socket.
  415. /**
  416. * This function causes all outstanding asynchronous connect, send and receive
  417. * operations to finish immediately, and the handlers for cancelled operations
  418. * will be passed the asio::error::operation_aborted error.
  419. *
  420. * @param ec Set to indicate what error occurred, if any.
  421. *
  422. * @note Calls to cancel() will always fail with
  423. * asio::error::operation_not_supported when run on Windows XP, Windows
  424. * Server 2003, and earlier versions of Windows, unless
  425. * ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
  426. * two issues that should be considered before enabling its use:
  427. *
  428. * @li It will only cancel asynchronous operations that were initiated in the
  429. * current thread.
  430. *
  431. * @li It can appear to complete without error, but the request to cancel the
  432. * unfinished operations may be silently ignored by the operating system.
  433. * Whether it works or not seems to depend on the drivers that are installed.
  434. *
  435. * For portable cancellation, consider using one of the following
  436. * alternatives:
  437. *
  438. * @li Disable asio's I/O completion port backend by defining
  439. * ASIO_DISABLE_IOCP.
  440. *
  441. * @li Use the close() function to simultaneously cancel the outstanding
  442. * operations and close the socket.
  443. *
  444. * When running on Windows Vista, Windows Server 2008, and later, the
  445. * CancelIoEx function is always used. This function does not have the
  446. * problems described above.
  447. */
  448. #if defined(ASIO_MSVC) && (ASIO_MSVC >= 1400) \
  449. && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
  450. && !defined(ASIO_ENABLE_CANCELIO)
  451. __declspec(deprecated("By default, this function always fails with "
  452. "operation_not_supported when used on Windows XP, Windows Server 2003, "
  453. "or earlier. Consult documentation for details."))
  454. #endif
  455. asio::error_code cancel(asio::error_code& ec)
  456. {
  457. return this->get_service().cancel(this->get_implementation(), ec);
  458. }
  459. /// Determine whether the socket is at the out-of-band data mark.
  460. /**
  461. * This function is used to check whether the socket input is currently
  462. * positioned at the out-of-band data mark.
  463. *
  464. * @return A bool indicating whether the socket is at the out-of-band data
  465. * mark.
  466. *
  467. * @throws asio::system_error Thrown on failure.
  468. */
  469. bool at_mark() const
  470. {
  471. asio::error_code ec;
  472. bool b = this->get_service().at_mark(this->get_implementation(), ec);
  473. asio::detail::throw_error(ec, "at_mark");
  474. return b;
  475. }
  476. /// Determine whether the socket is at the out-of-band data mark.
  477. /**
  478. * This function is used to check whether the socket input is currently
  479. * positioned at the out-of-band data mark.
  480. *
  481. * @param ec Set to indicate what error occurred, if any.
  482. *
  483. * @return A bool indicating whether the socket is at the out-of-band data
  484. * mark.
  485. */
  486. bool at_mark(asio::error_code& ec) const
  487. {
  488. return this->get_service().at_mark(this->get_implementation(), ec);
  489. }
  490. /// Determine the number of bytes available for reading.
  491. /**
  492. * This function is used to determine the number of bytes that may be read
  493. * without blocking.
  494. *
  495. * @return The number of bytes that may be read without blocking, or 0 if an
  496. * error occurs.
  497. *
  498. * @throws asio::system_error Thrown on failure.
  499. */
  500. std::size_t available() const
  501. {
  502. asio::error_code ec;
  503. std::size_t s = this->get_service().available(
  504. this->get_implementation(), ec);
  505. asio::detail::throw_error(ec, "available");
  506. return s;
  507. }
  508. /// Determine the number of bytes available for reading.
  509. /**
  510. * This function is used to determine the number of bytes that may be read
  511. * without blocking.
  512. *
  513. * @param ec Set to indicate what error occurred, if any.
  514. *
  515. * @return The number of bytes that may be read without blocking, or 0 if an
  516. * error occurs.
  517. */
  518. std::size_t available(asio::error_code& ec) const
  519. {
  520. return this->get_service().available(this->get_implementation(), ec);
  521. }
  522. /// Bind the socket to the given local endpoint.
  523. /**
  524. * This function binds the socket to the specified endpoint on the local
  525. * machine.
  526. *
  527. * @param endpoint An endpoint on the local machine to which the socket will
  528. * be bound.
  529. *
  530. * @throws asio::system_error Thrown on failure.
  531. *
  532. * @par Example
  533. * @code
  534. * asio::ip::tcp::socket socket(io_context);
  535. * socket.open(asio::ip::tcp::v4());
  536. * socket.bind(asio::ip::tcp::endpoint(
  537. * asio::ip::tcp::v4(), 12345));
  538. * @endcode
  539. */
  540. void bind(const endpoint_type& endpoint)
  541. {
  542. asio::error_code ec;
  543. this->get_service().bind(this->get_implementation(), endpoint, ec);
  544. asio::detail::throw_error(ec, "bind");
  545. }
  546. /// Bind the socket to the given local endpoint.
  547. /**
  548. * This function binds the socket to the specified endpoint on the local
  549. * machine.
  550. *
  551. * @param endpoint An endpoint on the local machine to which the socket will
  552. * be bound.
  553. *
  554. * @param ec Set to indicate what error occurred, if any.
  555. *
  556. * @par Example
  557. * @code
  558. * asio::ip::tcp::socket socket(io_context);
  559. * socket.open(asio::ip::tcp::v4());
  560. * asio::error_code ec;
  561. * socket.bind(asio::ip::tcp::endpoint(
  562. * asio::ip::tcp::v4(), 12345), ec);
  563. * if (ec)
  564. * {
  565. * // An error occurred.
  566. * }
  567. * @endcode
  568. */
  569. asio::error_code bind(const endpoint_type& endpoint,
  570. asio::error_code& ec)
  571. {
  572. return this->get_service().bind(this->get_implementation(), endpoint, ec);
  573. }
  574. /// Connect the socket to the specified endpoint.
  575. /**
  576. * This function is used to connect a socket to the specified remote endpoint.
  577. * The function call will block until the connection is successfully made or
  578. * an error occurs.
  579. *
  580. * The socket is automatically opened if it is not already open. If the
  581. * connect fails, and the socket was automatically opened, the socket is
  582. * not returned to the closed state.
  583. *
  584. * @param peer_endpoint The remote endpoint to which the socket will be
  585. * connected.
  586. *
  587. * @throws asio::system_error Thrown on failure.
  588. *
  589. * @par Example
  590. * @code
  591. * asio::ip::tcp::socket socket(io_context);
  592. * asio::ip::tcp::endpoint endpoint(
  593. * asio::ip::address::from_string("1.2.3.4"), 12345);
  594. * socket.connect(endpoint);
  595. * @endcode
  596. */
  597. void connect(const endpoint_type& peer_endpoint)
  598. {
  599. asio::error_code ec;
  600. if (!is_open())
  601. {
  602. this->get_service().open(this->get_implementation(),
  603. peer_endpoint.protocol(), ec);
  604. asio::detail::throw_error(ec, "connect");
  605. }
  606. this->get_service().connect(this->get_implementation(), peer_endpoint, ec);
  607. asio::detail::throw_error(ec, "connect");
  608. }
  609. /// Connect the socket to the specified endpoint.
  610. /**
  611. * This function is used to connect a socket to the specified remote endpoint.
  612. * The function call will block until the connection is successfully made or
  613. * an error occurs.
  614. *
  615. * The socket is automatically opened if it is not already open. If the
  616. * connect fails, and the socket was automatically opened, the socket is
  617. * not returned to the closed state.
  618. *
  619. * @param peer_endpoint The remote endpoint to which the socket will be
  620. * connected.
  621. *
  622. * @param ec Set to indicate what error occurred, if any.
  623. *
  624. * @par Example
  625. * @code
  626. * asio::ip::tcp::socket socket(io_context);
  627. * asio::ip::tcp::endpoint endpoint(
  628. * asio::ip::address::from_string("1.2.3.4"), 12345);
  629. * asio::error_code ec;
  630. * socket.connect(endpoint, ec);
  631. * if (ec)
  632. * {
  633. * // An error occurred.
  634. * }
  635. * @endcode
  636. */
  637. asio::error_code connect(const endpoint_type& peer_endpoint,
  638. asio::error_code& ec)
  639. {
  640. if (!is_open())
  641. {
  642. if (this->get_service().open(this->get_implementation(),
  643. peer_endpoint.protocol(), ec))
  644. {
  645. return ec;
  646. }
  647. }
  648. return this->get_service().connect(
  649. this->get_implementation(), peer_endpoint, ec);
  650. }
  651. /// Start an asynchronous connect.
  652. /**
  653. * This function is used to asynchronously connect a socket to the specified
  654. * remote endpoint. The function call always returns immediately.
  655. *
  656. * The socket is automatically opened if it is not already open. If the
  657. * connect fails, and the socket was automatically opened, the socket is
  658. * not returned to the closed state.
  659. *
  660. * @param peer_endpoint The remote endpoint to which the socket will be
  661. * connected. Copies will be made of the endpoint object as required.
  662. *
  663. * @param handler The handler to be called when the connection operation
  664. * completes. Copies will be made of the handler as required. The function
  665. * signature of the handler must be:
  666. * @code void handler(
  667. * const asio::error_code& error // Result of operation
  668. * ); @endcode
  669. * Regardless of whether the asynchronous operation completes immediately or
  670. * not, the handler will not be invoked from within this function. Invocation
  671. * of the handler will be performed in a manner equivalent to using
  672. * asio::io_context::post().
  673. *
  674. * @par Example
  675. * @code
  676. * void connect_handler(const asio::error_code& error)
  677. * {
  678. * if (!error)
  679. * {
  680. * // Connect succeeded.
  681. * }
  682. * }
  683. *
  684. * ...
  685. *
  686. * asio::ip::tcp::socket socket(io_context);
  687. * asio::ip::tcp::endpoint endpoint(
  688. * asio::ip::address::from_string("1.2.3.4"), 12345);
  689. * socket.async_connect(endpoint, connect_handler);
  690. * @endcode
  691. */
  692. template <typename ConnectHandler>
  693. ASIO_INITFN_RESULT_TYPE(ConnectHandler,
  694. void (asio::error_code))
  695. async_connect(const endpoint_type& peer_endpoint,
  696. ASIO_MOVE_ARG(ConnectHandler) handler)
  697. {
  698. // If you get an error on the following line it means that your handler does
  699. // not meet the documented type requirements for a ConnectHandler.
  700. ASIO_CONNECT_HANDLER_CHECK(ConnectHandler, handler) type_check;
  701. if (!is_open())
  702. {
  703. asio::error_code ec;
  704. const protocol_type protocol = peer_endpoint.protocol();
  705. if (this->get_service().open(this->get_implementation(), protocol, ec))
  706. {
  707. async_completion<ConnectHandler,
  708. void (asio::error_code)> init(handler);
  709. asio::post(this->get_executor(),
  710. asio::detail::bind_handler(
  711. ASIO_MOVE_CAST(ASIO_HANDLER_TYPE(
  712. ConnectHandler, void (asio::error_code)))(
  713. init.handler), ec));
  714. return init.result.get();
  715. }
  716. }
  717. return this->get_service().async_connect(this->get_implementation(),
  718. peer_endpoint, ASIO_MOVE_CAST(ConnectHandler)(handler));
  719. }
  720. /// Set an option on the socket.
  721. /**
  722. * This function is used to set an option on the socket.
  723. *
  724. * @param option The new option value to be set on the socket.
  725. *
  726. * @throws asio::system_error Thrown on failure.
  727. *
  728. * @sa SettableSocketOption @n
  729. * asio::socket_base::broadcast @n
  730. * asio::socket_base::do_not_route @n
  731. * asio::socket_base::keep_alive @n
  732. * asio::socket_base::linger @n
  733. * asio::socket_base::receive_buffer_size @n
  734. * asio::socket_base::receive_low_watermark @n
  735. * asio::socket_base::reuse_address @n
  736. * asio::socket_base::send_buffer_size @n
  737. * asio::socket_base::send_low_watermark @n
  738. * asio::ip::multicast::join_group @n
  739. * asio::ip::multicast::leave_group @n
  740. * asio::ip::multicast::enable_loopback @n
  741. * asio::ip::multicast::outbound_interface @n
  742. * asio::ip::multicast::hops @n
  743. * asio::ip::tcp::no_delay
  744. *
  745. * @par Example
  746. * Setting the IPPROTO_TCP/TCP_NODELAY option:
  747. * @code
  748. * asio::ip::tcp::socket socket(io_context);
  749. * ...
  750. * asio::ip::tcp::no_delay option(true);
  751. * socket.set_option(option);
  752. * @endcode
  753. */
  754. template <typename SettableSocketOption>
  755. void set_option(const SettableSocketOption& option)
  756. {
  757. asio::error_code ec;
  758. this->get_service().set_option(this->get_implementation(), option, ec);
  759. asio::detail::throw_error(ec, "set_option");
  760. }
  761. /// Set an option on the socket.
  762. /**
  763. * This function is used to set an option on the socket.
  764. *
  765. * @param option The new option value to be set on the socket.
  766. *
  767. * @param ec Set to indicate what error occurred, if any.
  768. *
  769. * @sa SettableSocketOption @n
  770. * asio::socket_base::broadcast @n
  771. * asio::socket_base::do_not_route @n
  772. * asio::socket_base::keep_alive @n
  773. * asio::socket_base::linger @n
  774. * asio::socket_base::receive_buffer_size @n
  775. * asio::socket_base::receive_low_watermark @n
  776. * asio::socket_base::reuse_address @n
  777. * asio::socket_base::send_buffer_size @n
  778. * asio::socket_base::send_low_watermark @n
  779. * asio::ip::multicast::join_group @n
  780. * asio::ip::multicast::leave_group @n
  781. * asio::ip::multicast::enable_loopback @n
  782. * asio::ip::multicast::outbound_interface @n
  783. * asio::ip::multicast::hops @n
  784. * asio::ip::tcp::no_delay
  785. *
  786. * @par Example
  787. * Setting the IPPROTO_TCP/TCP_NODELAY option:
  788. * @code
  789. * asio::ip::tcp::socket socket(io_context);
  790. * ...
  791. * asio::ip::tcp::no_delay option(true);
  792. * asio::error_code ec;
  793. * socket.set_option(option, ec);
  794. * if (ec)
  795. * {
  796. * // An error occurred.
  797. * }
  798. * @endcode
  799. */
  800. template <typename SettableSocketOption>
  801. asio::error_code set_option(const SettableSocketOption& option,
  802. asio::error_code& ec)
  803. {
  804. return this->get_service().set_option(
  805. this->get_implementation(), option, ec);
  806. }
  807. /// Get an option from the socket.
  808. /**
  809. * This function is used to get the current value of an option on the socket.
  810. *
  811. * @param option The option value to be obtained from the socket.
  812. *
  813. * @throws asio::system_error Thrown on failure.
  814. *
  815. * @sa GettableSocketOption @n
  816. * asio::socket_base::broadcast @n
  817. * asio::socket_base::do_not_route @n
  818. * asio::socket_base::keep_alive @n
  819. * asio::socket_base::linger @n
  820. * asio::socket_base::receive_buffer_size @n
  821. * asio::socket_base::receive_low_watermark @n
  822. * asio::socket_base::reuse_address @n
  823. * asio::socket_base::send_buffer_size @n
  824. * asio::socket_base::send_low_watermark @n
  825. * asio::ip::multicast::join_group @n
  826. * asio::ip::multicast::leave_group @n
  827. * asio::ip::multicast::enable_loopback @n
  828. * asio::ip::multicast::outbound_interface @n
  829. * asio::ip::multicast::hops @n
  830. * asio::ip::tcp::no_delay
  831. *
  832. * @par Example
  833. * Getting the value of the SOL_SOCKET/SO_KEEPALIVE option:
  834. * @code
  835. * asio::ip::tcp::socket socket(io_context);
  836. * ...
  837. * asio::ip::tcp::socket::keep_alive option;
  838. * socket.get_option(option);
  839. * bool is_set = option.value();
  840. * @endcode
  841. */
  842. template <typename GettableSocketOption>
  843. void get_option(GettableSocketOption& option) const
  844. {
  845. asio::error_code ec;
  846. this->get_service().get_option(this->get_implementation(), option, ec);
  847. asio::detail::throw_error(ec, "get_option");
  848. }
  849. /// Get an option from the socket.
  850. /**
  851. * This function is used to get the current value of an option on the socket.
  852. *
  853. * @param option The option value to be obtained from the socket.
  854. *
  855. * @param ec Set to indicate what error occurred, if any.
  856. *
  857. * @sa GettableSocketOption @n
  858. * asio::socket_base::broadcast @n
  859. * asio::socket_base::do_not_route @n
  860. * asio::socket_base::keep_alive @n
  861. * asio::socket_base::linger @n
  862. * asio::socket_base::receive_buffer_size @n
  863. * asio::socket_base::receive_low_watermark @n
  864. * asio::socket_base::reuse_address @n
  865. * asio::socket_base::send_buffer_size @n
  866. * asio::socket_base::send_low_watermark @n
  867. * asio::ip::multicast::join_group @n
  868. * asio::ip::multicast::leave_group @n
  869. * asio::ip::multicast::enable_loopback @n
  870. * asio::ip::multicast::outbound_interface @n
  871. * asio::ip::multicast::hops @n
  872. * asio::ip::tcp::no_delay
  873. *
  874. * @par Example
  875. * Getting the value of the SOL_SOCKET/SO_KEEPALIVE option:
  876. * @code
  877. * asio::ip::tcp::socket socket(io_context);
  878. * ...
  879. * asio::ip::tcp::socket::keep_alive option;
  880. * asio::error_code ec;
  881. * socket.get_option(option, ec);
  882. * if (ec)
  883. * {
  884. * // An error occurred.
  885. * }
  886. * bool is_set = option.value();
  887. * @endcode
  888. */
  889. template <typename GettableSocketOption>
  890. asio::error_code get_option(GettableSocketOption& option,
  891. asio::error_code& ec) const
  892. {
  893. return this->get_service().get_option(
  894. this->get_implementation(), option, ec);
  895. }
  896. /// Perform an IO control command on the socket.
  897. /**
  898. * This function is used to execute an IO control command on the socket.
  899. *
  900. * @param command The IO control command to be performed on the socket.
  901. *
  902. * @throws asio::system_error Thrown on failure.
  903. *
  904. * @sa IoControlCommand @n
  905. * asio::socket_base::bytes_readable @n
  906. * asio::socket_base::non_blocking_io
  907. *
  908. * @par Example
  909. * Getting the number of bytes ready to read:
  910. * @code
  911. * asio::ip::tcp::socket socket(io_context);
  912. * ...
  913. * asio::ip::tcp::socket::bytes_readable command;
  914. * socket.io_control(command);
  915. * std::size_t bytes_readable = command.get();
  916. * @endcode
  917. */
  918. template <typename IoControlCommand>
  919. void io_control(IoControlCommand& command)
  920. {
  921. asio::error_code ec;
  922. this->get_service().io_control(this->get_implementation(), command, ec);
  923. asio::detail::throw_error(ec, "io_control");
  924. }
  925. /// Perform an IO control command on the socket.
  926. /**
  927. * This function is used to execute an IO control command on the socket.
  928. *
  929. * @param command The IO control command to be performed on the socket.
  930. *
  931. * @param ec Set to indicate what error occurred, if any.
  932. *
  933. * @sa IoControlCommand @n
  934. * asio::socket_base::bytes_readable @n
  935. * asio::socket_base::non_blocking_io
  936. *
  937. * @par Example
  938. * Getting the number of bytes ready to read:
  939. * @code
  940. * asio::ip::tcp::socket socket(io_context);
  941. * ...
  942. * asio::ip::tcp::socket::bytes_readable command;
  943. * asio::error_code ec;
  944. * socket.io_control(command, ec);
  945. * if (ec)
  946. * {
  947. * // An error occurred.
  948. * }
  949. * std::size_t bytes_readable = command.get();
  950. * @endcode
  951. */
  952. template <typename IoControlCommand>
  953. asio::error_code io_control(IoControlCommand& command,
  954. asio::error_code& ec)
  955. {
  956. return this->get_service().io_control(
  957. this->get_implementation(), command, ec);
  958. }
  959. /// Gets the non-blocking mode of the socket.
  960. /**
  961. * @returns @c true if the socket's synchronous operations will fail with
  962. * asio::error::would_block if they are unable to perform the requested
  963. * operation immediately. If @c false, synchronous operations will block
  964. * until complete.
  965. *
  966. * @note The non-blocking mode has no effect on the behaviour of asynchronous
  967. * operations. Asynchronous operations will never fail with the error
  968. * asio::error::would_block.
  969. */
  970. bool non_blocking() const
  971. {
  972. return this->get_service().non_blocking(this->get_implementation());
  973. }
  974. /// Sets the non-blocking mode of the socket.
  975. /**
  976. * @param mode If @c true, the socket's synchronous operations will fail with
  977. * asio::error::would_block if they are unable to perform the requested
  978. * operation immediately. If @c false, synchronous operations will block
  979. * until complete.
  980. *
  981. * @throws asio::system_error Thrown on failure.
  982. *
  983. * @note The non-blocking mode has no effect on the behaviour of asynchronous
  984. * operations. Asynchronous operations will never fail with the error
  985. * asio::error::would_block.
  986. */
  987. void non_blocking(bool mode)
  988. {
  989. asio::error_code ec;
  990. this->get_service().non_blocking(this->get_implementation(), mode, ec);
  991. asio::detail::throw_error(ec, "non_blocking");
  992. }
  993. /// Sets the non-blocking mode of the socket.
  994. /**
  995. * @param mode If @c true, the socket's synchronous operations will fail with
  996. * asio::error::would_block if they are unable to perform the requested
  997. * operation immediately. If @c false, synchronous operations will block
  998. * until complete.
  999. *
  1000. * @param ec Set to indicate what error occurred, if any.
  1001. *
  1002. * @note The non-blocking mode has no effect on the behaviour of asynchronous
  1003. * operations. Asynchronous operations will never fail with the error
  1004. * asio::error::would_block.
  1005. */
  1006. asio::error_code non_blocking(
  1007. bool mode, asio::error_code& ec)
  1008. {
  1009. return this->get_service().non_blocking(
  1010. this->get_implementation(), mode, ec);
  1011. }
  1012. /// Gets the non-blocking mode of the native socket implementation.
  1013. /**
  1014. * This function is used to retrieve the non-blocking mode of the underlying
  1015. * native socket. This mode has no effect on the behaviour of the socket
  1016. * object's synchronous operations.
  1017. *
  1018. * @returns @c true if the underlying socket is in non-blocking mode and
  1019. * direct system calls may fail with asio::error::would_block (or the
  1020. * equivalent system error).
  1021. *
  1022. * @note The current non-blocking mode is cached by the socket object.
  1023. * Consequently, the return value may be incorrect if the non-blocking mode
  1024. * was set directly on the native socket.
  1025. *
  1026. * @par Example
  1027. * This function is intended to allow the encapsulation of arbitrary
  1028. * non-blocking system calls as asynchronous operations, in a way that is
  1029. * transparent to the user of the socket object. The following example
  1030. * illustrates how Linux's @c sendfile system call might be encapsulated:
  1031. * @code template <typename Handler>
  1032. * struct sendfile_op
  1033. * {
  1034. * tcp::socket& sock_;
  1035. * int fd_;
  1036. * Handler handler_;
  1037. * off_t offset_;
  1038. * std::size_t total_bytes_transferred_;
  1039. *
  1040. * // Function call operator meeting WriteHandler requirements.
  1041. * // Used as the handler for the async_write_some operation.
  1042. * void operator()(asio::error_code ec, std::size_t)
  1043. * {
  1044. * // Put the underlying socket into non-blocking mode.
  1045. * if (!ec)
  1046. * if (!sock_.native_non_blocking())
  1047. * sock_.native_non_blocking(true, ec);
  1048. *
  1049. * if (!ec)
  1050. * {
  1051. * for (;;)
  1052. * {
  1053. * // Try the system call.
  1054. * errno = 0;
  1055. * int n = ::sendfile(sock_.native_handle(), fd_, &offset_, 65536);
  1056. * ec = asio::error_code(n < 0 ? errno : 0,
  1057. * asio::error::get_system_category());
  1058. * total_bytes_transferred_ += ec ? 0 : n;
  1059. *
  1060. * // Retry operation immediately if interrupted by signal.
  1061. * if (ec == asio::error::interrupted)
  1062. * continue;
  1063. *
  1064. * // Check if we need to run the operation again.
  1065. * if (ec == asio::error::would_block
  1066. * || ec == asio::error::try_again)
  1067. * {
  1068. * // We have to wait for the socket to become ready again.
  1069. * sock_.async_wait(tcp::socket::wait_write, *this);
  1070. * return;
  1071. * }
  1072. *
  1073. * if (ec || n == 0)
  1074. * {
  1075. * // An error occurred, or we have reached the end of the file.
  1076. * // Either way we must exit the loop so we can call the handler.
  1077. * break;
  1078. * }
  1079. *
  1080. * // Loop around to try calling sendfile again.
  1081. * }
  1082. * }
  1083. *
  1084. * // Pass result back to user's handler.
  1085. * handler_(ec, total_bytes_transferred_);
  1086. * }
  1087. * };
  1088. *
  1089. * template <typename Handler>
  1090. * void async_sendfile(tcp::socket& sock, int fd, Handler h)
  1091. * {
  1092. * sendfile_op<Handler> op = { sock, fd, h, 0, 0 };
  1093. * sock.async_wait(tcp::socket::wait_write, op);
  1094. * } @endcode
  1095. */
  1096. bool native_non_blocking() const
  1097. {
  1098. return this->get_service().native_non_blocking(this->get_implementation());
  1099. }
  1100. /// Sets the non-blocking mode of the native socket implementation.
  1101. /**
  1102. * This function is used to modify the non-blocking mode of the underlying
  1103. * native socket. It has no effect on the behaviour of the socket object's
  1104. * synchronous operations.
  1105. *
  1106. * @param mode If @c true, the underlying socket is put into non-blocking
  1107. * mode and direct system calls may fail with asio::error::would_block
  1108. * (or the equivalent system error).
  1109. *
  1110. * @throws asio::system_error Thrown on failure. If the @c mode is
  1111. * @c false, but the current value of @c non_blocking() is @c true, this
  1112. * function fails with asio::error::invalid_argument, as the
  1113. * combination does not make sense.
  1114. *
  1115. * @par Example
  1116. * This function is intended to allow the encapsulation of arbitrary
  1117. * non-blocking system calls as asynchronous operations, in a way that is
  1118. * transparent to the user of the socket object. The following example
  1119. * illustrates how Linux's @c sendfile system call might be encapsulated:
  1120. * @code template <typename Handler>
  1121. * struct sendfile_op
  1122. * {
  1123. * tcp::socket& sock_;
  1124. * int fd_;
  1125. * Handler handler_;
  1126. * off_t offset_;
  1127. * std::size_t total_bytes_transferred_;
  1128. *
  1129. * // Function call operator meeting WriteHandler requirements.
  1130. * // Used as the handler for the async_write_some operation.
  1131. * void operator()(asio::error_code ec, std::size_t)
  1132. * {
  1133. * // Put the underlying socket into non-blocking mode.
  1134. * if (!ec)
  1135. * if (!sock_.native_non_blocking())
  1136. * sock_.native_non_blocking(true, ec);
  1137. *
  1138. * if (!ec)
  1139. * {
  1140. * for (;;)
  1141. * {
  1142. * // Try the system call.
  1143. * errno = 0;
  1144. * int n = ::sendfile(sock_.native_handle(), fd_, &offset_, 65536);
  1145. * ec = asio::error_code(n < 0 ? errno : 0,
  1146. * asio::error::get_system_category());
  1147. * total_bytes_transferred_ += ec ? 0 : n;
  1148. *
  1149. * // Retry operation immediately if interrupted by signal.
  1150. * if (ec == asio::error::interrupted)
  1151. * continue;
  1152. *
  1153. * // Check if we need to run the operation again.
  1154. * if (ec == asio::error::would_block
  1155. * || ec == asio::error::try_again)
  1156. * {
  1157. * // We have to wait for the socket to become ready again.
  1158. * sock_.async_wait(tcp::socket::wait_write, *this);
  1159. * return;
  1160. * }
  1161. *
  1162. * if (ec || n == 0)
  1163. * {
  1164. * // An error occurred, or we have reached the end of the file.
  1165. * // Either way we must exit the loop so we can call the handler.
  1166. * break;
  1167. * }
  1168. *
  1169. * // Loop around to try calling sendfile again.
  1170. * }
  1171. * }
  1172. *
  1173. * // Pass result back to user's handler.
  1174. * handler_(ec, total_bytes_transferred_);
  1175. * }
  1176. * };
  1177. *
  1178. * template <typename Handler>
  1179. * void async_sendfile(tcp::socket& sock, int fd, Handler h)
  1180. * {
  1181. * sendfile_op<Handler> op = { sock, fd, h, 0, 0 };
  1182. * sock.async_wait(tcp::socket::wait_write, op);
  1183. * } @endcode
  1184. */
  1185. void native_non_blocking(bool mode)
  1186. {
  1187. asio::error_code ec;
  1188. this->get_service().native_non_blocking(
  1189. this->get_implementation(), mode, ec);
  1190. asio::detail::throw_error(ec, "native_non_blocking");
  1191. }
  1192. /// Sets the non-blocking mode of the native socket implementation.
  1193. /**
  1194. * This function is used to modify the non-blocking mode of the underlying
  1195. * native socket. It has no effect on the behaviour of the socket object's
  1196. * synchronous operations.
  1197. *
  1198. * @param mode If @c true, the underlying socket is put into non-blocking
  1199. * mode and direct system calls may fail with asio::error::would_block
  1200. * (or the equivalent system error).
  1201. *
  1202. * @param ec Set to indicate what error occurred, if any. If the @c mode is
  1203. * @c false, but the current value of @c non_blocking() is @c true, this
  1204. * function fails with asio::error::invalid_argument, as the
  1205. * combination does not make sense.
  1206. *
  1207. * @par Example
  1208. * This function is intended to allow the encapsulation of arbitrary
  1209. * non-blocking system calls as asynchronous operations, in a way that is
  1210. * transparent to the user of the socket object. The following example
  1211. * illustrates how Linux's @c sendfile system call might be encapsulated:
  1212. * @code template <typename Handler>
  1213. * struct sendfile_op
  1214. * {
  1215. * tcp::socket& sock_;
  1216. * int fd_;
  1217. * Handler handler_;
  1218. * off_t offset_;
  1219. * std::size_t total_bytes_transferred_;
  1220. *
  1221. * // Function call operator meeting WriteHandler requirements.
  1222. * // Used as the handler for the async_write_some operation.
  1223. * void operator()(asio::error_code ec, std::size_t)
  1224. * {
  1225. * // Put the underlying socket into non-blocking mode.
  1226. * if (!ec)
  1227. * if (!sock_.native_non_blocking())
  1228. * sock_.native_non_blocking(true, ec);
  1229. *
  1230. * if (!ec)
  1231. * {
  1232. * for (;;)
  1233. * {
  1234. * // Try the system call.
  1235. * errno = 0;
  1236. * int n = ::sendfile(sock_.native_handle(), fd_, &offset_, 65536);
  1237. * ec = asio::error_code(n < 0 ? errno : 0,
  1238. * asio::error::get_system_category());
  1239. * total_bytes_transferred_ += ec ? 0 : n;
  1240. *
  1241. * // Retry operation immediately if interrupted by signal.
  1242. * if (ec == asio::error::interrupted)
  1243. * continue;
  1244. *
  1245. * // Check if we need to run the operation again.
  1246. * if (ec == asio::error::would_block
  1247. * || ec == asio::error::try_again)
  1248. * {
  1249. * // We have to wait for the socket to become ready again.
  1250. * sock_.async_wait(tcp::socket::wait_write, *this);
  1251. * return;
  1252. * }
  1253. *
  1254. * if (ec || n == 0)
  1255. * {
  1256. * // An error occurred, or we have reached the end of the file.
  1257. * // Either way we must exit the loop so we can call the handler.
  1258. * break;
  1259. * }
  1260. *
  1261. * // Loop around to try calling sendfile again.
  1262. * }
  1263. * }
  1264. *
  1265. * // Pass result back to user's handler.
  1266. * handler_(ec, total_bytes_transferred_);
  1267. * }
  1268. * };
  1269. *
  1270. * template <typename Handler>
  1271. * void async_sendfile(tcp::socket& sock, int fd, Handler h)
  1272. * {
  1273. * sendfile_op<Handler> op = { sock, fd, h, 0, 0 };
  1274. * sock.async_wait(tcp::socket::wait_write, op);
  1275. * } @endcode
  1276. */
  1277. asio::error_code native_non_blocking(
  1278. bool mode, asio::error_code& ec)
  1279. {
  1280. return this->get_service().native_non_blocking(
  1281. this->get_implementation(), mode, ec);
  1282. }
  1283. /// Get the local endpoint of the socket.
  1284. /**
  1285. * This function is used to obtain the locally bound endpoint of the socket.
  1286. *
  1287. * @returns An object that represents the local endpoint of the socket.
  1288. *
  1289. * @throws asio::system_error Thrown on failure.
  1290. *
  1291. * @par Example
  1292. * @code
  1293. * asio::ip::tcp::socket socket(io_context);
  1294. * ...
  1295. * asio::ip::tcp::endpoint endpoint = socket.local_endpoint();
  1296. * @endcode
  1297. */
  1298. endpoint_type local_endpoint() const
  1299. {
  1300. asio::error_code ec;
  1301. endpoint_type ep = this->get_service().local_endpoint(
  1302. this->get_implementation(), ec);
  1303. asio::detail::throw_error(ec, "local_endpoint");
  1304. return ep;
  1305. }
  1306. /// Get the local endpoint of the socket.
  1307. /**
  1308. * This function is used to obtain the locally bound endpoint of the socket.
  1309. *
  1310. * @param ec Set to indicate what error occurred, if any.
  1311. *
  1312. * @returns An object that represents the local endpoint of the socket.
  1313. * Returns a default-constructed endpoint object if an error occurred.
  1314. *
  1315. * @par Example
  1316. * @code
  1317. * asio::ip::tcp::socket socket(io_context);
  1318. * ...
  1319. * asio::error_code ec;
  1320. * asio::ip::tcp::endpoint endpoint = socket.local_endpoint(ec);
  1321. * if (ec)
  1322. * {
  1323. * // An error occurred.
  1324. * }
  1325. * @endcode
  1326. */
  1327. endpoint_type local_endpoint(asio::error_code& ec) const
  1328. {
  1329. return this->get_service().local_endpoint(this->get_implementation(), ec);
  1330. }
  1331. /// Get the remote endpoint of the socket.
  1332. /**
  1333. * This function is used to obtain the remote endpoint of the socket.
  1334. *
  1335. * @returns An object that represents the remote endpoint of the socket.
  1336. *
  1337. * @throws asio::system_error Thrown on failure.
  1338. *
  1339. * @par Example
  1340. * @code
  1341. * asio::ip::tcp::socket socket(io_context);
  1342. * ...
  1343. * asio::ip::tcp::endpoint endpoint = socket.remote_endpoint();
  1344. * @endcode
  1345. */
  1346. endpoint_type remote_endpoint() const
  1347. {
  1348. asio::error_code ec;
  1349. endpoint_type ep = this->get_service().remote_endpoint(
  1350. this->get_implementation(), ec);
  1351. asio::detail::throw_error(ec, "remote_endpoint");
  1352. return ep;
  1353. }
  1354. /// Get the remote endpoint of the socket.
  1355. /**
  1356. * This function is used to obtain the remote endpoint of the socket.
  1357. *
  1358. * @param ec Set to indicate what error occurred, if any.
  1359. *
  1360. * @returns An object that represents the remote endpoint of the socket.
  1361. * Returns a default-constructed endpoint object if an error occurred.
  1362. *
  1363. * @par Example
  1364. * @code
  1365. * asio::ip::tcp::socket socket(io_context);
  1366. * ...
  1367. * asio::error_code ec;
  1368. * asio::ip::tcp::endpoint endpoint = socket.remote_endpoint(ec);
  1369. * if (ec)
  1370. * {
  1371. * // An error occurred.
  1372. * }
  1373. * @endcode
  1374. */
  1375. endpoint_type remote_endpoint(asio::error_code& ec) const
  1376. {
  1377. return this->get_service().remote_endpoint(this->get_implementation(), ec);
  1378. }
  1379. /// Disable sends or receives on the socket.
  1380. /**
  1381. * This function is used to disable send operations, receive operations, or
  1382. * both.
  1383. *
  1384. * @param what Determines what types of operation will no longer be allowed.
  1385. *
  1386. * @throws asio::system_error Thrown on failure.
  1387. *
  1388. * @par Example
  1389. * Shutting down the send side of the socket:
  1390. * @code
  1391. * asio::ip::tcp::socket socket(io_context);
  1392. * ...
  1393. * socket.shutdown(asio::ip::tcp::socket::shutdown_send);
  1394. * @endcode
  1395. */
  1396. void shutdown(shutdown_type what)
  1397. {
  1398. asio::error_code ec;
  1399. this->get_service().shutdown(this->get_implementation(), what, ec);
  1400. asio::detail::throw_error(ec, "shutdown");
  1401. }
  1402. /// Disable sends or receives on the socket.
  1403. /**
  1404. * This function is used to disable send operations, receive operations, or
  1405. * both.
  1406. *
  1407. * @param what Determines what types of operation will no longer be allowed.
  1408. *
  1409. * @param ec Set to indicate what error occurred, if any.
  1410. *
  1411. * @par Example
  1412. * Shutting down the send side of the socket:
  1413. * @code
  1414. * asio::ip::tcp::socket socket(io_context);
  1415. * ...
  1416. * asio::error_code ec;
  1417. * socket.shutdown(asio::ip::tcp::socket::shutdown_send, ec);
  1418. * if (ec)
  1419. * {
  1420. * // An error occurred.
  1421. * }
  1422. * @endcode
  1423. */
  1424. asio::error_code shutdown(shutdown_type what,
  1425. asio::error_code& ec)
  1426. {
  1427. return this->get_service().shutdown(this->get_implementation(), what, ec);
  1428. }
  1429. /// Wait for the socket to become ready to read, ready to write, or to have
  1430. /// pending error conditions.
  1431. /**
  1432. * This function is used to perform a blocking wait for a socket to enter
  1433. * a ready to read, write or error condition state.
  1434. *
  1435. * @param w Specifies the desired socket state.
  1436. *
  1437. * @par Example
  1438. * Waiting for a socket to become readable.
  1439. * @code
  1440. * asio::ip::tcp::socket socket(io_context);
  1441. * ...
  1442. * socket.wait(asio::ip::tcp::socket::wait_read);
  1443. * @endcode
  1444. */
  1445. void wait(wait_type w)
  1446. {
  1447. asio::error_code ec;
  1448. this->get_service().wait(this->get_implementation(), w, ec);
  1449. asio::detail::throw_error(ec, "wait");
  1450. }
  1451. /// Wait for the socket to become ready to read, ready to write, or to have
  1452. /// pending error conditions.
  1453. /**
  1454. * This function is used to perform a blocking wait for a socket to enter
  1455. * a ready to read, write or error condition state.
  1456. *
  1457. * @param w Specifies the desired socket state.
  1458. *
  1459. * @param ec Set to indicate what error occurred, if any.
  1460. *
  1461. * @par Example
  1462. * Waiting for a socket to become readable.
  1463. * @code
  1464. * asio::ip::tcp::socket socket(io_context);
  1465. * ...
  1466. * asio::error_code ec;
  1467. * socket.wait(asio::ip::tcp::socket::wait_read, ec);
  1468. * @endcode
  1469. */
  1470. asio::error_code wait(wait_type w, asio::error_code& ec)
  1471. {
  1472. return this->get_service().wait(this->get_implementation(), w, ec);
  1473. }
  1474. /// Asynchronously wait for the socket to become ready to read, ready to
  1475. /// write, or to have pending error conditions.
  1476. /**
  1477. * This function is used to perform an asynchronous wait for a socket to enter
  1478. * a ready to read, write or error condition state.
  1479. *
  1480. * @param w Specifies the desired socket state.
  1481. *
  1482. * @param handler The handler to be called when the wait operation completes.
  1483. * Copies will be made of the handler as required. The function signature of
  1484. * the handler must be:
  1485. * @code void handler(
  1486. * const asio::error_code& error // Result of operation
  1487. * ); @endcode
  1488. * Regardless of whether the asynchronous operation completes immediately or
  1489. * not, the handler will not be invoked from within this function. Invocation
  1490. * of the handler will be performed in a manner equivalent to using
  1491. * asio::io_context::post().
  1492. *
  1493. * @par Example
  1494. * @code
  1495. * void wait_handler(const asio::error_code& error)
  1496. * {
  1497. * if (!error)
  1498. * {
  1499. * // Wait succeeded.
  1500. * }
  1501. * }
  1502. *
  1503. * ...
  1504. *
  1505. * asio::ip::tcp::socket socket(io_context);
  1506. * ...
  1507. * socket.async_wait(asio::ip::tcp::socket::wait_read, wait_handler);
  1508. * @endcode
  1509. */
  1510. template <typename WaitHandler>
  1511. ASIO_INITFN_RESULT_TYPE(WaitHandler,
  1512. void (asio::error_code))
  1513. async_wait(wait_type w, ASIO_MOVE_ARG(WaitHandler) handler)
  1514. {
  1515. // If you get an error on the following line it means that your handler does
  1516. // not meet the documented type requirements for a WaitHandler.
  1517. ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
  1518. return this->get_service().async_wait(this->get_implementation(),
  1519. w, ASIO_MOVE_CAST(WaitHandler)(handler));
  1520. }
  1521. protected:
  1522. /// Protected destructor to prevent deletion through this type.
  1523. ~basic_socket()
  1524. {
  1525. }
  1526. };
  1527. } // namespace asio
  1528. #include "asio/detail/pop_options.hpp"
  1529. #endif // ASIO_BASIC_SOCKET_HPP