basic_socket_acceptor.hpp 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710
  1. //
  2. // basic_socket_acceptor.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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::post().
  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::post().
  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::post().
  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::post().
  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::post().
  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::post().
  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::post().
  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::post().
  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::post().
  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