basic_socket_acceptor.hpp 69 KB

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