basic_socket.hpp 63 KB

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