socket_acceptor_service.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. //
  2. // socket_acceptor_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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_SOCKET_ACCEPTOR_SERVICE_HPP
  11. #define BOOST_ASIO_SOCKET_ACCEPTOR_SERVICE_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. #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  17. #include <boost/asio/basic_socket.hpp>
  18. #include <boost/asio/detail/type_traits.hpp>
  19. #include <boost/asio/error.hpp>
  20. #include <boost/asio/io_context.hpp>
  21. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  22. # include <boost/asio/detail/null_socket_service.hpp>
  23. #elif defined(BOOST_ASIO_HAS_IOCP)
  24. # include <boost/asio/detail/win_iocp_socket_service.hpp>
  25. #else
  26. # include <boost/asio/detail/reactive_socket_service.hpp>
  27. #endif
  28. #include <boost/asio/detail/push_options.hpp>
  29. namespace boost {
  30. namespace asio {
  31. /// Default service implementation for a socket acceptor.
  32. template <typename Protocol>
  33. class socket_acceptor_service
  34. #if defined(GENERATING_DOCUMENTATION)
  35. : public boost::asio::io_context::service
  36. #else
  37. : public boost::asio::detail::service_base<socket_acceptor_service<Protocol> >
  38. #endif
  39. {
  40. public:
  41. #if defined(GENERATING_DOCUMENTATION)
  42. /// The unique service identifier.
  43. static boost::asio::io_context::id id;
  44. #endif
  45. /// The protocol type.
  46. typedef Protocol protocol_type;
  47. /// The endpoint type.
  48. typedef typename protocol_type::endpoint endpoint_type;
  49. private:
  50. // The type of the platform-specific implementation.
  51. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  52. typedef detail::null_socket_service<Protocol> service_impl_type;
  53. #elif defined(BOOST_ASIO_HAS_IOCP)
  54. typedef detail::win_iocp_socket_service<Protocol> service_impl_type;
  55. #else
  56. typedef detail::reactive_socket_service<Protocol> service_impl_type;
  57. #endif
  58. public:
  59. /// The native type of the socket acceptor.
  60. #if defined(GENERATING_DOCUMENTATION)
  61. typedef implementation_defined implementation_type;
  62. #else
  63. typedef typename service_impl_type::implementation_type implementation_type;
  64. #endif
  65. /// The native acceptor type.
  66. #if defined(GENERATING_DOCUMENTATION)
  67. typedef implementation_defined native_handle_type;
  68. #else
  69. typedef typename service_impl_type::native_handle_type native_handle_type;
  70. #endif
  71. /// Construct a new socket acceptor service for the specified io_context.
  72. explicit socket_acceptor_service(boost::asio::io_context& io_context)
  73. : boost::asio::detail::service_base<
  74. socket_acceptor_service<Protocol> >(io_context),
  75. service_impl_(io_context)
  76. {
  77. }
  78. /// Construct a new socket acceptor implementation.
  79. void construct(implementation_type& impl)
  80. {
  81. service_impl_.construct(impl);
  82. }
  83. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  84. /// Move-construct a new socket acceptor implementation.
  85. void move_construct(implementation_type& impl,
  86. implementation_type& other_impl)
  87. {
  88. service_impl_.move_construct(impl, other_impl);
  89. }
  90. /// Move-assign from another socket acceptor implementation.
  91. void move_assign(implementation_type& impl,
  92. socket_acceptor_service& other_service,
  93. implementation_type& other_impl)
  94. {
  95. service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
  96. }
  97. // All acceptor services have access to each other's implementations.
  98. template <typename Protocol1> friend class socket_acceptor_service;
  99. /// Move-construct a new socket acceptor implementation from another protocol
  100. /// type.
  101. template <typename Protocol1>
  102. void converting_move_construct(implementation_type& impl,
  103. socket_acceptor_service<Protocol1>& other_service,
  104. typename socket_acceptor_service<
  105. Protocol1>::implementation_type& other_impl,
  106. typename enable_if<is_convertible<
  107. Protocol1, Protocol>::value>::type* = 0)
  108. {
  109. service_impl_.template converting_move_construct<Protocol1>(
  110. impl, other_service.service_impl_, other_impl);
  111. }
  112. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  113. /// Destroy a socket acceptor implementation.
  114. void destroy(implementation_type& impl)
  115. {
  116. service_impl_.destroy(impl);
  117. }
  118. /// Open a new socket acceptor implementation.
  119. BOOST_ASIO_SYNC_OP_VOID open(implementation_type& impl,
  120. const protocol_type& protocol, boost::system::error_code& ec)
  121. {
  122. service_impl_.open(impl, protocol, ec);
  123. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  124. }
  125. /// Assign an existing native acceptor to a socket acceptor.
  126. BOOST_ASIO_SYNC_OP_VOID assign(implementation_type& impl,
  127. const protocol_type& protocol, const native_handle_type& native_acceptor,
  128. boost::system::error_code& ec)
  129. {
  130. service_impl_.assign(impl, protocol, native_acceptor, ec);
  131. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  132. }
  133. /// Determine whether the acceptor is open.
  134. bool is_open(const implementation_type& impl) const
  135. {
  136. return service_impl_.is_open(impl);
  137. }
  138. /// Cancel all asynchronous operations associated with the acceptor.
  139. BOOST_ASIO_SYNC_OP_VOID cancel(implementation_type& impl,
  140. boost::system::error_code& ec)
  141. {
  142. service_impl_.cancel(impl, ec);
  143. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  144. }
  145. /// Bind the socket acceptor to the specified local endpoint.
  146. BOOST_ASIO_SYNC_OP_VOID bind(implementation_type& impl,
  147. const endpoint_type& endpoint, boost::system::error_code& ec)
  148. {
  149. service_impl_.bind(impl, endpoint, ec);
  150. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  151. }
  152. /// Place the socket acceptor into the state where it will listen for new
  153. /// connections.
  154. BOOST_ASIO_SYNC_OP_VOID listen(implementation_type& impl, int backlog,
  155. boost::system::error_code& ec)
  156. {
  157. service_impl_.listen(impl, backlog, ec);
  158. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  159. }
  160. /// Close a socket acceptor implementation.
  161. BOOST_ASIO_SYNC_OP_VOID close(implementation_type& impl,
  162. boost::system::error_code& ec)
  163. {
  164. service_impl_.close(impl, ec);
  165. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  166. }
  167. /// Release ownership of the underlying acceptor.
  168. native_handle_type release(implementation_type& impl,
  169. boost::system::error_code& ec)
  170. {
  171. return service_impl_.release(impl, ec);
  172. }
  173. /// Get the native acceptor implementation.
  174. native_handle_type native_handle(implementation_type& impl)
  175. {
  176. return service_impl_.native_handle(impl);
  177. }
  178. /// Set a socket option.
  179. template <typename SettableSocketOption>
  180. BOOST_ASIO_SYNC_OP_VOID set_option(implementation_type& impl,
  181. const SettableSocketOption& option, boost::system::error_code& ec)
  182. {
  183. service_impl_.set_option(impl, option, ec);
  184. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  185. }
  186. /// Get a socket option.
  187. template <typename GettableSocketOption>
  188. BOOST_ASIO_SYNC_OP_VOID get_option(const implementation_type& impl,
  189. GettableSocketOption& option, boost::system::error_code& ec) const
  190. {
  191. service_impl_.get_option(impl, option, ec);
  192. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  193. }
  194. /// Perform an IO control command on the socket.
  195. template <typename IoControlCommand>
  196. BOOST_ASIO_SYNC_OP_VOID io_control(implementation_type& impl,
  197. IoControlCommand& command, boost::system::error_code& ec)
  198. {
  199. service_impl_.io_control(impl, command, ec);
  200. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  201. }
  202. /// Gets the non-blocking mode of the acceptor.
  203. bool non_blocking(const implementation_type& impl) const
  204. {
  205. return service_impl_.non_blocking(impl);
  206. }
  207. /// Sets the non-blocking mode of the acceptor.
  208. BOOST_ASIO_SYNC_OP_VOID non_blocking(implementation_type& impl,
  209. bool mode, boost::system::error_code& ec)
  210. {
  211. service_impl_.non_blocking(impl, mode, ec);
  212. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  213. }
  214. /// Gets the non-blocking mode of the native acceptor implementation.
  215. bool native_non_blocking(const implementation_type& impl) const
  216. {
  217. return service_impl_.native_non_blocking(impl);
  218. }
  219. /// Sets the non-blocking mode of the native acceptor implementation.
  220. BOOST_ASIO_SYNC_OP_VOID native_non_blocking(implementation_type& impl,
  221. bool mode, boost::system::error_code& ec)
  222. {
  223. service_impl_.native_non_blocking(impl, mode, ec);
  224. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  225. }
  226. /// Get the local endpoint.
  227. endpoint_type local_endpoint(const implementation_type& impl,
  228. boost::system::error_code& ec) const
  229. {
  230. return service_impl_.local_endpoint(impl, ec);
  231. }
  232. /// Wait for the acceptor to become ready to read, ready to write, or to have
  233. /// pending error conditions.
  234. BOOST_ASIO_SYNC_OP_VOID wait(implementation_type& impl,
  235. socket_base::wait_type w, boost::system::error_code& ec)
  236. {
  237. service_impl_.wait(impl, w, ec);
  238. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  239. }
  240. /// Asynchronously wait for the acceptor to become ready to read, ready to
  241. /// write, or to have pending error conditions.
  242. template <typename WaitHandler>
  243. BOOST_ASIO_INITFN_RESULT_TYPE(WaitHandler,
  244. void (boost::system::error_code))
  245. async_wait(implementation_type& impl, socket_base::wait_type w,
  246. BOOST_ASIO_MOVE_ARG(WaitHandler) handler)
  247. {
  248. async_completion<WaitHandler,
  249. void (boost::system::error_code)> init(handler);
  250. service_impl_.async_wait(impl, w, init.completion_handler);
  251. return init.result.get();
  252. }
  253. /// Accept a new connection.
  254. template <typename Protocol1, typename SocketService>
  255. BOOST_ASIO_SYNC_OP_VOID accept(implementation_type& impl,
  256. basic_socket<Protocol1, SocketService>& peer,
  257. endpoint_type* peer_endpoint, boost::system::error_code& ec,
  258. typename enable_if<is_convertible<Protocol, Protocol1>::value>::type* = 0)
  259. {
  260. service_impl_.accept(impl, peer, peer_endpoint, ec);
  261. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  262. }
  263. #if defined(BOOST_ASIO_HAS_MOVE)
  264. /// Accept a new connection.
  265. typename Protocol::socket accept(implementation_type& impl,
  266. io_context* peer_io_context, endpoint_type* peer_endpoint,
  267. boost::system::error_code& ec)
  268. {
  269. return service_impl_.accept(impl, peer_io_context, peer_endpoint, ec);
  270. }
  271. #endif // defined(BOOST_ASIO_HAS_MOVE)
  272. /// Start an asynchronous accept.
  273. template <typename Protocol1, typename SocketService, typename AcceptHandler>
  274. BOOST_ASIO_INITFN_RESULT_TYPE(AcceptHandler,
  275. void (boost::system::error_code))
  276. async_accept(implementation_type& impl,
  277. basic_socket<Protocol1, SocketService>& peer,
  278. endpoint_type* peer_endpoint,
  279. BOOST_ASIO_MOVE_ARG(AcceptHandler) handler,
  280. typename enable_if<is_convertible<Protocol, Protocol1>::value>::type* = 0)
  281. {
  282. async_completion<AcceptHandler,
  283. void (boost::system::error_code)> init(handler);
  284. service_impl_.async_accept(impl,
  285. peer, peer_endpoint, init.completion_handler);
  286. return init.result.get();
  287. }
  288. #if defined(BOOST_ASIO_HAS_MOVE)
  289. /// Start an asynchronous accept.
  290. template <typename MoveAcceptHandler>
  291. BOOST_ASIO_INITFN_RESULT_TYPE(MoveAcceptHandler,
  292. void (boost::system::error_code, typename Protocol::socket))
  293. async_accept(implementation_type& impl,
  294. boost::asio::io_context* peer_io_context, endpoint_type* peer_endpoint,
  295. BOOST_ASIO_MOVE_ARG(MoveAcceptHandler) handler)
  296. {
  297. async_completion<MoveAcceptHandler,
  298. void (boost::system::error_code,
  299. typename Protocol::socket)> init(handler);
  300. service_impl_.async_accept(impl,
  301. peer_io_context, peer_endpoint, init.completion_handler);
  302. return init.result.get();
  303. }
  304. #endif // defined(BOOST_ASIO_HAS_MOVE)
  305. private:
  306. // Destroy all user-defined handler objects owned by the service.
  307. void shutdown()
  308. {
  309. service_impl_.shutdown();
  310. }
  311. // The platform-specific implementation.
  312. service_impl_type service_impl_;
  313. };
  314. } // namespace asio
  315. } // namespace boost
  316. #include <boost/asio/detail/pop_options.hpp>
  317. #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  318. #endif // BOOST_ASIO_SOCKET_ACCEPTOR_SERVICE_HPP