basic_socket.hpp 66 KB

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