basic_socket_acceptor.hpp 26 KB

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