basic_socket_acceptor.hpp 86 KB

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