basic_socket.hpp 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938
  1. //
  2. // basic_socket.hpp
  3. // ~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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 BOOST_ASIO_BASIC_SOCKET_HPP
  11. #define BOOST_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 <boost/asio/any_io_executor.hpp>
  17. #include <boost/asio/detail/config.hpp>
  18. #include <boost/asio/async_result.hpp>
  19. #include <boost/asio/detail/handler_type_requirements.hpp>
  20. #include <boost/asio/detail/io_object_impl.hpp>
  21. #include <boost/asio/detail/non_const_lvalue.hpp>
  22. #include <boost/asio/detail/throw_error.hpp>
  23. #include <boost/asio/detail/type_traits.hpp>
  24. #include <boost/asio/error.hpp>
  25. #include <boost/asio/execution_context.hpp>
  26. #include <boost/asio/post.hpp>
  27. #include <boost/asio/socket_base.hpp>
  28. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  29. # include <boost/asio/detail/null_socket_service.hpp>
  30. #elif defined(BOOST_ASIO_HAS_IOCP)
  31. # include <boost/asio/detail/win_iocp_socket_service.hpp>
  32. #elif defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
  33. # include <boost/asio/detail/io_uring_socket_service.hpp>
  34. #else
  35. # include <boost/asio/detail/reactive_socket_service.hpp>
  36. #endif
  37. #include <boost/asio/detail/push_options.hpp>
  38. namespace boost {
  39. namespace asio {
  40. #if !defined(BOOST_ASIO_BASIC_SOCKET_FWD_DECL)
  41. #define BOOST_ASIO_BASIC_SOCKET_FWD_DECL
  42. // Forward declaration with defaulted arguments.
  43. template <typename Protocol, typename Executor = any_io_executor>
  44. class basic_socket;
  45. #endif // !defined(BOOST_ASIO_BASIC_SOCKET_FWD_DECL)
  46. /// Provides socket functionality.
  47. /**
  48. * The basic_socket class template provides functionality that is common to both
  49. * stream-oriented and datagram-oriented sockets.
  50. *
  51. * @par Thread Safety
  52. * @e Distinct @e objects: Safe.@n
  53. * @e Shared @e objects: Unsafe.
  54. */
  55. template <typename Protocol, typename Executor>
  56. class basic_socket
  57. : public socket_base
  58. {
  59. private:
  60. class initiate_async_connect;
  61. class initiate_async_wait;
  62. public:
  63. /// The type of the executor associated with the object.
  64. typedef Executor executor_type;
  65. /// Rebinds the socket type to another executor.
  66. template <typename Executor1>
  67. struct rebind_executor
  68. {
  69. /// The socket type when rebound to the specified executor.
  70. typedef basic_socket<Protocol, Executor1> other;
  71. };
  72. /// The native representation of a socket.
  73. #if defined(GENERATING_DOCUMENTATION)
  74. typedef implementation_defined native_handle_type;
  75. #elif defined(BOOST_ASIO_WINDOWS_RUNTIME)
  76. typedef typename detail::null_socket_service<
  77. Protocol>::native_handle_type native_handle_type;
  78. #elif defined(BOOST_ASIO_HAS_IOCP)
  79. typedef typename detail::win_iocp_socket_service<
  80. Protocol>::native_handle_type native_handle_type;
  81. #elif defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
  82. typedef typename detail::io_uring_socket_service<
  83. Protocol>::native_handle_type native_handle_type;
  84. #else
  85. typedef typename detail::reactive_socket_service<
  86. Protocol>::native_handle_type native_handle_type;
  87. #endif
  88. /// The protocol type.
  89. typedef Protocol protocol_type;
  90. /// The endpoint type.
  91. typedef typename Protocol::endpoint endpoint_type;
  92. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  93. /// A basic_socket is always the lowest layer.
  94. typedef basic_socket<Protocol, Executor> lowest_layer_type;
  95. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  96. /// Construct a basic_socket without opening it.
  97. /**
  98. * This constructor creates a socket without opening it.
  99. *
  100. * @param ex The I/O executor that the socket will use, by default, to
  101. * dispatch handlers for any asynchronous operations performed on the socket.
  102. */
  103. explicit basic_socket(const executor_type& ex)
  104. : impl_(0, ex)
  105. {
  106. }
  107. /// Construct a basic_socket without opening it.
  108. /**
  109. * This constructor creates a socket without opening it.
  110. *
  111. * @param context An execution context which provides the I/O executor that
  112. * the socket will use, by default, to dispatch handlers for any asynchronous
  113. * operations performed on the socket.
  114. */
  115. template <typename ExecutionContext>
  116. explicit basic_socket(ExecutionContext& context,
  117. constraint_t<
  118. is_convertible<ExecutionContext&, execution_context&>::value
  119. > = 0)
  120. : impl_(0, 0, context)
  121. {
  122. }
  123. /// Construct and open a basic_socket.
  124. /**
  125. * This constructor creates and opens a socket.
  126. *
  127. * @param ex The I/O executor that the socket will use, by default, to
  128. * dispatch handlers for any asynchronous operations performed on the socket.
  129. *
  130. * @param protocol An object specifying protocol parameters to be used.
  131. *
  132. * @throws boost::system::system_error Thrown on failure.
  133. */
  134. basic_socket(const executor_type& ex, const protocol_type& protocol)
  135. : impl_(0, ex)
  136. {
  137. boost::system::error_code ec;
  138. impl_.get_service().open(impl_.get_implementation(), protocol, ec);
  139. boost::asio::detail::throw_error(ec, "open");
  140. }
  141. /// Construct and open a basic_socket.
  142. /**
  143. * This constructor creates and opens a socket.
  144. *
  145. * @param context An execution context which provides the I/O executor that
  146. * the socket will use, by default, to dispatch handlers for any asynchronous
  147. * operations performed on the socket.
  148. *
  149. * @param protocol An object specifying protocol parameters to be used.
  150. *
  151. * @throws boost::system::system_error Thrown on failure.
  152. */
  153. template <typename ExecutionContext>
  154. basic_socket(ExecutionContext& context, const protocol_type& protocol,
  155. constraint_t<
  156. is_convertible<ExecutionContext&, execution_context&>::value,
  157. defaulted_constraint
  158. > = defaulted_constraint())
  159. : impl_(0, 0, context)
  160. {
  161. boost::system::error_code ec;
  162. impl_.get_service().open(impl_.get_implementation(), protocol, ec);
  163. boost::asio::detail::throw_error(ec, "open");
  164. }
  165. /// Construct a basic_socket, opening it and binding it to the given local
  166. /// endpoint.
  167. /**
  168. * This constructor creates a socket and automatically opens it bound to the
  169. * specified endpoint on the local machine. The protocol used is the protocol
  170. * associated with the given endpoint.
  171. *
  172. * @param ex The I/O executor that the socket will use, by default, to
  173. * dispatch handlers for any asynchronous operations performed on the socket.
  174. *
  175. * @param endpoint An endpoint on the local machine to which the socket will
  176. * be bound.
  177. *
  178. * @throws boost::system::system_error Thrown on failure.
  179. */
  180. basic_socket(const executor_type& ex, const endpoint_type& endpoint)
  181. : impl_(0, ex)
  182. {
  183. boost::system::error_code ec;
  184. const protocol_type protocol = endpoint.protocol();
  185. impl_.get_service().open(impl_.get_implementation(), protocol, ec);
  186. boost::asio::detail::throw_error(ec, "open");
  187. impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
  188. boost::asio::detail::throw_error(ec, "bind");
  189. }
  190. /// Construct a basic_socket, opening it and binding it to the given local
  191. /// endpoint.
  192. /**
  193. * This constructor creates a socket and automatically opens it bound to the
  194. * specified endpoint on the local machine. The protocol used is the protocol
  195. * associated with the given endpoint.
  196. *
  197. * @param context An execution context which provides the I/O executor that
  198. * the socket will use, by default, to dispatch handlers for any asynchronous
  199. * operations performed on the socket.
  200. *
  201. * @param endpoint An endpoint on the local machine to which the socket will
  202. * be bound.
  203. *
  204. * @throws boost::system::system_error Thrown on failure.
  205. */
  206. template <typename ExecutionContext>
  207. basic_socket(ExecutionContext& context, const endpoint_type& endpoint,
  208. constraint_t<
  209. is_convertible<ExecutionContext&, execution_context&>::value
  210. > = 0)
  211. : impl_(0, 0, context)
  212. {
  213. boost::system::error_code ec;
  214. const protocol_type protocol = endpoint.protocol();
  215. impl_.get_service().open(impl_.get_implementation(), protocol, ec);
  216. boost::asio::detail::throw_error(ec, "open");
  217. impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
  218. boost::asio::detail::throw_error(ec, "bind");
  219. }
  220. /// Construct a basic_socket on an existing native socket.
  221. /**
  222. * This constructor creates a socket object to hold an existing native socket.
  223. *
  224. * @param ex The I/O executor that the socket will use, by default, to
  225. * dispatch handlers for any asynchronous operations performed on the socket.
  226. *
  227. * @param protocol An object specifying protocol parameters to be used.
  228. *
  229. * @param native_socket A native socket.
  230. *
  231. * @throws boost::system::system_error Thrown on failure.
  232. */
  233. basic_socket(const executor_type& ex, const protocol_type& protocol,
  234. const native_handle_type& native_socket)
  235. : impl_(0, ex)
  236. {
  237. boost::system::error_code ec;
  238. impl_.get_service().assign(impl_.get_implementation(),
  239. protocol, native_socket, ec);
  240. boost::asio::detail::throw_error(ec, "assign");
  241. }
  242. /// Construct a basic_socket on an existing native socket.
  243. /**
  244. * This constructor creates a socket object to hold an existing native socket.
  245. *
  246. * @param context An execution context which provides the I/O executor that
  247. * the socket will use, by default, to dispatch handlers for any asynchronous
  248. * operations performed on the socket.
  249. *
  250. * @param protocol An object specifying protocol parameters to be used.
  251. *
  252. * @param native_socket A native socket.
  253. *
  254. * @throws boost::system::system_error Thrown on failure.
  255. */
  256. template <typename ExecutionContext>
  257. basic_socket(ExecutionContext& context, const protocol_type& protocol,
  258. const native_handle_type& native_socket,
  259. constraint_t<
  260. is_convertible<ExecutionContext&, execution_context&>::value
  261. > = 0)
  262. : impl_(0, 0, context)
  263. {
  264. boost::system::error_code ec;
  265. impl_.get_service().assign(impl_.get_implementation(),
  266. protocol, native_socket, ec);
  267. boost::asio::detail::throw_error(ec, "assign");
  268. }
  269. /// Move-construct a basic_socket from another.
  270. /**
  271. * This constructor moves a socket from one object to another.
  272. *
  273. * @param other The other basic_socket object from which the move will
  274. * occur.
  275. *
  276. * @note Following the move, the moved-from object is in the same state as if
  277. * constructed using the @c basic_socket(const executor_type&) constructor.
  278. */
  279. basic_socket(basic_socket&& other) noexcept
  280. : impl_(std::move(other.impl_))
  281. {
  282. }
  283. /// Move-assign a basic_socket from another.
  284. /**
  285. * This assignment operator moves a socket from one object to another.
  286. *
  287. * @param other The other basic_socket object from which the move will
  288. * occur.
  289. *
  290. * @note Following the move, the moved-from object is in the same state as if
  291. * constructed using the @c basic_socket(const executor_type&) constructor.
  292. */
  293. basic_socket& operator=(basic_socket&& other)
  294. {
  295. impl_ = std::move(other.impl_);
  296. return *this;
  297. }
  298. // All sockets have access to each other's implementations.
  299. template <typename Protocol1, typename Executor1>
  300. friend class basic_socket;
  301. /// Move-construct a basic_socket from a socket of another protocol type.
  302. /**
  303. * This constructor moves a socket from one object to another.
  304. *
  305. * @param other The other basic_socket object from which the move will
  306. * occur.
  307. *
  308. * @note Following the move, the moved-from object is in the same state as if
  309. * constructed using the @c basic_socket(const executor_type&) constructor.
  310. */
  311. template <typename Protocol1, typename Executor1>
  312. basic_socket(basic_socket<Protocol1, Executor1>&& other,
  313. constraint_t<
  314. is_convertible<Protocol1, Protocol>::value
  315. && is_convertible<Executor1, Executor>::value
  316. > = 0)
  317. : impl_(std::move(other.impl_))
  318. {
  319. }
  320. /// Move-assign a basic_socket from a socket of another protocol type.
  321. /**
  322. * This assignment operator moves a socket from one object to another.
  323. *
  324. * @param other The other basic_socket object from which the move will
  325. * occur.
  326. *
  327. * @note Following the move, the moved-from object is in the same state as if
  328. * constructed using the @c basic_socket(const executor_type&) constructor.
  329. */
  330. template <typename Protocol1, typename Executor1>
  331. constraint_t<
  332. is_convertible<Protocol1, Protocol>::value
  333. && is_convertible<Executor1, Executor>::value,
  334. basic_socket&
  335. > operator=(basic_socket<Protocol1, Executor1>&& other)
  336. {
  337. basic_socket tmp(std::move(other));
  338. impl_ = std::move(tmp.impl_);
  339. return *this;
  340. }
  341. /// Get the executor associated with the object.
  342. const executor_type& get_executor() noexcept
  343. {
  344. return impl_.get_executor();
  345. }
  346. #if !defined(BOOST_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(BOOST_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 boost::system::system_error Thrown on failure.
  381. *
  382. * @par Example
  383. * @code
  384. * boost::asio::ip::tcp::socket socket(my_context);
  385. * socket.open(boost::asio::ip::tcp::v4());
  386. * @endcode
  387. */
  388. void open(const protocol_type& protocol = protocol_type())
  389. {
  390. boost::system::error_code ec;
  391. impl_.get_service().open(impl_.get_implementation(), protocol, ec);
  392. boost::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. * boost::asio::ip::tcp::socket socket(my_context);
  405. * boost::system::error_code ec;
  406. * socket.open(boost::asio::ip::tcp::v4(), ec);
  407. * if (ec)
  408. * {
  409. * // An error occurred.
  410. * }
  411. * @endcode
  412. */
  413. BOOST_ASIO_SYNC_OP_VOID open(const protocol_type& protocol,
  414. boost::system::error_code& ec)
  415. {
  416. impl_.get_service().open(impl_.get_implementation(), protocol, ec);
  417. BOOST_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 boost::system::system_error Thrown on failure.
  428. */
  429. void assign(const protocol_type& protocol,
  430. const native_handle_type& native_socket)
  431. {
  432. boost::system::error_code ec;
  433. impl_.get_service().assign(impl_.get_implementation(),
  434. protocol, native_socket, ec);
  435. boost::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. BOOST_ASIO_SYNC_OP_VOID assign(const protocol_type& protocol,
  448. const native_handle_type& native_socket, boost::system::error_code& ec)
  449. {
  450. impl_.get_service().assign(impl_.get_implementation(),
  451. protocol, native_socket, ec);
  452. BOOST_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 boost::asio::error::operation_aborted error.
  464. *
  465. * @throws boost::system::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. boost::system::error_code ec;
  474. impl_.get_service().close(impl_.get_implementation(), ec);
  475. boost::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 boost::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. * boost::asio::ip::tcp::socket socket(my_context);
  489. * ...
  490. * boost::system::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. BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec)
  502. {
  503. impl_.get_service().close(impl_.get_implementation(), ec);
  504. BOOST_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 boost::asio::error::operation_aborted error. Ownership
  511. * of the native socket is then transferred to the caller.
  512. *
  513. * @throws boost::system::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 boost::asio::error::operation_not_supported on
  517. * these platforms.
  518. */
  519. #if defined(BOOST_ASIO_MSVC) && (BOOST_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. boost::system::error_code ec;
  528. native_handle_type s = impl_.get_service().release(
  529. impl_.get_implementation(), ec);
  530. boost::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 boost::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 boost::asio::error::operation_not_supported on
  544. * these platforms.
  545. */
  546. #if defined(BOOST_ASIO_MSVC) && (BOOST_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(boost::system::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 boost::asio::error::operation_aborted error.
  571. *
  572. * @throws boost::system::system_error Thrown on failure.
  573. *
  574. * @note Calls to cancel() will always fail with
  575. * boost::asio::error::operation_not_supported when run on Windows XP, Windows
  576. * Server 2003, and earlier versions of Windows, unless
  577. * BOOST_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. * BOOST_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(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC >= 1400) \
  601. && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
  602. && !defined(BOOST_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. boost::system::error_code ec;
  610. impl_.get_service().cancel(impl_.get_implementation(), ec);
  611. boost::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 boost::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. * boost::asio::error::operation_not_supported when run on Windows XP, Windows
  623. * Server 2003, and earlier versions of Windows, unless
  624. * BOOST_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. * BOOST_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(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC >= 1400) \
  648. && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
  649. && !defined(BOOST_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. BOOST_ASIO_SYNC_OP_VOID cancel(boost::system::error_code& ec)
  655. {
  656. impl_.get_service().cancel(impl_.get_implementation(), ec);
  657. BOOST_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 boost::system::system_error Thrown on failure.
  668. */
  669. bool at_mark() const
  670. {
  671. boost::system::error_code ec;
  672. bool b = impl_.get_service().at_mark(impl_.get_implementation(), ec);
  673. boost::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(boost::system::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 boost::system::system_error Thrown on failure.
  699. */
  700. std::size_t available() const
  701. {
  702. boost::system::error_code ec;
  703. std::size_t s = impl_.get_service().available(
  704. impl_.get_implementation(), ec);
  705. boost::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(boost::system::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 boost::system::system_error Thrown on failure.
  731. *
  732. * @par Example
  733. * @code
  734. * boost::asio::ip::tcp::socket socket(my_context);
  735. * socket.open(boost::asio::ip::tcp::v4());
  736. * socket.bind(boost::asio::ip::tcp::endpoint(
  737. * boost::asio::ip::tcp::v4(), 12345));
  738. * @endcode
  739. */
  740. void bind(const endpoint_type& endpoint)
  741. {
  742. boost::system::error_code ec;
  743. impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
  744. boost::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. * boost::asio::ip::tcp::socket socket(my_context);
  759. * socket.open(boost::asio::ip::tcp::v4());
  760. * boost::system::error_code ec;
  761. * socket.bind(boost::asio::ip::tcp::endpoint(
  762. * boost::asio::ip::tcp::v4(), 12345), ec);
  763. * if (ec)
  764. * {
  765. * // An error occurred.
  766. * }
  767. * @endcode
  768. */
  769. BOOST_ASIO_SYNC_OP_VOID bind(const endpoint_type& endpoint,
  770. boost::system::error_code& ec)
  771. {
  772. impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
  773. BOOST_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 boost::system::system_error Thrown on failure.
  789. *
  790. * @par Example
  791. * @code
  792. * boost::asio::ip::tcp::socket socket(my_context);
  793. * boost::asio::ip::tcp::endpoint endpoint(
  794. * boost::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. boost::system::error_code ec;
  801. if (!is_open())
  802. {
  803. impl_.get_service().open(impl_.get_implementation(),
  804. peer_endpoint.protocol(), ec);
  805. boost::asio::detail::throw_error(ec, "connect");
  806. }
  807. impl_.get_service().connect(impl_.get_implementation(), peer_endpoint, ec);
  808. boost::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. * boost::asio::ip::tcp::socket socket(my_context);
  828. * boost::asio::ip::tcp::endpoint endpoint(
  829. * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
  830. * boost::system::error_code ec;
  831. * socket.connect(endpoint, ec);
  832. * if (ec)
  833. * {
  834. * // An error occurred.
  835. * }
  836. * @endcode
  837. */
  838. BOOST_ASIO_SYNC_OP_VOID connect(const endpoint_type& peer_endpoint,
  839. boost::system::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. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  848. }
  849. }
  850. impl_.get_service().connect(impl_.get_implementation(), peer_endpoint, ec);
  851. BOOST_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 boost::system::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 boost::asio::post().
  878. *
  879. * @par Completion Signature
  880. * @code void(boost::system::error_code) @endcode
  881. *
  882. * @par Example
  883. * @code
  884. * void connect_handler(const boost::system::error_code& error)
  885. * {
  886. * if (!error)
  887. * {
  888. * // Connect succeeded.
  889. * }
  890. * }
  891. *
  892. * ...
  893. *
  894. * boost::asio::ip::tcp::socket socket(my_context);
  895. * boost::asio::ip::tcp::endpoint endpoint(
  896. * boost::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 boost::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. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code))
  912. ConnectToken = default_completion_token_t<executor_type>>
  913. auto async_connect(const endpoint_type& peer_endpoint,
  914. ConnectToken&& token = default_completion_token_t<executor_type>())
  915. -> decltype(
  916. async_initiate<ConnectToken, void (boost::system::error_code)>(
  917. declval<initiate_async_connect>(), token,
  918. peer_endpoint, declval<boost::system::error_code&>()))
  919. {
  920. boost::system::error_code open_ec;
  921. if (!is_open())
  922. {
  923. const protocol_type protocol = peer_endpoint.protocol();
  924. impl_.get_service().open(impl_.get_implementation(), protocol, open_ec);
  925. }
  926. return async_initiate<ConnectToken, void (boost::system::error_code)>(
  927. initiate_async_connect(this), token, peer_endpoint, open_ec);
  928. }
  929. /// Set an option on the socket.
  930. /**
  931. * This function is used to set an option on the socket.
  932. *
  933. * @param option The new option value to be set on the socket.
  934. *
  935. * @throws boost::system::system_error Thrown on failure.
  936. *
  937. * @sa SettableSocketOption @n
  938. * boost::asio::socket_base::broadcast @n
  939. * boost::asio::socket_base::do_not_route @n
  940. * boost::asio::socket_base::keep_alive @n
  941. * boost::asio::socket_base::linger @n
  942. * boost::asio::socket_base::receive_buffer_size @n
  943. * boost::asio::socket_base::receive_low_watermark @n
  944. * boost::asio::socket_base::reuse_address @n
  945. * boost::asio::socket_base::send_buffer_size @n
  946. * boost::asio::socket_base::send_low_watermark @n
  947. * boost::asio::ip::multicast::join_group @n
  948. * boost::asio::ip::multicast::leave_group @n
  949. * boost::asio::ip::multicast::enable_loopback @n
  950. * boost::asio::ip::multicast::outbound_interface @n
  951. * boost::asio::ip::multicast::hops @n
  952. * boost::asio::ip::tcp::no_delay
  953. *
  954. * @par Example
  955. * Setting the IPPROTO_TCP/TCP_NODELAY option:
  956. * @code
  957. * boost::asio::ip::tcp::socket socket(my_context);
  958. * ...
  959. * boost::asio::ip::tcp::no_delay option(true);
  960. * socket.set_option(option);
  961. * @endcode
  962. */
  963. template <typename SettableSocketOption>
  964. void set_option(const SettableSocketOption& option)
  965. {
  966. boost::system::error_code ec;
  967. impl_.get_service().set_option(impl_.get_implementation(), option, ec);
  968. boost::asio::detail::throw_error(ec, "set_option");
  969. }
  970. /// Set an option on the socket.
  971. /**
  972. * This function is used to set an option on the socket.
  973. *
  974. * @param option The new option value to be set on the socket.
  975. *
  976. * @param ec Set to indicate what error occurred, if any.
  977. *
  978. * @sa SettableSocketOption @n
  979. * boost::asio::socket_base::broadcast @n
  980. * boost::asio::socket_base::do_not_route @n
  981. * boost::asio::socket_base::keep_alive @n
  982. * boost::asio::socket_base::linger @n
  983. * boost::asio::socket_base::receive_buffer_size @n
  984. * boost::asio::socket_base::receive_low_watermark @n
  985. * boost::asio::socket_base::reuse_address @n
  986. * boost::asio::socket_base::send_buffer_size @n
  987. * boost::asio::socket_base::send_low_watermark @n
  988. * boost::asio::ip::multicast::join_group @n
  989. * boost::asio::ip::multicast::leave_group @n
  990. * boost::asio::ip::multicast::enable_loopback @n
  991. * boost::asio::ip::multicast::outbound_interface @n
  992. * boost::asio::ip::multicast::hops @n
  993. * boost::asio::ip::tcp::no_delay
  994. *
  995. * @par Example
  996. * Setting the IPPROTO_TCP/TCP_NODELAY option:
  997. * @code
  998. * boost::asio::ip::tcp::socket socket(my_context);
  999. * ...
  1000. * boost::asio::ip::tcp::no_delay option(true);
  1001. * boost::system::error_code ec;
  1002. * socket.set_option(option, ec);
  1003. * if (ec)
  1004. * {
  1005. * // An error occurred.
  1006. * }
  1007. * @endcode
  1008. */
  1009. template <typename SettableSocketOption>
  1010. BOOST_ASIO_SYNC_OP_VOID set_option(const SettableSocketOption& option,
  1011. boost::system::error_code& ec)
  1012. {
  1013. impl_.get_service().set_option(impl_.get_implementation(), option, ec);
  1014. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1015. }
  1016. /// Get an option from the socket.
  1017. /**
  1018. * This function is used to get the current value of an option on the socket.
  1019. *
  1020. * @param option The option value to be obtained from the socket.
  1021. *
  1022. * @throws boost::system::system_error Thrown on failure.
  1023. *
  1024. * @sa GettableSocketOption @n
  1025. * boost::asio::socket_base::broadcast @n
  1026. * boost::asio::socket_base::do_not_route @n
  1027. * boost::asio::socket_base::keep_alive @n
  1028. * boost::asio::socket_base::linger @n
  1029. * boost::asio::socket_base::receive_buffer_size @n
  1030. * boost::asio::socket_base::receive_low_watermark @n
  1031. * boost::asio::socket_base::reuse_address @n
  1032. * boost::asio::socket_base::send_buffer_size @n
  1033. * boost::asio::socket_base::send_low_watermark @n
  1034. * boost::asio::ip::multicast::join_group @n
  1035. * boost::asio::ip::multicast::leave_group @n
  1036. * boost::asio::ip::multicast::enable_loopback @n
  1037. * boost::asio::ip::multicast::outbound_interface @n
  1038. * boost::asio::ip::multicast::hops @n
  1039. * boost::asio::ip::tcp::no_delay
  1040. *
  1041. * @par Example
  1042. * Getting the value of the SOL_SOCKET/SO_KEEPALIVE option:
  1043. * @code
  1044. * boost::asio::ip::tcp::socket socket(my_context);
  1045. * ...
  1046. * boost::asio::ip::tcp::socket::keep_alive option;
  1047. * socket.get_option(option);
  1048. * bool is_set = option.value();
  1049. * @endcode
  1050. */
  1051. template <typename GettableSocketOption>
  1052. void get_option(GettableSocketOption& option) const
  1053. {
  1054. boost::system::error_code ec;
  1055. impl_.get_service().get_option(impl_.get_implementation(), option, ec);
  1056. boost::asio::detail::throw_error(ec, "get_option");
  1057. }
  1058. /// Get an option from the socket.
  1059. /**
  1060. * This function is used to get the current value of an option on the socket.
  1061. *
  1062. * @param option The option value to be obtained from the socket.
  1063. *
  1064. * @param ec Set to indicate what error occurred, if any.
  1065. *
  1066. * @sa GettableSocketOption @n
  1067. * boost::asio::socket_base::broadcast @n
  1068. * boost::asio::socket_base::do_not_route @n
  1069. * boost::asio::socket_base::keep_alive @n
  1070. * boost::asio::socket_base::linger @n
  1071. * boost::asio::socket_base::receive_buffer_size @n
  1072. * boost::asio::socket_base::receive_low_watermark @n
  1073. * boost::asio::socket_base::reuse_address @n
  1074. * boost::asio::socket_base::send_buffer_size @n
  1075. * boost::asio::socket_base::send_low_watermark @n
  1076. * boost::asio::ip::multicast::join_group @n
  1077. * boost::asio::ip::multicast::leave_group @n
  1078. * boost::asio::ip::multicast::enable_loopback @n
  1079. * boost::asio::ip::multicast::outbound_interface @n
  1080. * boost::asio::ip::multicast::hops @n
  1081. * boost::asio::ip::tcp::no_delay
  1082. *
  1083. * @par Example
  1084. * Getting the value of the SOL_SOCKET/SO_KEEPALIVE option:
  1085. * @code
  1086. * boost::asio::ip::tcp::socket socket(my_context);
  1087. * ...
  1088. * boost::asio::ip::tcp::socket::keep_alive option;
  1089. * boost::system::error_code ec;
  1090. * socket.get_option(option, ec);
  1091. * if (ec)
  1092. * {
  1093. * // An error occurred.
  1094. * }
  1095. * bool is_set = option.value();
  1096. * @endcode
  1097. */
  1098. template <typename GettableSocketOption>
  1099. BOOST_ASIO_SYNC_OP_VOID get_option(GettableSocketOption& option,
  1100. boost::system::error_code& ec) const
  1101. {
  1102. impl_.get_service().get_option(impl_.get_implementation(), option, ec);
  1103. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1104. }
  1105. /// Perform an IO control command on the socket.
  1106. /**
  1107. * This function is used to execute an IO control command on the socket.
  1108. *
  1109. * @param command The IO control command to be performed on the socket.
  1110. *
  1111. * @throws boost::system::system_error Thrown on failure.
  1112. *
  1113. * @sa IoControlCommand @n
  1114. * boost::asio::socket_base::bytes_readable @n
  1115. * boost::asio::socket_base::non_blocking_io
  1116. *
  1117. * @par Example
  1118. * Getting the number of bytes ready to read:
  1119. * @code
  1120. * boost::asio::ip::tcp::socket socket(my_context);
  1121. * ...
  1122. * boost::asio::ip::tcp::socket::bytes_readable command;
  1123. * socket.io_control(command);
  1124. * std::size_t bytes_readable = command.get();
  1125. * @endcode
  1126. */
  1127. template <typename IoControlCommand>
  1128. void io_control(IoControlCommand& command)
  1129. {
  1130. boost::system::error_code ec;
  1131. impl_.get_service().io_control(impl_.get_implementation(), command, ec);
  1132. boost::asio::detail::throw_error(ec, "io_control");
  1133. }
  1134. /// Perform an IO control command on the socket.
  1135. /**
  1136. * This function is used to execute an IO control command on the socket.
  1137. *
  1138. * @param command The IO control command to be performed on the socket.
  1139. *
  1140. * @param ec Set to indicate what error occurred, if any.
  1141. *
  1142. * @sa IoControlCommand @n
  1143. * boost::asio::socket_base::bytes_readable @n
  1144. * boost::asio::socket_base::non_blocking_io
  1145. *
  1146. * @par Example
  1147. * Getting the number of bytes ready to read:
  1148. * @code
  1149. * boost::asio::ip::tcp::socket socket(my_context);
  1150. * ...
  1151. * boost::asio::ip::tcp::socket::bytes_readable command;
  1152. * boost::system::error_code ec;
  1153. * socket.io_control(command, ec);
  1154. * if (ec)
  1155. * {
  1156. * // An error occurred.
  1157. * }
  1158. * std::size_t bytes_readable = command.get();
  1159. * @endcode
  1160. */
  1161. template <typename IoControlCommand>
  1162. BOOST_ASIO_SYNC_OP_VOID io_control(IoControlCommand& command,
  1163. boost::system::error_code& ec)
  1164. {
  1165. impl_.get_service().io_control(impl_.get_implementation(), command, ec);
  1166. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1167. }
  1168. /// Gets the non-blocking mode of the socket.
  1169. /**
  1170. * @returns @c true if the socket's synchronous operations will fail with
  1171. * boost::asio::error::would_block if they are unable to perform the requested
  1172. * operation immediately. If @c false, synchronous operations will block
  1173. * until complete.
  1174. *
  1175. * @note The non-blocking mode has no effect on the behaviour of asynchronous
  1176. * operations. Asynchronous operations will never fail with the error
  1177. * boost::asio::error::would_block.
  1178. */
  1179. bool non_blocking() const
  1180. {
  1181. return impl_.get_service().non_blocking(impl_.get_implementation());
  1182. }
  1183. /// Sets the non-blocking mode of the socket.
  1184. /**
  1185. * @param mode If @c true, the socket's synchronous operations will fail with
  1186. * boost::asio::error::would_block if they are unable to perform the requested
  1187. * operation immediately. If @c false, synchronous operations will block
  1188. * until complete.
  1189. *
  1190. * @throws boost::system::system_error Thrown on failure.
  1191. *
  1192. * @note The non-blocking mode has no effect on the behaviour of asynchronous
  1193. * operations. Asynchronous operations will never fail with the error
  1194. * boost::asio::error::would_block.
  1195. */
  1196. void non_blocking(bool mode)
  1197. {
  1198. boost::system::error_code ec;
  1199. impl_.get_service().non_blocking(impl_.get_implementation(), mode, ec);
  1200. boost::asio::detail::throw_error(ec, "non_blocking");
  1201. }
  1202. /// Sets the non-blocking mode of the socket.
  1203. /**
  1204. * @param mode If @c true, the socket's synchronous operations will fail with
  1205. * boost::asio::error::would_block if they are unable to perform the requested
  1206. * operation immediately. If @c false, synchronous operations will block
  1207. * until complete.
  1208. *
  1209. * @param ec Set to indicate what error occurred, if any.
  1210. *
  1211. * @note The non-blocking mode has no effect on the behaviour of asynchronous
  1212. * operations. Asynchronous operations will never fail with the error
  1213. * boost::asio::error::would_block.
  1214. */
  1215. BOOST_ASIO_SYNC_OP_VOID non_blocking(
  1216. bool mode, boost::system::error_code& ec)
  1217. {
  1218. impl_.get_service().non_blocking(impl_.get_implementation(), mode, ec);
  1219. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1220. }
  1221. /// Gets the non-blocking mode of the native socket implementation.
  1222. /**
  1223. * This function is used to retrieve the non-blocking mode of the underlying
  1224. * native socket. This mode has no effect on the behaviour of the socket
  1225. * object's synchronous operations.
  1226. *
  1227. * @returns @c true if the underlying socket is in non-blocking mode and
  1228. * direct system calls may fail with boost::asio::error::would_block (or the
  1229. * equivalent system error).
  1230. *
  1231. * @note The current non-blocking mode is cached by the socket object.
  1232. * Consequently, the return value may be incorrect if the non-blocking mode
  1233. * was set directly on the native socket.
  1234. *
  1235. * @par Example
  1236. * This function is intended to allow the encapsulation of arbitrary
  1237. * non-blocking system calls as asynchronous operations, in a way that is
  1238. * transparent to the user of the socket object. The following example
  1239. * illustrates how Linux's @c sendfile system call might be encapsulated:
  1240. * @code template <typename Handler>
  1241. * struct sendfile_op
  1242. * {
  1243. * tcp::socket& sock_;
  1244. * int fd_;
  1245. * Handler handler_;
  1246. * off_t offset_;
  1247. * std::size_t total_bytes_transferred_;
  1248. *
  1249. * // Function call operator meeting WriteHandler requirements.
  1250. * // Used as the handler for the async_write_some operation.
  1251. * void operator()(boost::system::error_code ec, std::size_t)
  1252. * {
  1253. * // Put the underlying socket into non-blocking mode.
  1254. * if (!ec)
  1255. * if (!sock_.native_non_blocking())
  1256. * sock_.native_non_blocking(true, ec);
  1257. *
  1258. * if (!ec)
  1259. * {
  1260. * for (;;)
  1261. * {
  1262. * // Try the system call.
  1263. * errno = 0;
  1264. * int n = ::sendfile(sock_.native_handle(), fd_, &offset_, 65536);
  1265. * ec = boost::system::error_code(n < 0 ? errno : 0,
  1266. * boost::asio::error::get_system_category());
  1267. * total_bytes_transferred_ += ec ? 0 : n;
  1268. *
  1269. * // Retry operation immediately if interrupted by signal.
  1270. * if (ec == boost::asio::error::interrupted)
  1271. * continue;
  1272. *
  1273. * // Check if we need to run the operation again.
  1274. * if (ec == boost::asio::error::would_block
  1275. * || ec == boost::asio::error::try_again)
  1276. * {
  1277. * // We have to wait for the socket to become ready again.
  1278. * sock_.async_wait(tcp::socket::wait_write, *this);
  1279. * return;
  1280. * }
  1281. *
  1282. * if (ec || n == 0)
  1283. * {
  1284. * // An error occurred, or we have reached the end of the file.
  1285. * // Either way we must exit the loop so we can call the handler.
  1286. * break;
  1287. * }
  1288. *
  1289. * // Loop around to try calling sendfile again.
  1290. * }
  1291. * }
  1292. *
  1293. * // Pass result back to user's handler.
  1294. * handler_(ec, total_bytes_transferred_);
  1295. * }
  1296. * };
  1297. *
  1298. * template <typename Handler>
  1299. * void async_sendfile(tcp::socket& sock, int fd, Handler h)
  1300. * {
  1301. * sendfile_op<Handler> op = { sock, fd, h, 0, 0 };
  1302. * sock.async_wait(tcp::socket::wait_write, op);
  1303. * } @endcode
  1304. */
  1305. bool native_non_blocking() const
  1306. {
  1307. return impl_.get_service().native_non_blocking(impl_.get_implementation());
  1308. }
  1309. /// Sets the non-blocking mode of the native socket implementation.
  1310. /**
  1311. * This function is used to modify the non-blocking mode of the underlying
  1312. * native socket. It has no effect on the behaviour of the socket object's
  1313. * synchronous operations.
  1314. *
  1315. * @param mode If @c true, the underlying socket is put into non-blocking
  1316. * mode and direct system calls may fail with boost::asio::error::would_block
  1317. * (or the equivalent system error).
  1318. *
  1319. * @throws boost::system::system_error Thrown on failure. If the @c mode is
  1320. * @c false, but the current value of @c non_blocking() is @c true, this
  1321. * function fails with boost::asio::error::invalid_argument, as the
  1322. * combination does not make sense.
  1323. *
  1324. * @par Example
  1325. * This function is intended to allow the encapsulation of arbitrary
  1326. * non-blocking system calls as asynchronous operations, in a way that is
  1327. * transparent to the user of the socket object. The following example
  1328. * illustrates how Linux's @c sendfile system call might be encapsulated:
  1329. * @code template <typename Handler>
  1330. * struct sendfile_op
  1331. * {
  1332. * tcp::socket& sock_;
  1333. * int fd_;
  1334. * Handler handler_;
  1335. * off_t offset_;
  1336. * std::size_t total_bytes_transferred_;
  1337. *
  1338. * // Function call operator meeting WriteHandler requirements.
  1339. * // Used as the handler for the async_write_some operation.
  1340. * void operator()(boost::system::error_code ec, std::size_t)
  1341. * {
  1342. * // Put the underlying socket into non-blocking mode.
  1343. * if (!ec)
  1344. * if (!sock_.native_non_blocking())
  1345. * sock_.native_non_blocking(true, ec);
  1346. *
  1347. * if (!ec)
  1348. * {
  1349. * for (;;)
  1350. * {
  1351. * // Try the system call.
  1352. * errno = 0;
  1353. * int n = ::sendfile(sock_.native_handle(), fd_, &offset_, 65536);
  1354. * ec = boost::system::error_code(n < 0 ? errno : 0,
  1355. * boost::asio::error::get_system_category());
  1356. * total_bytes_transferred_ += ec ? 0 : n;
  1357. *
  1358. * // Retry operation immediately if interrupted by signal.
  1359. * if (ec == boost::asio::error::interrupted)
  1360. * continue;
  1361. *
  1362. * // Check if we need to run the operation again.
  1363. * if (ec == boost::asio::error::would_block
  1364. * || ec == boost::asio::error::try_again)
  1365. * {
  1366. * // We have to wait for the socket to become ready again.
  1367. * sock_.async_wait(tcp::socket::wait_write, *this);
  1368. * return;
  1369. * }
  1370. *
  1371. * if (ec || n == 0)
  1372. * {
  1373. * // An error occurred, or we have reached the end of the file.
  1374. * // Either way we must exit the loop so we can call the handler.
  1375. * break;
  1376. * }
  1377. *
  1378. * // Loop around to try calling sendfile again.
  1379. * }
  1380. * }
  1381. *
  1382. * // Pass result back to user's handler.
  1383. * handler_(ec, total_bytes_transferred_);
  1384. * }
  1385. * };
  1386. *
  1387. * template <typename Handler>
  1388. * void async_sendfile(tcp::socket& sock, int fd, Handler h)
  1389. * {
  1390. * sendfile_op<Handler> op = { sock, fd, h, 0, 0 };
  1391. * sock.async_wait(tcp::socket::wait_write, op);
  1392. * } @endcode
  1393. */
  1394. void native_non_blocking(bool mode)
  1395. {
  1396. boost::system::error_code ec;
  1397. impl_.get_service().native_non_blocking(
  1398. impl_.get_implementation(), mode, ec);
  1399. boost::asio::detail::throw_error(ec, "native_non_blocking");
  1400. }
  1401. /// Sets the non-blocking mode of the native socket implementation.
  1402. /**
  1403. * This function is used to modify the non-blocking mode of the underlying
  1404. * native socket. It has no effect on the behaviour of the socket object's
  1405. * synchronous operations.
  1406. *
  1407. * @param mode If @c true, the underlying socket is put into non-blocking
  1408. * mode and direct system calls may fail with boost::asio::error::would_block
  1409. * (or the equivalent system error).
  1410. *
  1411. * @param ec Set to indicate what error occurred, if any. If the @c mode is
  1412. * @c false, but the current value of @c non_blocking() is @c true, this
  1413. * function fails with boost::asio::error::invalid_argument, as the
  1414. * combination does not make sense.
  1415. *
  1416. * @par Example
  1417. * This function is intended to allow the encapsulation of arbitrary
  1418. * non-blocking system calls as asynchronous operations, in a way that is
  1419. * transparent to the user of the socket object. The following example
  1420. * illustrates how Linux's @c sendfile system call might be encapsulated:
  1421. * @code template <typename Handler>
  1422. * struct sendfile_op
  1423. * {
  1424. * tcp::socket& sock_;
  1425. * int fd_;
  1426. * Handler handler_;
  1427. * off_t offset_;
  1428. * std::size_t total_bytes_transferred_;
  1429. *
  1430. * // Function call operator meeting WriteHandler requirements.
  1431. * // Used as the handler for the async_write_some operation.
  1432. * void operator()(boost::system::error_code ec, std::size_t)
  1433. * {
  1434. * // Put the underlying socket into non-blocking mode.
  1435. * if (!ec)
  1436. * if (!sock_.native_non_blocking())
  1437. * sock_.native_non_blocking(true, ec);
  1438. *
  1439. * if (!ec)
  1440. * {
  1441. * for (;;)
  1442. * {
  1443. * // Try the system call.
  1444. * errno = 0;
  1445. * int n = ::sendfile(sock_.native_handle(), fd_, &offset_, 65536);
  1446. * ec = boost::system::error_code(n < 0 ? errno : 0,
  1447. * boost::asio::error::get_system_category());
  1448. * total_bytes_transferred_ += ec ? 0 : n;
  1449. *
  1450. * // Retry operation immediately if interrupted by signal.
  1451. * if (ec == boost::asio::error::interrupted)
  1452. * continue;
  1453. *
  1454. * // Check if we need to run the operation again.
  1455. * if (ec == boost::asio::error::would_block
  1456. * || ec == boost::asio::error::try_again)
  1457. * {
  1458. * // We have to wait for the socket to become ready again.
  1459. * sock_.async_wait(tcp::socket::wait_write, *this);
  1460. * return;
  1461. * }
  1462. *
  1463. * if (ec || n == 0)
  1464. * {
  1465. * // An error occurred, or we have reached the end of the file.
  1466. * // Either way we must exit the loop so we can call the handler.
  1467. * break;
  1468. * }
  1469. *
  1470. * // Loop around to try calling sendfile again.
  1471. * }
  1472. * }
  1473. *
  1474. * // Pass result back to user's handler.
  1475. * handler_(ec, total_bytes_transferred_);
  1476. * }
  1477. * };
  1478. *
  1479. * template <typename Handler>
  1480. * void async_sendfile(tcp::socket& sock, int fd, Handler h)
  1481. * {
  1482. * sendfile_op<Handler> op = { sock, fd, h, 0, 0 };
  1483. * sock.async_wait(tcp::socket::wait_write, op);
  1484. * } @endcode
  1485. */
  1486. BOOST_ASIO_SYNC_OP_VOID native_non_blocking(
  1487. bool mode, boost::system::error_code& ec)
  1488. {
  1489. impl_.get_service().native_non_blocking(
  1490. impl_.get_implementation(), mode, ec);
  1491. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1492. }
  1493. /// Get the local endpoint of the socket.
  1494. /**
  1495. * This function is used to obtain the locally bound endpoint of the socket.
  1496. *
  1497. * @returns An object that represents the local endpoint of the socket.
  1498. *
  1499. * @throws boost::system::system_error Thrown on failure.
  1500. *
  1501. * @par Example
  1502. * @code
  1503. * boost::asio::ip::tcp::socket socket(my_context);
  1504. * ...
  1505. * boost::asio::ip::tcp::endpoint endpoint = socket.local_endpoint();
  1506. * @endcode
  1507. */
  1508. endpoint_type local_endpoint() const
  1509. {
  1510. boost::system::error_code ec;
  1511. endpoint_type ep = impl_.get_service().local_endpoint(
  1512. impl_.get_implementation(), ec);
  1513. boost::asio::detail::throw_error(ec, "local_endpoint");
  1514. return ep;
  1515. }
  1516. /// Get the local endpoint of the socket.
  1517. /**
  1518. * This function is used to obtain the locally bound endpoint of the socket.
  1519. *
  1520. * @param ec Set to indicate what error occurred, if any.
  1521. *
  1522. * @returns An object that represents the local endpoint of the socket.
  1523. * Returns a default-constructed endpoint object if an error occurred.
  1524. *
  1525. * @par Example
  1526. * @code
  1527. * boost::asio::ip::tcp::socket socket(my_context);
  1528. * ...
  1529. * boost::system::error_code ec;
  1530. * boost::asio::ip::tcp::endpoint endpoint = socket.local_endpoint(ec);
  1531. * if (ec)
  1532. * {
  1533. * // An error occurred.
  1534. * }
  1535. * @endcode
  1536. */
  1537. endpoint_type local_endpoint(boost::system::error_code& ec) const
  1538. {
  1539. return impl_.get_service().local_endpoint(impl_.get_implementation(), ec);
  1540. }
  1541. /// Get the remote endpoint of the socket.
  1542. /**
  1543. * This function is used to obtain the remote endpoint of the socket.
  1544. *
  1545. * @returns An object that represents the remote endpoint of the socket.
  1546. *
  1547. * @throws boost::system::system_error Thrown on failure.
  1548. *
  1549. * @par Example
  1550. * @code
  1551. * boost::asio::ip::tcp::socket socket(my_context);
  1552. * ...
  1553. * boost::asio::ip::tcp::endpoint endpoint = socket.remote_endpoint();
  1554. * @endcode
  1555. */
  1556. endpoint_type remote_endpoint() const
  1557. {
  1558. boost::system::error_code ec;
  1559. endpoint_type ep = impl_.get_service().remote_endpoint(
  1560. impl_.get_implementation(), ec);
  1561. boost::asio::detail::throw_error(ec, "remote_endpoint");
  1562. return ep;
  1563. }
  1564. /// Get the remote endpoint of the socket.
  1565. /**
  1566. * This function is used to obtain the remote endpoint of the socket.
  1567. *
  1568. * @param ec Set to indicate what error occurred, if any.
  1569. *
  1570. * @returns An object that represents the remote endpoint of the socket.
  1571. * Returns a default-constructed endpoint object if an error occurred.
  1572. *
  1573. * @par Example
  1574. * @code
  1575. * boost::asio::ip::tcp::socket socket(my_context);
  1576. * ...
  1577. * boost::system::error_code ec;
  1578. * boost::asio::ip::tcp::endpoint endpoint = socket.remote_endpoint(ec);
  1579. * if (ec)
  1580. * {
  1581. * // An error occurred.
  1582. * }
  1583. * @endcode
  1584. */
  1585. endpoint_type remote_endpoint(boost::system::error_code& ec) const
  1586. {
  1587. return impl_.get_service().remote_endpoint(impl_.get_implementation(), ec);
  1588. }
  1589. /// Disable sends or receives on the socket.
  1590. /**
  1591. * This function is used to disable send operations, receive operations, or
  1592. * both.
  1593. *
  1594. * @param what Determines what types of operation will no longer be allowed.
  1595. *
  1596. * @throws boost::system::system_error Thrown on failure.
  1597. *
  1598. * @par Example
  1599. * Shutting down the send side of the socket:
  1600. * @code
  1601. * boost::asio::ip::tcp::socket socket(my_context);
  1602. * ...
  1603. * socket.shutdown(boost::asio::ip::tcp::socket::shutdown_send);
  1604. * @endcode
  1605. */
  1606. void shutdown(shutdown_type what)
  1607. {
  1608. boost::system::error_code ec;
  1609. impl_.get_service().shutdown(impl_.get_implementation(), what, ec);
  1610. boost::asio::detail::throw_error(ec, "shutdown");
  1611. }
  1612. /// Disable sends or receives on the socket.
  1613. /**
  1614. * This function is used to disable send operations, receive operations, or
  1615. * both.
  1616. *
  1617. * @param what Determines what types of operation will no longer be allowed.
  1618. *
  1619. * @param ec Set to indicate what error occurred, if any.
  1620. *
  1621. * @par Example
  1622. * Shutting down the send side of the socket:
  1623. * @code
  1624. * boost::asio::ip::tcp::socket socket(my_context);
  1625. * ...
  1626. * boost::system::error_code ec;
  1627. * socket.shutdown(boost::asio::ip::tcp::socket::shutdown_send, ec);
  1628. * if (ec)
  1629. * {
  1630. * // An error occurred.
  1631. * }
  1632. * @endcode
  1633. */
  1634. BOOST_ASIO_SYNC_OP_VOID shutdown(shutdown_type what,
  1635. boost::system::error_code& ec)
  1636. {
  1637. impl_.get_service().shutdown(impl_.get_implementation(), what, ec);
  1638. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1639. }
  1640. /// Wait for the socket to become ready to read, ready to write, or to have
  1641. /// pending error conditions.
  1642. /**
  1643. * This function is used to perform a blocking wait for a socket to enter
  1644. * a ready to read, write or error condition state.
  1645. *
  1646. * @param w Specifies the desired socket state.
  1647. *
  1648. * @par Example
  1649. * Waiting for a socket to become readable.
  1650. * @code
  1651. * boost::asio::ip::tcp::socket socket(my_context);
  1652. * ...
  1653. * socket.wait(boost::asio::ip::tcp::socket::wait_read);
  1654. * @endcode
  1655. */
  1656. void wait(wait_type w)
  1657. {
  1658. boost::system::error_code ec;
  1659. impl_.get_service().wait(impl_.get_implementation(), w, ec);
  1660. boost::asio::detail::throw_error(ec, "wait");
  1661. }
  1662. /// Wait for the socket to become ready to read, ready to write, or to have
  1663. /// pending error conditions.
  1664. /**
  1665. * This function is used to perform a blocking wait for a socket to enter
  1666. * a ready to read, write or error condition state.
  1667. *
  1668. * @param w Specifies the desired socket state.
  1669. *
  1670. * @param ec Set to indicate what error occurred, if any.
  1671. *
  1672. * @par Example
  1673. * Waiting for a socket to become readable.
  1674. * @code
  1675. * boost::asio::ip::tcp::socket socket(my_context);
  1676. * ...
  1677. * boost::system::error_code ec;
  1678. * socket.wait(boost::asio::ip::tcp::socket::wait_read, ec);
  1679. * @endcode
  1680. */
  1681. BOOST_ASIO_SYNC_OP_VOID wait(wait_type w, boost::system::error_code& ec)
  1682. {
  1683. impl_.get_service().wait(impl_.get_implementation(), w, ec);
  1684. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1685. }
  1686. /// Asynchronously wait for the socket to become ready to read, ready to
  1687. /// write, or to have pending error conditions.
  1688. /**
  1689. * This function is used to perform an asynchronous wait for a socket to enter
  1690. * a ready to read, write or error condition state. It is an initiating
  1691. * function for an @ref asynchronous_operation, and always returns
  1692. * immediately.
  1693. *
  1694. * @param w Specifies the desired socket state.
  1695. *
  1696. * @param token The @ref completion_token that will be used to produce a
  1697. * completion handler, which will be called when the wait completes. Potential
  1698. * completion tokens include @ref use_future, @ref use_awaitable, @ref
  1699. * yield_context, or a function object with the correct completion signature.
  1700. * The function signature of the completion handler must be:
  1701. * @code void handler(
  1702. * const boost::system::error_code& error // Result of operation.
  1703. * ); @endcode
  1704. * Regardless of whether the asynchronous operation completes immediately or
  1705. * not, the completion handler will not be invoked from within this function.
  1706. * On immediate completion, invocation of the handler will be performed in a
  1707. * manner equivalent to using boost::asio::post().
  1708. *
  1709. * @par Completion Signature
  1710. * @code void(boost::system::error_code) @endcode
  1711. *
  1712. * @par Example
  1713. * @code
  1714. * void wait_handler(const boost::system::error_code& error)
  1715. * {
  1716. * if (!error)
  1717. * {
  1718. * // Wait succeeded.
  1719. * }
  1720. * }
  1721. *
  1722. * ...
  1723. *
  1724. * boost::asio::ip::tcp::socket socket(my_context);
  1725. * ...
  1726. * socket.async_wait(boost::asio::ip::tcp::socket::wait_read, wait_handler);
  1727. * @endcode
  1728. *
  1729. * @par Per-Operation Cancellation
  1730. * On POSIX or Windows operating systems, this asynchronous operation supports
  1731. * cancellation for the following boost::asio::cancellation_type values:
  1732. *
  1733. * @li @c cancellation_type::terminal
  1734. *
  1735. * @li @c cancellation_type::partial
  1736. *
  1737. * @li @c cancellation_type::total
  1738. */
  1739. template <
  1740. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code))
  1741. WaitToken = default_completion_token_t<executor_type>>
  1742. auto async_wait(wait_type w,
  1743. WaitToken&& token = default_completion_token_t<executor_type>())
  1744. -> decltype(
  1745. async_initiate<WaitToken, void (boost::system::error_code)>(
  1746. declval<initiate_async_wait>(), token, w))
  1747. {
  1748. return async_initiate<WaitToken, void (boost::system::error_code)>(
  1749. initiate_async_wait(this), token, w);
  1750. }
  1751. protected:
  1752. /// Protected destructor to prevent deletion through this type.
  1753. /**
  1754. * This function destroys the socket, cancelling any outstanding asynchronous
  1755. * operations associated with the socket as if by calling @c cancel.
  1756. */
  1757. ~basic_socket()
  1758. {
  1759. }
  1760. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  1761. detail::io_object_impl<
  1762. detail::null_socket_service<Protocol>, Executor> impl_;
  1763. #elif defined(BOOST_ASIO_HAS_IOCP)
  1764. detail::io_object_impl<
  1765. detail::win_iocp_socket_service<Protocol>, Executor> impl_;
  1766. #elif defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
  1767. detail::io_object_impl<
  1768. detail::io_uring_socket_service<Protocol>, Executor> impl_;
  1769. #else
  1770. detail::io_object_impl<
  1771. detail::reactive_socket_service<Protocol>, Executor> impl_;
  1772. #endif
  1773. private:
  1774. // Disallow copying and assignment.
  1775. basic_socket(const basic_socket&) = delete;
  1776. basic_socket& operator=(const basic_socket&) = delete;
  1777. class initiate_async_connect
  1778. {
  1779. public:
  1780. typedef Executor executor_type;
  1781. explicit initiate_async_connect(basic_socket* self)
  1782. : self_(self)
  1783. {
  1784. }
  1785. const executor_type& get_executor() const noexcept
  1786. {
  1787. return self_->get_executor();
  1788. }
  1789. template <typename ConnectHandler>
  1790. void operator()(ConnectHandler&& handler,
  1791. const endpoint_type& peer_endpoint,
  1792. const boost::system::error_code& open_ec) const
  1793. {
  1794. // If you get an error on the following line it means that your handler
  1795. // does not meet the documented type requirements for a ConnectHandler.
  1796. BOOST_ASIO_CONNECT_HANDLER_CHECK(ConnectHandler, handler) type_check;
  1797. if (open_ec)
  1798. {
  1799. boost::asio::post(self_->impl_.get_executor(),
  1800. boost::asio::detail::bind_handler(
  1801. static_cast<ConnectHandler&&>(handler), open_ec));
  1802. }
  1803. else
  1804. {
  1805. detail::non_const_lvalue<ConnectHandler> handler2(handler);
  1806. self_->impl_.get_service().async_connect(
  1807. self_->impl_.get_implementation(), peer_endpoint,
  1808. handler2.value, self_->impl_.get_executor());
  1809. }
  1810. }
  1811. private:
  1812. basic_socket* self_;
  1813. };
  1814. class initiate_async_wait
  1815. {
  1816. public:
  1817. typedef Executor executor_type;
  1818. explicit initiate_async_wait(basic_socket* self)
  1819. : self_(self)
  1820. {
  1821. }
  1822. const executor_type& get_executor() const noexcept
  1823. {
  1824. return self_->get_executor();
  1825. }
  1826. template <typename WaitHandler>
  1827. void operator()(WaitHandler&& handler, wait_type w) const
  1828. {
  1829. // If you get an error on the following line it means that your handler
  1830. // does not meet the documented type requirements for a WaitHandler.
  1831. BOOST_ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
  1832. detail::non_const_lvalue<WaitHandler> handler2(handler);
  1833. self_->impl_.get_service().async_wait(
  1834. self_->impl_.get_implementation(), w,
  1835. handler2.value, self_->impl_.get_executor());
  1836. }
  1837. private:
  1838. basic_socket* self_;
  1839. };
  1840. };
  1841. } // namespace asio
  1842. } // namespace boost
  1843. #include <boost/asio/detail/pop_options.hpp>
  1844. #endif // BOOST_ASIO_BASIC_SOCKET_HPP