Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

basic_socket_acceptor.hpp 79KB

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