basic_socket_acceptor.hpp 37 KB

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