basic_socket_acceptor.hpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. //
  2. // basic_socket_acceptor.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_BASIC_SOCKET_ACCEPTOR_HPP
  11. #define BOOST_ASIO_BASIC_SOCKET_ACCEPTOR_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <boost/asio/basic_io_object.hpp>
  17. #include <boost/asio/basic_socket.hpp>
  18. #include <boost/asio/detail/handler_type_requirements.hpp>
  19. #include <boost/asio/detail/throw_error.hpp>
  20. #include <boost/asio/detail/type_traits.hpp>
  21. #include <boost/asio/error.hpp>
  22. #include <boost/asio/socket_acceptor_service.hpp>
  23. #include <boost/asio/socket_base.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. /// Provides the ability to accept new connections.
  28. /**
  29. * The basic_socket_acceptor class template is used for accepting new socket
  30. * connections.
  31. *
  32. * @par Thread Safety
  33. * @e Distinct @e objects: Safe.@n
  34. * @e Shared @e objects: Unsafe.
  35. *
  36. * @par Example
  37. * Opening a socket acceptor with the SO_REUSEADDR option enabled:
  38. * @code
  39. * boost::asio::ip::tcp::acceptor acceptor(io_service);
  40. * boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), port);
  41. * acceptor.open(endpoint.protocol());
  42. * acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
  43. * acceptor.bind(endpoint);
  44. * acceptor.listen();
  45. * @endcode
  46. */
  47. template <typename Protocol,
  48. typename SocketAcceptorService = socket_acceptor_service<Protocol> >
  49. class basic_socket_acceptor
  50. : public basic_io_object<SocketAcceptorService>,
  51. public socket_base
  52. {
  53. public:
  54. /// (Deprecated: Use native_handle_type.) The native representation of an
  55. /// acceptor.
  56. typedef typename SocketAcceptorService::native_handle_type native_type;
  57. /// The native representation of an acceptor.
  58. typedef typename SocketAcceptorService::native_handle_type native_handle_type;
  59. /// The protocol type.
  60. typedef Protocol protocol_type;
  61. /// The endpoint type.
  62. typedef typename Protocol::endpoint endpoint_type;
  63. /// Construct an acceptor without opening it.
  64. /**
  65. * This constructor creates an acceptor without opening it to listen for new
  66. * connections. The open() function must be called before the acceptor can
  67. * accept new socket connections.
  68. *
  69. * @param io_service The io_service object that the acceptor will use to
  70. * dispatch handlers for any asynchronous operations performed on the
  71. * acceptor.
  72. */
  73. explicit basic_socket_acceptor(boost::asio::io_service& io_service)
  74. : basic_io_object<SocketAcceptorService>(io_service)
  75. {
  76. }
  77. /// Construct an open acceptor.
  78. /**
  79. * This constructor creates an acceptor and automatically opens it.
  80. *
  81. * @param io_service The io_service object that the acceptor will use to
  82. * dispatch handlers for any asynchronous operations performed on the
  83. * acceptor.
  84. *
  85. * @param protocol An object specifying protocol parameters to be used.
  86. *
  87. * @throws boost::system::system_error Thrown on failure.
  88. */
  89. basic_socket_acceptor(boost::asio::io_service& io_service,
  90. const protocol_type& protocol)
  91. : basic_io_object<SocketAcceptorService>(io_service)
  92. {
  93. boost::system::error_code ec;
  94. this->get_service().open(this->get_implementation(), protocol, ec);
  95. boost::asio::detail::throw_error(ec, "open");
  96. }
  97. /// Construct an acceptor opened on the given endpoint.
  98. /**
  99. * This constructor creates an acceptor and automatically opens it to listen
  100. * for new connections on the specified endpoint.
  101. *
  102. * @param io_service The io_service object that the acceptor will use to
  103. * dispatch handlers for any asynchronous operations performed on the
  104. * acceptor.
  105. *
  106. * @param endpoint An endpoint on the local machine on which the acceptor
  107. * will listen for new connections.
  108. *
  109. * @param reuse_addr Whether the constructor should set the socket option
  110. * socket_base::reuse_address.
  111. *
  112. * @throws boost::system::system_error Thrown on failure.
  113. *
  114. * @note This constructor is equivalent to the following code:
  115. * @code
  116. * basic_socket_acceptor<Protocol> acceptor(io_service);
  117. * acceptor.open(endpoint.protocol());
  118. * if (reuse_addr)
  119. * acceptor.set_option(socket_base::reuse_address(true));
  120. * acceptor.bind(endpoint);
  121. * acceptor.listen(listen_backlog);
  122. * @endcode
  123. */
  124. basic_socket_acceptor(boost::asio::io_service& io_service,
  125. const endpoint_type& endpoint, bool reuse_addr = true)
  126. : basic_io_object<SocketAcceptorService>(io_service)
  127. {
  128. boost::system::error_code ec;
  129. const protocol_type protocol = endpoint.protocol();
  130. this->get_service().open(this->get_implementation(), protocol, ec);
  131. boost::asio::detail::throw_error(ec, "open");
  132. if (reuse_addr)
  133. {
  134. this->get_service().set_option(this->get_implementation(),
  135. socket_base::reuse_address(true), ec);
  136. boost::asio::detail::throw_error(ec, "set_option");
  137. }
  138. this->get_service().bind(this->get_implementation(), endpoint, ec);
  139. boost::asio::detail::throw_error(ec, "bind");
  140. this->get_service().listen(this->get_implementation(),
  141. socket_base::max_connections, ec);
  142. boost::asio::detail::throw_error(ec, "listen");
  143. }
  144. /// Construct a basic_socket_acceptor on an existing native acceptor.
  145. /**
  146. * This constructor creates an acceptor object to hold an existing native
  147. * acceptor.
  148. *
  149. * @param io_service The io_service object that the acceptor will use to
  150. * dispatch handlers for any asynchronous operations performed on the
  151. * acceptor.
  152. *
  153. * @param protocol An object specifying protocol parameters to be used.
  154. *
  155. * @param native_acceptor A native acceptor.
  156. *
  157. * @throws boost::system::system_error Thrown on failure.
  158. */
  159. basic_socket_acceptor(boost::asio::io_service& io_service,
  160. const protocol_type& protocol, const native_handle_type& native_acceptor)
  161. : basic_io_object<SocketAcceptorService>(io_service)
  162. {
  163. boost::system::error_code ec;
  164. this->get_service().assign(this->get_implementation(),
  165. protocol, native_acceptor, ec);
  166. boost::asio::detail::throw_error(ec, "assign");
  167. }
  168. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  169. /// Move-construct a basic_socket_acceptor from another.
  170. /**
  171. * This constructor moves an acceptor from one object to another.
  172. *
  173. * @param other The other basic_socket_acceptor object from which the move
  174. * will occur.
  175. *
  176. * @note Following the move, the moved-from object is in the same state as if
  177. * constructed using the @c basic_socket_acceptor(io_service&) constructor.
  178. */
  179. basic_socket_acceptor(basic_socket_acceptor&& other)
  180. : basic_io_object<SocketAcceptorService>(
  181. BOOST_ASIO_MOVE_CAST(basic_socket_acceptor)(other))
  182. {
  183. }
  184. /// Move-assign a basic_socket_acceptor from another.
  185. /**
  186. * This assignment operator moves an acceptor from one object to another.
  187. *
  188. * @param other The other basic_socket_acceptor object from which the move
  189. * will occur.
  190. *
  191. * @note Following the move, the moved-from object is in the same state as if
  192. * constructed using the @c basic_socket_acceptor(io_service&) constructor.
  193. */
  194. basic_socket_acceptor& operator=(basic_socket_acceptor&& other)
  195. {
  196. basic_io_object<SocketAcceptorService>::operator=(
  197. BOOST_ASIO_MOVE_CAST(basic_socket_acceptor)(other));
  198. return *this;
  199. }
  200. // All socket acceptors have access to each other's implementations.
  201. template <typename Protocol1, typename SocketAcceptorService1>
  202. friend class basic_socket_acceptor;
  203. /// Move-construct a basic_socket_acceptor from an acceptor of another
  204. /// protocol type.
  205. /**
  206. * This constructor moves an acceptor from one object to another.
  207. *
  208. * @param other The other basic_socket_acceptor object from which the move
  209. * will occur.
  210. *
  211. * @note Following the move, the moved-from object is in the same state as if
  212. * constructed using the @c basic_socket(io_service&) constructor.
  213. */
  214. template <typename Protocol1, typename SocketAcceptorService1>
  215. basic_socket_acceptor(
  216. basic_socket_acceptor<Protocol1, SocketAcceptorService1>&& other,
  217. typename enable_if<is_convertible<Protocol1, Protocol>::value>::type* = 0)
  218. : basic_io_object<SocketAcceptorService>(other.get_io_service())
  219. {
  220. this->get_service().template converting_move_construct<Protocol1>(
  221. this->get_implementation(), other.get_implementation());
  222. }
  223. /// Move-assign a basic_socket_acceptor from an acceptor of another protocol
  224. /// type.
  225. /**
  226. * This assignment operator moves an acceptor from one object to another.
  227. *
  228. * @param other The other basic_socket_acceptor object from which the move
  229. * will occur.
  230. *
  231. * @note Following the move, the moved-from object is in the same state as if
  232. * constructed using the @c basic_socket(io_service&) constructor.
  233. */
  234. template <typename Protocol1, typename SocketAcceptorService1>
  235. typename enable_if<is_convertible<Protocol1, Protocol>::value,
  236. basic_socket_acceptor>::type& operator=(
  237. basic_socket_acceptor<Protocol1, SocketAcceptorService1>&& other)
  238. {
  239. basic_socket_acceptor tmp(BOOST_ASIO_MOVE_CAST2(basic_socket_acceptor<
  240. Protocol1, SocketAcceptorService1>)(other));
  241. basic_io_object<SocketAcceptorService>::operator=(
  242. BOOST_ASIO_MOVE_CAST(basic_socket_acceptor)(tmp));
  243. return *this;
  244. }
  245. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  246. /// Open the acceptor using the specified protocol.
  247. /**
  248. * This function opens the socket acceptor so that it will use the specified
  249. * protocol.
  250. *
  251. * @param protocol An object specifying which protocol is to be used.
  252. *
  253. * @throws boost::system::system_error Thrown on failure.
  254. *
  255. * @par Example
  256. * @code
  257. * boost::asio::ip::tcp::acceptor acceptor(io_service);
  258. * acceptor.open(boost::asio::ip::tcp::v4());
  259. * @endcode
  260. */
  261. void open(const protocol_type& protocol = protocol_type())
  262. {
  263. boost::system::error_code ec;
  264. this->get_service().open(this->get_implementation(), protocol, ec);
  265. boost::asio::detail::throw_error(ec, "open");
  266. }
  267. /// Open the acceptor using the specified protocol.
  268. /**
  269. * This function opens the socket acceptor so that it will use the specified
  270. * protocol.
  271. *
  272. * @param protocol An object specifying which protocol is to be used.
  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. * boost::system::error_code ec;
  280. * acceptor.open(boost::asio::ip::tcp::v4(), ec);
  281. * if (ec)
  282. * {
  283. * // An error occurred.
  284. * }
  285. * @endcode
  286. */
  287. boost::system::error_code open(const protocol_type& protocol,
  288. boost::system::error_code& ec)
  289. {
  290. return this->get_service().open(this->get_implementation(), protocol, ec);
  291. }
  292. /// Assigns an existing native acceptor to the acceptor.
  293. /*
  294. * This function opens the acceptor to hold an existing native acceptor.
  295. *
  296. * @param protocol An object specifying which protocol is to be used.
  297. *
  298. * @param native_acceptor A native acceptor.
  299. *
  300. * @throws boost::system::system_error Thrown on failure.
  301. */
  302. void assign(const protocol_type& protocol,
  303. const native_handle_type& native_acceptor)
  304. {
  305. boost::system::error_code ec;
  306. this->get_service().assign(this->get_implementation(),
  307. protocol, native_acceptor, ec);
  308. boost::asio::detail::throw_error(ec, "assign");
  309. }
  310. /// Assigns an existing native acceptor to the acceptor.
  311. /*
  312. * This function opens the acceptor to hold an existing native acceptor.
  313. *
  314. * @param protocol An object specifying which protocol is to be used.
  315. *
  316. * @param native_acceptor A native acceptor.
  317. *
  318. * @param ec Set to indicate what error occurred, if any.
  319. */
  320. boost::system::error_code assign(const protocol_type& protocol,
  321. const native_handle_type& native_acceptor, boost::system::error_code& ec)
  322. {
  323. return this->get_service().assign(this->get_implementation(),
  324. protocol, native_acceptor, ec);
  325. }
  326. /// Determine whether the acceptor is open.
  327. bool is_open() const
  328. {
  329. return this->get_service().is_open(this->get_implementation());
  330. }
  331. /// Bind the acceptor to the given local endpoint.
  332. /**
  333. * This function binds the socket acceptor to the specified endpoint on the
  334. * local machine.
  335. *
  336. * @param endpoint An endpoint on the local machine to which the socket
  337. * acceptor will be bound.
  338. *
  339. * @throws boost::system::system_error Thrown on failure.
  340. *
  341. * @par Example
  342. * @code
  343. * boost::asio::ip::tcp::acceptor acceptor(io_service);
  344. * boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 12345);
  345. * acceptor.open(endpoint.protocol());
  346. * acceptor.bind(endpoint);
  347. * @endcode
  348. */
  349. void bind(const endpoint_type& endpoint)
  350. {
  351. boost::system::error_code ec;
  352. this->get_service().bind(this->get_implementation(), endpoint, ec);
  353. boost::asio::detail::throw_error(ec, "bind");
  354. }
  355. /// Bind the acceptor to the given local endpoint.
  356. /**
  357. * This function binds the socket acceptor to the specified endpoint on the
  358. * local machine.
  359. *
  360. * @param endpoint An endpoint on the local machine to which the socket
  361. * acceptor will be bound.
  362. *
  363. * @param ec Set to indicate what error occurred, if any.
  364. *
  365. * @par Example
  366. * @code
  367. * boost::asio::ip::tcp::acceptor acceptor(io_service);
  368. * boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 12345);
  369. * acceptor.open(endpoint.protocol());
  370. * boost::system::error_code ec;
  371. * acceptor.bind(endpoint, ec);
  372. * if (ec)
  373. * {
  374. * // An error occurred.
  375. * }
  376. * @endcode
  377. */
  378. boost::system::error_code bind(const endpoint_type& endpoint,
  379. boost::system::error_code& ec)
  380. {
  381. return this->get_service().bind(this->get_implementation(), endpoint, ec);
  382. }
  383. /// Place the acceptor into the state where it will listen for new
  384. /// connections.
  385. /**
  386. * This function puts the socket acceptor into the state where it may accept
  387. * new connections.
  388. *
  389. * @param backlog The maximum length of the queue of pending connections.
  390. *
  391. * @throws boost::system::system_error Thrown on failure.
  392. */
  393. void listen(int backlog = socket_base::max_connections)
  394. {
  395. boost::system::error_code ec;
  396. this->get_service().listen(this->get_implementation(), backlog, ec);
  397. boost::asio::detail::throw_error(ec, "listen");
  398. }
  399. /// Place the acceptor into the state where it will listen for new
  400. /// connections.
  401. /**
  402. * This function puts the socket acceptor into the state where it may accept
  403. * new connections.
  404. *
  405. * @param backlog The maximum length of the queue of pending connections.
  406. *
  407. * @param ec Set to indicate what error occurred, if any.
  408. *
  409. * @par Example
  410. * @code
  411. * boost::asio::ip::tcp::acceptor acceptor(io_service);
  412. * ...
  413. * boost::system::error_code ec;
  414. * acceptor.listen(boost::asio::socket_base::max_connections, ec);
  415. * if (ec)
  416. * {
  417. * // An error occurred.
  418. * }
  419. * @endcode
  420. */
  421. boost::system::error_code listen(int backlog, boost::system::error_code& ec)
  422. {
  423. return this->get_service().listen(this->get_implementation(), backlog, ec);
  424. }
  425. /// Close the acceptor.
  426. /**
  427. * This function is used to close the acceptor. Any asynchronous accept
  428. * operations will be cancelled immediately.
  429. *
  430. * A subsequent call to open() is required before the acceptor can again be
  431. * used to again perform socket accept operations.
  432. *
  433. * @throws boost::system::system_error Thrown on failure.
  434. */
  435. void close()
  436. {
  437. boost::system::error_code ec;
  438. this->get_service().close(this->get_implementation(), ec);
  439. boost::asio::detail::throw_error(ec, "close");
  440. }
  441. /// Close the acceptor.
  442. /**
  443. * This function is used to close the acceptor. Any asynchronous accept
  444. * operations will be cancelled immediately.
  445. *
  446. * A subsequent call to open() is required before the acceptor can again be
  447. * used to again perform socket accept operations.
  448. *
  449. * @param ec Set to indicate what error occurred, if any.
  450. *
  451. * @par Example
  452. * @code
  453. * boost::asio::ip::tcp::acceptor acceptor(io_service);
  454. * ...
  455. * boost::system::error_code ec;
  456. * acceptor.close(ec);
  457. * if (ec)
  458. * {
  459. * // An error occurred.
  460. * }
  461. * @endcode
  462. */
  463. boost::system::error_code close(boost::system::error_code& ec)
  464. {
  465. return this->get_service().close(this->get_implementation(), ec);
  466. }
  467. /// (Deprecated: Use native_handle().) Get the native acceptor representation.
  468. /**
  469. * This function may be used to obtain the underlying representation of the
  470. * acceptor. This is intended to allow access to native acceptor functionality
  471. * that is not otherwise provided.
  472. */
  473. native_type native()
  474. {
  475. return this->get_service().native_handle(this->get_implementation());
  476. }
  477. /// Get the native acceptor representation.
  478. /**
  479. * This function may be used to obtain the underlying representation of the
  480. * acceptor. This is intended to allow access to native acceptor functionality
  481. * that is not otherwise provided.
  482. */
  483. native_handle_type native_handle()
  484. {
  485. return this->get_service().native_handle(this->get_implementation());
  486. }
  487. /// Cancel all asynchronous operations associated with the acceptor.
  488. /**
  489. * This function causes all outstanding asynchronous connect, send and receive
  490. * operations to finish immediately, and the handlers for cancelled operations
  491. * will be passed the boost::asio::error::operation_aborted error.
  492. *
  493. * @throws boost::system::system_error Thrown on failure.
  494. */
  495. void cancel()
  496. {
  497. boost::system::error_code ec;
  498. this->get_service().cancel(this->get_implementation(), ec);
  499. boost::asio::detail::throw_error(ec, "cancel");
  500. }
  501. /// Cancel all asynchronous operations associated with the acceptor.
  502. /**
  503. * This function causes all outstanding asynchronous connect, send and receive
  504. * operations to finish immediately, and the handlers for cancelled operations
  505. * will be passed the boost::asio::error::operation_aborted error.
  506. *
  507. * @param ec Set to indicate what error occurred, if any.
  508. */
  509. boost::system::error_code cancel(boost::system::error_code& ec)
  510. {
  511. return this->get_service().cancel(this->get_implementation(), ec);
  512. }
  513. /// Set an option on the acceptor.
  514. /**
  515. * This function is used to set an option on the acceptor.
  516. *
  517. * @param option The new option value to be set on the acceptor.
  518. *
  519. * @throws boost::system::system_error Thrown on failure.
  520. *
  521. * @sa SettableSocketOption @n
  522. * boost::asio::socket_base::reuse_address
  523. * boost::asio::socket_base::enable_connection_aborted
  524. *
  525. * @par Example
  526. * Setting the SOL_SOCKET/SO_REUSEADDR option:
  527. * @code
  528. * boost::asio::ip::tcp::acceptor acceptor(io_service);
  529. * ...
  530. * boost::asio::ip::tcp::acceptor::reuse_address option(true);
  531. * acceptor.set_option(option);
  532. * @endcode
  533. */
  534. template <typename SettableSocketOption>
  535. void set_option(const SettableSocketOption& option)
  536. {
  537. boost::system::error_code ec;
  538. this->get_service().set_option(this->get_implementation(), option, ec);
  539. boost::asio::detail::throw_error(ec, "set_option");
  540. }
  541. /// Set an option on the acceptor.
  542. /**
  543. * This function is used to set an option on the acceptor.
  544. *
  545. * @param option The new option value to be set on the acceptor.
  546. *
  547. * @param ec Set to indicate what error occurred, if any.
  548. *
  549. * @sa SettableSocketOption @n
  550. * boost::asio::socket_base::reuse_address
  551. * boost::asio::socket_base::enable_connection_aborted
  552. *
  553. * @par Example
  554. * Setting the SOL_SOCKET/SO_REUSEADDR option:
  555. * @code
  556. * boost::asio::ip::tcp::acceptor acceptor(io_service);
  557. * ...
  558. * boost::asio::ip::tcp::acceptor::reuse_address option(true);
  559. * boost::system::error_code ec;
  560. * acceptor.set_option(option, ec);
  561. * if (ec)
  562. * {
  563. * // An error occurred.
  564. * }
  565. * @endcode
  566. */
  567. template <typename SettableSocketOption>
  568. boost::system::error_code set_option(const SettableSocketOption& option,
  569. boost::system::error_code& ec)
  570. {
  571. return this->get_service().set_option(
  572. this->get_implementation(), option, ec);
  573. }
  574. /// Get an option from the acceptor.
  575. /**
  576. * This function is used to get the current value of an option on the
  577. * acceptor.
  578. *
  579. * @param option The option value to be obtained from the acceptor.
  580. *
  581. * @throws boost::system::system_error Thrown on failure.
  582. *
  583. * @sa GettableSocketOption @n
  584. * boost::asio::socket_base::reuse_address
  585. *
  586. * @par Example
  587. * Getting the value of the SOL_SOCKET/SO_REUSEADDR option:
  588. * @code
  589. * boost::asio::ip::tcp::acceptor acceptor(io_service);
  590. * ...
  591. * boost::asio::ip::tcp::acceptor::reuse_address option;
  592. * acceptor.get_option(option);
  593. * bool is_set = option.get();
  594. * @endcode
  595. */
  596. template <typename GettableSocketOption>
  597. void get_option(GettableSocketOption& option)
  598. {
  599. boost::system::error_code ec;
  600. this->get_service().get_option(this->get_implementation(), option, ec);
  601. boost::asio::detail::throw_error(ec, "get_option");
  602. }
  603. /// Get an option from the acceptor.
  604. /**
  605. * This function is used to get the current value of an option on the
  606. * acceptor.
  607. *
  608. * @param option The option value to be obtained from the acceptor.
  609. *
  610. * @param ec Set to indicate what error occurred, if any.
  611. *
  612. * @sa GettableSocketOption @n
  613. * boost::asio::socket_base::reuse_address
  614. *
  615. * @par Example
  616. * Getting the value of the SOL_SOCKET/SO_REUSEADDR option:
  617. * @code
  618. * boost::asio::ip::tcp::acceptor acceptor(io_service);
  619. * ...
  620. * boost::asio::ip::tcp::acceptor::reuse_address option;
  621. * boost::system::error_code ec;
  622. * acceptor.get_option(option, ec);
  623. * if (ec)
  624. * {
  625. * // An error occurred.
  626. * }
  627. * bool is_set = option.get();
  628. * @endcode
  629. */
  630. template <typename GettableSocketOption>
  631. boost::system::error_code get_option(GettableSocketOption& option,
  632. boost::system::error_code& ec)
  633. {
  634. return this->get_service().get_option(
  635. this->get_implementation(), option, ec);
  636. }
  637. /// Perform an IO control command on the acceptor.
  638. /**
  639. * This function is used to execute an IO control command on the acceptor.
  640. *
  641. * @param command The IO control command to be performed on the acceptor.
  642. *
  643. * @throws boost::system::system_error Thrown on failure.
  644. *
  645. * @sa IoControlCommand @n
  646. * boost::asio::socket_base::non_blocking_io
  647. *
  648. * @par Example
  649. * Getting the number of bytes ready to read:
  650. * @code
  651. * boost::asio::ip::tcp::acceptor acceptor(io_service);
  652. * ...
  653. * boost::asio::ip::tcp::acceptor::non_blocking_io command(true);
  654. * socket.io_control(command);
  655. * @endcode
  656. */
  657. template <typename IoControlCommand>
  658. void io_control(IoControlCommand& command)
  659. {
  660. boost::system::error_code ec;
  661. this->get_service().io_control(this->get_implementation(), command, ec);
  662. boost::asio::detail::throw_error(ec, "io_control");
  663. }
  664. /// Perform an IO control command on the acceptor.
  665. /**
  666. * This function is used to execute an IO control command on the acceptor.
  667. *
  668. * @param command The IO control command to be performed on the acceptor.
  669. *
  670. * @param ec Set to indicate what error occurred, if any.
  671. *
  672. * @sa IoControlCommand @n
  673. * boost::asio::socket_base::non_blocking_io
  674. *
  675. * @par Example
  676. * Getting the number of bytes ready to read:
  677. * @code
  678. * boost::asio::ip::tcp::acceptor acceptor(io_service);
  679. * ...
  680. * boost::asio::ip::tcp::acceptor::non_blocking_io command(true);
  681. * boost::system::error_code ec;
  682. * socket.io_control(command, ec);
  683. * if (ec)
  684. * {
  685. * // An error occurred.
  686. * }
  687. * @endcode
  688. */
  689. template <typename IoControlCommand>
  690. boost::system::error_code io_control(IoControlCommand& command,
  691. boost::system::error_code& ec)
  692. {
  693. return this->get_service().io_control(
  694. this->get_implementation(), command, ec);
  695. }
  696. /// Gets the non-blocking mode of the acceptor.
  697. /**
  698. * @returns @c true if the acceptor's synchronous operations will fail with
  699. * boost::asio::error::would_block if they are unable to perform the requested
  700. * operation immediately. If @c false, synchronous operations will block
  701. * until complete.
  702. *
  703. * @note The non-blocking mode has no effect on the behaviour of asynchronous
  704. * operations. Asynchronous operations will never fail with the error
  705. * boost::asio::error::would_block.
  706. */
  707. bool non_blocking() const
  708. {
  709. return this->get_service().non_blocking(this->get_implementation());
  710. }
  711. /// Sets the non-blocking mode of the acceptor.
  712. /**
  713. * @param mode If @c true, the acceptor's synchronous operations will fail
  714. * with boost::asio::error::would_block if they are unable to perform the
  715. * requested operation immediately. If @c false, synchronous operations will
  716. * block until complete.
  717. *
  718. * @throws boost::system::system_error Thrown on failure.
  719. *
  720. * @note The non-blocking mode has no effect on the behaviour of asynchronous
  721. * operations. Asynchronous operations will never fail with the error
  722. * boost::asio::error::would_block.
  723. */
  724. void non_blocking(bool mode)
  725. {
  726. boost::system::error_code ec;
  727. this->get_service().non_blocking(this->get_implementation(), mode, ec);
  728. boost::asio::detail::throw_error(ec, "non_blocking");
  729. }
  730. /// Sets the non-blocking mode of the acceptor.
  731. /**
  732. * @param mode If @c true, the acceptor's synchronous operations will fail
  733. * with boost::asio::error::would_block if they are unable to perform the
  734. * requested operation immediately. If @c false, synchronous operations will
  735. * block until complete.
  736. *
  737. * @param ec Set to indicate what error occurred, if any.
  738. *
  739. * @note The non-blocking mode has no effect on the behaviour of asynchronous
  740. * operations. Asynchronous operations will never fail with the error
  741. * boost::asio::error::would_block.
  742. */
  743. boost::system::error_code non_blocking(
  744. bool mode, boost::system::error_code& ec)
  745. {
  746. return this->get_service().non_blocking(
  747. this->get_implementation(), mode, ec);
  748. }
  749. /// Gets the non-blocking mode of the native acceptor implementation.
  750. /**
  751. * This function is used to retrieve the non-blocking mode of the underlying
  752. * native acceptor. This mode has no effect on the behaviour of the acceptor
  753. * object's synchronous operations.
  754. *
  755. * @returns @c true if the underlying acceptor is in non-blocking mode and
  756. * direct system calls may fail with boost::asio::error::would_block (or the
  757. * equivalent system error).
  758. *
  759. * @note The current non-blocking mode is cached by the acceptor object.
  760. * Consequently, the return value may be incorrect if the non-blocking mode
  761. * was set directly on the native acceptor.
  762. */
  763. bool native_non_blocking() const
  764. {
  765. return this->get_service().native_non_blocking(this->get_implementation());
  766. }
  767. /// Sets the non-blocking mode of the native acceptor implementation.
  768. /**
  769. * This function is used to modify the non-blocking mode of the underlying
  770. * native acceptor. It has no effect on the behaviour of the acceptor object's
  771. * synchronous operations.
  772. *
  773. * @param mode If @c true, the underlying acceptor is put into non-blocking
  774. * mode and direct system calls may fail with boost::asio::error::would_block
  775. * (or the equivalent system error).
  776. *
  777. * @throws boost::system::system_error Thrown on failure. If the @c mode is
  778. * @c false, but the current value of @c non_blocking() is @c true, this
  779. * function fails with boost::asio::error::invalid_argument, as the
  780. * combination does not make sense.
  781. */
  782. void native_non_blocking(bool mode)
  783. {
  784. boost::system::error_code ec;
  785. this->get_service().native_non_blocking(
  786. this->get_implementation(), mode, ec);
  787. boost::asio::detail::throw_error(ec, "native_non_blocking");
  788. }
  789. /// Sets the non-blocking mode of the native acceptor implementation.
  790. /**
  791. * This function is used to modify the non-blocking mode of the underlying
  792. * native acceptor. It has no effect on the behaviour of the acceptor object's
  793. * synchronous operations.
  794. *
  795. * @param mode If @c true, the underlying acceptor is put into non-blocking
  796. * mode and direct system calls may fail with boost::asio::error::would_block
  797. * (or the equivalent system error).
  798. *
  799. * @param ec Set to indicate what error occurred, if any. If the @c mode is
  800. * @c false, but the current value of @c non_blocking() is @c true, this
  801. * function fails with boost::asio::error::invalid_argument, as the
  802. * combination does not make sense.
  803. */
  804. boost::system::error_code native_non_blocking(
  805. bool mode, boost::system::error_code& ec)
  806. {
  807. return this->get_service().native_non_blocking(
  808. this->get_implementation(), mode, ec);
  809. }
  810. /// Get the local endpoint of the acceptor.
  811. /**
  812. * This function is used to obtain the locally bound endpoint of the acceptor.
  813. *
  814. * @returns An object that represents the local endpoint of the acceptor.
  815. *
  816. * @throws boost::system::system_error Thrown on failure.
  817. *
  818. * @par Example
  819. * @code
  820. * boost::asio::ip::tcp::acceptor acceptor(io_service);
  821. * ...
  822. * boost::asio::ip::tcp::endpoint endpoint = acceptor.local_endpoint();
  823. * @endcode
  824. */
  825. endpoint_type local_endpoint() const
  826. {
  827. boost::system::error_code ec;
  828. endpoint_type ep = this->get_service().local_endpoint(
  829. this->get_implementation(), ec);
  830. boost::asio::detail::throw_error(ec, "local_endpoint");
  831. return ep;
  832. }
  833. /// Get the local endpoint of the acceptor.
  834. /**
  835. * This function is used to obtain the locally bound endpoint of the acceptor.
  836. *
  837. * @param ec Set to indicate what error occurred, if any.
  838. *
  839. * @returns An object that represents the local endpoint of the acceptor.
  840. * Returns a default-constructed endpoint object if an error occurred and the
  841. * error handler did not throw an exception.
  842. *
  843. * @par Example
  844. * @code
  845. * boost::asio::ip::tcp::acceptor acceptor(io_service);
  846. * ...
  847. * boost::system::error_code ec;
  848. * boost::asio::ip::tcp::endpoint endpoint = acceptor.local_endpoint(ec);
  849. * if (ec)
  850. * {
  851. * // An error occurred.
  852. * }
  853. * @endcode
  854. */
  855. endpoint_type local_endpoint(boost::system::error_code& ec) const
  856. {
  857. return this->get_service().local_endpoint(this->get_implementation(), ec);
  858. }
  859. /// Accept a new connection.
  860. /**
  861. * This function is used to accept a new connection from a peer into the
  862. * given socket. The function call will block until a new connection has been
  863. * accepted successfully or an error occurs.
  864. *
  865. * @param peer The socket into which the new connection will be accepted.
  866. *
  867. * @throws boost::system::system_error Thrown on failure.
  868. *
  869. * @par Example
  870. * @code
  871. * boost::asio::ip::tcp::acceptor acceptor(io_service);
  872. * ...
  873. * boost::asio::ip::tcp::socket socket(io_service);
  874. * acceptor.accept(socket);
  875. * @endcode
  876. */
  877. template <typename Protocol1, typename SocketService>
  878. void accept(basic_socket<Protocol1, SocketService>& peer,
  879. typename enable_if<is_convertible<Protocol, Protocol1>::value>::type* = 0)
  880. {
  881. boost::system::error_code ec;
  882. this->get_service().accept(this->get_implementation(),
  883. peer, static_cast<endpoint_type*>(0), ec);
  884. boost::asio::detail::throw_error(ec, "accept");
  885. }
  886. /// Accept a new connection.
  887. /**
  888. * This function is used to accept a new connection from a peer into the
  889. * given socket. The function call will block until a new connection has been
  890. * accepted successfully or an error occurs.
  891. *
  892. * @param peer The socket into which the new connection will be accepted.
  893. *
  894. * @param ec Set to indicate what error occurred, if any.
  895. *
  896. * @par Example
  897. * @code
  898. * boost::asio::ip::tcp::acceptor acceptor(io_service);
  899. * ...
  900. * boost::asio::ip::tcp::soocket socket(io_service);
  901. * boost::system::error_code ec;
  902. * acceptor.accept(socket, ec);
  903. * if (ec)
  904. * {
  905. * // An error occurred.
  906. * }
  907. * @endcode
  908. */
  909. template <typename Protocol1, typename SocketService>
  910. boost::system::error_code accept(
  911. basic_socket<Protocol1, SocketService>& peer,
  912. boost::system::error_code& ec,
  913. typename enable_if<is_convertible<Protocol, Protocol1>::value>::type* = 0)
  914. {
  915. return this->get_service().accept(this->get_implementation(),
  916. peer, static_cast<endpoint_type*>(0), ec);
  917. }
  918. /// Start an asynchronous accept.
  919. /**
  920. * This function is used to asynchronously accept a new connection into a
  921. * socket. The function call always returns immediately.
  922. *
  923. * @param peer The socket into which the new connection will be accepted.
  924. * Ownership of the peer object is retained by the caller, which must
  925. * guarantee that it is valid until the handler is called.
  926. *
  927. * @param handler The handler to be called when the accept operation
  928. * completes. Copies will be made of the handler as required. The function
  929. * signature of the handler must be:
  930. * @code void handler(
  931. * const boost::system::error_code& error // Result of operation.
  932. * ); @endcode
  933. * Regardless of whether the asynchronous operation completes immediately or
  934. * not, the handler will not be invoked from within this function. Invocation
  935. * of the handler will be performed in a manner equivalent to using
  936. * boost::asio::io_service::post().
  937. *
  938. * @par Example
  939. * @code
  940. * void accept_handler(const boost::system::error_code& error)
  941. * {
  942. * if (!error)
  943. * {
  944. * // Accept succeeded.
  945. * }
  946. * }
  947. *
  948. * ...
  949. *
  950. * boost::asio::ip::tcp::acceptor acceptor(io_service);
  951. * ...
  952. * boost::asio::ip::tcp::socket socket(io_service);
  953. * acceptor.async_accept(socket, accept_handler);
  954. * @endcode
  955. */
  956. template <typename Protocol1, typename SocketService, typename AcceptHandler>
  957. BOOST_ASIO_INITFN_RESULT_TYPE(AcceptHandler,
  958. void (boost::system::error_code))
  959. async_accept(basic_socket<Protocol1, SocketService>& peer,
  960. BOOST_ASIO_MOVE_ARG(AcceptHandler) handler,
  961. typename enable_if<is_convertible<Protocol, Protocol1>::value>::type* = 0)
  962. {
  963. // If you get an error on the following line it means that your handler does
  964. // not meet the documented type requirements for a AcceptHandler.
  965. BOOST_ASIO_ACCEPT_HANDLER_CHECK(AcceptHandler, handler) type_check;
  966. return this->get_service().async_accept(this->get_implementation(),
  967. peer, static_cast<endpoint_type*>(0),
  968. BOOST_ASIO_MOVE_CAST(AcceptHandler)(handler));
  969. }
  970. /// Accept a new connection and obtain the endpoint of the peer
  971. /**
  972. * This function is used to accept a new connection from a peer into the
  973. * given socket, and additionally provide the endpoint of the remote peer.
  974. * The function call will block until a new connection has been accepted
  975. * successfully or an error occurs.
  976. *
  977. * @param peer The socket into which the new connection will be accepted.
  978. *
  979. * @param peer_endpoint An endpoint object which will receive the endpoint of
  980. * the remote peer.
  981. *
  982. * @throws boost::system::system_error Thrown on failure.
  983. *
  984. * @par Example
  985. * @code
  986. * boost::asio::ip::tcp::acceptor acceptor(io_service);
  987. * ...
  988. * boost::asio::ip::tcp::socket socket(io_service);
  989. * boost::asio::ip::tcp::endpoint endpoint;
  990. * acceptor.accept(socket, endpoint);
  991. * @endcode
  992. */
  993. template <typename SocketService>
  994. void accept(basic_socket<protocol_type, SocketService>& peer,
  995. endpoint_type& peer_endpoint)
  996. {
  997. boost::system::error_code ec;
  998. this->get_service().accept(this->get_implementation(),
  999. peer, &peer_endpoint, ec);
  1000. boost::asio::detail::throw_error(ec, "accept");
  1001. }
  1002. /// Accept a new connection and obtain the endpoint of the peer
  1003. /**
  1004. * This function is used to accept a new connection from a peer into the
  1005. * given socket, and additionally provide the endpoint of the remote peer.
  1006. * The function call will block until a new connection has been accepted
  1007. * successfully or an error occurs.
  1008. *
  1009. * @param peer The socket into which the new connection will be accepted.
  1010. *
  1011. * @param peer_endpoint An endpoint object which will receive the endpoint of
  1012. * the remote peer.
  1013. *
  1014. * @param ec Set to indicate what error occurred, if any.
  1015. *
  1016. * @par Example
  1017. * @code
  1018. * boost::asio::ip::tcp::acceptor acceptor(io_service);
  1019. * ...
  1020. * boost::asio::ip::tcp::socket socket(io_service);
  1021. * boost::asio::ip::tcp::endpoint endpoint;
  1022. * boost::system::error_code ec;
  1023. * acceptor.accept(socket, endpoint, ec);
  1024. * if (ec)
  1025. * {
  1026. * // An error occurred.
  1027. * }
  1028. * @endcode
  1029. */
  1030. template <typename SocketService>
  1031. boost::system::error_code accept(
  1032. basic_socket<protocol_type, SocketService>& peer,
  1033. endpoint_type& peer_endpoint, boost::system::error_code& ec)
  1034. {
  1035. return this->get_service().accept(
  1036. this->get_implementation(), peer, &peer_endpoint, ec);
  1037. }
  1038. /// Start an asynchronous accept.
  1039. /**
  1040. * This function is used to asynchronously accept a new connection into a
  1041. * socket, and additionally obtain the endpoint of the remote peer. The
  1042. * function call always returns immediately.
  1043. *
  1044. * @param peer The socket into which the new connection will be accepted.
  1045. * Ownership of the peer object is retained by the caller, which must
  1046. * guarantee that it is valid until the handler is called.
  1047. *
  1048. * @param peer_endpoint An endpoint object into which the endpoint of the
  1049. * remote peer will be written. Ownership of the peer_endpoint object is
  1050. * retained by the caller, which must guarantee that it is valid until the
  1051. * handler is called.
  1052. *
  1053. * @param handler The handler to be called when the accept operation
  1054. * completes. Copies will be made of the handler as required. The function
  1055. * signature of the handler must be:
  1056. * @code void handler(
  1057. * const boost::system::error_code& error // Result of operation.
  1058. * ); @endcode
  1059. * Regardless of whether the asynchronous operation completes immediately or
  1060. * not, the handler will not be invoked from within this function. Invocation
  1061. * of the handler will be performed in a manner equivalent to using
  1062. * boost::asio::io_service::post().
  1063. */
  1064. template <typename SocketService, typename AcceptHandler>
  1065. BOOST_ASIO_INITFN_RESULT_TYPE(AcceptHandler,
  1066. void (boost::system::error_code))
  1067. async_accept(basic_socket<protocol_type, SocketService>& peer,
  1068. endpoint_type& peer_endpoint, BOOST_ASIO_MOVE_ARG(AcceptHandler) handler)
  1069. {
  1070. // If you get an error on the following line it means that your handler does
  1071. // not meet the documented type requirements for a AcceptHandler.
  1072. BOOST_ASIO_ACCEPT_HANDLER_CHECK(AcceptHandler, handler) type_check;
  1073. return this->get_service().async_accept(this->get_implementation(), peer,
  1074. &peer_endpoint, BOOST_ASIO_MOVE_CAST(AcceptHandler)(handler));
  1075. }
  1076. };
  1077. } // namespace asio
  1078. } // namespace boost
  1079. #include <boost/asio/detail/pop_options.hpp>
  1080. #endif // BOOST_ASIO_BASIC_SOCKET_ACCEPTOR_HPP