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 57KB

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