basic_socket_acceptor.hpp 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708
  1. //
  2. // basic_socket_acceptor.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_BASIC_SOCKET_ACCEPTOR_HPP
  11. #define ASIO_BASIC_SOCKET_ACCEPTOR_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <utility>
  16. #include "asio/detail/config.hpp"
  17. #include "asio/any_io_executor.hpp"
  18. #include "asio/basic_socket.hpp"
  19. #include "asio/detail/handler_type_requirements.hpp"
  20. #include "asio/detail/io_object_impl.hpp"
  21. #include "asio/detail/non_const_lvalue.hpp"
  22. #include "asio/detail/throw_error.hpp"
  23. #include "asio/detail/type_traits.hpp"
  24. #include "asio/error.hpp"
  25. #include "asio/execution_context.hpp"
  26. #include "asio/socket_base.hpp"
  27. #if defined(ASIO_WINDOWS_RUNTIME)
  28. # include "asio/detail/null_socket_service.hpp"
  29. #elif defined(ASIO_HAS_IOCP)
  30. # include "asio/detail/win_iocp_socket_service.hpp"
  31. #elif defined(ASIO_HAS_IO_URING_AS_DEFAULT)
  32. # include "asio/detail/io_uring_socket_service.hpp"
  33. #else
  34. # include "asio/detail/reactive_socket_service.hpp"
  35. #endif
  36. #include "asio/detail/push_options.hpp"
  37. namespace asio {
  38. #if !defined(ASIO_BASIC_SOCKET_ACCEPTOR_FWD_DECL)
  39. #define ASIO_BASIC_SOCKET_ACCEPTOR_FWD_DECL
  40. // Forward declaration with defaulted arguments.
  41. template <typename Protocol, typename Executor = any_io_executor>
  42. class basic_socket_acceptor;
  43. #endif // !defined(ASIO_BASIC_SOCKET_ACCEPTOR_FWD_DECL)
  44. /// Provides the ability to accept new connections.
  45. /**
  46. * The basic_socket_acceptor class template is used for accepting new socket
  47. * connections.
  48. *
  49. * @par Thread Safety
  50. * @e Distinct @e objects: Safe.@n
  51. * @e Shared @e objects: Unsafe.
  52. *
  53. * Synchronous @c accept operations are thread safe, if the underlying
  54. * operating system calls are also thread safe. This means that it is permitted
  55. * to perform concurrent calls to synchronous @c accept operations on a single
  56. * socket object. Other synchronous operations, such as @c open or @c close, are
  57. * not thread safe.
  58. *
  59. * @par Example
  60. * Opening a socket acceptor with the SO_REUSEADDR option enabled:
  61. * @code
  62. * asio::ip::tcp::acceptor acceptor(my_context);
  63. * asio::ip::tcp::endpoint endpoint(asio::ip::tcp::v4(), port);
  64. * acceptor.open(endpoint.protocol());
  65. * acceptor.set_option(asio::ip::tcp::acceptor::reuse_address(true));
  66. * acceptor.bind(endpoint);
  67. * acceptor.listen();
  68. * @endcode
  69. */
  70. template <typename Protocol, typename Executor>
  71. class basic_socket_acceptor
  72. : public socket_base
  73. {
  74. private:
  75. class initiate_async_wait;
  76. class initiate_async_accept;
  77. class initiate_async_move_accept;
  78. public:
  79. /// The type of the executor associated with the object.
  80. typedef Executor executor_type;
  81. /// Rebinds the acceptor type to another executor.
  82. template <typename Executor1>
  83. struct rebind_executor
  84. {
  85. /// The socket type when rebound to the specified executor.
  86. typedef basic_socket_acceptor<Protocol, Executor1> other;
  87. };
  88. /// The native representation of an acceptor.
  89. #if defined(GENERATING_DOCUMENTATION)
  90. typedef implementation_defined native_handle_type;
  91. #elif defined(ASIO_WINDOWS_RUNTIME)
  92. typedef typename detail::null_socket_service<
  93. Protocol>::native_handle_type native_handle_type;
  94. #elif defined(ASIO_HAS_IOCP)
  95. typedef typename detail::win_iocp_socket_service<
  96. Protocol>::native_handle_type native_handle_type;
  97. #elif defined(ASIO_HAS_IO_URING_AS_DEFAULT)
  98. typedef typename detail::io_uring_socket_service<
  99. Protocol>::native_handle_type native_handle_type;
  100. #else
  101. typedef typename detail::reactive_socket_service<
  102. Protocol>::native_handle_type native_handle_type;
  103. #endif
  104. /// The protocol type.
  105. typedef Protocol protocol_type;
  106. /// The endpoint type.
  107. typedef typename Protocol::endpoint endpoint_type;
  108. /// Construct an acceptor without opening it.
  109. /**
  110. * This constructor creates an acceptor without opening it to listen for new
  111. * connections. The open() function must be called before the acceptor can
  112. * accept new socket connections.
  113. *
  114. * @param ex The I/O executor that the acceptor will use, by default, to
  115. * dispatch handlers for any asynchronous operations performed on the
  116. * acceptor.
  117. */
  118. explicit basic_socket_acceptor(const executor_type& ex)
  119. : impl_(0, ex)
  120. {
  121. }
  122. /// Construct an acceptor without opening it.
  123. /**
  124. * This constructor creates an acceptor without opening it to listen for new
  125. * connections. The open() function must be called before the acceptor can
  126. * accept new socket connections.
  127. *
  128. * @param context An execution context which provides the I/O executor that
  129. * the acceptor will use, by default, to dispatch handlers for any
  130. * asynchronous operations performed on the acceptor.
  131. */
  132. template <typename ExecutionContext>
  133. explicit basic_socket_acceptor(ExecutionContext& context,
  134. constraint_t<
  135. is_convertible<ExecutionContext&, execution_context&>::value
  136. > = 0)
  137. : impl_(0, 0, context)
  138. {
  139. }
  140. /// Construct an open acceptor.
  141. /**
  142. * This constructor creates an acceptor and automatically opens it.
  143. *
  144. * @param ex The I/O executor that the acceptor will use, by default, to
  145. * dispatch handlers for any asynchronous operations performed on the
  146. * acceptor.
  147. *
  148. * @param protocol An object specifying protocol parameters to be used.
  149. *
  150. * @throws asio::system_error Thrown on failure.
  151. */
  152. basic_socket_acceptor(const executor_type& ex, const protocol_type& protocol)
  153. : impl_(0, ex)
  154. {
  155. asio::error_code ec;
  156. impl_.get_service().open(impl_.get_implementation(), protocol, ec);
  157. asio::detail::throw_error(ec, "open");
  158. }
  159. /// Construct an open acceptor.
  160. /**
  161. * This constructor creates an acceptor and automatically opens it.
  162. *
  163. * @param context An execution context which provides the I/O executor that
  164. * the acceptor will use, by default, to dispatch handlers for any
  165. * asynchronous operations performed on the acceptor.
  166. *
  167. * @param protocol An object specifying protocol parameters to be used.
  168. *
  169. * @throws asio::system_error Thrown on failure.
  170. */
  171. template <typename ExecutionContext>
  172. basic_socket_acceptor(ExecutionContext& context,
  173. const protocol_type& protocol,
  174. constraint_t<
  175. is_convertible<ExecutionContext&, execution_context&>::value,
  176. defaulted_constraint
  177. > = defaulted_constraint())
  178. : impl_(0, 0, context)
  179. {
  180. asio::error_code ec;
  181. impl_.get_service().open(impl_.get_implementation(), protocol, ec);
  182. asio::detail::throw_error(ec, "open");
  183. }
  184. /// Construct an acceptor opened on the given endpoint.
  185. /**
  186. * This constructor creates an acceptor and automatically opens it to listen
  187. * for new connections on the specified endpoint.
  188. *
  189. * @param ex The I/O executor that the acceptor will use, by default, to
  190. * dispatch handlers for any asynchronous operations performed on the
  191. * acceptor.
  192. *
  193. * @param endpoint An endpoint on the local machine on which the acceptor
  194. * will listen for new connections.
  195. *
  196. * @param reuse_addr Whether the constructor should set the socket option
  197. * socket_base::reuse_address.
  198. *
  199. * @throws asio::system_error Thrown on failure.
  200. *
  201. * @note This constructor is equivalent to the following code:
  202. * @code
  203. * basic_socket_acceptor<Protocol> acceptor(my_context);
  204. * acceptor.open(endpoint.protocol());
  205. * if (reuse_addr)
  206. * acceptor.set_option(socket_base::reuse_address(true));
  207. * acceptor.bind(endpoint);
  208. * acceptor.listen();
  209. * @endcode
  210. */
  211. basic_socket_acceptor(const executor_type& ex,
  212. const endpoint_type& endpoint, bool reuse_addr = true)
  213. : impl_(0, ex)
  214. {
  215. asio::error_code ec;
  216. const protocol_type protocol = endpoint.protocol();
  217. impl_.get_service().open(impl_.get_implementation(), protocol, ec);
  218. asio::detail::throw_error(ec, "open");
  219. if (reuse_addr)
  220. {
  221. impl_.get_service().set_option(impl_.get_implementation(),
  222. socket_base::reuse_address(true), ec);
  223. asio::detail::throw_error(ec, "set_option");
  224. }
  225. impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
  226. asio::detail::throw_error(ec, "bind");
  227. impl_.get_service().listen(impl_.get_implementation(),
  228. socket_base::max_listen_connections, ec);
  229. asio::detail::throw_error(ec, "listen");
  230. }
  231. /// Construct an acceptor opened on the given endpoint.
  232. /**
  233. * This constructor creates an acceptor and automatically opens it to listen
  234. * for new connections on the specified endpoint.
  235. *
  236. * @param context An execution context which provides the I/O executor that
  237. * the acceptor will use, by default, to dispatch handlers for any
  238. * asynchronous operations performed on the acceptor.
  239. *
  240. * @param endpoint An endpoint on the local machine on which the acceptor
  241. * will listen for new connections.
  242. *
  243. * @param reuse_addr Whether the constructor should set the socket option
  244. * socket_base::reuse_address.
  245. *
  246. * @throws asio::system_error Thrown on failure.
  247. *
  248. * @note This constructor is equivalent to the following code:
  249. * @code
  250. * basic_socket_acceptor<Protocol> acceptor(my_context);
  251. * acceptor.open(endpoint.protocol());
  252. * if (reuse_addr)
  253. * acceptor.set_option(socket_base::reuse_address(true));
  254. * acceptor.bind(endpoint);
  255. * acceptor.listen();
  256. * @endcode
  257. */
  258. template <typename ExecutionContext>
  259. basic_socket_acceptor(ExecutionContext& context,
  260. const endpoint_type& endpoint, bool reuse_addr = true,
  261. constraint_t<
  262. is_convertible<ExecutionContext&, execution_context&>::value
  263. > = 0)
  264. : impl_(0, 0, context)
  265. {
  266. asio::error_code ec;
  267. const protocol_type protocol = endpoint.protocol();
  268. impl_.get_service().open(impl_.get_implementation(), protocol, ec);
  269. asio::detail::throw_error(ec, "open");
  270. if (reuse_addr)
  271. {
  272. impl_.get_service().set_option(impl_.get_implementation(),
  273. socket_base::reuse_address(true), ec);
  274. asio::detail::throw_error(ec, "set_option");
  275. }
  276. impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
  277. asio::detail::throw_error(ec, "bind");
  278. impl_.get_service().listen(impl_.get_implementation(),
  279. socket_base::max_listen_connections, ec);
  280. asio::detail::throw_error(ec, "listen");
  281. }
  282. /// Construct a basic_socket_acceptor on an existing native acceptor.
  283. /**
  284. * This constructor creates an acceptor object to hold an existing native
  285. * acceptor.
  286. *
  287. * @param ex The I/O executor that the acceptor will use, by default, to
  288. * dispatch handlers for any asynchronous operations performed on the
  289. * acceptor.
  290. *
  291. * @param protocol An object specifying protocol parameters to be used.
  292. *
  293. * @param native_acceptor A native acceptor.
  294. *
  295. * @throws asio::system_error Thrown on failure.
  296. */
  297. basic_socket_acceptor(const executor_type& ex,
  298. const protocol_type& protocol, const native_handle_type& native_acceptor)
  299. : impl_(0, ex)
  300. {
  301. asio::error_code ec;
  302. impl_.get_service().assign(impl_.get_implementation(),
  303. protocol, native_acceptor, ec);
  304. asio::detail::throw_error(ec, "assign");
  305. }
  306. /// Construct a basic_socket_acceptor on an existing native acceptor.
  307. /**
  308. * This constructor creates an acceptor object to hold an existing native
  309. * acceptor.
  310. *
  311. * @param context An execution context which provides the I/O executor that
  312. * the acceptor will use, by default, to dispatch handlers for any
  313. * asynchronous operations performed on the acceptor.
  314. *
  315. * @param protocol An object specifying protocol parameters to be used.
  316. *
  317. * @param native_acceptor A native acceptor.
  318. *
  319. * @throws asio::system_error Thrown on failure.
  320. */
  321. template <typename ExecutionContext>
  322. basic_socket_acceptor(ExecutionContext& context,
  323. const protocol_type& protocol, const native_handle_type& native_acceptor,
  324. constraint_t<
  325. is_convertible<ExecutionContext&, execution_context&>::value
  326. > = 0)
  327. : impl_(0, 0, context)
  328. {
  329. asio::error_code ec;
  330. impl_.get_service().assign(impl_.get_implementation(),
  331. protocol, native_acceptor, ec);
  332. asio::detail::throw_error(ec, "assign");
  333. }
  334. /// Move-construct a basic_socket_acceptor from another.
  335. /**
  336. * This constructor moves an acceptor from one object to another.
  337. *
  338. * @param other The other basic_socket_acceptor object from which the move
  339. * will occur.
  340. *
  341. * @note Following the move, the moved-from object is in the same state as if
  342. * constructed using the @c basic_socket_acceptor(const executor_type&)
  343. * constructor.
  344. */
  345. basic_socket_acceptor(basic_socket_acceptor&& other) noexcept
  346. : impl_(std::move(other.impl_))
  347. {
  348. }
  349. /// Move-assign a basic_socket_acceptor from another.
  350. /**
  351. * This assignment operator moves an acceptor from one object to another.
  352. *
  353. * @param other The other basic_socket_acceptor object from which the move
  354. * will occur.
  355. *
  356. * @note Following the move, the moved-from object is in the same state as if
  357. * constructed using the @c basic_socket_acceptor(const executor_type&)
  358. * constructor.
  359. */
  360. basic_socket_acceptor& operator=(basic_socket_acceptor&& other)
  361. {
  362. impl_ = std::move(other.impl_);
  363. return *this;
  364. }
  365. // All socket acceptors have access to each other's implementations.
  366. template <typename Protocol1, typename Executor1>
  367. friend class basic_socket_acceptor;
  368. /// Move-construct a basic_socket_acceptor from an acceptor of another
  369. /// protocol type.
  370. /**
  371. * This constructor moves an acceptor from one object to another.
  372. *
  373. * @param other The other basic_socket_acceptor object from which the move
  374. * will occur.
  375. *
  376. * @note Following the move, the moved-from object is in the same state as if
  377. * constructed using the @c basic_socket_acceptor(const executor_type&)
  378. * constructor.
  379. */
  380. template <typename Protocol1, typename Executor1>
  381. basic_socket_acceptor(basic_socket_acceptor<Protocol1, Executor1>&& other,
  382. constraint_t<
  383. is_convertible<Protocol1, Protocol>::value
  384. && is_convertible<Executor1, Executor>::value
  385. > = 0)
  386. : impl_(std::move(other.impl_))
  387. {
  388. }
  389. /// Move-assign a basic_socket_acceptor from an acceptor of another protocol
  390. /// type.
  391. /**
  392. * This assignment operator moves an acceptor from one object to another.
  393. *
  394. * @param other The other basic_socket_acceptor object from which the move
  395. * will occur.
  396. *
  397. * @note Following the move, the moved-from object is in the same state as if
  398. * constructed using the @c basic_socket_acceptor(const executor_type&)
  399. * constructor.
  400. */
  401. template <typename Protocol1, typename Executor1>
  402. constraint_t<
  403. is_convertible<Protocol1, Protocol>::value
  404. && is_convertible<Executor1, Executor>::value,
  405. basic_socket_acceptor&
  406. > operator=(basic_socket_acceptor<Protocol1, Executor1>&& other)
  407. {
  408. basic_socket_acceptor tmp(std::move(other));
  409. impl_ = std::move(tmp.impl_);
  410. return *this;
  411. }
  412. /// Destroys the acceptor.
  413. /**
  414. * This function destroys the acceptor, cancelling any outstanding
  415. * asynchronous operations associated with the acceptor as if by calling
  416. * @c cancel.
  417. */
  418. ~basic_socket_acceptor()
  419. {
  420. }
  421. /// Get the executor associated with the object.
  422. const executor_type& get_executor() noexcept
  423. {
  424. return impl_.get_executor();
  425. }
  426. /// Open the acceptor using the specified protocol.
  427. /**
  428. * This function opens the socket acceptor so that it will use the specified
  429. * protocol.
  430. *
  431. * @param protocol An object specifying which protocol is to be used.
  432. *
  433. * @throws asio::system_error Thrown on failure.
  434. *
  435. * @par Example
  436. * @code
  437. * asio::ip::tcp::acceptor acceptor(my_context);
  438. * acceptor.open(asio::ip::tcp::v4());
  439. * @endcode
  440. */
  441. void open(const protocol_type& protocol = protocol_type())
  442. {
  443. asio::error_code ec;
  444. impl_.get_service().open(impl_.get_implementation(), protocol, ec);
  445. asio::detail::throw_error(ec, "open");
  446. }
  447. /// Open the acceptor using the specified protocol.
  448. /**
  449. * This function opens the socket acceptor so that it will use the specified
  450. * protocol.
  451. *
  452. * @param protocol An object specifying which protocol is to be used.
  453. *
  454. * @param ec Set to indicate what error occurred, if any.
  455. *
  456. * @par Example
  457. * @code
  458. * asio::ip::tcp::acceptor acceptor(my_context);
  459. * asio::error_code ec;
  460. * acceptor.open(asio::ip::tcp::v4(), ec);
  461. * if (ec)
  462. * {
  463. * // An error occurred.
  464. * }
  465. * @endcode
  466. */
  467. ASIO_SYNC_OP_VOID open(const protocol_type& protocol,
  468. asio::error_code& ec)
  469. {
  470. impl_.get_service().open(impl_.get_implementation(), protocol, ec);
  471. ASIO_SYNC_OP_VOID_RETURN(ec);
  472. }
  473. /// Assigns an existing native acceptor to the acceptor.
  474. /*
  475. * This function opens the acceptor to hold an existing native acceptor.
  476. *
  477. * @param protocol An object specifying which protocol is to be used.
  478. *
  479. * @param native_acceptor A native acceptor.
  480. *
  481. * @throws asio::system_error Thrown on failure.
  482. */
  483. void assign(const protocol_type& protocol,
  484. const native_handle_type& native_acceptor)
  485. {
  486. asio::error_code ec;
  487. impl_.get_service().assign(impl_.get_implementation(),
  488. protocol, native_acceptor, ec);
  489. asio::detail::throw_error(ec, "assign");
  490. }
  491. /// Assigns an existing native acceptor to the acceptor.
  492. /*
  493. * This function opens the acceptor to hold an existing native acceptor.
  494. *
  495. * @param protocol An object specifying which protocol is to be used.
  496. *
  497. * @param native_acceptor A native acceptor.
  498. *
  499. * @param ec Set to indicate what error occurred, if any.
  500. */
  501. ASIO_SYNC_OP_VOID assign(const protocol_type& protocol,
  502. const native_handle_type& native_acceptor, asio::error_code& ec)
  503. {
  504. impl_.get_service().assign(impl_.get_implementation(),
  505. protocol, native_acceptor, ec);
  506. ASIO_SYNC_OP_VOID_RETURN(ec);
  507. }
  508. /// Determine whether the acceptor is open.
  509. bool is_open() const
  510. {
  511. return impl_.get_service().is_open(impl_.get_implementation());
  512. }
  513. /// Bind the acceptor to the given local endpoint.
  514. /**
  515. * This function binds the socket acceptor to the specified endpoint on the
  516. * local machine.
  517. *
  518. * @param endpoint An endpoint on the local machine to which the socket
  519. * acceptor will be bound.
  520. *
  521. * @throws asio::system_error Thrown on failure.
  522. *
  523. * @par Example
  524. * @code
  525. * asio::ip::tcp::acceptor acceptor(my_context);
  526. * asio::ip::tcp::endpoint endpoint(asio::ip::tcp::v4(), 12345);
  527. * acceptor.open(endpoint.protocol());
  528. * acceptor.bind(endpoint);
  529. * @endcode
  530. */
  531. void bind(const endpoint_type& endpoint)
  532. {
  533. asio::error_code ec;
  534. impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
  535. asio::detail::throw_error(ec, "bind");
  536. }
  537. /// Bind the acceptor to the given local endpoint.
  538. /**
  539. * This function binds the socket acceptor to the specified endpoint on the
  540. * local machine.
  541. *
  542. * @param endpoint An endpoint on the local machine to which the socket
  543. * acceptor will be bound.
  544. *
  545. * @param ec Set to indicate what error occurred, if any.
  546. *
  547. * @par Example
  548. * @code
  549. * asio::ip::tcp::acceptor acceptor(my_context);
  550. * asio::ip::tcp::endpoint endpoint(asio::ip::tcp::v4(), 12345);
  551. * acceptor.open(endpoint.protocol());
  552. * asio::error_code ec;
  553. * acceptor.bind(endpoint, ec);
  554. * if (ec)
  555. * {
  556. * // An error occurred.
  557. * }
  558. * @endcode
  559. */
  560. ASIO_SYNC_OP_VOID bind(const endpoint_type& endpoint,
  561. asio::error_code& ec)
  562. {
  563. impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
  564. ASIO_SYNC_OP_VOID_RETURN(ec);
  565. }
  566. /// Place the acceptor into the state where it will listen for new
  567. /// connections.
  568. /**
  569. * This function puts the socket acceptor into the state where it may accept
  570. * new connections.
  571. *
  572. * @param backlog The maximum length of the queue of pending connections.
  573. *
  574. * @throws asio::system_error Thrown on failure.
  575. */
  576. void listen(int backlog = socket_base::max_listen_connections)
  577. {
  578. asio::error_code ec;
  579. impl_.get_service().listen(impl_.get_implementation(), backlog, ec);
  580. asio::detail::throw_error(ec, "listen");
  581. }
  582. /// Place the acceptor into the state where it will listen for new
  583. /// connections.
  584. /**
  585. * This function puts the socket acceptor into the state where it may accept
  586. * new connections.
  587. *
  588. * @param backlog The maximum length of the queue of pending connections.
  589. *
  590. * @param ec Set to indicate what error occurred, if any.
  591. *
  592. * @par Example
  593. * @code
  594. * asio::ip::tcp::acceptor acceptor(my_context);
  595. * ...
  596. * asio::error_code ec;
  597. * acceptor.listen(asio::socket_base::max_listen_connections, ec);
  598. * if (ec)
  599. * {
  600. * // An error occurred.
  601. * }
  602. * @endcode
  603. */
  604. ASIO_SYNC_OP_VOID listen(int backlog, asio::error_code& ec)
  605. {
  606. impl_.get_service().listen(impl_.get_implementation(), backlog, ec);
  607. ASIO_SYNC_OP_VOID_RETURN(ec);
  608. }
  609. /// Close the acceptor.
  610. /**
  611. * This function is used to close the acceptor. Any asynchronous accept
  612. * operations will be cancelled immediately.
  613. *
  614. * A subsequent call to open() is required before the acceptor can again be
  615. * used to again perform socket accept operations.
  616. *
  617. * @throws asio::system_error Thrown on failure.
  618. */
  619. void close()
  620. {
  621. asio::error_code ec;
  622. impl_.get_service().close(impl_.get_implementation(), ec);
  623. asio::detail::throw_error(ec, "close");
  624. }
  625. /// Close the acceptor.
  626. /**
  627. * This function is used to close the acceptor. Any asynchronous accept
  628. * operations will be cancelled immediately.
  629. *
  630. * A subsequent call to open() is required before the acceptor can again be
  631. * used to again perform socket accept operations.
  632. *
  633. * @param ec Set to indicate what error occurred, if any.
  634. *
  635. * @par Example
  636. * @code
  637. * asio::ip::tcp::acceptor acceptor(my_context);
  638. * ...
  639. * asio::error_code ec;
  640. * acceptor.close(ec);
  641. * if (ec)
  642. * {
  643. * // An error occurred.
  644. * }
  645. * @endcode
  646. */
  647. ASIO_SYNC_OP_VOID close(asio::error_code& ec)
  648. {
  649. impl_.get_service().close(impl_.get_implementation(), ec);
  650. ASIO_SYNC_OP_VOID_RETURN(ec);
  651. }
  652. /// Release ownership of the underlying native acceptor.
  653. /**
  654. * This function causes all outstanding asynchronous accept operations to
  655. * finish immediately, and the handlers for cancelled operations will be
  656. * passed the asio::error::operation_aborted error. Ownership of the
  657. * native acceptor is then transferred to the caller.
  658. *
  659. * @throws asio::system_error Thrown on failure.
  660. *
  661. * @note This function is unsupported on Windows versions prior to Windows
  662. * 8.1, and will fail with asio::error::operation_not_supported on
  663. * these platforms.
  664. */
  665. #if defined(ASIO_MSVC) && (ASIO_MSVC >= 1400) \
  666. && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0603)
  667. __declspec(deprecated("This function always fails with "
  668. "operation_not_supported when used on Windows versions "
  669. "prior to Windows 8.1."))
  670. #endif
  671. native_handle_type release()
  672. {
  673. asio::error_code ec;
  674. native_handle_type s = impl_.get_service().release(
  675. impl_.get_implementation(), ec);
  676. asio::detail::throw_error(ec, "release");
  677. return s;
  678. }
  679. /// Release ownership of the underlying native acceptor.
  680. /**
  681. * This function causes all outstanding asynchronous accept operations to
  682. * finish immediately, and the handlers for cancelled operations will be
  683. * passed the asio::error::operation_aborted error. Ownership of the
  684. * native acceptor is then transferred to the caller.
  685. *
  686. * @param ec Set to indicate what error occurred, if any.
  687. *
  688. * @note This function is unsupported on Windows versions prior to Windows
  689. * 8.1, and will fail with asio::error::operation_not_supported on
  690. * these platforms.
  691. */
  692. #if defined(ASIO_MSVC) && (ASIO_MSVC >= 1400) \
  693. && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0603)
  694. __declspec(deprecated("This function always fails with "
  695. "operation_not_supported when used on Windows versions "
  696. "prior to Windows 8.1."))
  697. #endif
  698. native_handle_type release(asio::error_code& ec)
  699. {
  700. return impl_.get_service().release(impl_.get_implementation(), ec);
  701. }
  702. /// Get the native acceptor representation.
  703. /**
  704. * This function may be used to obtain the underlying representation of the
  705. * acceptor. This is intended to allow access to native acceptor functionality
  706. * that is not otherwise provided.
  707. */
  708. native_handle_type native_handle()
  709. {
  710. return impl_.get_service().native_handle(impl_.get_implementation());
  711. }
  712. /// Cancel all asynchronous operations associated with the acceptor.
  713. /**
  714. * This function causes all outstanding asynchronous connect, send and receive
  715. * operations to finish immediately, and the handlers for cancelled operations
  716. * will be passed the asio::error::operation_aborted error.
  717. *
  718. * @throws asio::system_error Thrown on failure.
  719. */
  720. void cancel()
  721. {
  722. asio::error_code ec;
  723. impl_.get_service().cancel(impl_.get_implementation(), ec);
  724. asio::detail::throw_error(ec, "cancel");
  725. }
  726. /// Cancel all asynchronous operations associated with the acceptor.
  727. /**
  728. * This function causes all outstanding asynchronous connect, send and receive
  729. * operations to finish immediately, and the handlers for cancelled operations
  730. * will be passed the asio::error::operation_aborted error.
  731. *
  732. * @param ec Set to indicate what error occurred, if any.
  733. */
  734. ASIO_SYNC_OP_VOID cancel(asio::error_code& ec)
  735. {
  736. impl_.get_service().cancel(impl_.get_implementation(), ec);
  737. ASIO_SYNC_OP_VOID_RETURN(ec);
  738. }
  739. /// Set an option on the acceptor.
  740. /**
  741. * This function is used to set an option on the acceptor.
  742. *
  743. * @param option The new option value to be set on the acceptor.
  744. *
  745. * @throws asio::system_error Thrown on failure.
  746. *
  747. * @sa SettableSocketOption @n
  748. * asio::socket_base::reuse_address
  749. * asio::socket_base::enable_connection_aborted
  750. *
  751. * @par Example
  752. * Setting the SOL_SOCKET/SO_REUSEADDR option:
  753. * @code
  754. * asio::ip::tcp::acceptor acceptor(my_context);
  755. * ...
  756. * asio::ip::tcp::acceptor::reuse_address option(true);
  757. * acceptor.set_option(option);
  758. * @endcode
  759. */
  760. template <typename SettableSocketOption>
  761. void set_option(const SettableSocketOption& option)
  762. {
  763. asio::error_code ec;
  764. impl_.get_service().set_option(impl_.get_implementation(), option, ec);
  765. asio::detail::throw_error(ec, "set_option");
  766. }
  767. /// Set an option on the acceptor.
  768. /**
  769. * This function is used to set an option on the acceptor.
  770. *
  771. * @param option The new option value to be set on the acceptor.
  772. *
  773. * @param ec Set to indicate what error occurred, if any.
  774. *
  775. * @sa SettableSocketOption @n
  776. * asio::socket_base::reuse_address
  777. * asio::socket_base::enable_connection_aborted
  778. *
  779. * @par Example
  780. * Setting the SOL_SOCKET/SO_REUSEADDR option:
  781. * @code
  782. * asio::ip::tcp::acceptor acceptor(my_context);
  783. * ...
  784. * asio::ip::tcp::acceptor::reuse_address option(true);
  785. * asio::error_code ec;
  786. * acceptor.set_option(option, ec);
  787. * if (ec)
  788. * {
  789. * // An error occurred.
  790. * }
  791. * @endcode
  792. */
  793. template <typename SettableSocketOption>
  794. ASIO_SYNC_OP_VOID set_option(const SettableSocketOption& option,
  795. asio::error_code& ec)
  796. {
  797. impl_.get_service().set_option(impl_.get_implementation(), option, ec);
  798. ASIO_SYNC_OP_VOID_RETURN(ec);
  799. }
  800. /// Get an option from the acceptor.
  801. /**
  802. * This function is used to get the current value of an option on the
  803. * acceptor.
  804. *
  805. * @param option The option value to be obtained from the acceptor.
  806. *
  807. * @throws asio::system_error Thrown on failure.
  808. *
  809. * @sa GettableSocketOption @n
  810. * asio::socket_base::reuse_address
  811. *
  812. * @par Example
  813. * Getting the value of the SOL_SOCKET/SO_REUSEADDR option:
  814. * @code
  815. * asio::ip::tcp::acceptor acceptor(my_context);
  816. * ...
  817. * asio::ip::tcp::acceptor::reuse_address option;
  818. * acceptor.get_option(option);
  819. * bool is_set = option.get();
  820. * @endcode
  821. */
  822. template <typename GettableSocketOption>
  823. void get_option(GettableSocketOption& option) const
  824. {
  825. asio::error_code ec;
  826. impl_.get_service().get_option(impl_.get_implementation(), option, ec);
  827. asio::detail::throw_error(ec, "get_option");
  828. }
  829. /// Get an option from the acceptor.
  830. /**
  831. * This function is used to get the current value of an option on the
  832. * acceptor.
  833. *
  834. * @param option The option value to be obtained from the acceptor.
  835. *
  836. * @param ec Set to indicate what error occurred, if any.
  837. *
  838. * @sa GettableSocketOption @n
  839. * asio::socket_base::reuse_address
  840. *
  841. * @par Example
  842. * Getting the value of the SOL_SOCKET/SO_REUSEADDR option:
  843. * @code
  844. * asio::ip::tcp::acceptor acceptor(my_context);
  845. * ...
  846. * asio::ip::tcp::acceptor::reuse_address option;
  847. * asio::error_code ec;
  848. * acceptor.get_option(option, ec);
  849. * if (ec)
  850. * {
  851. * // An error occurred.
  852. * }
  853. * bool is_set = option.get();
  854. * @endcode
  855. */
  856. template <typename GettableSocketOption>
  857. ASIO_SYNC_OP_VOID get_option(GettableSocketOption& option,
  858. asio::error_code& ec) const
  859. {
  860. impl_.get_service().get_option(impl_.get_implementation(), option, ec);
  861. ASIO_SYNC_OP_VOID_RETURN(ec);
  862. }
  863. /// Perform an IO control command on the acceptor.
  864. /**
  865. * This function is used to execute an IO control command on the acceptor.
  866. *
  867. * @param command The IO control command to be performed on the acceptor.
  868. *
  869. * @throws asio::system_error Thrown on failure.
  870. *
  871. * @sa IoControlCommand @n
  872. * asio::socket_base::non_blocking_io
  873. *
  874. * @par Example
  875. * Getting the number of bytes ready to read:
  876. * @code
  877. * asio::ip::tcp::acceptor acceptor(my_context);
  878. * ...
  879. * asio::ip::tcp::acceptor::non_blocking_io command(true);
  880. * socket.io_control(command);
  881. * @endcode
  882. */
  883. template <typename IoControlCommand>
  884. void io_control(IoControlCommand& command)
  885. {
  886. asio::error_code ec;
  887. impl_.get_service().io_control(impl_.get_implementation(), command, ec);
  888. asio::detail::throw_error(ec, "io_control");
  889. }
  890. /// Perform an IO control command on the acceptor.
  891. /**
  892. * This function is used to execute an IO control command on the acceptor.
  893. *
  894. * @param command The IO control command to be performed on the acceptor.
  895. *
  896. * @param ec Set to indicate what error occurred, if any.
  897. *
  898. * @sa IoControlCommand @n
  899. * asio::socket_base::non_blocking_io
  900. *
  901. * @par Example
  902. * Getting the number of bytes ready to read:
  903. * @code
  904. * asio::ip::tcp::acceptor acceptor(my_context);
  905. * ...
  906. * asio::ip::tcp::acceptor::non_blocking_io command(true);
  907. * asio::error_code ec;
  908. * socket.io_control(command, ec);
  909. * if (ec)
  910. * {
  911. * // An error occurred.
  912. * }
  913. * @endcode
  914. */
  915. template <typename IoControlCommand>
  916. ASIO_SYNC_OP_VOID io_control(IoControlCommand& command,
  917. asio::error_code& ec)
  918. {
  919. impl_.get_service().io_control(impl_.get_implementation(), command, ec);
  920. ASIO_SYNC_OP_VOID_RETURN(ec);
  921. }
  922. /// Gets the non-blocking mode of the acceptor.
  923. /**
  924. * @returns @c true if the acceptor's synchronous operations will fail with
  925. * asio::error::would_block if they are unable to perform the requested
  926. * operation immediately. If @c false, synchronous operations will block
  927. * until complete.
  928. *
  929. * @note The non-blocking mode has no effect on the behaviour of asynchronous
  930. * operations. Asynchronous operations will never fail with the error
  931. * asio::error::would_block.
  932. */
  933. bool non_blocking() const
  934. {
  935. return impl_.get_service().non_blocking(impl_.get_implementation());
  936. }
  937. /// Sets the non-blocking mode of the acceptor.
  938. /**
  939. * @param mode If @c true, the acceptor's synchronous operations will fail
  940. * with asio::error::would_block if they are unable to perform the
  941. * requested operation immediately. If @c false, synchronous operations will
  942. * block until complete.
  943. *
  944. * @throws asio::system_error Thrown on failure.
  945. *
  946. * @note The non-blocking mode has no effect on the behaviour of asynchronous
  947. * operations. Asynchronous operations will never fail with the error
  948. * asio::error::would_block.
  949. */
  950. void non_blocking(bool mode)
  951. {
  952. asio::error_code ec;
  953. impl_.get_service().non_blocking(impl_.get_implementation(), mode, ec);
  954. asio::detail::throw_error(ec, "non_blocking");
  955. }
  956. /// Sets the non-blocking mode of the acceptor.
  957. /**
  958. * @param mode If @c true, the acceptor's synchronous operations will fail
  959. * with asio::error::would_block if they are unable to perform the
  960. * requested operation immediately. If @c false, synchronous operations will
  961. * block until complete.
  962. *
  963. * @param ec Set to indicate what error occurred, if any.
  964. *
  965. * @note The non-blocking mode has no effect on the behaviour of asynchronous
  966. * operations. Asynchronous operations will never fail with the error
  967. * asio::error::would_block.
  968. */
  969. ASIO_SYNC_OP_VOID non_blocking(
  970. bool mode, asio::error_code& ec)
  971. {
  972. impl_.get_service().non_blocking(impl_.get_implementation(), mode, ec);
  973. ASIO_SYNC_OP_VOID_RETURN(ec);
  974. }
  975. /// Gets the non-blocking mode of the native acceptor implementation.
  976. /**
  977. * This function is used to retrieve the non-blocking mode of the underlying
  978. * native acceptor. This mode has no effect on the behaviour of the acceptor
  979. * object's synchronous operations.
  980. *
  981. * @returns @c true if the underlying acceptor is in non-blocking mode and
  982. * direct system calls may fail with asio::error::would_block (or the
  983. * equivalent system error).
  984. *
  985. * @note The current non-blocking mode is cached by the acceptor object.
  986. * Consequently, the return value may be incorrect if the non-blocking mode
  987. * was set directly on the native acceptor.
  988. */
  989. bool native_non_blocking() const
  990. {
  991. return impl_.get_service().native_non_blocking(impl_.get_implementation());
  992. }
  993. /// Sets the non-blocking mode of the native acceptor implementation.
  994. /**
  995. * This function is used to modify the non-blocking mode of the underlying
  996. * native acceptor. It has no effect on the behaviour of the acceptor object's
  997. * synchronous operations.
  998. *
  999. * @param mode If @c true, the underlying acceptor is put into non-blocking
  1000. * mode and direct system calls may fail with asio::error::would_block
  1001. * (or the equivalent system error).
  1002. *
  1003. * @throws asio::system_error Thrown on failure. If the @c mode is
  1004. * @c false, but the current value of @c non_blocking() is @c true, this
  1005. * function fails with asio::error::invalid_argument, as the
  1006. * combination does not make sense.
  1007. */
  1008. void native_non_blocking(bool mode)
  1009. {
  1010. asio::error_code ec;
  1011. impl_.get_service().native_non_blocking(
  1012. impl_.get_implementation(), mode, ec);
  1013. asio::detail::throw_error(ec, "native_non_blocking");
  1014. }
  1015. /// Sets the non-blocking mode of the native acceptor implementation.
  1016. /**
  1017. * This function is used to modify the non-blocking mode of the underlying
  1018. * native acceptor. It has no effect on the behaviour of the acceptor object's
  1019. * synchronous operations.
  1020. *
  1021. * @param mode If @c true, the underlying acceptor is put into non-blocking
  1022. * mode and direct system calls may fail with asio::error::would_block
  1023. * (or the equivalent system error).
  1024. *
  1025. * @param ec Set to indicate what error occurred, if any. If the @c mode is
  1026. * @c false, but the current value of @c non_blocking() is @c true, this
  1027. * function fails with asio::error::invalid_argument, as the
  1028. * combination does not make sense.
  1029. */
  1030. ASIO_SYNC_OP_VOID native_non_blocking(
  1031. bool mode, asio::error_code& ec)
  1032. {
  1033. impl_.get_service().native_non_blocking(
  1034. impl_.get_implementation(), mode, ec);
  1035. ASIO_SYNC_OP_VOID_RETURN(ec);
  1036. }
  1037. /// Get the local endpoint of the acceptor.
  1038. /**
  1039. * This function is used to obtain the locally bound endpoint of the acceptor.
  1040. *
  1041. * @returns An object that represents the local endpoint of the acceptor.
  1042. *
  1043. * @throws asio::system_error Thrown on failure.
  1044. *
  1045. * @par Example
  1046. * @code
  1047. * asio::ip::tcp::acceptor acceptor(my_context);
  1048. * ...
  1049. * asio::ip::tcp::endpoint endpoint = acceptor.local_endpoint();
  1050. * @endcode
  1051. */
  1052. endpoint_type local_endpoint() const
  1053. {
  1054. asio::error_code ec;
  1055. endpoint_type ep = impl_.get_service().local_endpoint(
  1056. impl_.get_implementation(), ec);
  1057. asio::detail::throw_error(ec, "local_endpoint");
  1058. return ep;
  1059. }
  1060. /// Get the local endpoint of the acceptor.
  1061. /**
  1062. * This function is used to obtain the locally bound endpoint of the acceptor.
  1063. *
  1064. * @param ec Set to indicate what error occurred, if any.
  1065. *
  1066. * @returns An object that represents the local endpoint of the acceptor.
  1067. * Returns a default-constructed endpoint object if an error occurred and the
  1068. * error handler did not throw an exception.
  1069. *
  1070. * @par Example
  1071. * @code
  1072. * asio::ip::tcp::acceptor acceptor(my_context);
  1073. * ...
  1074. * asio::error_code ec;
  1075. * asio::ip::tcp::endpoint endpoint = acceptor.local_endpoint(ec);
  1076. * if (ec)
  1077. * {
  1078. * // An error occurred.
  1079. * }
  1080. * @endcode
  1081. */
  1082. endpoint_type local_endpoint(asio::error_code& ec) const
  1083. {
  1084. return impl_.get_service().local_endpoint(impl_.get_implementation(), ec);
  1085. }
  1086. /// Wait for the acceptor to become ready to read, ready to write, or to have
  1087. /// pending error conditions.
  1088. /**
  1089. * This function is used to perform a blocking wait for an acceptor to enter
  1090. * a ready to read, write or error condition state.
  1091. *
  1092. * @param w Specifies the desired acceptor state.
  1093. *
  1094. * @par Example
  1095. * Waiting for an acceptor to become readable.
  1096. * @code
  1097. * asio::ip::tcp::acceptor acceptor(my_context);
  1098. * ...
  1099. * acceptor.wait(asio::ip::tcp::acceptor::wait_read);
  1100. * @endcode
  1101. */
  1102. void wait(wait_type w)
  1103. {
  1104. asio::error_code ec;
  1105. impl_.get_service().wait(impl_.get_implementation(), w, ec);
  1106. asio::detail::throw_error(ec, "wait");
  1107. }
  1108. /// Wait for the acceptor to become ready to read, ready to write, or to have
  1109. /// pending error conditions.
  1110. /**
  1111. * This function is used to perform a blocking wait for an acceptor to enter
  1112. * a ready to read, write or error condition state.
  1113. *
  1114. * @param w Specifies the desired acceptor state.
  1115. *
  1116. * @param ec Set to indicate what error occurred, if any.
  1117. *
  1118. * @par Example
  1119. * Waiting for an acceptor to become readable.
  1120. * @code
  1121. * asio::ip::tcp::acceptor acceptor(my_context);
  1122. * ...
  1123. * asio::error_code ec;
  1124. * acceptor.wait(asio::ip::tcp::acceptor::wait_read, ec);
  1125. * @endcode
  1126. */
  1127. ASIO_SYNC_OP_VOID wait(wait_type w, asio::error_code& ec)
  1128. {
  1129. impl_.get_service().wait(impl_.get_implementation(), w, ec);
  1130. ASIO_SYNC_OP_VOID_RETURN(ec);
  1131. }
  1132. /// Asynchronously wait for the acceptor to become ready to read, ready to
  1133. /// write, or to have pending error conditions.
  1134. /**
  1135. * This function is used to perform an asynchronous wait for an acceptor to
  1136. * enter a ready to read, write or error condition state. It is an initiating
  1137. * function for an @ref asynchronous_operation, and always returns
  1138. * immediately.
  1139. *
  1140. * @param w Specifies the desired acceptor state.
  1141. *
  1142. * @param token The @ref completion_token that will be used to produce a
  1143. * completion handler, which will be called when the wait completes.
  1144. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  1145. * @ref yield_context, or a function object with the correct completion
  1146. * signature. The function signature of the completion handler must be:
  1147. * @code void handler(
  1148. * const asio::error_code& error // Result of operation.
  1149. * ); @endcode
  1150. * Regardless of whether the asynchronous operation completes immediately or
  1151. * not, the completion handler will not be invoked from within this function.
  1152. * On immediate completion, invocation of the handler will be performed in a
  1153. * manner equivalent to using asio::post().
  1154. *
  1155. * @par Completion Signature
  1156. * @code void(asio::error_code) @endcode
  1157. *
  1158. * @par Example
  1159. * @code
  1160. * void wait_handler(const asio::error_code& error)
  1161. * {
  1162. * if (!error)
  1163. * {
  1164. * // Wait succeeded.
  1165. * }
  1166. * }
  1167. *
  1168. * ...
  1169. *
  1170. * asio::ip::tcp::acceptor acceptor(my_context);
  1171. * ...
  1172. * acceptor.async_wait(
  1173. * asio::ip::tcp::acceptor::wait_read,
  1174. * wait_handler);
  1175. * @endcode
  1176. *
  1177. * @par Per-Operation Cancellation
  1178. * On POSIX or Windows operating systems, this asynchronous operation supports
  1179. * cancellation for the following asio::cancellation_type values:
  1180. *
  1181. * @li @c cancellation_type::terminal
  1182. *
  1183. * @li @c cancellation_type::partial
  1184. *
  1185. * @li @c cancellation_type::total
  1186. */
  1187. template <
  1188. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code))
  1189. WaitToken = default_completion_token_t<executor_type>>
  1190. auto async_wait(wait_type w,
  1191. WaitToken&& token = default_completion_token_t<executor_type>())
  1192. -> decltype(
  1193. async_initiate<WaitToken, void (asio::error_code)>(
  1194. declval<initiate_async_wait>(), token, w))
  1195. {
  1196. return async_initiate<WaitToken, void (asio::error_code)>(
  1197. initiate_async_wait(this), token, w);
  1198. }
  1199. #if !defined(ASIO_NO_EXTENSIONS)
  1200. /// Accept a new connection.
  1201. /**
  1202. * This function is used to accept a new connection from a peer into the
  1203. * given socket. The function call will block until a new connection has been
  1204. * accepted successfully or an error occurs.
  1205. *
  1206. * @param peer The socket into which the new connection will be accepted.
  1207. *
  1208. * @throws asio::system_error Thrown on failure.
  1209. *
  1210. * @par Example
  1211. * @code
  1212. * asio::ip::tcp::acceptor acceptor(my_context);
  1213. * ...
  1214. * asio::ip::tcp::socket socket(my_context);
  1215. * acceptor.accept(socket);
  1216. * @endcode
  1217. */
  1218. template <typename Protocol1, typename Executor1>
  1219. void accept(basic_socket<Protocol1, Executor1>& peer,
  1220. constraint_t<
  1221. is_convertible<Protocol, Protocol1>::value
  1222. > = 0)
  1223. {
  1224. asio::error_code ec;
  1225. impl_.get_service().accept(impl_.get_implementation(),
  1226. peer, static_cast<endpoint_type*>(0), ec);
  1227. asio::detail::throw_error(ec, "accept");
  1228. }
  1229. /// Accept a new connection.
  1230. /**
  1231. * This function is used to accept a new connection from a peer into the
  1232. * given socket. The function call will block until a new connection has been
  1233. * accepted successfully or an error occurs.
  1234. *
  1235. * @param peer The socket into which the new connection will be accepted.
  1236. *
  1237. * @param ec Set to indicate what error occurred, if any.
  1238. *
  1239. * @par Example
  1240. * @code
  1241. * asio::ip::tcp::acceptor acceptor(my_context);
  1242. * ...
  1243. * asio::ip::tcp::socket socket(my_context);
  1244. * asio::error_code ec;
  1245. * acceptor.accept(socket, ec);
  1246. * if (ec)
  1247. * {
  1248. * // An error occurred.
  1249. * }
  1250. * @endcode
  1251. */
  1252. template <typename Protocol1, typename Executor1>
  1253. ASIO_SYNC_OP_VOID accept(
  1254. basic_socket<Protocol1, Executor1>& peer, asio::error_code& ec,
  1255. constraint_t<
  1256. is_convertible<Protocol, Protocol1>::value
  1257. > = 0)
  1258. {
  1259. impl_.get_service().accept(impl_.get_implementation(),
  1260. peer, static_cast<endpoint_type*>(0), ec);
  1261. ASIO_SYNC_OP_VOID_RETURN(ec);
  1262. }
  1263. /// Start an asynchronous accept.
  1264. /**
  1265. * This function is used to asynchronously accept a new connection into a
  1266. * socket, and additionally obtain the endpoint of the remote peer. It is an
  1267. * initiating function for an @ref asynchronous_operation, and always returns
  1268. * immediately.
  1269. *
  1270. * @param peer The socket into which the new connection will be accepted.
  1271. * Ownership of the peer object is retained by the caller, which must
  1272. * guarantee that it is valid until the completion handler is called.
  1273. *
  1274. * @param token The @ref completion_token that will be used to produce a
  1275. * completion handler, which will be called when the accept completes.
  1276. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  1277. * @ref yield_context, or a function object with the correct completion
  1278. * signature. The function signature of the completion handler must be:
  1279. * @code void handler(
  1280. * const asio::error_code& error // Result of operation.
  1281. * ); @endcode
  1282. * Regardless of whether the asynchronous operation completes immediately or
  1283. * not, the completion handler will not be invoked from within this function.
  1284. * On immediate completion, invocation of the handler will be performed in a
  1285. * manner equivalent to using asio::post().
  1286. *
  1287. * @par Completion Signature
  1288. * @code void(asio::error_code) @endcode
  1289. *
  1290. * @par Example
  1291. * @code
  1292. * void accept_handler(const asio::error_code& error)
  1293. * {
  1294. * if (!error)
  1295. * {
  1296. * // Accept succeeded.
  1297. * }
  1298. * }
  1299. *
  1300. * ...
  1301. *
  1302. * asio::ip::tcp::acceptor acceptor(my_context);
  1303. * ...
  1304. * asio::ip::tcp::socket socket(my_context);
  1305. * acceptor.async_accept(socket, accept_handler);
  1306. * @endcode
  1307. *
  1308. * @par Per-Operation Cancellation
  1309. * On POSIX or Windows operating systems, this asynchronous operation supports
  1310. * cancellation for the following asio::cancellation_type values:
  1311. *
  1312. * @li @c cancellation_type::terminal
  1313. *
  1314. * @li @c cancellation_type::partial
  1315. *
  1316. * @li @c cancellation_type::total
  1317. */
  1318. template <typename Protocol1, typename Executor1,
  1319. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code))
  1320. AcceptToken = default_completion_token_t<executor_type>>
  1321. auto async_accept(basic_socket<Protocol1, Executor1>& peer,
  1322. AcceptToken&& token = default_completion_token_t<executor_type>(),
  1323. constraint_t<
  1324. is_convertible<Protocol, Protocol1>::value
  1325. > = 0)
  1326. -> decltype(
  1327. async_initiate<AcceptToken, void (asio::error_code)>(
  1328. declval<initiate_async_accept>(), token,
  1329. &peer, static_cast<endpoint_type*>(0)))
  1330. {
  1331. return async_initiate<AcceptToken, void (asio::error_code)>(
  1332. initiate_async_accept(this), token,
  1333. &peer, static_cast<endpoint_type*>(0));
  1334. }
  1335. /// Accept a new connection and obtain the endpoint of the peer
  1336. /**
  1337. * This function is used to accept a new connection from a peer into the
  1338. * given socket, and additionally provide the endpoint of the remote peer.
  1339. * The function call will block until a new connection has been accepted
  1340. * successfully or an error occurs.
  1341. *
  1342. * @param peer The socket into which the new connection will be accepted.
  1343. *
  1344. * @param peer_endpoint An endpoint object which will receive the endpoint of
  1345. * the remote peer.
  1346. *
  1347. * @throws asio::system_error Thrown on failure.
  1348. *
  1349. * @par Example
  1350. * @code
  1351. * asio::ip::tcp::acceptor acceptor(my_context);
  1352. * ...
  1353. * asio::ip::tcp::socket socket(my_context);
  1354. * asio::ip::tcp::endpoint endpoint;
  1355. * acceptor.accept(socket, endpoint);
  1356. * @endcode
  1357. */
  1358. template <typename Executor1>
  1359. void accept(basic_socket<protocol_type, Executor1>& peer,
  1360. endpoint_type& peer_endpoint)
  1361. {
  1362. asio::error_code ec;
  1363. impl_.get_service().accept(impl_.get_implementation(),
  1364. peer, &peer_endpoint, ec);
  1365. asio::detail::throw_error(ec, "accept");
  1366. }
  1367. /// Accept a new connection and obtain the endpoint of the peer
  1368. /**
  1369. * This function is used to accept a new connection from a peer into the
  1370. * given socket, and additionally provide the endpoint of the remote peer.
  1371. * The function call will block until a new connection has been accepted
  1372. * successfully or an error occurs.
  1373. *
  1374. * @param peer The socket into which the new connection will be accepted.
  1375. *
  1376. * @param peer_endpoint An endpoint object which will receive the endpoint of
  1377. * the remote peer.
  1378. *
  1379. * @param ec Set to indicate what error occurred, if any.
  1380. *
  1381. * @par Example
  1382. * @code
  1383. * asio::ip::tcp::acceptor acceptor(my_context);
  1384. * ...
  1385. * asio::ip::tcp::socket socket(my_context);
  1386. * asio::ip::tcp::endpoint endpoint;
  1387. * asio::error_code ec;
  1388. * acceptor.accept(socket, endpoint, ec);
  1389. * if (ec)
  1390. * {
  1391. * // An error occurred.
  1392. * }
  1393. * @endcode
  1394. */
  1395. template <typename Executor1>
  1396. ASIO_SYNC_OP_VOID accept(basic_socket<protocol_type, Executor1>& peer,
  1397. endpoint_type& peer_endpoint, asio::error_code& ec)
  1398. {
  1399. impl_.get_service().accept(
  1400. impl_.get_implementation(), peer, &peer_endpoint, ec);
  1401. ASIO_SYNC_OP_VOID_RETURN(ec);
  1402. }
  1403. /// Start an asynchronous accept.
  1404. /**
  1405. * This function is used to asynchronously accept a new connection into a
  1406. * socket, and additionally obtain the endpoint of the remote peer. It is an
  1407. * initiating function for an @ref asynchronous_operation, and always returns
  1408. * immediately.
  1409. *
  1410. * @param peer The socket into which the new connection will be accepted.
  1411. * Ownership of the peer object is retained by the caller, which must
  1412. * guarantee that it is valid until the completion handler is called.
  1413. *
  1414. * @param peer_endpoint An endpoint object into which the endpoint of the
  1415. * remote peer will be written. Ownership of the peer_endpoint object is
  1416. * retained by the caller, which must guarantee that it is valid until the
  1417. * handler is called.
  1418. *
  1419. * @param token The @ref completion_token that will be used to produce a
  1420. * completion handler, which will be called when the accept completes.
  1421. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  1422. * @ref yield_context, or a function object with the correct completion
  1423. * signature. The function signature of the completion handler must be:
  1424. * @code void handler(
  1425. * const asio::error_code& error // Result of operation.
  1426. * ); @endcode
  1427. * Regardless of whether the asynchronous operation completes immediately or
  1428. * not, the completion handler will not be invoked from within this function.
  1429. * On immediate completion, invocation of the handler will be performed in a
  1430. * manner equivalent to using asio::post().
  1431. *
  1432. * @par Completion Signature
  1433. * @code void(asio::error_code) @endcode
  1434. *
  1435. * @par Per-Operation Cancellation
  1436. * On POSIX or Windows operating systems, this asynchronous operation supports
  1437. * cancellation for the following asio::cancellation_type values:
  1438. *
  1439. * @li @c cancellation_type::terminal
  1440. *
  1441. * @li @c cancellation_type::partial
  1442. *
  1443. * @li @c cancellation_type::total
  1444. */
  1445. template <typename Executor1,
  1446. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code))
  1447. AcceptToken = default_completion_token_t<executor_type>>
  1448. auto async_accept(basic_socket<protocol_type, Executor1>& peer,
  1449. endpoint_type& peer_endpoint,
  1450. AcceptToken&& token = default_completion_token_t<executor_type>())
  1451. -> decltype(
  1452. async_initiate<AcceptToken, void (asio::error_code)>(
  1453. declval<initiate_async_accept>(), token, &peer, &peer_endpoint))
  1454. {
  1455. return async_initiate<AcceptToken, void (asio::error_code)>(
  1456. initiate_async_accept(this), token, &peer, &peer_endpoint);
  1457. }
  1458. #endif // !defined(ASIO_NO_EXTENSIONS)
  1459. /// Accept a new connection.
  1460. /**
  1461. * This function is used to accept a new connection from a peer. The function
  1462. * call will block until a new connection has been accepted successfully or
  1463. * an error occurs.
  1464. *
  1465. * This overload requires that the Protocol template parameter satisfy the
  1466. * AcceptableProtocol type requirements.
  1467. *
  1468. * @returns A socket object representing the newly accepted connection.
  1469. *
  1470. * @throws asio::system_error Thrown on failure.
  1471. *
  1472. * @par Example
  1473. * @code
  1474. * asio::ip::tcp::acceptor acceptor(my_context);
  1475. * ...
  1476. * asio::ip::tcp::socket socket(acceptor.accept());
  1477. * @endcode
  1478. */
  1479. typename Protocol::socket::template rebind_executor<executor_type>::other
  1480. accept()
  1481. {
  1482. asio::error_code ec;
  1483. typename Protocol::socket::template rebind_executor<
  1484. executor_type>::other peer(impl_.get_executor());
  1485. impl_.get_service().accept(impl_.get_implementation(), peer, 0, ec);
  1486. asio::detail::throw_error(ec, "accept");
  1487. return peer;
  1488. }
  1489. /// Accept a new connection.
  1490. /**
  1491. * This function is used to accept a new connection from a peer. The function
  1492. * call will block until a new connection has been accepted successfully or
  1493. * an error occurs.
  1494. *
  1495. * This overload requires that the Protocol template parameter satisfy the
  1496. * AcceptableProtocol type requirements.
  1497. *
  1498. * @param ec Set to indicate what error occurred, if any.
  1499. *
  1500. * @returns On success, a socket object representing the newly accepted
  1501. * connection. On error, a socket object where is_open() is false.
  1502. *
  1503. * @par Example
  1504. * @code
  1505. * asio::ip::tcp::acceptor acceptor(my_context);
  1506. * ...
  1507. * asio::ip::tcp::socket socket(acceptor.accept(ec));
  1508. * if (ec)
  1509. * {
  1510. * // An error occurred.
  1511. * }
  1512. * @endcode
  1513. */
  1514. typename Protocol::socket::template rebind_executor<executor_type>::other
  1515. accept(asio::error_code& ec)
  1516. {
  1517. typename Protocol::socket::template rebind_executor<
  1518. executor_type>::other peer(impl_.get_executor());
  1519. impl_.get_service().accept(impl_.get_implementation(), peer, 0, ec);
  1520. return peer;
  1521. }
  1522. /// Start an asynchronous accept.
  1523. /**
  1524. * This function is used to asynchronously accept a new connection. It is an
  1525. * initiating function for an @ref asynchronous_operation, and always returns
  1526. * immediately.
  1527. *
  1528. * This overload requires that the Protocol template parameter satisfy the
  1529. * AcceptableProtocol type requirements.
  1530. *
  1531. * @param token The @ref completion_token that will be used to produce a
  1532. * completion handler, which will be called when the accept completes.
  1533. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  1534. * @ref yield_context, or a function object with the correct completion
  1535. * signature. The function signature of the completion handler must be:
  1536. * @code void handler(
  1537. * // Result of operation.
  1538. * const asio::error_code& error,
  1539. *
  1540. * // On success, the newly accepted socket.
  1541. * typename Protocol::socket::template
  1542. * rebind_executor<executor_type>::other peer
  1543. * ); @endcode
  1544. * Regardless of whether the asynchronous operation completes immediately or
  1545. * not, the completion handler will not be invoked from within this function.
  1546. * On immediate completion, invocation of the handler will be performed in a
  1547. * manner equivalent to using asio::post().
  1548. *
  1549. * @par Completion Signature
  1550. * @code void(asio::error_code,
  1551. * typename Protocol::socket::template
  1552. * rebind_executor<executor_type>::other)) @endcode
  1553. *
  1554. * @par Example
  1555. * @code
  1556. * void accept_handler(const asio::error_code& error,
  1557. * asio::ip::tcp::socket peer)
  1558. * {
  1559. * if (!error)
  1560. * {
  1561. * // Accept succeeded.
  1562. * }
  1563. * }
  1564. *
  1565. * ...
  1566. *
  1567. * asio::ip::tcp::acceptor acceptor(my_context);
  1568. * ...
  1569. * acceptor.async_accept(accept_handler);
  1570. * @endcode
  1571. *
  1572. * @par Per-Operation Cancellation
  1573. * On POSIX or Windows operating systems, this asynchronous operation supports
  1574. * cancellation for the following asio::cancellation_type values:
  1575. *
  1576. * @li @c cancellation_type::terminal
  1577. *
  1578. * @li @c cancellation_type::partial
  1579. *
  1580. * @li @c cancellation_type::total
  1581. */
  1582. template <
  1583. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  1584. typename Protocol::socket::template rebind_executor<
  1585. executor_type>::other)) MoveAcceptToken
  1586. = default_completion_token_t<executor_type>>
  1587. auto async_accept(
  1588. MoveAcceptToken&& token = default_completion_token_t<executor_type>())
  1589. -> decltype(
  1590. async_initiate<MoveAcceptToken,
  1591. void (asio::error_code, typename Protocol::socket::template
  1592. rebind_executor<executor_type>::other)>(
  1593. declval<initiate_async_move_accept>(), token,
  1594. declval<const executor_type&>(), static_cast<endpoint_type*>(0),
  1595. static_cast<typename Protocol::socket::template
  1596. rebind_executor<executor_type>::other*>(0)))
  1597. {
  1598. return async_initiate<MoveAcceptToken,
  1599. void (asio::error_code, typename Protocol::socket::template
  1600. rebind_executor<executor_type>::other)>(
  1601. initiate_async_move_accept(this), token,
  1602. impl_.get_executor(), static_cast<endpoint_type*>(0),
  1603. static_cast<typename Protocol::socket::template
  1604. rebind_executor<executor_type>::other*>(0));
  1605. }
  1606. /// Accept a new connection.
  1607. /**
  1608. * This function is used to accept a new connection from a peer. The function
  1609. * call will block until a new connection has been accepted successfully or
  1610. * an error occurs.
  1611. *
  1612. * This overload requires that the Protocol template parameter satisfy the
  1613. * AcceptableProtocol type requirements.
  1614. *
  1615. * @param ex The I/O executor object to be used for the newly
  1616. * accepted socket.
  1617. *
  1618. * @returns A socket object representing the newly accepted connection.
  1619. *
  1620. * @throws asio::system_error Thrown on failure.
  1621. *
  1622. * @par Example
  1623. * @code
  1624. * asio::ip::tcp::acceptor acceptor(my_context);
  1625. * ...
  1626. * asio::ip::tcp::socket socket(acceptor.accept());
  1627. * @endcode
  1628. */
  1629. template <typename Executor1>
  1630. typename Protocol::socket::template rebind_executor<Executor1>::other
  1631. accept(const Executor1& ex,
  1632. constraint_t<
  1633. is_executor<Executor1>::value
  1634. || execution::is_executor<Executor1>::value
  1635. > = 0)
  1636. {
  1637. asio::error_code ec;
  1638. typename Protocol::socket::template
  1639. rebind_executor<Executor1>::other peer(ex);
  1640. impl_.get_service().accept(impl_.get_implementation(), peer, 0, ec);
  1641. asio::detail::throw_error(ec, "accept");
  1642. return peer;
  1643. }
  1644. /// Accept a new connection.
  1645. /**
  1646. * This function is used to accept a new connection from a peer. The function
  1647. * call will block until a new connection has been accepted successfully or
  1648. * an error occurs.
  1649. *
  1650. * This overload requires that the Protocol template parameter satisfy the
  1651. * AcceptableProtocol type requirements.
  1652. *
  1653. * @param context The I/O execution context object to be used for the newly
  1654. * accepted socket.
  1655. *
  1656. * @returns A socket object representing the newly accepted connection.
  1657. *
  1658. * @throws asio::system_error Thrown on failure.
  1659. *
  1660. * @par Example
  1661. * @code
  1662. * asio::ip::tcp::acceptor acceptor(my_context);
  1663. * ...
  1664. * asio::ip::tcp::socket socket(acceptor.accept());
  1665. * @endcode
  1666. */
  1667. template <typename ExecutionContext>
  1668. typename Protocol::socket::template rebind_executor<
  1669. typename ExecutionContext::executor_type>::other
  1670. accept(ExecutionContext& context,
  1671. constraint_t<
  1672. is_convertible<ExecutionContext&, execution_context&>::value
  1673. > = 0)
  1674. {
  1675. asio::error_code ec;
  1676. typename Protocol::socket::template rebind_executor<
  1677. typename ExecutionContext::executor_type>::other peer(context);
  1678. impl_.get_service().accept(impl_.get_implementation(), peer, 0, ec);
  1679. asio::detail::throw_error(ec, "accept");
  1680. return peer;
  1681. }
  1682. /// Accept a new connection.
  1683. /**
  1684. * This function is used to accept a new connection from a peer. The function
  1685. * call will block until a new connection has been accepted successfully or
  1686. * an error occurs.
  1687. *
  1688. * This overload requires that the Protocol template parameter satisfy the
  1689. * AcceptableProtocol type requirements.
  1690. *
  1691. * @param ex The I/O executor object to be used for the newly accepted
  1692. * socket.
  1693. *
  1694. * @param ec Set to indicate what error occurred, if any.
  1695. *
  1696. * @returns On success, a socket object representing the newly accepted
  1697. * connection. On error, a socket object where is_open() is false.
  1698. *
  1699. * @par Example
  1700. * @code
  1701. * asio::ip::tcp::acceptor acceptor(my_context);
  1702. * ...
  1703. * asio::ip::tcp::socket socket(acceptor.accept(my_context2, ec));
  1704. * if (ec)
  1705. * {
  1706. * // An error occurred.
  1707. * }
  1708. * @endcode
  1709. */
  1710. template <typename Executor1>
  1711. typename Protocol::socket::template rebind_executor<Executor1>::other
  1712. accept(const Executor1& ex, asio::error_code& ec,
  1713. constraint_t<
  1714. is_executor<Executor1>::value
  1715. || execution::is_executor<Executor1>::value
  1716. > = 0)
  1717. {
  1718. typename Protocol::socket::template
  1719. rebind_executor<Executor1>::other peer(ex);
  1720. impl_.get_service().accept(impl_.get_implementation(), peer, 0, ec);
  1721. return peer;
  1722. }
  1723. /// Accept a new connection.
  1724. /**
  1725. * This function is used to accept a new connection from a peer. The function
  1726. * call will block until a new connection has been accepted successfully or
  1727. * an error occurs.
  1728. *
  1729. * This overload requires that the Protocol template parameter satisfy the
  1730. * AcceptableProtocol type requirements.
  1731. *
  1732. * @param context The I/O execution context object to be used for the newly
  1733. * accepted socket.
  1734. *
  1735. * @param ec Set to indicate what error occurred, if any.
  1736. *
  1737. * @returns On success, a socket object representing the newly accepted
  1738. * connection. On error, a socket object where is_open() is false.
  1739. *
  1740. * @par Example
  1741. * @code
  1742. * asio::ip::tcp::acceptor acceptor(my_context);
  1743. * ...
  1744. * asio::ip::tcp::socket socket(acceptor.accept(my_context2, ec));
  1745. * if (ec)
  1746. * {
  1747. * // An error occurred.
  1748. * }
  1749. * @endcode
  1750. */
  1751. template <typename ExecutionContext>
  1752. typename Protocol::socket::template rebind_executor<
  1753. typename ExecutionContext::executor_type>::other
  1754. accept(ExecutionContext& context, asio::error_code& ec,
  1755. constraint_t<
  1756. is_convertible<ExecutionContext&, execution_context&>::value
  1757. > = 0)
  1758. {
  1759. typename Protocol::socket::template rebind_executor<
  1760. typename ExecutionContext::executor_type>::other peer(context);
  1761. impl_.get_service().accept(impl_.get_implementation(), peer, 0, ec);
  1762. return peer;
  1763. }
  1764. /// Start an asynchronous accept.
  1765. /**
  1766. * This function is used to asynchronously accept a new connection. It is an
  1767. * initiating function for an @ref asynchronous_operation, and always returns
  1768. * immediately.
  1769. *
  1770. * This overload requires that the Protocol template parameter satisfy the
  1771. * AcceptableProtocol type requirements.
  1772. *
  1773. * @param ex The I/O executor object to be used for the newly accepted
  1774. * socket.
  1775. *
  1776. * @param token The @ref completion_token that will be used to produce a
  1777. * completion handler, which will be called when the accept completes.
  1778. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  1779. * @ref yield_context, or a function object with the correct completion
  1780. * signature. The function signature of the completion handler must be:
  1781. * @code void handler(
  1782. * // Result of operation.
  1783. * const asio::error_code& error,
  1784. *
  1785. * // On success, the newly accepted socket.
  1786. * typename Protocol::socket::template rebind_executor<
  1787. * Executor1>::other peer
  1788. * ); @endcode
  1789. * Regardless of whether the asynchronous operation completes immediately or
  1790. * not, the completion handler will not be invoked from within this function.
  1791. * On immediate completion, invocation of the handler will be performed in a
  1792. * manner equivalent to using asio::post().
  1793. *
  1794. * @par Completion Signature
  1795. * @code void(asio::error_code,
  1796. * typename Protocol::socket::template rebind_executor<
  1797. * Executor1>::other)) @endcode
  1798. *
  1799. * @par Example
  1800. * @code
  1801. * void accept_handler(const asio::error_code& error,
  1802. * asio::ip::tcp::socket peer)
  1803. * {
  1804. * if (!error)
  1805. * {
  1806. * // Accept succeeded.
  1807. * }
  1808. * }
  1809. *
  1810. * ...
  1811. *
  1812. * asio::ip::tcp::acceptor acceptor(my_context);
  1813. * ...
  1814. * acceptor.async_accept(my_context2, accept_handler);
  1815. * @endcode
  1816. *
  1817. * @par Per-Operation Cancellation
  1818. * On POSIX or Windows operating systems, this asynchronous operation supports
  1819. * cancellation for the following asio::cancellation_type values:
  1820. *
  1821. * @li @c cancellation_type::terminal
  1822. *
  1823. * @li @c cancellation_type::partial
  1824. *
  1825. * @li @c cancellation_type::total
  1826. */
  1827. template <typename Executor1,
  1828. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  1829. typename Protocol::socket::template rebind_executor<
  1830. constraint_t<is_executor<Executor1>::value
  1831. || execution::is_executor<Executor1>::value,
  1832. Executor1>>::other)) MoveAcceptToken
  1833. = default_completion_token_t<executor_type>>
  1834. auto async_accept(const Executor1& ex,
  1835. MoveAcceptToken&& token = default_completion_token_t<executor_type>(),
  1836. constraint_t<
  1837. is_executor<Executor1>::value
  1838. || execution::is_executor<Executor1>::value
  1839. > = 0)
  1840. -> decltype(
  1841. async_initiate<MoveAcceptToken,
  1842. void (asio::error_code,
  1843. typename Protocol::socket::template rebind_executor<
  1844. Executor1>::other)>(
  1845. declval<initiate_async_move_accept>(), token,
  1846. ex, static_cast<endpoint_type*>(0),
  1847. static_cast<typename Protocol::socket::template
  1848. rebind_executor<Executor1>::other*>(0)))
  1849. {
  1850. return async_initiate<MoveAcceptToken,
  1851. void (asio::error_code,
  1852. typename Protocol::socket::template rebind_executor<
  1853. Executor1>::other)>(
  1854. initiate_async_move_accept(this), token,
  1855. ex, static_cast<endpoint_type*>(0),
  1856. static_cast<typename Protocol::socket::template
  1857. rebind_executor<Executor1>::other*>(0));
  1858. }
  1859. /// Start an asynchronous accept.
  1860. /**
  1861. * This function is used to asynchronously accept a new connection. It is an
  1862. * initiating function for an @ref asynchronous_operation, and always returns
  1863. * immediately.
  1864. *
  1865. * This overload requires that the Protocol template parameter satisfy the
  1866. * AcceptableProtocol type requirements.
  1867. *
  1868. * @param context The I/O execution context object to be used for the newly
  1869. * accepted socket.
  1870. *
  1871. * @param token The @ref completion_token that will be used to produce a
  1872. * completion handler, which will be called when the accept completes.
  1873. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  1874. * @ref yield_context, or a function object with the correct completion
  1875. * signature. The function signature of the completion handler must be:
  1876. * @code void handler(
  1877. * // Result of operation.
  1878. * const asio::error_code& error,
  1879. *
  1880. * // On success, the newly accepted socket.
  1881. * typename Protocol::socket::template rebind_executor<
  1882. * typename ExecutionContext::executor_type>::other peer
  1883. * ); @endcode
  1884. * Regardless of whether the asynchronous operation completes immediately or
  1885. * not, the completion handler will not be invoked from within this function.
  1886. * On immediate completion, invocation of the handler will be performed in a
  1887. * manner equivalent to using asio::post().
  1888. *
  1889. * @par Completion Signature
  1890. * @code void(asio::error_code,
  1891. * typename Protocol::socket::template rebind_executor<
  1892. * typename ExecutionContext::executor_type>::other)) @endcode
  1893. *
  1894. * @par Example
  1895. * @code
  1896. * void accept_handler(const asio::error_code& error,
  1897. * asio::ip::tcp::socket peer)
  1898. * {
  1899. * if (!error)
  1900. * {
  1901. * // Accept succeeded.
  1902. * }
  1903. * }
  1904. *
  1905. * ...
  1906. *
  1907. * asio::ip::tcp::acceptor acceptor(my_context);
  1908. * ...
  1909. * acceptor.async_accept(my_context2, accept_handler);
  1910. * @endcode
  1911. *
  1912. * @par Per-Operation Cancellation
  1913. * On POSIX or Windows operating systems, this asynchronous operation supports
  1914. * cancellation for the following asio::cancellation_type values:
  1915. *
  1916. * @li @c cancellation_type::terminal
  1917. *
  1918. * @li @c cancellation_type::partial
  1919. *
  1920. * @li @c cancellation_type::total
  1921. */
  1922. template <typename ExecutionContext,
  1923. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  1924. typename Protocol::socket::template rebind_executor<
  1925. typename ExecutionContext::executor_type>::other)) MoveAcceptToken
  1926. = default_completion_token_t<executor_type>>
  1927. auto async_accept(ExecutionContext& context,
  1928. MoveAcceptToken&& token = default_completion_token_t<executor_type>(),
  1929. constraint_t<
  1930. is_convertible<ExecutionContext&, execution_context&>::value
  1931. > = 0)
  1932. -> decltype(
  1933. async_initiate<MoveAcceptToken,
  1934. void (asio::error_code,
  1935. typename Protocol::socket::template rebind_executor<
  1936. typename ExecutionContext::executor_type>::other)>(
  1937. declval<initiate_async_move_accept>(), token,
  1938. context.get_executor(), static_cast<endpoint_type*>(0),
  1939. static_cast<typename Protocol::socket::template rebind_executor<
  1940. typename ExecutionContext::executor_type>::other*>(0)))
  1941. {
  1942. return async_initiate<MoveAcceptToken,
  1943. void (asio::error_code,
  1944. typename Protocol::socket::template rebind_executor<
  1945. typename ExecutionContext::executor_type>::other)>(
  1946. initiate_async_move_accept(this), token,
  1947. context.get_executor(), static_cast<endpoint_type*>(0),
  1948. static_cast<typename Protocol::socket::template rebind_executor<
  1949. typename ExecutionContext::executor_type>::other*>(0));
  1950. }
  1951. /// Accept a new connection.
  1952. /**
  1953. * This function is used to accept a new connection from a peer. The function
  1954. * call will block until a new connection has been accepted successfully or
  1955. * an error occurs.
  1956. *
  1957. * This overload requires that the Protocol template parameter satisfy the
  1958. * AcceptableProtocol type requirements.
  1959. *
  1960. * @param peer_endpoint An endpoint object into which the endpoint of the
  1961. * remote peer will be written.
  1962. *
  1963. * @returns A socket object representing the newly accepted connection.
  1964. *
  1965. * @throws asio::system_error Thrown on failure.
  1966. *
  1967. * @par Example
  1968. * @code
  1969. * asio::ip::tcp::acceptor acceptor(my_context);
  1970. * ...
  1971. * asio::ip::tcp::endpoint endpoint;
  1972. * asio::ip::tcp::socket socket(acceptor.accept(endpoint));
  1973. * @endcode
  1974. */
  1975. typename Protocol::socket::template rebind_executor<executor_type>::other
  1976. accept(endpoint_type& peer_endpoint)
  1977. {
  1978. asio::error_code ec;
  1979. typename Protocol::socket::template rebind_executor<
  1980. executor_type>::other peer(impl_.get_executor());
  1981. impl_.get_service().accept(impl_.get_implementation(),
  1982. peer, &peer_endpoint, ec);
  1983. asio::detail::throw_error(ec, "accept");
  1984. return peer;
  1985. }
  1986. /// Accept a new connection.
  1987. /**
  1988. * This function is used to accept a new connection from a peer. The function
  1989. * call will block until a new connection has been accepted successfully or
  1990. * an error occurs.
  1991. *
  1992. * This overload requires that the Protocol template parameter satisfy the
  1993. * AcceptableProtocol type requirements.
  1994. *
  1995. * @param peer_endpoint An endpoint object into which the endpoint of the
  1996. * remote peer will be written.
  1997. *
  1998. * @param ec Set to indicate what error occurred, if any.
  1999. *
  2000. * @returns On success, a socket object representing the newly accepted
  2001. * connection. On error, a socket object where is_open() is false.
  2002. *
  2003. * @par Example
  2004. * @code
  2005. * asio::ip::tcp::acceptor acceptor(my_context);
  2006. * ...
  2007. * asio::ip::tcp::endpoint endpoint;
  2008. * asio::ip::tcp::socket socket(acceptor.accept(endpoint, ec));
  2009. * if (ec)
  2010. * {
  2011. * // An error occurred.
  2012. * }
  2013. * @endcode
  2014. */
  2015. typename Protocol::socket::template rebind_executor<executor_type>::other
  2016. accept(endpoint_type& peer_endpoint, asio::error_code& ec)
  2017. {
  2018. typename Protocol::socket::template rebind_executor<
  2019. executor_type>::other peer(impl_.get_executor());
  2020. impl_.get_service().accept(impl_.get_implementation(),
  2021. peer, &peer_endpoint, ec);
  2022. return peer;
  2023. }
  2024. /// Start an asynchronous accept.
  2025. /**
  2026. * This function is used to asynchronously accept a new connection. It is an
  2027. * initiating function for an @ref asynchronous_operation, and always returns
  2028. * immediately.
  2029. *
  2030. * This overload requires that the Protocol template parameter satisfy the
  2031. * AcceptableProtocol type requirements.
  2032. *
  2033. * @param peer_endpoint An endpoint object into which the endpoint of the
  2034. * remote peer will be written. Ownership of the peer_endpoint object is
  2035. * retained by the caller, which must guarantee that it is valid until the
  2036. * completion handler is called.
  2037. *
  2038. * @param token The @ref completion_token that will be used to produce a
  2039. * completion handler, which will be called when the accept completes.
  2040. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  2041. * @ref yield_context, or a function object with the correct completion
  2042. * signature. The function signature of the completion handler must be:
  2043. * @code void handler(
  2044. * // Result of operation.
  2045. * const asio::error_code& error,
  2046. *
  2047. * // On success, the newly accepted socket.
  2048. * typename Protocol::socket::template
  2049. * rebind_executor<executor_type>::other peer
  2050. * ); @endcode
  2051. * Regardless of whether the asynchronous operation completes immediately or
  2052. * not, the completion handler will not be invoked from within this function.
  2053. * On immediate completion, invocation of the handler will be performed in a
  2054. * manner equivalent to using asio::post().
  2055. *
  2056. * @par Completion Signature
  2057. * @code void(asio::error_code,
  2058. * typename Protocol::socket::template
  2059. * rebind_executor<executor_type>::other)) @endcode
  2060. *
  2061. * @par Example
  2062. * @code
  2063. * void accept_handler(const asio::error_code& error,
  2064. * asio::ip::tcp::socket peer)
  2065. * {
  2066. * if (!error)
  2067. * {
  2068. * // Accept succeeded.
  2069. * }
  2070. * }
  2071. *
  2072. * ...
  2073. *
  2074. * asio::ip::tcp::acceptor acceptor(my_context);
  2075. * ...
  2076. * asio::ip::tcp::endpoint endpoint;
  2077. * acceptor.async_accept(endpoint, accept_handler);
  2078. * @endcode
  2079. *
  2080. * @par Per-Operation Cancellation
  2081. * On POSIX or Windows operating systems, this asynchronous operation supports
  2082. * cancellation for the following asio::cancellation_type values:
  2083. *
  2084. * @li @c cancellation_type::terminal
  2085. *
  2086. * @li @c cancellation_type::partial
  2087. *
  2088. * @li @c cancellation_type::total
  2089. */
  2090. template <
  2091. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  2092. typename Protocol::socket::template rebind_executor<
  2093. executor_type>::other)) MoveAcceptToken
  2094. = default_completion_token_t<executor_type>>
  2095. auto async_accept(endpoint_type& peer_endpoint,
  2096. MoveAcceptToken&& token = default_completion_token_t<executor_type>())
  2097. -> decltype(
  2098. async_initiate<MoveAcceptToken,
  2099. void (asio::error_code, typename Protocol::socket::template
  2100. rebind_executor<executor_type>::other)>(
  2101. declval<initiate_async_move_accept>(), token,
  2102. declval<const executor_type&>(), &peer_endpoint,
  2103. static_cast<typename Protocol::socket::template
  2104. rebind_executor<executor_type>::other*>(0)))
  2105. {
  2106. return async_initiate<MoveAcceptToken,
  2107. void (asio::error_code, typename Protocol::socket::template
  2108. rebind_executor<executor_type>::other)>(
  2109. initiate_async_move_accept(this), token,
  2110. impl_.get_executor(), &peer_endpoint,
  2111. static_cast<typename Protocol::socket::template
  2112. rebind_executor<executor_type>::other*>(0));
  2113. }
  2114. /// Accept a new connection.
  2115. /**
  2116. * This function is used to accept a new connection from a peer. The function
  2117. * call will block until a new connection has been accepted successfully or
  2118. * an error occurs.
  2119. *
  2120. * This overload requires that the Protocol template parameter satisfy the
  2121. * AcceptableProtocol type requirements.
  2122. *
  2123. * @param ex The I/O executor object to be used for the newly accepted
  2124. * socket.
  2125. *
  2126. * @param peer_endpoint An endpoint object into which the endpoint of the
  2127. * remote peer will be written.
  2128. *
  2129. * @returns A socket object representing the newly accepted connection.
  2130. *
  2131. * @throws asio::system_error Thrown on failure.
  2132. *
  2133. * @par Example
  2134. * @code
  2135. * asio::ip::tcp::acceptor acceptor(my_context);
  2136. * ...
  2137. * asio::ip::tcp::endpoint endpoint;
  2138. * asio::ip::tcp::socket socket(
  2139. * acceptor.accept(my_context2, endpoint));
  2140. * @endcode
  2141. */
  2142. template <typename Executor1>
  2143. typename Protocol::socket::template rebind_executor<Executor1>::other
  2144. accept(const Executor1& ex, endpoint_type& peer_endpoint,
  2145. constraint_t<
  2146. is_executor<Executor1>::value
  2147. || execution::is_executor<Executor1>::value
  2148. > = 0)
  2149. {
  2150. asio::error_code ec;
  2151. typename Protocol::socket::template
  2152. rebind_executor<Executor1>::other peer(ex);
  2153. impl_.get_service().accept(impl_.get_implementation(),
  2154. peer, &peer_endpoint, ec);
  2155. asio::detail::throw_error(ec, "accept");
  2156. return peer;
  2157. }
  2158. /// Accept a new connection.
  2159. /**
  2160. * This function is used to accept a new connection from a peer. The function
  2161. * call will block until a new connection has been accepted successfully or
  2162. * an error occurs.
  2163. *
  2164. * This overload requires that the Protocol template parameter satisfy the
  2165. * AcceptableProtocol type requirements.
  2166. *
  2167. * @param context The I/O execution context object to be used for the newly
  2168. * accepted socket.
  2169. *
  2170. * @param peer_endpoint An endpoint object into which the endpoint of the
  2171. * remote peer will be written.
  2172. *
  2173. * @returns A socket object representing the newly accepted connection.
  2174. *
  2175. * @throws asio::system_error Thrown on failure.
  2176. *
  2177. * @par Example
  2178. * @code
  2179. * asio::ip::tcp::acceptor acceptor(my_context);
  2180. * ...
  2181. * asio::ip::tcp::endpoint endpoint;
  2182. * asio::ip::tcp::socket socket(
  2183. * acceptor.accept(my_context2, endpoint));
  2184. * @endcode
  2185. */
  2186. template <typename ExecutionContext>
  2187. typename Protocol::socket::template rebind_executor<
  2188. typename ExecutionContext::executor_type>::other
  2189. accept(ExecutionContext& context, endpoint_type& peer_endpoint,
  2190. constraint_t<
  2191. is_convertible<ExecutionContext&, execution_context&>::value
  2192. > = 0)
  2193. {
  2194. asio::error_code ec;
  2195. typename Protocol::socket::template rebind_executor<
  2196. typename ExecutionContext::executor_type>::other peer(context);
  2197. impl_.get_service().accept(impl_.get_implementation(),
  2198. peer, &peer_endpoint, ec);
  2199. asio::detail::throw_error(ec, "accept");
  2200. return peer;
  2201. }
  2202. /// Accept a new connection.
  2203. /**
  2204. * This function is used to accept a new connection from a peer. The function
  2205. * call will block until a new connection has been accepted successfully or
  2206. * an error occurs.
  2207. *
  2208. * This overload requires that the Protocol template parameter satisfy the
  2209. * AcceptableProtocol type requirements.
  2210. *
  2211. * @param ex The I/O executor object to be used for the newly accepted
  2212. * socket.
  2213. *
  2214. * @param peer_endpoint An endpoint object into which the endpoint of the
  2215. * remote peer will be written.
  2216. *
  2217. * @param ec Set to indicate what error occurred, if any.
  2218. *
  2219. * @returns On success, a socket object representing the newly accepted
  2220. * connection. On error, a socket object where is_open() is false.
  2221. *
  2222. * @par Example
  2223. * @code
  2224. * asio::ip::tcp::acceptor acceptor(my_context);
  2225. * ...
  2226. * asio::ip::tcp::endpoint endpoint;
  2227. * asio::ip::tcp::socket socket(
  2228. * acceptor.accept(my_context2, endpoint, ec));
  2229. * if (ec)
  2230. * {
  2231. * // An error occurred.
  2232. * }
  2233. * @endcode
  2234. */
  2235. template <typename Executor1>
  2236. typename Protocol::socket::template rebind_executor<Executor1>::other
  2237. accept(const executor_type& ex,
  2238. endpoint_type& peer_endpoint, asio::error_code& ec,
  2239. constraint_t<
  2240. is_executor<Executor1>::value
  2241. || execution::is_executor<Executor1>::value
  2242. > = 0)
  2243. {
  2244. typename Protocol::socket::template
  2245. rebind_executor<Executor1>::other peer(ex);
  2246. impl_.get_service().accept(impl_.get_implementation(),
  2247. peer, &peer_endpoint, ec);
  2248. return peer;
  2249. }
  2250. /// Accept a new connection.
  2251. /**
  2252. * This function is used to accept a new connection from a peer. The function
  2253. * call will block until a new connection has been accepted successfully or
  2254. * an error occurs.
  2255. *
  2256. * This overload requires that the Protocol template parameter satisfy the
  2257. * AcceptableProtocol type requirements.
  2258. *
  2259. * @param context The I/O execution context object to be used for the newly
  2260. * accepted socket.
  2261. *
  2262. * @param peer_endpoint An endpoint object into which the endpoint of the
  2263. * remote peer will be written.
  2264. *
  2265. * @param ec Set to indicate what error occurred, if any.
  2266. *
  2267. * @returns On success, a socket object representing the newly accepted
  2268. * connection. On error, a socket object where is_open() is false.
  2269. *
  2270. * @par Example
  2271. * @code
  2272. * asio::ip::tcp::acceptor acceptor(my_context);
  2273. * ...
  2274. * asio::ip::tcp::endpoint endpoint;
  2275. * asio::ip::tcp::socket socket(
  2276. * acceptor.accept(my_context2, endpoint, ec));
  2277. * if (ec)
  2278. * {
  2279. * // An error occurred.
  2280. * }
  2281. * @endcode
  2282. */
  2283. template <typename ExecutionContext>
  2284. typename Protocol::socket::template rebind_executor<
  2285. typename ExecutionContext::executor_type>::other
  2286. accept(ExecutionContext& context,
  2287. endpoint_type& peer_endpoint, asio::error_code& ec,
  2288. constraint_t<
  2289. is_convertible<ExecutionContext&, execution_context&>::value
  2290. > = 0)
  2291. {
  2292. typename Protocol::socket::template rebind_executor<
  2293. typename ExecutionContext::executor_type>::other peer(context);
  2294. impl_.get_service().accept(impl_.get_implementation(),
  2295. peer, &peer_endpoint, ec);
  2296. return peer;
  2297. }
  2298. /// Start an asynchronous accept.
  2299. /**
  2300. * This function is used to asynchronously accept a new connection. It is an
  2301. * initiating function for an @ref asynchronous_operation, and always returns
  2302. * immediately.
  2303. *
  2304. * This overload requires that the Protocol template parameter satisfy the
  2305. * AcceptableProtocol type requirements.
  2306. *
  2307. * @param ex The I/O executor object to be used for the newly accepted
  2308. * socket.
  2309. *
  2310. * @param peer_endpoint An endpoint object into which the endpoint of the
  2311. * remote peer will be written. Ownership of the peer_endpoint object is
  2312. * retained by the caller, which must guarantee that it is valid until the
  2313. * completion handler is called.
  2314. *
  2315. * @param token The @ref completion_token that will be used to produce a
  2316. * completion handler, which will be called when the accept completes.
  2317. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  2318. * @ref yield_context, or a function object with the correct completion
  2319. * signature. The function signature of the completion handler must be:
  2320. * @code void handler(
  2321. * // Result of operation.
  2322. * const asio::error_code& error,
  2323. *
  2324. * // On success, the newly accepted socket.
  2325. * typename Protocol::socket::template rebind_executor<
  2326. * Executor1>::other peer
  2327. * ); @endcode
  2328. * Regardless of whether the asynchronous operation completes immediately or
  2329. * not, the completion handler will not be invoked from within this function.
  2330. * On immediate completion, invocation of the handler will be performed in a
  2331. * manner equivalent to using asio::post().
  2332. *
  2333. * @par Completion Signature
  2334. * @code void(asio::error_code,
  2335. * typename Protocol::socket::template rebind_executor<
  2336. * Executor1>::other)) @endcode
  2337. *
  2338. * @par Example
  2339. * @code
  2340. * void accept_handler(const asio::error_code& error,
  2341. * asio::ip::tcp::socket peer)
  2342. * {
  2343. * if (!error)
  2344. * {
  2345. * // Accept succeeded.
  2346. * }
  2347. * }
  2348. *
  2349. * ...
  2350. *
  2351. * asio::ip::tcp::acceptor acceptor(my_context);
  2352. * ...
  2353. * asio::ip::tcp::endpoint endpoint;
  2354. * acceptor.async_accept(my_context2, endpoint, accept_handler);
  2355. * @endcode
  2356. *
  2357. * @par Per-Operation Cancellation
  2358. * On POSIX or Windows operating systems, this asynchronous operation supports
  2359. * cancellation for the following asio::cancellation_type values:
  2360. *
  2361. * @li @c cancellation_type::terminal
  2362. *
  2363. * @li @c cancellation_type::partial
  2364. *
  2365. * @li @c cancellation_type::total
  2366. */
  2367. template <typename Executor1,
  2368. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  2369. typename Protocol::socket::template rebind_executor<
  2370. constraint_t<is_executor<Executor1>::value
  2371. || execution::is_executor<Executor1>::value,
  2372. Executor1>>::other)) MoveAcceptToken
  2373. = default_completion_token_t<executor_type>>
  2374. auto async_accept(const Executor1& ex, endpoint_type& peer_endpoint,
  2375. MoveAcceptToken&& token = default_completion_token_t<executor_type>(),
  2376. constraint_t<
  2377. is_executor<Executor1>::value
  2378. || execution::is_executor<Executor1>::value
  2379. > = 0)
  2380. -> decltype(
  2381. async_initiate<MoveAcceptToken,
  2382. void (asio::error_code,
  2383. typename Protocol::socket::template rebind_executor<
  2384. Executor1>::other)>(
  2385. declval<initiate_async_move_accept>(), token, ex, &peer_endpoint,
  2386. static_cast<typename Protocol::socket::template
  2387. rebind_executor<Executor1>::other*>(0)))
  2388. {
  2389. return async_initiate<MoveAcceptToken,
  2390. void (asio::error_code,
  2391. typename Protocol::socket::template rebind_executor<
  2392. Executor1>::other)>(
  2393. initiate_async_move_accept(this), token, ex, &peer_endpoint,
  2394. static_cast<typename Protocol::socket::template
  2395. rebind_executor<Executor1>::other*>(0));
  2396. }
  2397. /// Start an asynchronous accept.
  2398. /**
  2399. * This function is used to asynchronously accept a new connection. It is an
  2400. * initiating function for an @ref asynchronous_operation, and always returns
  2401. * immediately.
  2402. *
  2403. * This overload requires that the Protocol template parameter satisfy the
  2404. * AcceptableProtocol type requirements.
  2405. *
  2406. * @param context The I/O execution context object to be used for the newly
  2407. * accepted socket.
  2408. *
  2409. * @param peer_endpoint An endpoint object into which the endpoint of the
  2410. * remote peer will be written. Ownership of the peer_endpoint object is
  2411. * retained by the caller, which must guarantee that it is valid until the
  2412. * completion handler is called.
  2413. *
  2414. * @param token The @ref completion_token that will be used to produce a
  2415. * completion handler, which will be called when the accept completes.
  2416. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  2417. * @ref yield_context, or a function object with the correct completion
  2418. * signature. The function signature of the completion handler must be:
  2419. * @code void handler(
  2420. * // Result of operation.
  2421. * const asio::error_code& error,
  2422. *
  2423. * // On success, the newly accepted socket.
  2424. * typename Protocol::socket::template rebind_executor<
  2425. * typename ExecutionContext::executor_type>::other peer
  2426. * ); @endcode
  2427. * Regardless of whether the asynchronous operation completes immediately or
  2428. * not, the completion handler will not be invoked from within this function.
  2429. * On immediate completion, invocation of the handler will be performed in a
  2430. * manner equivalent to using asio::post().
  2431. *
  2432. * @par Completion Signature
  2433. * @code void(asio::error_code,
  2434. * typename Protocol::socket::template rebind_executor<
  2435. * typename ExecutionContext::executor_type>::other)) @endcode
  2436. *
  2437. * @par Example
  2438. * @code
  2439. * void accept_handler(const asio::error_code& error,
  2440. * asio::ip::tcp::socket peer)
  2441. * {
  2442. * if (!error)
  2443. * {
  2444. * // Accept succeeded.
  2445. * }
  2446. * }
  2447. *
  2448. * ...
  2449. *
  2450. * asio::ip::tcp::acceptor acceptor(my_context);
  2451. * ...
  2452. * asio::ip::tcp::endpoint endpoint;
  2453. * acceptor.async_accept(my_context2, endpoint, accept_handler);
  2454. * @endcode
  2455. *
  2456. * @par Per-Operation Cancellation
  2457. * On POSIX or Windows operating systems, this asynchronous operation supports
  2458. * cancellation for the following asio::cancellation_type values:
  2459. *
  2460. * @li @c cancellation_type::terminal
  2461. *
  2462. * @li @c cancellation_type::partial
  2463. *
  2464. * @li @c cancellation_type::total
  2465. */
  2466. template <typename ExecutionContext,
  2467. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  2468. typename Protocol::socket::template rebind_executor<
  2469. typename ExecutionContext::executor_type>::other)) MoveAcceptToken
  2470. = default_completion_token_t<executor_type>>
  2471. auto async_accept(ExecutionContext& context, endpoint_type& peer_endpoint,
  2472. MoveAcceptToken&& token = default_completion_token_t<executor_type>(),
  2473. constraint_t<
  2474. is_convertible<ExecutionContext&, execution_context&>::value
  2475. > = 0)
  2476. -> decltype(
  2477. async_initiate<MoveAcceptToken,
  2478. void (asio::error_code,
  2479. typename Protocol::socket::template rebind_executor<
  2480. typename ExecutionContext::executor_type>::other)>(
  2481. declval<initiate_async_move_accept>(), token,
  2482. context.get_executor(), &peer_endpoint,
  2483. static_cast<typename Protocol::socket::template rebind_executor<
  2484. typename ExecutionContext::executor_type>::other*>(0)))
  2485. {
  2486. return async_initiate<MoveAcceptToken,
  2487. void (asio::error_code,
  2488. typename Protocol::socket::template rebind_executor<
  2489. typename ExecutionContext::executor_type>::other)>(
  2490. initiate_async_move_accept(this), token,
  2491. context.get_executor(), &peer_endpoint,
  2492. static_cast<typename Protocol::socket::template rebind_executor<
  2493. typename ExecutionContext::executor_type>::other*>(0));
  2494. }
  2495. private:
  2496. // Disallow copying and assignment.
  2497. basic_socket_acceptor(const basic_socket_acceptor&) = delete;
  2498. basic_socket_acceptor& operator=(
  2499. const basic_socket_acceptor&) = delete;
  2500. class initiate_async_wait
  2501. {
  2502. public:
  2503. typedef Executor executor_type;
  2504. explicit initiate_async_wait(basic_socket_acceptor* self)
  2505. : self_(self)
  2506. {
  2507. }
  2508. const executor_type& get_executor() const noexcept
  2509. {
  2510. return self_->get_executor();
  2511. }
  2512. template <typename WaitHandler>
  2513. void operator()(WaitHandler&& handler, wait_type w) const
  2514. {
  2515. // If you get an error on the following line it means that your handler
  2516. // does not meet the documented type requirements for a WaitHandler.
  2517. ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
  2518. detail::non_const_lvalue<WaitHandler> handler2(handler);
  2519. self_->impl_.get_service().async_wait(
  2520. self_->impl_.get_implementation(), w,
  2521. handler2.value, self_->impl_.get_executor());
  2522. }
  2523. private:
  2524. basic_socket_acceptor* self_;
  2525. };
  2526. class initiate_async_accept
  2527. {
  2528. public:
  2529. typedef Executor executor_type;
  2530. explicit initiate_async_accept(basic_socket_acceptor* self)
  2531. : self_(self)
  2532. {
  2533. }
  2534. const executor_type& get_executor() const noexcept
  2535. {
  2536. return self_->get_executor();
  2537. }
  2538. template <typename AcceptHandler, typename Protocol1, typename Executor1>
  2539. void operator()(AcceptHandler&& handler,
  2540. basic_socket<Protocol1, Executor1>* peer,
  2541. endpoint_type* peer_endpoint) const
  2542. {
  2543. // If you get an error on the following line it means that your handler
  2544. // does not meet the documented type requirements for a AcceptHandler.
  2545. ASIO_ACCEPT_HANDLER_CHECK(AcceptHandler, handler) type_check;
  2546. detail::non_const_lvalue<AcceptHandler> handler2(handler);
  2547. self_->impl_.get_service().async_accept(
  2548. self_->impl_.get_implementation(), *peer, peer_endpoint,
  2549. handler2.value, self_->impl_.get_executor());
  2550. }
  2551. private:
  2552. basic_socket_acceptor* self_;
  2553. };
  2554. class initiate_async_move_accept
  2555. {
  2556. public:
  2557. typedef Executor executor_type;
  2558. explicit initiate_async_move_accept(basic_socket_acceptor* self)
  2559. : self_(self)
  2560. {
  2561. }
  2562. const executor_type& get_executor() const noexcept
  2563. {
  2564. return self_->get_executor();
  2565. }
  2566. template <typename MoveAcceptHandler, typename Executor1, typename Socket>
  2567. void operator()(MoveAcceptHandler&& handler,
  2568. const Executor1& peer_ex, endpoint_type* peer_endpoint, Socket*) const
  2569. {
  2570. // If you get an error on the following line it means that your handler
  2571. // does not meet the documented type requirements for a MoveAcceptHandler.
  2572. ASIO_MOVE_ACCEPT_HANDLER_CHECK(
  2573. MoveAcceptHandler, handler, Socket) type_check;
  2574. detail::non_const_lvalue<MoveAcceptHandler> handler2(handler);
  2575. self_->impl_.get_service().async_move_accept(
  2576. self_->impl_.get_implementation(), peer_ex, peer_endpoint,
  2577. handler2.value, self_->impl_.get_executor());
  2578. }
  2579. private:
  2580. basic_socket_acceptor* self_;
  2581. };
  2582. #if defined(ASIO_WINDOWS_RUNTIME)
  2583. detail::io_object_impl<
  2584. detail::null_socket_service<Protocol>, Executor> impl_;
  2585. #elif defined(ASIO_HAS_IOCP)
  2586. detail::io_object_impl<
  2587. detail::win_iocp_socket_service<Protocol>, Executor> impl_;
  2588. #elif defined(ASIO_HAS_IO_URING_AS_DEFAULT)
  2589. detail::io_object_impl<
  2590. detail::io_uring_socket_service<Protocol>, Executor> impl_;
  2591. #else
  2592. detail::io_object_impl<
  2593. detail::reactive_socket_service<Protocol>, Executor> impl_;
  2594. #endif
  2595. };
  2596. } // namespace asio
  2597. #include "asio/detail/pop_options.hpp"
  2598. #endif // ASIO_BASIC_SOCKET_ACCEPTOR_HPP