basic_socket.hpp 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937
  1. //
  2. // basic_socket.hpp
  3. // ~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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/any_io_executor.hpp"
  16. #include "asio/detail/config.hpp"
  17. #include "asio/async_result.hpp"
  18. #include "asio/detail/handler_type_requirements.hpp"
  19. #include "asio/detail/io_object_impl.hpp"
  20. #include "asio/detail/non_const_lvalue.hpp"
  21. #include "asio/detail/throw_error.hpp"
  22. #include "asio/detail/type_traits.hpp"
  23. #include "asio/error.hpp"
  24. #include "asio/execution_context.hpp"
  25. #include "asio/post.hpp"
  26. #include "asio/socket_base.hpp"
  27. #if defined(ASIO_WINDOWS_RUNTIME)
  28. # include "asio/detail/null_socket_service.hpp"
  29. #elif defined(ASIO_HAS_IOCP)
  30. # include "asio/detail/win_iocp_socket_service.hpp"
  31. #elif defined(ASIO_HAS_IO_URING_AS_DEFAULT)
  32. # include "asio/detail/io_uring_socket_service.hpp"
  33. #else
  34. # include "asio/detail/reactive_socket_service.hpp"
  35. #endif
  36. #if defined(ASIO_HAS_MOVE)
  37. # include <utility>
  38. #endif // defined(ASIO_HAS_MOVE)
  39. #include "asio/detail/push_options.hpp"
  40. namespace asio {
  41. #if !defined(ASIO_BASIC_SOCKET_FWD_DECL)
  42. #define ASIO_BASIC_SOCKET_FWD_DECL
  43. // Forward declaration with defaulted arguments.
  44. template <typename Protocol, typename Executor = any_io_executor>
  45. class basic_socket;
  46. #endif // !defined(ASIO_BASIC_SOCKET_FWD_DECL)
  47. /// Provides socket functionality.
  48. /**
  49. * The basic_socket class template provides functionality that is common to both
  50. * stream-oriented and datagram-oriented sockets.
  51. *
  52. * @par Thread Safety
  53. * @e Distinct @e objects: Safe.@n
  54. * @e Shared @e objects: Unsafe.
  55. */
  56. template <typename Protocol, typename Executor>
  57. class basic_socket
  58. : public socket_base
  59. {
  60. public:
  61. /// The type of the executor associated with the object.
  62. typedef Executor executor_type;
  63. /// Rebinds the socket type to another executor.
  64. template <typename Executor1>
  65. struct rebind_executor
  66. {
  67. /// The socket type when rebound to the specified executor.
  68. typedef basic_socket<Protocol, Executor1> other;
  69. };
  70. /// The native representation of a socket.
  71. #if defined(GENERATING_DOCUMENTATION)
  72. typedef implementation_defined native_handle_type;
  73. #elif defined(ASIO_WINDOWS_RUNTIME)
  74. typedef typename detail::null_socket_service<
  75. Protocol>::native_handle_type native_handle_type;
  76. #elif defined(ASIO_HAS_IOCP)
  77. typedef typename detail::win_iocp_socket_service<
  78. Protocol>::native_handle_type native_handle_type;
  79. #elif defined(ASIO_HAS_IO_URING_AS_DEFAULT)
  80. typedef typename detail::io_uring_socket_service<
  81. Protocol>::native_handle_type native_handle_type;
  82. #else
  83. typedef typename detail::reactive_socket_service<
  84. Protocol>::native_handle_type native_handle_type;
  85. #endif
  86. /// The protocol type.
  87. typedef Protocol protocol_type;
  88. /// The endpoint type.
  89. typedef typename Protocol::endpoint endpoint_type;
  90. #if !defined(ASIO_NO_EXTENSIONS)
  91. /// A basic_socket is always the lowest layer.
  92. typedef basic_socket<Protocol, Executor> lowest_layer_type;
  93. #endif // !defined(ASIO_NO_EXTENSIONS)
  94. /// Construct a basic_socket without opening it.
  95. /**
  96. * This constructor creates a socket without opening it.
  97. *
  98. * @param ex The I/O executor that the socket will use, by default, to
  99. * dispatch handlers for any asynchronous operations performed on the socket.
  100. */
  101. explicit basic_socket(const executor_type& ex)
  102. : impl_(0, ex)
  103. {
  104. }
  105. /// Construct a basic_socket without opening it.
  106. /**
  107. * This constructor creates a socket without opening it.
  108. *
  109. * @param context An execution context which provides the I/O executor that
  110. * the socket will use, by default, to dispatch handlers for any asynchronous
  111. * operations performed on the socket.
  112. */
  113. template <typename ExecutionContext>
  114. explicit basic_socket(ExecutionContext& context,
  115. typename constraint<
  116. is_convertible<ExecutionContext&, execution_context&>::value
  117. >::type = 0)
  118. : impl_(0, 0, context)
  119. {
  120. }
  121. /// Construct and open a basic_socket.
  122. /**
  123. * This constructor creates and opens a socket.
  124. *
  125. * @param ex The I/O executor that the socket will use, by default, to
  126. * dispatch handlers for any asynchronous operations performed on the socket.
  127. *
  128. * @param protocol An object specifying protocol parameters to be used.
  129. *
  130. * @throws asio::system_error Thrown on failure.
  131. */
  132. basic_socket(const executor_type& ex, const protocol_type& protocol)
  133. : impl_(0, ex)
  134. {
  135. asio::error_code ec;
  136. impl_.get_service().open(impl_.get_implementation(), protocol, ec);
  137. asio::detail::throw_error(ec, "open");
  138. }
  139. /// Construct and open a basic_socket.
  140. /**
  141. * This constructor creates and opens a socket.
  142. *
  143. * @param context An execution context which provides the I/O executor that
  144. * the socket will use, by default, to dispatch handlers for any asynchronous
  145. * operations performed on the socket.
  146. *
  147. * @param protocol An object specifying protocol parameters to be used.
  148. *
  149. * @throws asio::system_error Thrown on failure.
  150. */
  151. template <typename ExecutionContext>
  152. basic_socket(ExecutionContext& context, const protocol_type& protocol,
  153. typename constraint<
  154. is_convertible<ExecutionContext&, execution_context&>::value,
  155. defaulted_constraint
  156. >::type = defaulted_constraint())
  157. : impl_(0, 0, context)
  158. {
  159. asio::error_code ec;
  160. impl_.get_service().open(impl_.get_implementation(), protocol, ec);
  161. asio::detail::throw_error(ec, "open");
  162. }
  163. /// Construct a basic_socket, opening it and binding it to the given local
  164. /// endpoint.
  165. /**
  166. * This constructor creates a socket and automatically opens it bound to the
  167. * specified endpoint on the local machine. The protocol used is the protocol
  168. * associated with the given endpoint.
  169. *
  170. * @param ex The I/O executor that the socket will use, by default, to
  171. * dispatch handlers for any asynchronous operations performed on the socket.
  172. *
  173. * @param endpoint An endpoint on the local machine to which the socket will
  174. * be bound.
  175. *
  176. * @throws asio::system_error Thrown on failure.
  177. */
  178. basic_socket(const executor_type& ex, const endpoint_type& endpoint)
  179. : impl_(0, ex)
  180. {
  181. asio::error_code ec;
  182. const protocol_type protocol = endpoint.protocol();
  183. impl_.get_service().open(impl_.get_implementation(), protocol, ec);
  184. asio::detail::throw_error(ec, "open");
  185. impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
  186. asio::detail::throw_error(ec, "bind");
  187. }
  188. /// Construct a basic_socket, opening it and binding it to the given local
  189. /// endpoint.
  190. /**
  191. * This constructor creates a socket and automatically opens it bound to the
  192. * specified endpoint on the local machine. The protocol used is the protocol
  193. * associated with the given endpoint.
  194. *
  195. * @param context An execution context which provides the I/O executor that
  196. * the socket will use, by default, to dispatch handlers for any asynchronous
  197. * operations performed on the socket.
  198. *
  199. * @param endpoint An endpoint on the local machine to which the socket will
  200. * be bound.
  201. *
  202. * @throws asio::system_error Thrown on failure.
  203. */
  204. template <typename ExecutionContext>
  205. basic_socket(ExecutionContext& context, const endpoint_type& endpoint,
  206. typename constraint<
  207. is_convertible<ExecutionContext&, execution_context&>::value
  208. >::type = 0)
  209. : impl_(0, 0, context)
  210. {
  211. asio::error_code ec;
  212. const protocol_type protocol = endpoint.protocol();
  213. impl_.get_service().open(impl_.get_implementation(), protocol, ec);
  214. asio::detail::throw_error(ec, "open");
  215. impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
  216. asio::detail::throw_error(ec, "bind");
  217. }
  218. /// Construct a basic_socket on an existing native socket.
  219. /**
  220. * This constructor creates a socket object to hold an existing native socket.
  221. *
  222. * @param ex The I/O executor that the socket will use, by default, to
  223. * dispatch handlers for any asynchronous operations performed on the socket.
  224. *
  225. * @param protocol An object specifying protocol parameters to be used.
  226. *
  227. * @param native_socket A native socket.
  228. *
  229. * @throws asio::system_error Thrown on failure.
  230. */
  231. basic_socket(const executor_type& ex, const protocol_type& protocol,
  232. const native_handle_type& native_socket)
  233. : impl_(0, ex)
  234. {
  235. asio::error_code ec;
  236. impl_.get_service().assign(impl_.get_implementation(),
  237. protocol, native_socket, ec);
  238. asio::detail::throw_error(ec, "assign");
  239. }
  240. /// Construct a basic_socket on an existing native socket.
  241. /**
  242. * This constructor creates a socket object to hold an existing native socket.
  243. *
  244. * @param context An execution context which provides the I/O executor that
  245. * the socket will use, by default, to dispatch handlers for any asynchronous
  246. * operations performed on the socket.
  247. *
  248. * @param protocol An object specifying protocol parameters to be used.
  249. *
  250. * @param native_socket A native socket.
  251. *
  252. * @throws asio::system_error Thrown on failure.
  253. */
  254. template <typename ExecutionContext>
  255. basic_socket(ExecutionContext& context, const protocol_type& protocol,
  256. const native_handle_type& native_socket,
  257. typename constraint<
  258. is_convertible<ExecutionContext&, execution_context&>::value
  259. >::type = 0)
  260. : impl_(0, 0, context)
  261. {
  262. asio::error_code ec;
  263. impl_.get_service().assign(impl_.get_implementation(),
  264. protocol, native_socket, ec);
  265. asio::detail::throw_error(ec, "assign");
  266. }
  267. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  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) ASIO_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. typename constraint<
  313. is_convertible<Protocol1, Protocol>::value
  314. && is_convertible<Executor1, Executor>::value
  315. >::type = 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. typename constraint<
  331. is_convertible<Protocol1, Protocol>::value
  332. && is_convertible<Executor1, Executor>::value,
  333. basic_socket&
  334. >::type 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. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  341. /// Get the executor associated with the object.
  342. executor_type get_executor() ASIO_NOEXCEPT
  343. {
  344. return impl_.get_executor();
  345. }
  346. #if !defined(ASIO_NO_EXTENSIONS)
  347. /// Get a reference to the lowest layer.
  348. /**
  349. * This function returns a reference to the lowest layer in a stack of
  350. * layers. Since a basic_socket cannot contain any further layers, it simply
  351. * returns a reference to itself.
  352. *
  353. * @return A reference to the lowest layer in the stack of layers. Ownership
  354. * is not transferred to the caller.
  355. */
  356. lowest_layer_type& lowest_layer()
  357. {
  358. return *this;
  359. }
  360. /// Get a const reference to the lowest layer.
  361. /**
  362. * This function returns a const reference to the lowest layer in a stack of
  363. * layers. Since a basic_socket cannot contain any further layers, it simply
  364. * returns a reference to itself.
  365. *
  366. * @return A const reference to the lowest layer in the stack of layers.
  367. * Ownership is not transferred to the caller.
  368. */
  369. const lowest_layer_type& lowest_layer() const
  370. {
  371. return *this;
  372. }
  373. #endif // !defined(ASIO_NO_EXTENSIONS)
  374. /// Open the socket using the specified protocol.
  375. /**
  376. * This function opens the socket so that it will use the specified protocol.
  377. *
  378. * @param protocol An object specifying protocol parameters to be used.
  379. *
  380. * @throws asio::system_error Thrown on failure.
  381. *
  382. * @par Example
  383. * @code
  384. * asio::ip::tcp::socket socket(my_context);
  385. * socket.open(asio::ip::tcp::v4());
  386. * @endcode
  387. */
  388. void open(const protocol_type& protocol = protocol_type())
  389. {
  390. asio::error_code ec;
  391. impl_.get_service().open(impl_.get_implementation(), protocol, ec);
  392. asio::detail::throw_error(ec, "open");
  393. }
  394. /// Open the socket using the specified protocol.
  395. /**
  396. * This function opens the socket so that it will use the specified protocol.
  397. *
  398. * @param protocol An object specifying which protocol is to be used.
  399. *
  400. * @param ec Set to indicate what error occurred, if any.
  401. *
  402. * @par Example
  403. * @code
  404. * asio::ip::tcp::socket socket(my_context);
  405. * asio::error_code ec;
  406. * socket.open(asio::ip::tcp::v4(), ec);
  407. * if (ec)
  408. * {
  409. * // An error occurred.
  410. * }
  411. * @endcode
  412. */
  413. ASIO_SYNC_OP_VOID open(const protocol_type& protocol,
  414. asio::error_code& ec)
  415. {
  416. impl_.get_service().open(impl_.get_implementation(), protocol, ec);
  417. ASIO_SYNC_OP_VOID_RETURN(ec);
  418. }
  419. /// Assign an existing native socket to the socket.
  420. /*
  421. * This function opens the socket to hold an existing native socket.
  422. *
  423. * @param protocol An object specifying which protocol is to be used.
  424. *
  425. * @param native_socket A native socket.
  426. *
  427. * @throws asio::system_error Thrown on failure.
  428. */
  429. void assign(const protocol_type& protocol,
  430. const native_handle_type& native_socket)
  431. {
  432. asio::error_code ec;
  433. impl_.get_service().assign(impl_.get_implementation(),
  434. protocol, native_socket, ec);
  435. asio::detail::throw_error(ec, "assign");
  436. }
  437. /// Assign an existing native socket to the socket.
  438. /*
  439. * This function opens the socket to hold an existing native socket.
  440. *
  441. * @param protocol An object specifying which protocol is to be used.
  442. *
  443. * @param native_socket A native socket.
  444. *
  445. * @param ec Set to indicate what error occurred, if any.
  446. */
  447. ASIO_SYNC_OP_VOID assign(const protocol_type& protocol,
  448. const native_handle_type& native_socket, asio::error_code& ec)
  449. {
  450. impl_.get_service().assign(impl_.get_implementation(),
  451. protocol, native_socket, ec);
  452. ASIO_SYNC_OP_VOID_RETURN(ec);
  453. }
  454. /// Determine whether the socket is open.
  455. bool is_open() const
  456. {
  457. return impl_.get_service().is_open(impl_.get_implementation());
  458. }
  459. /// Close the socket.
  460. /**
  461. * This function is used to close the socket. Any asynchronous send, receive
  462. * or connect operations will be cancelled immediately, and will complete
  463. * with the asio::error::operation_aborted error.
  464. *
  465. * @throws asio::system_error Thrown on failure. Note that, even if
  466. * the function indicates an error, the underlying descriptor is closed.
  467. *
  468. * @note For portable behaviour with respect to graceful closure of a
  469. * connected socket, call shutdown() before closing the socket.
  470. */
  471. void close()
  472. {
  473. asio::error_code ec;
  474. impl_.get_service().close(impl_.get_implementation(), ec);
  475. asio::detail::throw_error(ec, "close");
  476. }
  477. /// Close the socket.
  478. /**
  479. * This function is used to close the socket. Any asynchronous send, receive
  480. * or connect operations will be cancelled immediately, and will complete
  481. * with the asio::error::operation_aborted error.
  482. *
  483. * @param ec Set to indicate what error occurred, if any. Note that, even if
  484. * the function indicates an error, the underlying descriptor is closed.
  485. *
  486. * @par Example
  487. * @code
  488. * asio::ip::tcp::socket socket(my_context);
  489. * ...
  490. * asio::error_code ec;
  491. * socket.close(ec);
  492. * if (ec)
  493. * {
  494. * // An error occurred.
  495. * }
  496. * @endcode
  497. *
  498. * @note For portable behaviour with respect to graceful closure of a
  499. * connected socket, call shutdown() before closing the socket.
  500. */
  501. ASIO_SYNC_OP_VOID close(asio::error_code& ec)
  502. {
  503. impl_.get_service().close(impl_.get_implementation(), ec);
  504. ASIO_SYNC_OP_VOID_RETURN(ec);
  505. }
  506. /// Release ownership of the underlying native socket.
  507. /**
  508. * This function causes all outstanding asynchronous connect, send and receive
  509. * operations to finish immediately, and the handlers for cancelled operations
  510. * will be passed the asio::error::operation_aborted error. Ownership
  511. * of the native socket is then transferred to the caller.
  512. *
  513. * @throws asio::system_error Thrown on failure.
  514. *
  515. * @note This function is unsupported on Windows versions prior to Windows
  516. * 8.1, and will fail with asio::error::operation_not_supported on
  517. * these platforms.
  518. */
  519. #if defined(ASIO_MSVC) && (ASIO_MSVC >= 1400) \
  520. && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0603)
  521. __declspec(deprecated("This function always fails with "
  522. "operation_not_supported when used on Windows versions "
  523. "prior to Windows 8.1."))
  524. #endif
  525. native_handle_type release()
  526. {
  527. asio::error_code ec;
  528. native_handle_type s = impl_.get_service().release(
  529. impl_.get_implementation(), ec);
  530. asio::detail::throw_error(ec, "release");
  531. return s;
  532. }
  533. /// Release ownership of the underlying native socket.
  534. /**
  535. * This function causes all outstanding asynchronous connect, send and receive
  536. * operations to finish immediately, and the handlers for cancelled operations
  537. * will be passed the asio::error::operation_aborted error. Ownership
  538. * of the native socket is then transferred to the caller.
  539. *
  540. * @param ec Set to indicate what error occurred, if any.
  541. *
  542. * @note This function is unsupported on Windows versions prior to Windows
  543. * 8.1, and will fail with asio::error::operation_not_supported on
  544. * these platforms.
  545. */
  546. #if defined(ASIO_MSVC) && (ASIO_MSVC >= 1400) \
  547. && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0603)
  548. __declspec(deprecated("This function always fails with "
  549. "operation_not_supported when used on Windows versions "
  550. "prior to Windows 8.1."))
  551. #endif
  552. native_handle_type release(asio::error_code& ec)
  553. {
  554. return impl_.get_service().release(impl_.get_implementation(), ec);
  555. }
  556. /// Get the native socket representation.
  557. /**
  558. * This function may be used to obtain the underlying representation of the
  559. * socket. This is intended to allow access to native socket functionality
  560. * that is not otherwise provided.
  561. */
  562. native_handle_type native_handle()
  563. {
  564. return impl_.get_service().native_handle(impl_.get_implementation());
  565. }
  566. /// Cancel all asynchronous operations associated with the socket.
  567. /**
  568. * This function causes all outstanding asynchronous connect, send and receive
  569. * operations to finish immediately, and the handlers for cancelled operations
  570. * will be passed the asio::error::operation_aborted error.
  571. *
  572. * @throws asio::system_error Thrown on failure.
  573. *
  574. * @note Calls to cancel() will always fail with
  575. * asio::error::operation_not_supported when run on Windows XP, Windows
  576. * Server 2003, and earlier versions of Windows, unless
  577. * ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
  578. * two issues that should be considered before enabling its use:
  579. *
  580. * @li It will only cancel asynchronous operations that were initiated in the
  581. * current thread.
  582. *
  583. * @li It can appear to complete without error, but the request to cancel the
  584. * unfinished operations may be silently ignored by the operating system.
  585. * Whether it works or not seems to depend on the drivers that are installed.
  586. *
  587. * For portable cancellation, consider using one of the following
  588. * alternatives:
  589. *
  590. * @li Disable asio's I/O completion port backend by defining
  591. * ASIO_DISABLE_IOCP.
  592. *
  593. * @li Use the close() function to simultaneously cancel the outstanding
  594. * operations and close the socket.
  595. *
  596. * When running on Windows Vista, Windows Server 2008, and later, the
  597. * CancelIoEx function is always used. This function does not have the
  598. * problems described above.
  599. */
  600. #if defined(ASIO_MSVC) && (ASIO_MSVC >= 1400) \
  601. && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
  602. && !defined(ASIO_ENABLE_CANCELIO)
  603. __declspec(deprecated("By default, this function always fails with "
  604. "operation_not_supported when used on Windows XP, Windows Server 2003, "
  605. "or earlier. Consult documentation for details."))
  606. #endif
  607. void cancel()
  608. {
  609. asio::error_code ec;
  610. impl_.get_service().cancel(impl_.get_implementation(), ec);
  611. asio::detail::throw_error(ec, "cancel");
  612. }
  613. /// Cancel all asynchronous operations associated with the socket.
  614. /**
  615. * This function causes all outstanding asynchronous connect, send and receive
  616. * operations to finish immediately, and the handlers for cancelled operations
  617. * will be passed the asio::error::operation_aborted error.
  618. *
  619. * @param ec Set to indicate what error occurred, if any.
  620. *
  621. * @note Calls to cancel() will always fail with
  622. * asio::error::operation_not_supported when run on Windows XP, Windows
  623. * Server 2003, and earlier versions of Windows, unless
  624. * ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
  625. * two issues that should be considered before enabling its use:
  626. *
  627. * @li It will only cancel asynchronous operations that were initiated in the
  628. * current thread.
  629. *
  630. * @li It can appear to complete without error, but the request to cancel the
  631. * unfinished operations may be silently ignored by the operating system.
  632. * Whether it works or not seems to depend on the drivers that are installed.
  633. *
  634. * For portable cancellation, consider using one of the following
  635. * alternatives:
  636. *
  637. * @li Disable asio's I/O completion port backend by defining
  638. * ASIO_DISABLE_IOCP.
  639. *
  640. * @li Use the close() function to simultaneously cancel the outstanding
  641. * operations and close the socket.
  642. *
  643. * When running on Windows Vista, Windows Server 2008, and later, the
  644. * CancelIoEx function is always used. This function does not have the
  645. * problems described above.
  646. */
  647. #if defined(ASIO_MSVC) && (ASIO_MSVC >= 1400) \
  648. && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
  649. && !defined(ASIO_ENABLE_CANCELIO)
  650. __declspec(deprecated("By default, this function always fails with "
  651. "operation_not_supported when used on Windows XP, Windows Server 2003, "
  652. "or earlier. Consult documentation for details."))
  653. #endif
  654. ASIO_SYNC_OP_VOID cancel(asio::error_code& ec)
  655. {
  656. impl_.get_service().cancel(impl_.get_implementation(), ec);
  657. ASIO_SYNC_OP_VOID_RETURN(ec);
  658. }
  659. /// Determine whether the socket is at the out-of-band data mark.
  660. /**
  661. * This function is used to check whether the socket input is currently
  662. * positioned at the out-of-band data mark.
  663. *
  664. * @return A bool indicating whether the socket is at the out-of-band data
  665. * mark.
  666. *
  667. * @throws asio::system_error Thrown on failure.
  668. */
  669. bool at_mark() const
  670. {
  671. asio::error_code ec;
  672. bool b = impl_.get_service().at_mark(impl_.get_implementation(), ec);
  673. asio::detail::throw_error(ec, "at_mark");
  674. return b;
  675. }
  676. /// Determine whether the socket is at the out-of-band data mark.
  677. /**
  678. * This function is used to check whether the socket input is currently
  679. * positioned at the out-of-band data mark.
  680. *
  681. * @param ec Set to indicate what error occurred, if any.
  682. *
  683. * @return A bool indicating whether the socket is at the out-of-band data
  684. * mark.
  685. */
  686. bool at_mark(asio::error_code& ec) const
  687. {
  688. return impl_.get_service().at_mark(impl_.get_implementation(), ec);
  689. }
  690. /// Determine the number of bytes available for reading.
  691. /**
  692. * This function is used to determine the number of bytes that may be read
  693. * without blocking.
  694. *
  695. * @return The number of bytes that may be read without blocking, or 0 if an
  696. * error occurs.
  697. *
  698. * @throws asio::system_error Thrown on failure.
  699. */
  700. std::size_t available() const
  701. {
  702. asio::error_code ec;
  703. std::size_t s = impl_.get_service().available(
  704. impl_.get_implementation(), ec);
  705. asio::detail::throw_error(ec, "available");
  706. return s;
  707. }
  708. /// Determine the number of bytes available for reading.
  709. /**
  710. * This function is used to determine the number of bytes that may be read
  711. * without blocking.
  712. *
  713. * @param ec Set to indicate what error occurred, if any.
  714. *
  715. * @return The number of bytes that may be read without blocking, or 0 if an
  716. * error occurs.
  717. */
  718. std::size_t available(asio::error_code& ec) const
  719. {
  720. return impl_.get_service().available(impl_.get_implementation(), ec);
  721. }
  722. /// Bind the socket to the given local endpoint.
  723. /**
  724. * This function binds the socket to the specified endpoint on the local
  725. * machine.
  726. *
  727. * @param endpoint An endpoint on the local machine to which the socket will
  728. * be bound.
  729. *
  730. * @throws asio::system_error Thrown on failure.
  731. *
  732. * @par Example
  733. * @code
  734. * asio::ip::tcp::socket socket(my_context);
  735. * socket.open(asio::ip::tcp::v4());
  736. * socket.bind(asio::ip::tcp::endpoint(
  737. * asio::ip::tcp::v4(), 12345));
  738. * @endcode
  739. */
  740. void bind(const endpoint_type& endpoint)
  741. {
  742. asio::error_code ec;
  743. impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
  744. asio::detail::throw_error(ec, "bind");
  745. }
  746. /// Bind the socket to the given local endpoint.
  747. /**
  748. * This function binds the socket to the specified endpoint on the local
  749. * machine.
  750. *
  751. * @param endpoint An endpoint on the local machine to which the socket will
  752. * be bound.
  753. *
  754. * @param ec Set to indicate what error occurred, if any.
  755. *
  756. * @par Example
  757. * @code
  758. * asio::ip::tcp::socket socket(my_context);
  759. * socket.open(asio::ip::tcp::v4());
  760. * asio::error_code ec;
  761. * socket.bind(asio::ip::tcp::endpoint(
  762. * asio::ip::tcp::v4(), 12345), ec);
  763. * if (ec)
  764. * {
  765. * // An error occurred.
  766. * }
  767. * @endcode
  768. */
  769. ASIO_SYNC_OP_VOID bind(const endpoint_type& endpoint,
  770. asio::error_code& ec)
  771. {
  772. impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
  773. ASIO_SYNC_OP_VOID_RETURN(ec);
  774. }
  775. /// Connect the socket to the specified endpoint.
  776. /**
  777. * This function is used to connect a socket to the specified remote endpoint.
  778. * The function call will block until the connection is successfully made or
  779. * an error occurs.
  780. *
  781. * The socket is automatically opened if it is not already open. If the
  782. * connect fails, and the socket was automatically opened, the socket is
  783. * not returned to the closed state.
  784. *
  785. * @param peer_endpoint The remote endpoint to which the socket will be
  786. * connected.
  787. *
  788. * @throws asio::system_error Thrown on failure.
  789. *
  790. * @par Example
  791. * @code
  792. * asio::ip::tcp::socket socket(my_context);
  793. * asio::ip::tcp::endpoint endpoint(
  794. * asio::ip::address::from_string("1.2.3.4"), 12345);
  795. * socket.connect(endpoint);
  796. * @endcode
  797. */
  798. void connect(const endpoint_type& peer_endpoint)
  799. {
  800. asio::error_code ec;
  801. if (!is_open())
  802. {
  803. impl_.get_service().open(impl_.get_implementation(),
  804. peer_endpoint.protocol(), ec);
  805. asio::detail::throw_error(ec, "connect");
  806. }
  807. impl_.get_service().connect(impl_.get_implementation(), peer_endpoint, ec);
  808. asio::detail::throw_error(ec, "connect");
  809. }
  810. /// Connect the socket to the specified endpoint.
  811. /**
  812. * This function is used to connect a socket to the specified remote endpoint.
  813. * The function call will block until the connection is successfully made or
  814. * an error occurs.
  815. *
  816. * The socket is automatically opened if it is not already open. If the
  817. * connect fails, and the socket was automatically opened, the socket is
  818. * not returned to the closed state.
  819. *
  820. * @param peer_endpoint The remote endpoint to which the socket will be
  821. * connected.
  822. *
  823. * @param ec Set to indicate what error occurred, if any.
  824. *
  825. * @par Example
  826. * @code
  827. * asio::ip::tcp::socket socket(my_context);
  828. * asio::ip::tcp::endpoint endpoint(
  829. * asio::ip::address::from_string("1.2.3.4"), 12345);
  830. * asio::error_code ec;
  831. * socket.connect(endpoint, ec);
  832. * if (ec)
  833. * {
  834. * // An error occurred.
  835. * }
  836. * @endcode
  837. */
  838. ASIO_SYNC_OP_VOID connect(const endpoint_type& peer_endpoint,
  839. asio::error_code& ec)
  840. {
  841. if (!is_open())
  842. {
  843. impl_.get_service().open(impl_.get_implementation(),
  844. peer_endpoint.protocol(), ec);
  845. if (ec)
  846. {
  847. ASIO_SYNC_OP_VOID_RETURN(ec);
  848. }
  849. }
  850. impl_.get_service().connect(impl_.get_implementation(), peer_endpoint, ec);
  851. ASIO_SYNC_OP_VOID_RETURN(ec);
  852. }
  853. /// Start an asynchronous connect.
  854. /**
  855. * This function is used to asynchronously connect a socket to the specified
  856. * remote endpoint. It is an initiating function for an @ref
  857. * asynchronous_operation, and always returns immediately.
  858. *
  859. * The socket is automatically opened if it is not already open. If the
  860. * connect fails, and the socket was automatically opened, the socket is
  861. * not returned to the closed state.
  862. *
  863. * @param peer_endpoint The remote endpoint to which the socket will be
  864. * connected. Copies will be made of the endpoint object as required.
  865. *
  866. * @param token The @ref completion_token that will be used to produce a
  867. * completion handler, which will be called when the connect completes.
  868. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  869. * @ref yield_context, or a function object with the correct completion
  870. * signature. The function signature of the completion handler must be:
  871. * @code void handler(
  872. * const asio::error_code& error // Result of operation.
  873. * ); @endcode
  874. * Regardless of whether the asynchronous operation completes immediately or
  875. * not, the completion handler will not be invoked from within this function.
  876. * On immediate completion, invocation of the handler will be performed in a
  877. * manner equivalent to using asio::post().
  878. *
  879. * @par Completion Signature
  880. * @code void(asio::error_code) @endcode
  881. *
  882. * @par Example
  883. * @code
  884. * void connect_handler(const asio::error_code& error)
  885. * {
  886. * if (!error)
  887. * {
  888. * // Connect succeeded.
  889. * }
  890. * }
  891. *
  892. * ...
  893. *
  894. * asio::ip::tcp::socket socket(my_context);
  895. * asio::ip::tcp::endpoint endpoint(
  896. * asio::ip::address::from_string("1.2.3.4"), 12345);
  897. * socket.async_connect(endpoint, connect_handler);
  898. * @endcode
  899. *
  900. * @par Per-Operation Cancellation
  901. * On POSIX or Windows operating systems, this asynchronous operation supports
  902. * cancellation for the following asio::cancellation_type values:
  903. *
  904. * @li @c cancellation_type::terminal
  905. *
  906. * @li @c cancellation_type::partial
  907. *
  908. * @li @c cancellation_type::total
  909. */
  910. template <
  911. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code))
  912. ConnectToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  913. ASIO_INITFN_AUTO_RESULT_TYPE(ConnectToken,
  914. void (asio::error_code))
  915. async_connect(const endpoint_type& peer_endpoint,
  916. ASIO_MOVE_ARG(ConnectToken) token
  917. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  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 ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1741. ASIO_INITFN_AUTO_RESULT_TYPE(WaitToken,
  1742. void (asio::error_code))
  1743. async_wait(wait_type w,
  1744. ASIO_MOVE_ARG(WaitToken) token
  1745. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  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&) ASIO_DELETED;
  1775. basic_socket& operator=(const basic_socket&) ASIO_DELETED;
  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. executor_type get_executor() const ASIO_NOEXCEPT
  1785. {
  1786. return self_->get_executor();
  1787. }
  1788. template <typename ConnectHandler>
  1789. void operator()(ASIO_MOVE_ARG(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. ASIO_MOVE_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. executor_type get_executor() const ASIO_NOEXCEPT
  1822. {
  1823. return self_->get_executor();
  1824. }
  1825. template <typename WaitHandler>
  1826. void operator()(ASIO_MOVE_ARG(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