basic_socket_acceptor.hpp 69 KB

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