basic_socket_acceptor.hpp 86 KB

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