basic_socket_acceptor.hpp 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687
  1. //
  2. // basic_socket_acceptor.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_BASIC_SOCKET_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 "asio/detail/config.hpp"
  16. #include "asio/any_io_executor.hpp"
  17. #include "asio/basic_socket.hpp"
  18. #include "asio/detail/handler_type_requirements.hpp"
  19. #include "asio/detail/io_object_impl.hpp"
  20. #include "asio/detail/non_const_lvalue.hpp"
  21. #include "asio/detail/throw_error.hpp"
  22. #include "asio/detail/type_traits.hpp"
  23. #include "asio/error.hpp"
  24. #include "asio/execution_context.hpp"
  25. #include "asio/socket_base.hpp"
  26. #if defined(ASIO_WINDOWS_RUNTIME)
  27. # include "asio/detail/null_socket_service.hpp"
  28. #elif defined(ASIO_HAS_IOCP)
  29. # include "asio/detail/win_iocp_socket_service.hpp"
  30. #elif defined(ASIO_HAS_IO_URING_AS_DEFAULT)
  31. # include "asio/detail/io_uring_socket_service.hpp"
  32. #else
  33. # include "asio/detail/reactive_socket_service.hpp"
  34. #endif
  35. #if defined(ASIO_HAS_MOVE)
  36. # include <utility>
  37. #endif // defined(ASIO_HAS_MOVE)
  38. #include "asio/detail/push_options.hpp"
  39. namespace asio {
  40. #if !defined(ASIO_BASIC_SOCKET_ACCEPTOR_FWD_DECL)
  41. #define ASIO_BASIC_SOCKET_ACCEPTOR_FWD_DECL
  42. // Forward declaration with defaulted arguments.
  43. template <typename Protocol, typename Executor = any_io_executor>
  44. class basic_socket_acceptor;
  45. #endif // !defined(ASIO_BASIC_SOCKET_ACCEPTOR_FWD_DECL)
  46. /// Provides the ability to accept new connections.
  47. /**
  48. * The basic_socket_acceptor class template is used for accepting new socket
  49. * connections.
  50. *
  51. * @par Thread Safety
  52. * @e Distinct @e objects: Safe.@n
  53. * @e Shared @e objects: Unsafe.
  54. *
  55. * Synchronous @c accept operations are thread safe, if the underlying
  56. * operating system calls are also thread safe. This means that it is permitted
  57. * to perform concurrent calls to synchronous @c accept operations on a single
  58. * socket object. Other synchronous operations, such as @c open or @c close, are
  59. * not thread safe.
  60. *
  61. * @par Example
  62. * Opening a socket acceptor with the SO_REUSEADDR option enabled:
  63. * @code
  64. * asio::ip::tcp::acceptor acceptor(my_context);
  65. * asio::ip::tcp::endpoint endpoint(asio::ip::tcp::v4(), port);
  66. * acceptor.open(endpoint.protocol());
  67. * acceptor.set_option(asio::ip::tcp::acceptor::reuse_address(true));
  68. * acceptor.bind(endpoint);
  69. * acceptor.listen();
  70. * @endcode
  71. */
  72. template <typename Protocol, typename Executor>
  73. class basic_socket_acceptor
  74. : public socket_base
  75. {
  76. public:
  77. /// The type of the executor associated with the object.
  78. typedef Executor executor_type;
  79. /// Rebinds the acceptor type to another executor.
  80. template <typename Executor1>
  81. struct rebind_executor
  82. {
  83. /// The socket type when rebound to the specified executor.
  84. typedef basic_socket_acceptor<Protocol, Executor1> other;
  85. };
  86. /// The native representation of an acceptor.
  87. #if defined(GENERATING_DOCUMENTATION)
  88. typedef implementation_defined native_handle_type;
  89. #elif defined(ASIO_WINDOWS_RUNTIME)
  90. typedef typename detail::null_socket_service<
  91. Protocol>::native_handle_type native_handle_type;
  92. #elif defined(ASIO_HAS_IOCP)
  93. typedef typename detail::win_iocp_socket_service<
  94. Protocol>::native_handle_type native_handle_type;
  95. #elif defined(ASIO_HAS_IO_URING_AS_DEFAULT)
  96. typedef typename detail::io_uring_socket_service<
  97. Protocol>::native_handle_type native_handle_type;
  98. #else
  99. typedef typename detail::reactive_socket_service<
  100. Protocol>::native_handle_type native_handle_type;
  101. #endif
  102. /// The protocol type.
  103. typedef Protocol protocol_type;
  104. /// The endpoint type.
  105. typedef typename Protocol::endpoint endpoint_type;
  106. /// Construct an acceptor without opening it.
  107. /**
  108. * This constructor creates an acceptor without opening it to listen for new
  109. * connections. The open() function must be called before the acceptor can
  110. * accept new socket connections.
  111. *
  112. * @param ex The I/O executor that the acceptor will use, by default, to
  113. * dispatch handlers for any asynchronous operations performed on the
  114. * acceptor.
  115. */
  116. explicit basic_socket_acceptor(const executor_type& ex)
  117. : impl_(0, ex)
  118. {
  119. }
  120. /// Construct an acceptor without opening it.
  121. /**
  122. * This constructor creates an acceptor without opening it to listen for new
  123. * connections. The open() function must be called before the acceptor can
  124. * accept new socket connections.
  125. *
  126. * @param context An execution context which provides the I/O executor that
  127. * the acceptor will use, by default, to dispatch handlers for any
  128. * asynchronous operations performed on the acceptor.
  129. */
  130. template <typename ExecutionContext>
  131. explicit basic_socket_acceptor(ExecutionContext& context,
  132. typename constraint<
  133. is_convertible<ExecutionContext&, execution_context&>::value
  134. >::type = 0)
  135. : impl_(0, 0, context)
  136. {
  137. }
  138. /// Construct an open acceptor.
  139. /**
  140. * This constructor creates an acceptor and automatically opens it.
  141. *
  142. * @param ex The I/O executor that the acceptor will use, by default, to
  143. * dispatch handlers for any asynchronous operations performed on the
  144. * acceptor.
  145. *
  146. * @param protocol An object specifying protocol parameters to be used.
  147. *
  148. * @throws asio::system_error Thrown on failure.
  149. */
  150. basic_socket_acceptor(const executor_type& ex, const protocol_type& protocol)
  151. : impl_(0, ex)
  152. {
  153. asio::error_code ec;
  154. impl_.get_service().open(impl_.get_implementation(), protocol, ec);
  155. asio::detail::throw_error(ec, "open");
  156. }
  157. /// Construct an open acceptor.
  158. /**
  159. * This constructor creates an acceptor and automatically opens it.
  160. *
  161. * @param context An execution context which provides the I/O executor that
  162. * the acceptor will use, by default, to dispatch handlers for any
  163. * asynchronous operations performed on the acceptor.
  164. *
  165. * @param protocol An object specifying protocol parameters to be used.
  166. *
  167. * @throws asio::system_error Thrown on failure.
  168. */
  169. template <typename ExecutionContext>
  170. basic_socket_acceptor(ExecutionContext& context,
  171. const protocol_type& protocol,
  172. typename constraint<
  173. is_convertible<ExecutionContext&, execution_context&>::value,
  174. defaulted_constraint
  175. >::type = defaulted_constraint())
  176. : impl_(0, 0, context)
  177. {
  178. asio::error_code ec;
  179. impl_.get_service().open(impl_.get_implementation(), protocol, ec);
  180. asio::detail::throw_error(ec, "open");
  181. }
  182. /// Construct an acceptor opened on the given endpoint.
  183. /**
  184. * This constructor creates an acceptor and automatically opens it to listen
  185. * for new connections on the specified endpoint.
  186. *
  187. * @param ex The I/O executor that the acceptor will use, by default, to
  188. * dispatch handlers for any asynchronous operations performed on the
  189. * acceptor.
  190. *
  191. * @param endpoint An endpoint on the local machine on which the acceptor
  192. * will listen for new connections.
  193. *
  194. * @param reuse_addr Whether the constructor should set the socket option
  195. * socket_base::reuse_address.
  196. *
  197. * @throws asio::system_error Thrown on failure.
  198. *
  199. * @note This constructor is equivalent to the following code:
  200. * @code
  201. * basic_socket_acceptor<Protocol> acceptor(my_context);
  202. * acceptor.open(endpoint.protocol());
  203. * if (reuse_addr)
  204. * acceptor.set_option(socket_base::reuse_address(true));
  205. * acceptor.bind(endpoint);
  206. * acceptor.listen();
  207. * @endcode
  208. */
  209. basic_socket_acceptor(const executor_type& ex,
  210. const endpoint_type& endpoint, bool reuse_addr = true)
  211. : impl_(0, ex)
  212. {
  213. asio::error_code ec;
  214. const protocol_type protocol = endpoint.protocol();
  215. impl_.get_service().open(impl_.get_implementation(), protocol, ec);
  216. asio::detail::throw_error(ec, "open");
  217. if (reuse_addr)
  218. {
  219. impl_.get_service().set_option(impl_.get_implementation(),
  220. socket_base::reuse_address(true), ec);
  221. asio::detail::throw_error(ec, "set_option");
  222. }
  223. impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
  224. asio::detail::throw_error(ec, "bind");
  225. impl_.get_service().listen(impl_.get_implementation(),
  226. socket_base::max_listen_connections, ec);
  227. asio::detail::throw_error(ec, "listen");
  228. }
  229. /// Construct an acceptor opened on the given endpoint.
  230. /**
  231. * This constructor creates an acceptor and automatically opens it to listen
  232. * for new connections on the specified endpoint.
  233. *
  234. * @param context An execution context which provides the I/O executor that
  235. * the acceptor will use, by default, to dispatch handlers for any
  236. * asynchronous operations performed on the acceptor.
  237. *
  238. * @param endpoint An endpoint on the local machine on which the acceptor
  239. * will listen for new connections.
  240. *
  241. * @param reuse_addr Whether the constructor should set the socket option
  242. * socket_base::reuse_address.
  243. *
  244. * @throws asio::system_error Thrown on failure.
  245. *
  246. * @note This constructor is equivalent to the following code:
  247. * @code
  248. * basic_socket_acceptor<Protocol> acceptor(my_context);
  249. * acceptor.open(endpoint.protocol());
  250. * if (reuse_addr)
  251. * acceptor.set_option(socket_base::reuse_address(true));
  252. * acceptor.bind(endpoint);
  253. * acceptor.listen();
  254. * @endcode
  255. */
  256. template <typename ExecutionContext>
  257. basic_socket_acceptor(ExecutionContext& context,
  258. const endpoint_type& endpoint, bool reuse_addr = true,
  259. typename constraint<
  260. is_convertible<ExecutionContext&, execution_context&>::value
  261. >::type = 0)
  262. : impl_(0, 0, context)
  263. {
  264. asio::error_code ec;
  265. const protocol_type protocol = endpoint.protocol();
  266. impl_.get_service().open(impl_.get_implementation(), protocol, ec);
  267. asio::detail::throw_error(ec, "open");
  268. if (reuse_addr)
  269. {
  270. impl_.get_service().set_option(impl_.get_implementation(),
  271. socket_base::reuse_address(true), ec);
  272. asio::detail::throw_error(ec, "set_option");
  273. }
  274. impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
  275. asio::detail::throw_error(ec, "bind");
  276. impl_.get_service().listen(impl_.get_implementation(),
  277. socket_base::max_listen_connections, ec);
  278. asio::detail::throw_error(ec, "listen");
  279. }
  280. /// Construct a basic_socket_acceptor on an existing native acceptor.
  281. /**
  282. * This constructor creates an acceptor object to hold an existing native
  283. * acceptor.
  284. *
  285. * @param ex The I/O executor that the acceptor will use, by default, to
  286. * dispatch handlers for any asynchronous operations performed on the
  287. * acceptor.
  288. *
  289. * @param protocol An object specifying protocol parameters to be used.
  290. *
  291. * @param native_acceptor A native acceptor.
  292. *
  293. * @throws asio::system_error Thrown on failure.
  294. */
  295. basic_socket_acceptor(const executor_type& ex,
  296. const protocol_type& protocol, const native_handle_type& native_acceptor)
  297. : impl_(0, ex)
  298. {
  299. asio::error_code ec;
  300. impl_.get_service().assign(impl_.get_implementation(),
  301. protocol, native_acceptor, ec);
  302. asio::detail::throw_error(ec, "assign");
  303. }
  304. /// Construct a basic_socket_acceptor on an existing native acceptor.
  305. /**
  306. * This constructor creates an acceptor object to hold an existing native
  307. * acceptor.
  308. *
  309. * @param context An execution context which provides the I/O executor that
  310. * the acceptor will use, by default, to dispatch handlers for any
  311. * asynchronous operations performed on the acceptor.
  312. *
  313. * @param protocol An object specifying protocol parameters to be used.
  314. *
  315. * @param native_acceptor A native acceptor.
  316. *
  317. * @throws asio::system_error Thrown on failure.
  318. */
  319. template <typename ExecutionContext>
  320. basic_socket_acceptor(ExecutionContext& context,
  321. const protocol_type& protocol, const native_handle_type& native_acceptor,
  322. typename constraint<
  323. is_convertible<ExecutionContext&, execution_context&>::value
  324. >::type = 0)
  325. : impl_(0, 0, context)
  326. {
  327. asio::error_code ec;
  328. impl_.get_service().assign(impl_.get_implementation(),
  329. protocol, native_acceptor, ec);
  330. asio::detail::throw_error(ec, "assign");
  331. }
  332. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  333. /// Move-construct a basic_socket_acceptor from another.
  334. /**
  335. * This constructor moves an acceptor from one object to another.
  336. *
  337. * @param other The other basic_socket_acceptor object from which the move
  338. * will occur.
  339. *
  340. * @note Following the move, the moved-from object is in the same state as if
  341. * constructed using the @c basic_socket_acceptor(const executor_type&)
  342. * constructor.
  343. */
  344. basic_socket_acceptor(basic_socket_acceptor&& other) ASIO_NOEXCEPT
  345. : impl_(std::move(other.impl_))
  346. {
  347. }
  348. /// Move-assign a basic_socket_acceptor from another.
  349. /**
  350. * This assignment operator moves an acceptor from one object to another.
  351. *
  352. * @param other The other basic_socket_acceptor object from which the move
  353. * will occur.
  354. *
  355. * @note Following the move, the moved-from object is in the same state as if
  356. * constructed using the @c basic_socket_acceptor(const executor_type&)
  357. * constructor.
  358. */
  359. basic_socket_acceptor& operator=(basic_socket_acceptor&& other)
  360. {
  361. impl_ = std::move(other.impl_);
  362. return *this;
  363. }
  364. // All socket acceptors have access to each other's implementations.
  365. template <typename Protocol1, typename Executor1>
  366. friend class basic_socket_acceptor;
  367. /// Move-construct a basic_socket_acceptor from an acceptor of another
  368. /// protocol type.
  369. /**
  370. * This constructor moves an acceptor from one object to another.
  371. *
  372. * @param other The other basic_socket_acceptor object from which the move
  373. * will occur.
  374. *
  375. * @note Following the move, the moved-from object is in the same state as if
  376. * constructed using the @c basic_socket_acceptor(const executor_type&)
  377. * constructor.
  378. */
  379. template <typename Protocol1, typename Executor1>
  380. basic_socket_acceptor(basic_socket_acceptor<Protocol1, Executor1>&& other,
  381. typename constraint<
  382. is_convertible<Protocol1, Protocol>::value
  383. && is_convertible<Executor1, Executor>::value
  384. >::type = 0)
  385. : impl_(std::move(other.impl_))
  386. {
  387. }
  388. /// Move-assign a basic_socket_acceptor from an acceptor of another protocol
  389. /// type.
  390. /**
  391. * This assignment operator moves an acceptor from one object to another.
  392. *
  393. * @param other The other basic_socket_acceptor object from which the move
  394. * will occur.
  395. *
  396. * @note Following the move, the moved-from object is in the same state as if
  397. * constructed using the @c basic_socket_acceptor(const executor_type&)
  398. * constructor.
  399. */
  400. template <typename Protocol1, typename Executor1>
  401. typename constraint<
  402. is_convertible<Protocol1, Protocol>::value
  403. && is_convertible<Executor1, Executor>::value,
  404. basic_socket_acceptor&
  405. >::type operator=(basic_socket_acceptor<Protocol1, Executor1>&& other)
  406. {
  407. basic_socket_acceptor tmp(std::move(other));
  408. impl_ = std::move(tmp.impl_);
  409. return *this;
  410. }
  411. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  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. executor_type get_executor() ASIO_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 ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1190. ASIO_INITFN_AUTO_RESULT_TYPE(WaitToken,
  1191. void (asio::error_code))
  1192. async_wait(wait_type w,
  1193. ASIO_MOVE_ARG(WaitToken) token
  1194. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  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. typename constraint<
  1221. is_convertible<Protocol, Protocol1>::value
  1222. >::type = 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. typename constraint<
  1256. is_convertible<Protocol, Protocol1>::value
  1257. >::type = 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 ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1321. ASIO_INITFN_AUTO_RESULT_TYPE(AcceptToken,
  1322. void (asio::error_code))
  1323. async_accept(basic_socket<Protocol1, Executor1>& peer,
  1324. ASIO_MOVE_ARG(AcceptToken) token
  1325. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type),
  1326. typename constraint<
  1327. is_convertible<Protocol, Protocol1>::value
  1328. >::type = 0)
  1329. {
  1330. return async_initiate<AcceptToken, void (asio::error_code)>(
  1331. initiate_async_accept(this), token,
  1332. &peer, static_cast<endpoint_type*>(0));
  1333. }
  1334. /// Accept a new connection and obtain the endpoint of the peer
  1335. /**
  1336. * This function is used to accept a new connection from a peer into the
  1337. * given socket, and additionally provide the endpoint of the remote peer.
  1338. * The function call will block until a new connection has been accepted
  1339. * successfully or an error occurs.
  1340. *
  1341. * @param peer The socket into which the new connection will be accepted.
  1342. *
  1343. * @param peer_endpoint An endpoint object which will receive the endpoint of
  1344. * the remote peer.
  1345. *
  1346. * @throws asio::system_error Thrown on failure.
  1347. *
  1348. * @par Example
  1349. * @code
  1350. * asio::ip::tcp::acceptor acceptor(my_context);
  1351. * ...
  1352. * asio::ip::tcp::socket socket(my_context);
  1353. * asio::ip::tcp::endpoint endpoint;
  1354. * acceptor.accept(socket, endpoint);
  1355. * @endcode
  1356. */
  1357. template <typename Executor1>
  1358. void accept(basic_socket<protocol_type, Executor1>& peer,
  1359. endpoint_type& peer_endpoint)
  1360. {
  1361. asio::error_code ec;
  1362. impl_.get_service().accept(impl_.get_implementation(),
  1363. peer, &peer_endpoint, ec);
  1364. asio::detail::throw_error(ec, "accept");
  1365. }
  1366. /// Accept a new connection and obtain the endpoint of the peer
  1367. /**
  1368. * This function is used to accept a new connection from a peer into the
  1369. * given socket, and additionally provide the endpoint of the remote peer.
  1370. * The function call will block until a new connection has been accepted
  1371. * successfully or an error occurs.
  1372. *
  1373. * @param peer The socket into which the new connection will be accepted.
  1374. *
  1375. * @param peer_endpoint An endpoint object which will receive the endpoint of
  1376. * the remote peer.
  1377. *
  1378. * @param ec Set to indicate what error occurred, if any.
  1379. *
  1380. * @par Example
  1381. * @code
  1382. * asio::ip::tcp::acceptor acceptor(my_context);
  1383. * ...
  1384. * asio::ip::tcp::socket socket(my_context);
  1385. * asio::ip::tcp::endpoint endpoint;
  1386. * asio::error_code ec;
  1387. * acceptor.accept(socket, endpoint, ec);
  1388. * if (ec)
  1389. * {
  1390. * // An error occurred.
  1391. * }
  1392. * @endcode
  1393. */
  1394. template <typename Executor1>
  1395. ASIO_SYNC_OP_VOID accept(basic_socket<protocol_type, Executor1>& peer,
  1396. endpoint_type& peer_endpoint, asio::error_code& ec)
  1397. {
  1398. impl_.get_service().accept(
  1399. impl_.get_implementation(), peer, &peer_endpoint, ec);
  1400. ASIO_SYNC_OP_VOID_RETURN(ec);
  1401. }
  1402. /// Start an asynchronous accept.
  1403. /**
  1404. * This function is used to asynchronously accept a new connection into a
  1405. * socket, and additionally obtain the endpoint of the remote peer. It is an
  1406. * initiating function for an @ref asynchronous_operation, and always returns
  1407. * immediately.
  1408. *
  1409. * @param peer The socket into which the new connection will be accepted.
  1410. * Ownership of the peer object is retained by the caller, which must
  1411. * guarantee that it is valid until the completion handler is called.
  1412. *
  1413. * @param peer_endpoint An endpoint object into which the endpoint of the
  1414. * remote peer will be written. Ownership of the peer_endpoint object is
  1415. * retained by the caller, which must guarantee that it is valid until the
  1416. * handler is called.
  1417. *
  1418. * @param token The @ref completion_token that will be used to produce a
  1419. * completion handler, which will be called when the accept completes.
  1420. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  1421. * @ref yield_context, or a function object with the correct completion
  1422. * signature. The function signature of the completion handler must be:
  1423. * @code void handler(
  1424. * const asio::error_code& error // Result of operation.
  1425. * ); @endcode
  1426. * Regardless of whether the asynchronous operation completes immediately or
  1427. * not, the completion handler will not be invoked from within this function.
  1428. * On immediate completion, invocation of the handler will be performed in a
  1429. * manner equivalent to using asio::post().
  1430. *
  1431. * @par Completion Signature
  1432. * @code void(asio::error_code) @endcode
  1433. *
  1434. * @par Per-Operation Cancellation
  1435. * On POSIX or Windows operating systems, this asynchronous operation supports
  1436. * cancellation for the following asio::cancellation_type values:
  1437. *
  1438. * @li @c cancellation_type::terminal
  1439. *
  1440. * @li @c cancellation_type::partial
  1441. *
  1442. * @li @c cancellation_type::total
  1443. */
  1444. template <typename Executor1,
  1445. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code))
  1446. AcceptToken ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1447. ASIO_INITFN_AUTO_RESULT_TYPE(AcceptToken,
  1448. void (asio::error_code))
  1449. async_accept(basic_socket<protocol_type, Executor1>& peer,
  1450. endpoint_type& peer_endpoint,
  1451. ASIO_MOVE_ARG(AcceptToken) token
  1452. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  1453. {
  1454. return async_initiate<AcceptToken, void (asio::error_code)>(
  1455. initiate_async_accept(this), token, &peer, &peer_endpoint);
  1456. }
  1457. #endif // !defined(ASIO_NO_EXTENSIONS)
  1458. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  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. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1587. ASIO_INITFN_AUTO_RESULT_TYPE(MoveAcceptToken,
  1588. void (asio::error_code,
  1589. typename Protocol::socket::template
  1590. rebind_executor<executor_type>::other))
  1591. async_accept(
  1592. ASIO_MOVE_ARG(MoveAcceptToken) token
  1593. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  1594. {
  1595. return async_initiate<MoveAcceptToken,
  1596. void (asio::error_code, typename Protocol::socket::template
  1597. rebind_executor<executor_type>::other)>(
  1598. initiate_async_move_accept(this), token,
  1599. impl_.get_executor(), static_cast<endpoint_type*>(0),
  1600. static_cast<typename Protocol::socket::template
  1601. rebind_executor<executor_type>::other*>(0));
  1602. }
  1603. /// Accept a new connection.
  1604. /**
  1605. * This function is used to accept a new connection from a peer. The function
  1606. * call will block until a new connection has been accepted successfully or
  1607. * an error occurs.
  1608. *
  1609. * This overload requires that the Protocol template parameter satisfy the
  1610. * AcceptableProtocol type requirements.
  1611. *
  1612. * @param ex The I/O executor object to be used for the newly
  1613. * accepted socket.
  1614. *
  1615. * @returns A socket object representing the newly accepted connection.
  1616. *
  1617. * @throws asio::system_error Thrown on failure.
  1618. *
  1619. * @par Example
  1620. * @code
  1621. * asio::ip::tcp::acceptor acceptor(my_context);
  1622. * ...
  1623. * asio::ip::tcp::socket socket(acceptor.accept());
  1624. * @endcode
  1625. */
  1626. template <typename Executor1>
  1627. typename Protocol::socket::template rebind_executor<Executor1>::other
  1628. accept(const Executor1& ex,
  1629. typename constraint<
  1630. is_executor<Executor1>::value
  1631. || execution::is_executor<Executor1>::value
  1632. >::type = 0)
  1633. {
  1634. asio::error_code ec;
  1635. typename Protocol::socket::template
  1636. rebind_executor<Executor1>::other peer(ex);
  1637. impl_.get_service().accept(impl_.get_implementation(), peer, 0, ec);
  1638. asio::detail::throw_error(ec, "accept");
  1639. return peer;
  1640. }
  1641. /// Accept a new connection.
  1642. /**
  1643. * This function is used to accept a new connection from a peer. The function
  1644. * call will block until a new connection has been accepted successfully or
  1645. * an error occurs.
  1646. *
  1647. * This overload requires that the Protocol template parameter satisfy the
  1648. * AcceptableProtocol type requirements.
  1649. *
  1650. * @param context The I/O execution context object to be used for the newly
  1651. * accepted socket.
  1652. *
  1653. * @returns A socket object representing the newly accepted connection.
  1654. *
  1655. * @throws asio::system_error Thrown on failure.
  1656. *
  1657. * @par Example
  1658. * @code
  1659. * asio::ip::tcp::acceptor acceptor(my_context);
  1660. * ...
  1661. * asio::ip::tcp::socket socket(acceptor.accept());
  1662. * @endcode
  1663. */
  1664. template <typename ExecutionContext>
  1665. typename Protocol::socket::template rebind_executor<
  1666. typename ExecutionContext::executor_type>::other
  1667. accept(ExecutionContext& context,
  1668. typename constraint<
  1669. is_convertible<ExecutionContext&, execution_context&>::value
  1670. >::type = 0)
  1671. {
  1672. asio::error_code ec;
  1673. typename Protocol::socket::template rebind_executor<
  1674. typename ExecutionContext::executor_type>::other peer(context);
  1675. impl_.get_service().accept(impl_.get_implementation(), peer, 0, ec);
  1676. asio::detail::throw_error(ec, "accept");
  1677. return peer;
  1678. }
  1679. /// Accept a new connection.
  1680. /**
  1681. * This function is used to accept a new connection from a peer. The function
  1682. * call will block until a new connection has been accepted successfully or
  1683. * an error occurs.
  1684. *
  1685. * This overload requires that the Protocol template parameter satisfy the
  1686. * AcceptableProtocol type requirements.
  1687. *
  1688. * @param ex The I/O executor object to be used for the newly accepted
  1689. * socket.
  1690. *
  1691. * @param ec Set to indicate what error occurred, if any.
  1692. *
  1693. * @returns On success, a socket object representing the newly accepted
  1694. * connection. On error, a socket object where is_open() is false.
  1695. *
  1696. * @par Example
  1697. * @code
  1698. * asio::ip::tcp::acceptor acceptor(my_context);
  1699. * ...
  1700. * asio::ip::tcp::socket socket(acceptor.accept(my_context2, ec));
  1701. * if (ec)
  1702. * {
  1703. * // An error occurred.
  1704. * }
  1705. * @endcode
  1706. */
  1707. template <typename Executor1>
  1708. typename Protocol::socket::template rebind_executor<Executor1>::other
  1709. accept(const Executor1& ex, asio::error_code& ec,
  1710. typename constraint<
  1711. is_executor<Executor1>::value
  1712. || execution::is_executor<Executor1>::value
  1713. >::type = 0)
  1714. {
  1715. typename Protocol::socket::template
  1716. rebind_executor<Executor1>::other peer(ex);
  1717. impl_.get_service().accept(impl_.get_implementation(), peer, 0, ec);
  1718. return peer;
  1719. }
  1720. /// Accept a new connection.
  1721. /**
  1722. * This function is used to accept a new connection from a peer. The function
  1723. * call will block until a new connection has been accepted successfully or
  1724. * an error occurs.
  1725. *
  1726. * This overload requires that the Protocol template parameter satisfy the
  1727. * AcceptableProtocol type requirements.
  1728. *
  1729. * @param context The I/O execution context object to be used for the newly
  1730. * accepted socket.
  1731. *
  1732. * @param ec Set to indicate what error occurred, if any.
  1733. *
  1734. * @returns On success, a socket object representing the newly accepted
  1735. * connection. On error, a socket object where is_open() is false.
  1736. *
  1737. * @par Example
  1738. * @code
  1739. * asio::ip::tcp::acceptor acceptor(my_context);
  1740. * ...
  1741. * asio::ip::tcp::socket socket(acceptor.accept(my_context2, ec));
  1742. * if (ec)
  1743. * {
  1744. * // An error occurred.
  1745. * }
  1746. * @endcode
  1747. */
  1748. template <typename ExecutionContext>
  1749. typename Protocol::socket::template rebind_executor<
  1750. typename ExecutionContext::executor_type>::other
  1751. accept(ExecutionContext& context, asio::error_code& ec,
  1752. typename constraint<
  1753. is_convertible<ExecutionContext&, execution_context&>::value
  1754. >::type = 0)
  1755. {
  1756. typename Protocol::socket::template rebind_executor<
  1757. typename ExecutionContext::executor_type>::other peer(context);
  1758. impl_.get_service().accept(impl_.get_implementation(), peer, 0, ec);
  1759. return peer;
  1760. }
  1761. /// Start an asynchronous accept.
  1762. /**
  1763. * This function is used to asynchronously accept a new connection. It is an
  1764. * initiating function for an @ref asynchronous_operation, and always returns
  1765. * immediately.
  1766. *
  1767. * This overload requires that the Protocol template parameter satisfy the
  1768. * AcceptableProtocol type requirements.
  1769. *
  1770. * @param ex The I/O executor object to be used for the newly accepted
  1771. * socket.
  1772. *
  1773. * @param token The @ref completion_token that will be used to produce a
  1774. * completion handler, which will be called when the accept completes.
  1775. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  1776. * @ref yield_context, or a function object with the correct completion
  1777. * signature. The function signature of the completion handler must be:
  1778. * @code void handler(
  1779. * // Result of operation.
  1780. * const asio::error_code& error,
  1781. *
  1782. * // On success, the newly accepted socket.
  1783. * typename Protocol::socket::template rebind_executor<
  1784. * Executor1>::other peer
  1785. * ); @endcode
  1786. * Regardless of whether the asynchronous operation completes immediately or
  1787. * not, the completion handler will not be invoked from within this function.
  1788. * On immediate completion, invocation of the handler will be performed in a
  1789. * manner equivalent to using asio::post().
  1790. *
  1791. * @par Completion Signature
  1792. * @code void(asio::error_code,
  1793. * typename Protocol::socket::template rebind_executor<
  1794. * Executor1>::other)) @endcode
  1795. *
  1796. * @par Example
  1797. * @code
  1798. * void accept_handler(const asio::error_code& error,
  1799. * asio::ip::tcp::socket peer)
  1800. * {
  1801. * if (!error)
  1802. * {
  1803. * // Accept succeeded.
  1804. * }
  1805. * }
  1806. *
  1807. * ...
  1808. *
  1809. * asio::ip::tcp::acceptor acceptor(my_context);
  1810. * ...
  1811. * acceptor.async_accept(my_context2, accept_handler);
  1812. * @endcode
  1813. *
  1814. * @par Per-Operation Cancellation
  1815. * On POSIX or Windows operating systems, this asynchronous operation supports
  1816. * cancellation for the following asio::cancellation_type values:
  1817. *
  1818. * @li @c cancellation_type::terminal
  1819. *
  1820. * @li @c cancellation_type::partial
  1821. *
  1822. * @li @c cancellation_type::total
  1823. */
  1824. template <typename Executor1,
  1825. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  1826. typename Protocol::socket::template rebind_executor<
  1827. Executor1>::other)) MoveAcceptToken
  1828. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1829. ASIO_INITFN_AUTO_RESULT_TYPE(MoveAcceptToken,
  1830. void (asio::error_code,
  1831. typename Protocol::socket::template rebind_executor<
  1832. Executor1>::other))
  1833. async_accept(const Executor1& ex,
  1834. ASIO_MOVE_ARG(MoveAcceptToken) token
  1835. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type),
  1836. typename constraint<
  1837. is_executor<Executor1>::value
  1838. || execution::is_executor<Executor1>::value
  1839. >::type = 0)
  1840. {
  1841. typedef typename Protocol::socket::template rebind_executor<
  1842. Executor1>::other other_socket_type;
  1843. return async_initiate<MoveAcceptToken,
  1844. void (asio::error_code, other_socket_type)>(
  1845. initiate_async_move_accept(this), token,
  1846. ex, static_cast<endpoint_type*>(0),
  1847. static_cast<other_socket_type*>(0));
  1848. }
  1849. /// Start an asynchronous accept.
  1850. /**
  1851. * This function is used to asynchronously accept a new connection. It is an
  1852. * initiating function for an @ref asynchronous_operation, and always returns
  1853. * immediately.
  1854. *
  1855. * This overload requires that the Protocol template parameter satisfy the
  1856. * AcceptableProtocol type requirements.
  1857. *
  1858. * @param context The I/O execution context object to be used for the newly
  1859. * accepted socket.
  1860. *
  1861. * @param token The @ref completion_token that will be used to produce a
  1862. * completion handler, which will be called when the accept completes.
  1863. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  1864. * @ref yield_context, or a function object with the correct completion
  1865. * signature. The function signature of the completion handler must be:
  1866. * @code void handler(
  1867. * // Result of operation.
  1868. * const asio::error_code& error,
  1869. *
  1870. * // On success, the newly accepted socket.
  1871. * typename Protocol::socket::template rebind_executor<
  1872. * typename ExecutionContext::executor_type>::other peer
  1873. * ); @endcode
  1874. * Regardless of whether the asynchronous operation completes immediately or
  1875. * not, the completion handler will not be invoked from within this function.
  1876. * On immediate completion, invocation of the handler will be performed in a
  1877. * manner equivalent to using asio::post().
  1878. *
  1879. * @par Completion Signature
  1880. * @code void(asio::error_code,
  1881. * typename Protocol::socket::template rebind_executor<
  1882. * typename ExecutionContext::executor_type>::other)) @endcode
  1883. *
  1884. * @par Example
  1885. * @code
  1886. * void accept_handler(const asio::error_code& error,
  1887. * asio::ip::tcp::socket peer)
  1888. * {
  1889. * if (!error)
  1890. * {
  1891. * // Accept succeeded.
  1892. * }
  1893. * }
  1894. *
  1895. * ...
  1896. *
  1897. * asio::ip::tcp::acceptor acceptor(my_context);
  1898. * ...
  1899. * acceptor.async_accept(my_context2, accept_handler);
  1900. * @endcode
  1901. *
  1902. * @par Per-Operation Cancellation
  1903. * On POSIX or Windows operating systems, this asynchronous operation supports
  1904. * cancellation for the following asio::cancellation_type values:
  1905. *
  1906. * @li @c cancellation_type::terminal
  1907. *
  1908. * @li @c cancellation_type::partial
  1909. *
  1910. * @li @c cancellation_type::total
  1911. */
  1912. template <typename ExecutionContext,
  1913. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  1914. typename Protocol::socket::template rebind_executor<
  1915. typename ExecutionContext::executor_type>::other)) MoveAcceptToken
  1916. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  1917. ASIO_INITFN_AUTO_RESULT_TYPE(MoveAcceptToken,
  1918. void (asio::error_code,
  1919. typename Protocol::socket::template rebind_executor<
  1920. typename ExecutionContext::executor_type>::other))
  1921. async_accept(ExecutionContext& context,
  1922. ASIO_MOVE_ARG(MoveAcceptToken) token
  1923. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type),
  1924. typename constraint<
  1925. is_convertible<ExecutionContext&, execution_context&>::value
  1926. >::type = 0)
  1927. {
  1928. typedef typename Protocol::socket::template rebind_executor<
  1929. typename ExecutionContext::executor_type>::other other_socket_type;
  1930. return async_initiate<MoveAcceptToken,
  1931. void (asio::error_code, other_socket_type)>(
  1932. initiate_async_move_accept(this), token,
  1933. context.get_executor(), static_cast<endpoint_type*>(0),
  1934. static_cast<other_socket_type*>(0));
  1935. }
  1936. /// Accept a new connection.
  1937. /**
  1938. * This function is used to accept a new connection from a peer. The function
  1939. * call will block until a new connection has been accepted successfully or
  1940. * an error occurs.
  1941. *
  1942. * This overload requires that the Protocol template parameter satisfy the
  1943. * AcceptableProtocol type requirements.
  1944. *
  1945. * @param peer_endpoint An endpoint object into which the endpoint of the
  1946. * remote peer will be written.
  1947. *
  1948. * @returns A socket object representing the newly accepted connection.
  1949. *
  1950. * @throws asio::system_error Thrown on failure.
  1951. *
  1952. * @par Example
  1953. * @code
  1954. * asio::ip::tcp::acceptor acceptor(my_context);
  1955. * ...
  1956. * asio::ip::tcp::endpoint endpoint;
  1957. * asio::ip::tcp::socket socket(acceptor.accept(endpoint));
  1958. * @endcode
  1959. */
  1960. typename Protocol::socket::template rebind_executor<executor_type>::other
  1961. accept(endpoint_type& peer_endpoint)
  1962. {
  1963. asio::error_code ec;
  1964. typename Protocol::socket::template rebind_executor<
  1965. executor_type>::other peer(impl_.get_executor());
  1966. impl_.get_service().accept(impl_.get_implementation(),
  1967. peer, &peer_endpoint, ec);
  1968. asio::detail::throw_error(ec, "accept");
  1969. return peer;
  1970. }
  1971. /// Accept a new connection.
  1972. /**
  1973. * This function is used to accept a new connection from a peer. The function
  1974. * call will block until a new connection has been accepted successfully or
  1975. * an error occurs.
  1976. *
  1977. * This overload requires that the Protocol template parameter satisfy the
  1978. * AcceptableProtocol type requirements.
  1979. *
  1980. * @param peer_endpoint An endpoint object into which the endpoint of the
  1981. * remote peer will be written.
  1982. *
  1983. * @param ec Set to indicate what error occurred, if any.
  1984. *
  1985. * @returns On success, a socket object representing the newly accepted
  1986. * connection. On error, a socket object where is_open() is false.
  1987. *
  1988. * @par Example
  1989. * @code
  1990. * asio::ip::tcp::acceptor acceptor(my_context);
  1991. * ...
  1992. * asio::ip::tcp::endpoint endpoint;
  1993. * asio::ip::tcp::socket socket(acceptor.accept(endpoint, ec));
  1994. * if (ec)
  1995. * {
  1996. * // An error occurred.
  1997. * }
  1998. * @endcode
  1999. */
  2000. typename Protocol::socket::template rebind_executor<executor_type>::other
  2001. accept(endpoint_type& peer_endpoint, asio::error_code& ec)
  2002. {
  2003. typename Protocol::socket::template rebind_executor<
  2004. executor_type>::other peer(impl_.get_executor());
  2005. impl_.get_service().accept(impl_.get_implementation(),
  2006. peer, &peer_endpoint, ec);
  2007. return peer;
  2008. }
  2009. /// Start an asynchronous accept.
  2010. /**
  2011. * This function is used to asynchronously accept a new connection. It is an
  2012. * initiating function for an @ref asynchronous_operation, and always returns
  2013. * immediately.
  2014. *
  2015. * This overload requires that the Protocol template parameter satisfy the
  2016. * AcceptableProtocol type requirements.
  2017. *
  2018. * @param peer_endpoint An endpoint object into which the endpoint of the
  2019. * remote peer will be written. Ownership of the peer_endpoint object is
  2020. * retained by the caller, which must guarantee that it is valid until the
  2021. * completion handler is called.
  2022. *
  2023. * @param token The @ref completion_token that will be used to produce a
  2024. * completion handler, which will be called when the accept completes.
  2025. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  2026. * @ref yield_context, or a function object with the correct completion
  2027. * signature. The function signature of the completion handler must be:
  2028. * @code void handler(
  2029. * // Result of operation.
  2030. * const asio::error_code& error,
  2031. *
  2032. * // On success, the newly accepted socket.
  2033. * typename Protocol::socket::template
  2034. * rebind_executor<executor_type>::other peer
  2035. * ); @endcode
  2036. * Regardless of whether the asynchronous operation completes immediately or
  2037. * not, the completion handler will not be invoked from within this function.
  2038. * On immediate completion, invocation of the handler will be performed in a
  2039. * manner equivalent to using asio::post().
  2040. *
  2041. * @par Completion Signature
  2042. * @code void(asio::error_code,
  2043. * typename Protocol::socket::template
  2044. * rebind_executor<executor_type>::other)) @endcode
  2045. *
  2046. * @par Example
  2047. * @code
  2048. * void accept_handler(const asio::error_code& error,
  2049. * asio::ip::tcp::socket peer)
  2050. * {
  2051. * if (!error)
  2052. * {
  2053. * // Accept succeeded.
  2054. * }
  2055. * }
  2056. *
  2057. * ...
  2058. *
  2059. * asio::ip::tcp::acceptor acceptor(my_context);
  2060. * ...
  2061. * asio::ip::tcp::endpoint endpoint;
  2062. * acceptor.async_accept(endpoint, accept_handler);
  2063. * @endcode
  2064. *
  2065. * @par Per-Operation Cancellation
  2066. * On POSIX or Windows operating systems, this asynchronous operation supports
  2067. * cancellation for the following asio::cancellation_type values:
  2068. *
  2069. * @li @c cancellation_type::terminal
  2070. *
  2071. * @li @c cancellation_type::partial
  2072. *
  2073. * @li @c cancellation_type::total
  2074. */
  2075. template <
  2076. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  2077. typename Protocol::socket::template rebind_executor<
  2078. executor_type>::other)) MoveAcceptToken
  2079. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  2080. ASIO_INITFN_AUTO_RESULT_TYPE(MoveAcceptToken,
  2081. void (asio::error_code,
  2082. typename Protocol::socket::template
  2083. rebind_executor<executor_type>::other))
  2084. async_accept(endpoint_type& peer_endpoint,
  2085. ASIO_MOVE_ARG(MoveAcceptToken) token
  2086. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  2087. {
  2088. return async_initiate<MoveAcceptToken,
  2089. void (asio::error_code, typename Protocol::socket::template
  2090. rebind_executor<executor_type>::other)>(
  2091. initiate_async_move_accept(this), token,
  2092. impl_.get_executor(), &peer_endpoint,
  2093. static_cast<typename Protocol::socket::template
  2094. rebind_executor<executor_type>::other*>(0));
  2095. }
  2096. /// Accept a new connection.
  2097. /**
  2098. * This function is used to accept a new connection from a peer. The function
  2099. * call will block until a new connection has been accepted successfully or
  2100. * an error occurs.
  2101. *
  2102. * This overload requires that the Protocol template parameter satisfy the
  2103. * AcceptableProtocol type requirements.
  2104. *
  2105. * @param ex The I/O executor object to be used for the newly accepted
  2106. * socket.
  2107. *
  2108. * @param peer_endpoint An endpoint object into which the endpoint of the
  2109. * remote peer will be written.
  2110. *
  2111. * @returns A socket object representing the newly accepted connection.
  2112. *
  2113. * @throws asio::system_error Thrown on failure.
  2114. *
  2115. * @par Example
  2116. * @code
  2117. * asio::ip::tcp::acceptor acceptor(my_context);
  2118. * ...
  2119. * asio::ip::tcp::endpoint endpoint;
  2120. * asio::ip::tcp::socket socket(
  2121. * acceptor.accept(my_context2, endpoint));
  2122. * @endcode
  2123. */
  2124. template <typename Executor1>
  2125. typename Protocol::socket::template rebind_executor<Executor1>::other
  2126. accept(const Executor1& ex, endpoint_type& peer_endpoint,
  2127. typename constraint<
  2128. is_executor<Executor1>::value
  2129. || execution::is_executor<Executor1>::value
  2130. >::type = 0)
  2131. {
  2132. asio::error_code ec;
  2133. typename Protocol::socket::template
  2134. rebind_executor<Executor1>::other peer(ex);
  2135. impl_.get_service().accept(impl_.get_implementation(),
  2136. peer, &peer_endpoint, ec);
  2137. asio::detail::throw_error(ec, "accept");
  2138. return peer;
  2139. }
  2140. /// Accept a new connection.
  2141. /**
  2142. * This function is used to accept a new connection from a peer. The function
  2143. * call will block until a new connection has been accepted successfully or
  2144. * an error occurs.
  2145. *
  2146. * This overload requires that the Protocol template parameter satisfy the
  2147. * AcceptableProtocol type requirements.
  2148. *
  2149. * @param context The I/O execution context object to be used for the newly
  2150. * accepted socket.
  2151. *
  2152. * @param peer_endpoint An endpoint object into which the endpoint of the
  2153. * remote peer will be written.
  2154. *
  2155. * @returns A socket object representing the newly accepted connection.
  2156. *
  2157. * @throws asio::system_error Thrown on failure.
  2158. *
  2159. * @par Example
  2160. * @code
  2161. * asio::ip::tcp::acceptor acceptor(my_context);
  2162. * ...
  2163. * asio::ip::tcp::endpoint endpoint;
  2164. * asio::ip::tcp::socket socket(
  2165. * acceptor.accept(my_context2, endpoint));
  2166. * @endcode
  2167. */
  2168. template <typename ExecutionContext>
  2169. typename Protocol::socket::template rebind_executor<
  2170. typename ExecutionContext::executor_type>::other
  2171. accept(ExecutionContext& context, endpoint_type& peer_endpoint,
  2172. typename constraint<
  2173. is_convertible<ExecutionContext&, execution_context&>::value
  2174. >::type = 0)
  2175. {
  2176. asio::error_code ec;
  2177. typename Protocol::socket::template rebind_executor<
  2178. typename ExecutionContext::executor_type>::other peer(context);
  2179. impl_.get_service().accept(impl_.get_implementation(),
  2180. peer, &peer_endpoint, ec);
  2181. asio::detail::throw_error(ec, "accept");
  2182. return peer;
  2183. }
  2184. /// Accept a new connection.
  2185. /**
  2186. * This function is used to accept a new connection from a peer. The function
  2187. * call will block until a new connection has been accepted successfully or
  2188. * an error occurs.
  2189. *
  2190. * This overload requires that the Protocol template parameter satisfy the
  2191. * AcceptableProtocol type requirements.
  2192. *
  2193. * @param ex The I/O executor object to be used for the newly accepted
  2194. * socket.
  2195. *
  2196. * @param peer_endpoint An endpoint object into which the endpoint of the
  2197. * remote peer will be written.
  2198. *
  2199. * @param ec Set to indicate what error occurred, if any.
  2200. *
  2201. * @returns On success, a socket object representing the newly accepted
  2202. * connection. On error, a socket object where is_open() is false.
  2203. *
  2204. * @par Example
  2205. * @code
  2206. * asio::ip::tcp::acceptor acceptor(my_context);
  2207. * ...
  2208. * asio::ip::tcp::endpoint endpoint;
  2209. * asio::ip::tcp::socket socket(
  2210. * acceptor.accept(my_context2, endpoint, ec));
  2211. * if (ec)
  2212. * {
  2213. * // An error occurred.
  2214. * }
  2215. * @endcode
  2216. */
  2217. template <typename Executor1>
  2218. typename Protocol::socket::template rebind_executor<Executor1>::other
  2219. accept(const executor_type& ex,
  2220. endpoint_type& peer_endpoint, asio::error_code& ec,
  2221. typename constraint<
  2222. is_executor<Executor1>::value
  2223. || execution::is_executor<Executor1>::value
  2224. >::type = 0)
  2225. {
  2226. typename Protocol::socket::template
  2227. rebind_executor<Executor1>::other peer(ex);
  2228. impl_.get_service().accept(impl_.get_implementation(),
  2229. peer, &peer_endpoint, ec);
  2230. return peer;
  2231. }
  2232. /// Accept a new connection.
  2233. /**
  2234. * This function is used to accept a new connection from a peer. The function
  2235. * call will block until a new connection has been accepted successfully or
  2236. * an error occurs.
  2237. *
  2238. * This overload requires that the Protocol template parameter satisfy the
  2239. * AcceptableProtocol type requirements.
  2240. *
  2241. * @param context The I/O execution context object to be used for the newly
  2242. * accepted socket.
  2243. *
  2244. * @param peer_endpoint An endpoint object into which the endpoint of the
  2245. * remote peer will be written.
  2246. *
  2247. * @param ec Set to indicate what error occurred, if any.
  2248. *
  2249. * @returns On success, a socket object representing the newly accepted
  2250. * connection. On error, a socket object where is_open() is false.
  2251. *
  2252. * @par Example
  2253. * @code
  2254. * asio::ip::tcp::acceptor acceptor(my_context);
  2255. * ...
  2256. * asio::ip::tcp::endpoint endpoint;
  2257. * asio::ip::tcp::socket socket(
  2258. * acceptor.accept(my_context2, endpoint, ec));
  2259. * if (ec)
  2260. * {
  2261. * // An error occurred.
  2262. * }
  2263. * @endcode
  2264. */
  2265. template <typename ExecutionContext>
  2266. typename Protocol::socket::template rebind_executor<
  2267. typename ExecutionContext::executor_type>::other
  2268. accept(ExecutionContext& context,
  2269. endpoint_type& peer_endpoint, asio::error_code& ec,
  2270. typename constraint<
  2271. is_convertible<ExecutionContext&, execution_context&>::value
  2272. >::type = 0)
  2273. {
  2274. typename Protocol::socket::template rebind_executor<
  2275. typename ExecutionContext::executor_type>::other peer(context);
  2276. impl_.get_service().accept(impl_.get_implementation(),
  2277. peer, &peer_endpoint, ec);
  2278. return peer;
  2279. }
  2280. /// Start an asynchronous accept.
  2281. /**
  2282. * This function is used to asynchronously accept a new connection. It is an
  2283. * initiating function for an @ref asynchronous_operation, and always returns
  2284. * immediately.
  2285. *
  2286. * This overload requires that the Protocol template parameter satisfy the
  2287. * AcceptableProtocol type requirements.
  2288. *
  2289. * @param ex The I/O executor object to be used for the newly accepted
  2290. * socket.
  2291. *
  2292. * @param peer_endpoint An endpoint object into which the endpoint of the
  2293. * remote peer will be written. Ownership of the peer_endpoint object is
  2294. * retained by the caller, which must guarantee that it is valid until the
  2295. * completion handler is called.
  2296. *
  2297. * @param token The @ref completion_token that will be used to produce a
  2298. * completion handler, which will be called when the accept completes.
  2299. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  2300. * @ref yield_context, or a function object with the correct completion
  2301. * signature. The function signature of the completion handler must be:
  2302. * @code void handler(
  2303. * // Result of operation.
  2304. * const asio::error_code& error,
  2305. *
  2306. * // On success, the newly accepted socket.
  2307. * typename Protocol::socket::template rebind_executor<
  2308. * Executor1>::other peer
  2309. * ); @endcode
  2310. * Regardless of whether the asynchronous operation completes immediately or
  2311. * not, the completion handler will not be invoked from within this function.
  2312. * On immediate completion, invocation of the handler will be performed in a
  2313. * manner equivalent to using asio::post().
  2314. *
  2315. * @par Completion Signature
  2316. * @code void(asio::error_code,
  2317. * typename Protocol::socket::template rebind_executor<
  2318. * Executor1>::other)) @endcode
  2319. *
  2320. * @par Example
  2321. * @code
  2322. * void accept_handler(const asio::error_code& error,
  2323. * asio::ip::tcp::socket peer)
  2324. * {
  2325. * if (!error)
  2326. * {
  2327. * // Accept succeeded.
  2328. * }
  2329. * }
  2330. *
  2331. * ...
  2332. *
  2333. * asio::ip::tcp::acceptor acceptor(my_context);
  2334. * ...
  2335. * asio::ip::tcp::endpoint endpoint;
  2336. * acceptor.async_accept(my_context2, endpoint, accept_handler);
  2337. * @endcode
  2338. *
  2339. * @par Per-Operation Cancellation
  2340. * On POSIX or Windows operating systems, this asynchronous operation supports
  2341. * cancellation for the following asio::cancellation_type values:
  2342. *
  2343. * @li @c cancellation_type::terminal
  2344. *
  2345. * @li @c cancellation_type::partial
  2346. *
  2347. * @li @c cancellation_type::total
  2348. */
  2349. template <typename Executor1,
  2350. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  2351. typename Protocol::socket::template rebind_executor<
  2352. Executor1>::other)) MoveAcceptToken
  2353. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  2354. ASIO_INITFN_AUTO_RESULT_TYPE(MoveAcceptToken,
  2355. void (asio::error_code,
  2356. typename Protocol::socket::template rebind_executor<
  2357. Executor1>::other))
  2358. async_accept(const Executor1& ex, endpoint_type& peer_endpoint,
  2359. ASIO_MOVE_ARG(MoveAcceptToken) token
  2360. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type),
  2361. typename constraint<
  2362. is_executor<Executor1>::value
  2363. || execution::is_executor<Executor1>::value
  2364. >::type = 0)
  2365. {
  2366. typedef typename Protocol::socket::template rebind_executor<
  2367. Executor1>::other other_socket_type;
  2368. return async_initiate<MoveAcceptToken,
  2369. void (asio::error_code, other_socket_type)>(
  2370. initiate_async_move_accept(this), token,
  2371. ex, &peer_endpoint,
  2372. static_cast<other_socket_type*>(0));
  2373. }
  2374. /// Start an asynchronous accept.
  2375. /**
  2376. * This function is used to asynchronously accept a new connection. It is an
  2377. * initiating function for an @ref asynchronous_operation, and always returns
  2378. * immediately.
  2379. *
  2380. * This overload requires that the Protocol template parameter satisfy the
  2381. * AcceptableProtocol type requirements.
  2382. *
  2383. * @param context The I/O execution context object to be used for the newly
  2384. * accepted socket.
  2385. *
  2386. * @param peer_endpoint An endpoint object into which the endpoint of the
  2387. * remote peer will be written. Ownership of the peer_endpoint object is
  2388. * retained by the caller, which must guarantee that it is valid until the
  2389. * completion handler is called.
  2390. *
  2391. * @param token The @ref completion_token that will be used to produce a
  2392. * completion handler, which will be called when the accept completes.
  2393. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  2394. * @ref yield_context, or a function object with the correct completion
  2395. * signature. The function signature of the completion handler must be:
  2396. * @code void handler(
  2397. * // Result of operation.
  2398. * const asio::error_code& error,
  2399. *
  2400. * // On success, the newly accepted socket.
  2401. * typename Protocol::socket::template rebind_executor<
  2402. * typename ExecutionContext::executor_type>::other peer
  2403. * ); @endcode
  2404. * Regardless of whether the asynchronous operation completes immediately or
  2405. * not, the completion handler will not be invoked from within this function.
  2406. * On immediate completion, invocation of the handler will be performed in a
  2407. * manner equivalent to using asio::post().
  2408. *
  2409. * @par Completion Signature
  2410. * @code void(asio::error_code,
  2411. * typename Protocol::socket::template rebind_executor<
  2412. * typename ExecutionContext::executor_type>::other)) @endcode
  2413. *
  2414. * @par Example
  2415. * @code
  2416. * void accept_handler(const asio::error_code& error,
  2417. * asio::ip::tcp::socket peer)
  2418. * {
  2419. * if (!error)
  2420. * {
  2421. * // Accept succeeded.
  2422. * }
  2423. * }
  2424. *
  2425. * ...
  2426. *
  2427. * asio::ip::tcp::acceptor acceptor(my_context);
  2428. * ...
  2429. * asio::ip::tcp::endpoint endpoint;
  2430. * acceptor.async_accept(my_context2, endpoint, accept_handler);
  2431. * @endcode
  2432. *
  2433. * @par Per-Operation Cancellation
  2434. * On POSIX or Windows operating systems, this asynchronous operation supports
  2435. * cancellation for the following asio::cancellation_type values:
  2436. *
  2437. * @li @c cancellation_type::terminal
  2438. *
  2439. * @li @c cancellation_type::partial
  2440. *
  2441. * @li @c cancellation_type::total
  2442. */
  2443. template <typename ExecutionContext,
  2444. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  2445. typename Protocol::socket::template rebind_executor<
  2446. typename ExecutionContext::executor_type>::other)) MoveAcceptToken
  2447. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  2448. ASIO_INITFN_AUTO_RESULT_TYPE(MoveAcceptToken,
  2449. void (asio::error_code,
  2450. typename Protocol::socket::template rebind_executor<
  2451. typename ExecutionContext::executor_type>::other))
  2452. async_accept(ExecutionContext& context,
  2453. endpoint_type& peer_endpoint,
  2454. ASIO_MOVE_ARG(MoveAcceptToken) token
  2455. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type),
  2456. typename constraint<
  2457. is_convertible<ExecutionContext&, execution_context&>::value
  2458. >::type = 0)
  2459. {
  2460. typedef typename Protocol::socket::template rebind_executor<
  2461. typename ExecutionContext::executor_type>::other other_socket_type;
  2462. return async_initiate<MoveAcceptToken,
  2463. void (asio::error_code, other_socket_type)>(
  2464. initiate_async_move_accept(this), token,
  2465. context.get_executor(), &peer_endpoint,
  2466. static_cast<other_socket_type*>(0));
  2467. }
  2468. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  2469. private:
  2470. // Disallow copying and assignment.
  2471. basic_socket_acceptor(const basic_socket_acceptor&) ASIO_DELETED;
  2472. basic_socket_acceptor& operator=(
  2473. const basic_socket_acceptor&) ASIO_DELETED;
  2474. class initiate_async_wait
  2475. {
  2476. public:
  2477. typedef Executor executor_type;
  2478. explicit initiate_async_wait(basic_socket_acceptor* self)
  2479. : self_(self)
  2480. {
  2481. }
  2482. executor_type get_executor() const ASIO_NOEXCEPT
  2483. {
  2484. return self_->get_executor();
  2485. }
  2486. template <typename WaitHandler>
  2487. void operator()(ASIO_MOVE_ARG(WaitHandler) handler, wait_type w) const
  2488. {
  2489. // If you get an error on the following line it means that your handler
  2490. // does not meet the documented type requirements for a WaitHandler.
  2491. ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
  2492. detail::non_const_lvalue<WaitHandler> handler2(handler);
  2493. self_->impl_.get_service().async_wait(
  2494. self_->impl_.get_implementation(), w,
  2495. handler2.value, self_->impl_.get_executor());
  2496. }
  2497. private:
  2498. basic_socket_acceptor* self_;
  2499. };
  2500. class initiate_async_accept
  2501. {
  2502. public:
  2503. typedef Executor executor_type;
  2504. explicit initiate_async_accept(basic_socket_acceptor* self)
  2505. : self_(self)
  2506. {
  2507. }
  2508. executor_type get_executor() const ASIO_NOEXCEPT
  2509. {
  2510. return self_->get_executor();
  2511. }
  2512. template <typename AcceptHandler, typename Protocol1, typename Executor1>
  2513. void operator()(ASIO_MOVE_ARG(AcceptHandler) handler,
  2514. basic_socket<Protocol1, Executor1>* peer,
  2515. endpoint_type* peer_endpoint) const
  2516. {
  2517. // If you get an error on the following line it means that your handler
  2518. // does not meet the documented type requirements for a AcceptHandler.
  2519. ASIO_ACCEPT_HANDLER_CHECK(AcceptHandler, handler) type_check;
  2520. detail::non_const_lvalue<AcceptHandler> handler2(handler);
  2521. self_->impl_.get_service().async_accept(
  2522. self_->impl_.get_implementation(), *peer, peer_endpoint,
  2523. handler2.value, self_->impl_.get_executor());
  2524. }
  2525. private:
  2526. basic_socket_acceptor* self_;
  2527. };
  2528. class initiate_async_move_accept
  2529. {
  2530. public:
  2531. typedef Executor executor_type;
  2532. explicit initiate_async_move_accept(basic_socket_acceptor* self)
  2533. : self_(self)
  2534. {
  2535. }
  2536. executor_type get_executor() const ASIO_NOEXCEPT
  2537. {
  2538. return self_->get_executor();
  2539. }
  2540. template <typename MoveAcceptHandler, typename Executor1, typename Socket>
  2541. void operator()(ASIO_MOVE_ARG(MoveAcceptHandler) handler,
  2542. const Executor1& peer_ex, endpoint_type* peer_endpoint, Socket*) const
  2543. {
  2544. // If you get an error on the following line it means that your handler
  2545. // does not meet the documented type requirements for a MoveAcceptHandler.
  2546. ASIO_MOVE_ACCEPT_HANDLER_CHECK(
  2547. MoveAcceptHandler, handler, Socket) type_check;
  2548. detail::non_const_lvalue<MoveAcceptHandler> handler2(handler);
  2549. self_->impl_.get_service().async_move_accept(
  2550. self_->impl_.get_implementation(), peer_ex, peer_endpoint,
  2551. handler2.value, self_->impl_.get_executor());
  2552. }
  2553. private:
  2554. basic_socket_acceptor* self_;
  2555. };
  2556. #if defined(ASIO_WINDOWS_RUNTIME)
  2557. detail::io_object_impl<
  2558. detail::null_socket_service<Protocol>, Executor> impl_;
  2559. #elif defined(ASIO_HAS_IOCP)
  2560. detail::io_object_impl<
  2561. detail::win_iocp_socket_service<Protocol>, Executor> impl_;
  2562. #elif defined(ASIO_HAS_IO_URING_AS_DEFAULT)
  2563. detail::io_object_impl<
  2564. detail::io_uring_socket_service<Protocol>, Executor> impl_;
  2565. #else
  2566. detail::io_object_impl<
  2567. detail::reactive_socket_service<Protocol>, Executor> impl_;
  2568. #endif
  2569. };
  2570. } // namespace asio
  2571. #include "asio/detail/pop_options.hpp"
  2572. #endif // ASIO_BASIC_SOCKET_ACCEPTOR_HPP