basic_socket_acceptor.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. //
  2. // basic_socket_acceptor.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 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/push_options.hpp"
  16. #include "asio/basic_io_object.hpp"
  17. #include "asio/basic_socket.hpp"
  18. #include "asio/error.hpp"
  19. #include "asio/socket_acceptor_service.hpp"
  20. #include "asio/socket_base.hpp"
  21. #include "asio/detail/throw_error.hpp"
  22. namespace asio {
  23. /// Provides the ability to accept new connections.
  24. /**
  25. * The basic_socket_acceptor class template is used for accepting new socket
  26. * connections.
  27. *
  28. * @par Thread Safety
  29. * @e Distinct @e objects: Safe.@n
  30. * @e Shared @e objects: Unsafe.
  31. *
  32. * @par Example
  33. * Opening a socket acceptor with the SO_REUSEADDR option enabled:
  34. * @code
  35. * asio::ip::tcp::acceptor acceptor(io_service);
  36. * asio::ip::tcp::endpoint endpoint(asio::ip::tcp::v4(), port);
  37. * acceptor.open(endpoint.protocol());
  38. * acceptor.set_option(asio::ip::tcp::acceptor::reuse_address(true));
  39. * acceptor.bind(endpoint);
  40. * acceptor.listen();
  41. * @endcode
  42. */
  43. template <typename Protocol,
  44. typename SocketAcceptorService = socket_acceptor_service<Protocol> >
  45. class basic_socket_acceptor
  46. : public basic_io_object<SocketAcceptorService>,
  47. public socket_base
  48. {
  49. public:
  50. /// The native representation of an acceptor.
  51. typedef typename SocketAcceptorService::native_type native_type;
  52. /// The protocol type.
  53. typedef Protocol protocol_type;
  54. /// The endpoint type.
  55. typedef typename Protocol::endpoint endpoint_type;
  56. /// Construct an acceptor without opening it.
  57. /**
  58. * This constructor creates an acceptor without opening it to listen for new
  59. * connections. The open() function must be called before the acceptor can
  60. * accept new socket connections.
  61. *
  62. * @param io_service The io_service object that the acceptor will use to
  63. * dispatch handlers for any asynchronous operations performed on the
  64. * acceptor.
  65. */
  66. explicit basic_socket_acceptor(asio::io_service& io_service)
  67. : basic_io_object<SocketAcceptorService>(io_service)
  68. {
  69. }
  70. /// Construct an open acceptor.
  71. /**
  72. * This constructor creates an acceptor and automatically opens it.
  73. *
  74. * @param io_service The io_service object that the acceptor will use to
  75. * dispatch handlers for any asynchronous operations performed on the
  76. * acceptor.
  77. *
  78. * @param protocol An object specifying protocol parameters to be used.
  79. *
  80. * @throws asio::system_error Thrown on failure.
  81. */
  82. basic_socket_acceptor(asio::io_service& io_service,
  83. const protocol_type& protocol)
  84. : basic_io_object<SocketAcceptorService>(io_service)
  85. {
  86. asio::error_code ec;
  87. this->service.open(this->implementation, protocol, ec);
  88. asio::detail::throw_error(ec);
  89. }
  90. /// Construct an acceptor opened on the given endpoint.
  91. /**
  92. * This constructor creates an acceptor and automatically opens it to listen
  93. * for new connections on the specified endpoint.
  94. *
  95. * @param io_service The io_service object that the acceptor will use to
  96. * dispatch handlers for any asynchronous operations performed on the
  97. * acceptor.
  98. *
  99. * @param endpoint An endpoint on the local machine on which the acceptor
  100. * will listen for new connections.
  101. *
  102. * @param reuse_addr Whether the constructor should set the socket option
  103. * socket_base::reuse_address.
  104. *
  105. * @throws asio::system_error Thrown on failure.
  106. *
  107. * @note This constructor is equivalent to the following code:
  108. * @code
  109. * basic_socket_acceptor<Protocol> acceptor(io_service);
  110. * acceptor.open(endpoint.protocol());
  111. * if (reuse_addr)
  112. * acceptor.set_option(socket_base::reuse_address(true));
  113. * acceptor.bind(endpoint);
  114. * acceptor.listen(listen_backlog);
  115. * @endcode
  116. */
  117. basic_socket_acceptor(asio::io_service& io_service,
  118. const endpoint_type& endpoint, bool reuse_addr = true)
  119. : basic_io_object<SocketAcceptorService>(io_service)
  120. {
  121. asio::error_code ec;
  122. this->service.open(this->implementation, endpoint.protocol(), ec);
  123. asio::detail::throw_error(ec);
  124. if (reuse_addr)
  125. {
  126. this->service.set_option(this->implementation,
  127. socket_base::reuse_address(true), ec);
  128. asio::detail::throw_error(ec);
  129. }
  130. this->service.bind(this->implementation, endpoint, ec);
  131. asio::detail::throw_error(ec);
  132. this->service.listen(this->implementation,
  133. socket_base::max_connections, ec);
  134. asio::detail::throw_error(ec);
  135. }
  136. /// Construct a basic_socket_acceptor on an existing native acceptor.
  137. /**
  138. * This constructor creates an acceptor object to hold an existing native
  139. * acceptor.
  140. *
  141. * @param io_service The io_service object that the acceptor will use to
  142. * dispatch handlers for any asynchronous operations performed on the
  143. * acceptor.
  144. *
  145. * @param protocol An object specifying protocol parameters to be used.
  146. *
  147. * @param native_acceptor A native acceptor.
  148. *
  149. * @throws asio::system_error Thrown on failure.
  150. */
  151. basic_socket_acceptor(asio::io_service& io_service,
  152. const protocol_type& protocol, const native_type& native_acceptor)
  153. : basic_io_object<SocketAcceptorService>(io_service)
  154. {
  155. asio::error_code ec;
  156. this->service.assign(this->implementation, protocol, native_acceptor, ec);
  157. asio::detail::throw_error(ec);
  158. }
  159. /// Open the acceptor using the specified protocol.
  160. /**
  161. * This function opens the socket acceptor so that it will use the specified
  162. * protocol.
  163. *
  164. * @param protocol An object specifying which protocol is to be used.
  165. *
  166. * @throws asio::system_error Thrown on failure.
  167. *
  168. * @par Example
  169. * @code
  170. * asio::ip::tcp::acceptor acceptor(io_service);
  171. * acceptor.open(asio::ip::tcp::v4());
  172. * @endcode
  173. */
  174. void open(const protocol_type& protocol = protocol_type())
  175. {
  176. asio::error_code ec;
  177. this->service.open(this->implementation, protocol, ec);
  178. asio::detail::throw_error(ec);
  179. }
  180. /// Open the acceptor using the specified protocol.
  181. /**
  182. * This function opens the socket acceptor so that it will use the specified
  183. * protocol.
  184. *
  185. * @param protocol An object specifying which protocol is to be used.
  186. *
  187. * @param ec Set to indicate what error occurred, if any.
  188. *
  189. * @par Example
  190. * @code
  191. * asio::ip::tcp::acceptor acceptor(io_service);
  192. * asio::error_code ec;
  193. * acceptor.open(asio::ip::tcp::v4(), ec);
  194. * if (ec)
  195. * {
  196. * // An error occurred.
  197. * }
  198. * @endcode
  199. */
  200. asio::error_code open(const protocol_type& protocol,
  201. asio::error_code& ec)
  202. {
  203. return this->service.open(this->implementation, protocol, ec);
  204. }
  205. /// Assigns an existing native acceptor to the acceptor.
  206. /*
  207. * This function opens the acceptor to hold an existing native acceptor.
  208. *
  209. * @param protocol An object specifying which protocol is to be used.
  210. *
  211. * @param native_acceptor A native acceptor.
  212. *
  213. * @throws asio::system_error Thrown on failure.
  214. */
  215. void assign(const protocol_type& protocol, const native_type& native_acceptor)
  216. {
  217. asio::error_code ec;
  218. this->service.assign(this->implementation, protocol, native_acceptor, ec);
  219. asio::detail::throw_error(ec);
  220. }
  221. /// Assigns an existing native acceptor to the acceptor.
  222. /*
  223. * This function opens the acceptor to hold an existing native acceptor.
  224. *
  225. * @param protocol An object specifying which protocol is to be used.
  226. *
  227. * @param native_acceptor A native acceptor.
  228. *
  229. * @param ec Set to indicate what error occurred, if any.
  230. */
  231. asio::error_code assign(const protocol_type& protocol,
  232. const native_type& native_acceptor, asio::error_code& ec)
  233. {
  234. return this->service.assign(this->implementation,
  235. protocol, native_acceptor, ec);
  236. }
  237. /// Determine whether the acceptor is open.
  238. bool is_open() const
  239. {
  240. return this->service.is_open(this->implementation);
  241. }
  242. /// Bind the acceptor to the given local endpoint.
  243. /**
  244. * This function binds the socket acceptor to the specified endpoint on the
  245. * local machine.
  246. *
  247. * @param endpoint An endpoint on the local machine to which the socket
  248. * acceptor will be bound.
  249. *
  250. * @throws asio::system_error Thrown on failure.
  251. *
  252. * @par Example
  253. * @code
  254. * asio::ip::tcp::acceptor acceptor(io_service);
  255. * acceptor.open(asio::ip::tcp::v4());
  256. * acceptor.bind(asio::ip::tcp::endpoint(12345));
  257. * @endcode
  258. */
  259. void bind(const endpoint_type& endpoint)
  260. {
  261. asio::error_code ec;
  262. this->service.bind(this->implementation, endpoint, ec);
  263. asio::detail::throw_error(ec);
  264. }
  265. /// Bind the acceptor to the given local endpoint.
  266. /**
  267. * This function binds the socket acceptor to the specified endpoint on the
  268. * local machine.
  269. *
  270. * @param endpoint An endpoint on the local machine to which the socket
  271. * acceptor will be bound.
  272. *
  273. * @param ec Set to indicate what error occurred, if any.
  274. *
  275. * @par Example
  276. * @code
  277. * asio::ip::tcp::acceptor acceptor(io_service);
  278. * acceptor.open(asio::ip::tcp::v4());
  279. * asio::error_code ec;
  280. * acceptor.bind(asio::ip::tcp::endpoint(12345), ec);
  281. * if (ec)
  282. * {
  283. * // An error occurred.
  284. * }
  285. * @endcode
  286. */
  287. asio::error_code bind(const endpoint_type& endpoint,
  288. asio::error_code& ec)
  289. {
  290. return this->service.bind(this->implementation, endpoint, ec);
  291. }
  292. /// Place the acceptor into the state where it will listen for new
  293. /// connections.
  294. /**
  295. * This function puts the socket acceptor into the state where it may accept
  296. * new connections.
  297. *
  298. * @param backlog The maximum length of the queue of pending connections.
  299. *
  300. * @throws asio::system_error Thrown on failure.
  301. */
  302. void listen(int backlog = socket_base::max_connections)
  303. {
  304. asio::error_code ec;
  305. this->service.listen(this->implementation, backlog, ec);
  306. asio::detail::throw_error(ec);
  307. }
  308. /// Place the acceptor into the state where it will listen for new
  309. /// connections.
  310. /**
  311. * This function puts the socket acceptor into the state where it may accept
  312. * new connections.
  313. *
  314. * @param backlog The maximum length of the queue of pending connections.
  315. *
  316. * @param ec Set to indicate what error occurred, if any.
  317. *
  318. * @par Example
  319. * @code
  320. * asio::ip::tcp::acceptor acceptor(io_service);
  321. * ...
  322. * asio::error_code ec;
  323. * acceptor.listen(asio::socket_base::max_connections, ec);
  324. * if (ec)
  325. * {
  326. * // An error occurred.
  327. * }
  328. * @endcode
  329. */
  330. asio::error_code listen(int backlog, asio::error_code& ec)
  331. {
  332. return this->service.listen(this->implementation, backlog, ec);
  333. }
  334. /// Close the acceptor.
  335. /**
  336. * This function is used to close the acceptor. Any asynchronous accept
  337. * operations will be cancelled immediately.
  338. *
  339. * A subsequent call to open() is required before the acceptor can again be
  340. * used to again perform socket accept operations.
  341. *
  342. * @throws asio::system_error Thrown on failure.
  343. */
  344. void close()
  345. {
  346. asio::error_code ec;
  347. this->service.close(this->implementation, ec);
  348. asio::detail::throw_error(ec);
  349. }
  350. /// Close the acceptor.
  351. /**
  352. * This function is used to close the acceptor. Any asynchronous accept
  353. * operations will be cancelled immediately.
  354. *
  355. * A subsequent call to open() is required before the acceptor can again be
  356. * used to again perform socket accept operations.
  357. *
  358. * @param ec Set to indicate what error occurred, if any.
  359. *
  360. * @par Example
  361. * @code
  362. * asio::ip::tcp::acceptor acceptor(io_service);
  363. * ...
  364. * asio::error_code ec;
  365. * acceptor.close(ec);
  366. * if (ec)
  367. * {
  368. * // An error occurred.
  369. * }
  370. * @endcode
  371. */
  372. asio::error_code close(asio::error_code& ec)
  373. {
  374. return this->service.close(this->implementation, ec);
  375. }
  376. /// Get the native acceptor representation.
  377. /**
  378. * This function may be used to obtain the underlying representation of the
  379. * acceptor. This is intended to allow access to native acceptor functionality
  380. * that is not otherwise provided.
  381. */
  382. native_type native()
  383. {
  384. return this->service.native(this->implementation);
  385. }
  386. /// Cancel all asynchronous operations associated with the acceptor.
  387. /**
  388. * This function causes all outstanding asynchronous connect, send and receive
  389. * operations to finish immediately, and the handlers for cancelled operations
  390. * will be passed the asio::error::operation_aborted error.
  391. *
  392. * @throws asio::system_error Thrown on failure.
  393. */
  394. void cancel()
  395. {
  396. asio::error_code ec;
  397. this->service.cancel(this->implementation, ec);
  398. asio::detail::throw_error(ec);
  399. }
  400. /// Cancel all asynchronous operations associated with the acceptor.
  401. /**
  402. * This function causes all outstanding asynchronous connect, send and receive
  403. * operations to finish immediately, and the handlers for cancelled operations
  404. * will be passed the asio::error::operation_aborted error.
  405. *
  406. * @param ec Set to indicate what error occurred, if any.
  407. */
  408. asio::error_code cancel(asio::error_code& ec)
  409. {
  410. return this->service.cancel(this->implementation, ec);
  411. }
  412. /// Set an option on the acceptor.
  413. /**
  414. * This function is used to set an option on the acceptor.
  415. *
  416. * @param option The new option value to be set on the acceptor.
  417. *
  418. * @throws asio::system_error Thrown on failure.
  419. *
  420. * @sa SettableSocketOption @n
  421. * asio::socket_base::reuse_address
  422. * asio::socket_base::enable_connection_aborted
  423. *
  424. * @par Example
  425. * Setting the SOL_SOCKET/SO_REUSEADDR option:
  426. * @code
  427. * asio::ip::tcp::acceptor acceptor(io_service);
  428. * ...
  429. * asio::ip::tcp::acceptor::reuse_address option(true);
  430. * acceptor.set_option(option);
  431. * @endcode
  432. */
  433. template <typename SettableSocketOption>
  434. void set_option(const SettableSocketOption& option)
  435. {
  436. asio::error_code ec;
  437. this->service.set_option(this->implementation, option, ec);
  438. asio::detail::throw_error(ec);
  439. }
  440. /// Set an option on the acceptor.
  441. /**
  442. * This function is used to set an option on the acceptor.
  443. *
  444. * @param option The new option value to be set on the acceptor.
  445. *
  446. * @param ec Set to indicate what error occurred, if any.
  447. *
  448. * @sa SettableSocketOption @n
  449. * asio::socket_base::reuse_address
  450. * asio::socket_base::enable_connection_aborted
  451. *
  452. * @par Example
  453. * Setting the SOL_SOCKET/SO_REUSEADDR option:
  454. * @code
  455. * asio::ip::tcp::acceptor acceptor(io_service);
  456. * ...
  457. * asio::ip::tcp::acceptor::reuse_address option(true);
  458. * asio::error_code ec;
  459. * acceptor.set_option(option, ec);
  460. * if (ec)
  461. * {
  462. * // An error occurred.
  463. * }
  464. * @endcode
  465. */
  466. template <typename SettableSocketOption>
  467. asio::error_code set_option(const SettableSocketOption& option,
  468. asio::error_code& ec)
  469. {
  470. return this->service.set_option(this->implementation, option, ec);
  471. }
  472. /// Get an option from the acceptor.
  473. /**
  474. * This function is used to get the current value of an option on the
  475. * acceptor.
  476. *
  477. * @param option The option value to be obtained from the acceptor.
  478. *
  479. * @throws asio::system_error Thrown on failure.
  480. *
  481. * @sa GettableSocketOption @n
  482. * asio::socket_base::reuse_address
  483. *
  484. * @par Example
  485. * Getting the value of the SOL_SOCKET/SO_REUSEADDR option:
  486. * @code
  487. * asio::ip::tcp::acceptor acceptor(io_service);
  488. * ...
  489. * asio::ip::tcp::acceptor::reuse_address option;
  490. * acceptor.get_option(option);
  491. * bool is_set = option.get();
  492. * @endcode
  493. */
  494. template <typename GettableSocketOption>
  495. void get_option(GettableSocketOption& option)
  496. {
  497. asio::error_code ec;
  498. this->service.get_option(this->implementation, option, ec);
  499. asio::detail::throw_error(ec);
  500. }
  501. /// Get an option from the acceptor.
  502. /**
  503. * This function is used to get the current value of an option on the
  504. * acceptor.
  505. *
  506. * @param option The option value to be obtained from the acceptor.
  507. *
  508. * @param ec Set to indicate what error occurred, if any.
  509. *
  510. * @sa GettableSocketOption @n
  511. * asio::socket_base::reuse_address
  512. *
  513. * @par Example
  514. * Getting the value of the SOL_SOCKET/SO_REUSEADDR option:
  515. * @code
  516. * asio::ip::tcp::acceptor acceptor(io_service);
  517. * ...
  518. * asio::ip::tcp::acceptor::reuse_address option;
  519. * asio::error_code ec;
  520. * acceptor.get_option(option, ec);
  521. * if (ec)
  522. * {
  523. * // An error occurred.
  524. * }
  525. * bool is_set = option.get();
  526. * @endcode
  527. */
  528. template <typename GettableSocketOption>
  529. asio::error_code get_option(GettableSocketOption& option,
  530. asio::error_code& ec)
  531. {
  532. return this->service.get_option(this->implementation, option, ec);
  533. }
  534. /// Get the local endpoint of the acceptor.
  535. /**
  536. * This function is used to obtain the locally bound endpoint of the acceptor.
  537. *
  538. * @returns An object that represents the local endpoint of the acceptor.
  539. *
  540. * @throws asio::system_error Thrown on failure.
  541. *
  542. * @par Example
  543. * @code
  544. * asio::ip::tcp::acceptor acceptor(io_service);
  545. * ...
  546. * asio::ip::tcp::endpoint endpoint = acceptor.local_endpoint();
  547. * @endcode
  548. */
  549. endpoint_type local_endpoint() const
  550. {
  551. asio::error_code ec;
  552. endpoint_type ep = this->service.local_endpoint(this->implementation, ec);
  553. asio::detail::throw_error(ec);
  554. return ep;
  555. }
  556. /// Get the local endpoint of the acceptor.
  557. /**
  558. * This function is used to obtain the locally bound endpoint of the acceptor.
  559. *
  560. * @param ec Set to indicate what error occurred, if any.
  561. *
  562. * @returns An object that represents the local endpoint of the acceptor.
  563. * Returns a default-constructed endpoint object if an error occurred and the
  564. * error handler did not throw an exception.
  565. *
  566. * @par Example
  567. * @code
  568. * asio::ip::tcp::acceptor acceptor(io_service);
  569. * ...
  570. * asio::error_code ec;
  571. * asio::ip::tcp::endpoint endpoint = acceptor.local_endpoint(ec);
  572. * if (ec)
  573. * {
  574. * // An error occurred.
  575. * }
  576. * @endcode
  577. */
  578. endpoint_type local_endpoint(asio::error_code& ec) const
  579. {
  580. return this->service.local_endpoint(this->implementation, ec);
  581. }
  582. /// Accept a new connection.
  583. /**
  584. * This function is used to accept a new connection from a peer into the
  585. * given socket. The function call will block until a new connection has been
  586. * accepted successfully or an error occurs.
  587. *
  588. * @param peer The socket into which the new connection will be accepted.
  589. *
  590. * @throws asio::system_error Thrown on failure.
  591. *
  592. * @par Example
  593. * @code
  594. * asio::ip::tcp::acceptor acceptor(io_service);
  595. * ...
  596. * asio::ip::tcp::socket socket(io_service);
  597. * acceptor.accept(socket);
  598. * @endcode
  599. */
  600. template <typename SocketService>
  601. void accept(basic_socket<protocol_type, SocketService>& peer)
  602. {
  603. asio::error_code ec;
  604. this->service.accept(this->implementation, peer, 0, ec);
  605. asio::detail::throw_error(ec);
  606. }
  607. /// Accept a new connection.
  608. /**
  609. * This function is used to accept a new connection from a peer into the
  610. * given socket. The function call will block until a new connection has been
  611. * accepted successfully or an error occurs.
  612. *
  613. * @param peer The socket into which the new connection will be accepted.
  614. *
  615. * @param ec Set to indicate what error occurred, if any.
  616. *
  617. * @par Example
  618. * @code
  619. * asio::ip::tcp::acceptor acceptor(io_service);
  620. * ...
  621. * asio::ip::tcp::soocket socket(io_service);
  622. * asio::error_code ec;
  623. * acceptor.accept(socket, ec);
  624. * if (ec)
  625. * {
  626. * // An error occurred.
  627. * }
  628. * @endcode
  629. */
  630. template <typename SocketService>
  631. asio::error_code accept(
  632. basic_socket<protocol_type, SocketService>& peer,
  633. asio::error_code& ec)
  634. {
  635. return this->service.accept(this->implementation, peer, 0, ec);
  636. }
  637. /// Start an asynchronous accept.
  638. /**
  639. * This function is used to asynchronously accept a new connection into a
  640. * socket. The function call always returns immediately.
  641. *
  642. * @param peer The socket into which the new connection will be accepted.
  643. * Ownership of the peer object is retained by the caller, which must
  644. * guarantee that it is valid until the handler is called.
  645. *
  646. * @param handler The handler to be called when the accept operation
  647. * completes. Copies will be made of the handler as required. The function
  648. * signature of the handler must be:
  649. * @code void handler(
  650. * const asio::error_code& error // Result of operation.
  651. * ); @endcode
  652. * Regardless of whether the asynchronous operation completes immediately or
  653. * not, the handler will not be invoked from within this function. Invocation
  654. * of the handler will be performed in a manner equivalent to using
  655. * asio::io_service::post().
  656. *
  657. * @par Example
  658. * @code
  659. * void accept_handler(const asio::error_code& error)
  660. * {
  661. * if (!error)
  662. * {
  663. * // Accept succeeded.
  664. * }
  665. * }
  666. *
  667. * ...
  668. *
  669. * asio::ip::tcp::acceptor acceptor(io_service);
  670. * ...
  671. * asio::ip::tcp::socket socket(io_service);
  672. * acceptor.async_accept(socket, accept_handler);
  673. * @endcode
  674. */
  675. template <typename SocketService, typename AcceptHandler>
  676. void async_accept(basic_socket<protocol_type, SocketService>& peer,
  677. AcceptHandler handler)
  678. {
  679. this->service.async_accept(this->implementation, peer, 0, handler);
  680. }
  681. /// Accept a new connection and obtain the endpoint of the peer
  682. /**
  683. * This function is used to accept a new connection from a peer into the
  684. * given socket, and additionally provide the endpoint of the remote peer.
  685. * The function call will block until a new connection has been accepted
  686. * successfully or an error occurs.
  687. *
  688. * @param peer The socket into which the new connection will be accepted.
  689. *
  690. * @param peer_endpoint An endpoint object which will receive the endpoint of
  691. * the remote peer.
  692. *
  693. * @throws asio::system_error Thrown on failure.
  694. *
  695. * @par Example
  696. * @code
  697. * asio::ip::tcp::acceptor acceptor(io_service);
  698. * ...
  699. * asio::ip::tcp::socket socket(io_service);
  700. * asio::ip::tcp::endpoint endpoint;
  701. * acceptor.accept(socket, endpoint);
  702. * @endcode
  703. */
  704. template <typename SocketService>
  705. void accept(basic_socket<protocol_type, SocketService>& peer,
  706. endpoint_type& peer_endpoint)
  707. {
  708. asio::error_code ec;
  709. this->service.accept(this->implementation, peer, &peer_endpoint, ec);
  710. asio::detail::throw_error(ec);
  711. }
  712. /// Accept a new connection and obtain the endpoint of the peer
  713. /**
  714. * This function is used to accept a new connection from a peer into the
  715. * given socket, and additionally provide the endpoint of the remote peer.
  716. * The function call will block until a new connection has been accepted
  717. * successfully or an error occurs.
  718. *
  719. * @param peer The socket into which the new connection will be accepted.
  720. *
  721. * @param peer_endpoint An endpoint object which will receive the endpoint of
  722. * the remote peer.
  723. *
  724. * @param ec Set to indicate what error occurred, if any.
  725. *
  726. * @par Example
  727. * @code
  728. * asio::ip::tcp::acceptor acceptor(io_service);
  729. * ...
  730. * asio::ip::tcp::socket socket(io_service);
  731. * asio::ip::tcp::endpoint endpoint;
  732. * asio::error_code ec;
  733. * acceptor.accept(socket, endpoint, ec);
  734. * if (ec)
  735. * {
  736. * // An error occurred.
  737. * }
  738. * @endcode
  739. */
  740. template <typename SocketService>
  741. asio::error_code accept(
  742. basic_socket<protocol_type, SocketService>& peer,
  743. endpoint_type& peer_endpoint, asio::error_code& ec)
  744. {
  745. return this->service.accept(this->implementation, peer, &peer_endpoint, ec);
  746. }
  747. /// Start an asynchronous accept.
  748. /**
  749. * This function is used to asynchronously accept a new connection into a
  750. * socket, and additionally obtain the endpoint of the remote peer. The
  751. * function call always returns immediately.
  752. *
  753. * @param peer The socket into which the new connection will be accepted.
  754. * Ownership of the peer object is retained by the caller, which must
  755. * guarantee that it is valid until the handler is called.
  756. *
  757. * @param peer_endpoint An endpoint object into which the endpoint of the
  758. * remote peer will be written. Ownership of the peer_endpoint object is
  759. * retained by the caller, which must guarantee that it is valid until the
  760. * handler is called.
  761. *
  762. * @param handler The handler to be called when the accept operation
  763. * completes. Copies will be made of the handler as required. The function
  764. * signature of the handler must be:
  765. * @code void handler(
  766. * const asio::error_code& error // Result of operation.
  767. * ); @endcode
  768. * Regardless of whether the asynchronous operation completes immediately or
  769. * not, the handler will not be invoked from within this function. Invocation
  770. * of the handler will be performed in a manner equivalent to using
  771. * asio::io_service::post().
  772. */
  773. template <typename SocketService, typename AcceptHandler>
  774. void async_accept(basic_socket<protocol_type, SocketService>& peer,
  775. endpoint_type& peer_endpoint, AcceptHandler handler)
  776. {
  777. this->service.async_accept(this->implementation,
  778. peer, &peer_endpoint, handler);
  779. }
  780. };
  781. } // namespace asio
  782. #include "asio/detail/pop_options.hpp"
  783. #endif // ASIO_BASIC_SOCKET_ACCEPTOR_HPP