basic_socket.hpp 66 KB

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