basic_socket_acceptor.hpp 95 KB

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