socket_acceptor_service.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //
  2. // socket_acceptor_service.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_SOCKET_ACCEPTOR_SERVICE_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #include "asio/basic_socket.hpp"
  17. #include "asio/detail/type_traits.hpp"
  18. #include "asio/error.hpp"
  19. #include "asio/io_service.hpp"
  20. #if defined(ASIO_WINDOWS_RUNTIME)
  21. # include "asio/detail/null_socket_service.hpp"
  22. #elif defined(ASIO_HAS_IOCP)
  23. # include "asio/detail/win_iocp_socket_service.hpp"
  24. #else
  25. # include "asio/detail/reactive_socket_service.hpp"
  26. #endif
  27. #include "asio/detail/push_options.hpp"
  28. namespace asio {
  29. /// Default service implementation for a socket acceptor.
  30. template <typename Protocol>
  31. class socket_acceptor_service
  32. #if defined(GENERATING_DOCUMENTATION)
  33. : public asio::io_service::service
  34. #else
  35. : public asio::detail::service_base<socket_acceptor_service<Protocol> >
  36. #endif
  37. {
  38. public:
  39. #if defined(GENERATING_DOCUMENTATION)
  40. /// The unique service identifier.
  41. static asio::io_service::id id;
  42. #endif
  43. /// The protocol type.
  44. typedef Protocol protocol_type;
  45. /// The endpoint type.
  46. typedef typename protocol_type::endpoint endpoint_type;
  47. private:
  48. // The type of the platform-specific implementation.
  49. #if defined(ASIO_WINDOWS_RUNTIME)
  50. typedef detail::null_socket_service<Protocol> service_impl_type;
  51. #elif defined(ASIO_HAS_IOCP)
  52. typedef detail::win_iocp_socket_service<Protocol> service_impl_type;
  53. #else
  54. typedef detail::reactive_socket_service<Protocol> service_impl_type;
  55. #endif
  56. public:
  57. /// The native type of the socket acceptor.
  58. #if defined(GENERATING_DOCUMENTATION)
  59. typedef implementation_defined implementation_type;
  60. #else
  61. typedef typename service_impl_type::implementation_type implementation_type;
  62. #endif
  63. /// (Deprecated: Use native_handle_type.) The native acceptor type.
  64. #if defined(GENERATING_DOCUMENTATION)
  65. typedef implementation_defined native_type;
  66. #else
  67. typedef typename service_impl_type::native_handle_type native_type;
  68. #endif
  69. /// The native acceptor type.
  70. #if defined(GENERATING_DOCUMENTATION)
  71. typedef implementation_defined native_handle_type;
  72. #else
  73. typedef typename service_impl_type::native_handle_type native_handle_type;
  74. #endif
  75. /// Construct a new socket acceptor service for the specified io_service.
  76. explicit socket_acceptor_service(asio::io_service& io_service)
  77. : asio::detail::service_base<
  78. socket_acceptor_service<Protocol> >(io_service),
  79. service_impl_(io_service)
  80. {
  81. }
  82. /// Construct a new socket acceptor implementation.
  83. void construct(implementation_type& impl)
  84. {
  85. service_impl_.construct(impl);
  86. }
  87. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  88. /// Move-construct a new socket acceptor implementation.
  89. void move_construct(implementation_type& impl,
  90. implementation_type& other_impl)
  91. {
  92. service_impl_.move_construct(impl, other_impl);
  93. }
  94. /// Move-assign from another socket acceptor implementation.
  95. void move_assign(implementation_type& impl,
  96. socket_acceptor_service& other_service,
  97. implementation_type& other_impl)
  98. {
  99. service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
  100. }
  101. /// Move-construct a new socket acceptor implementation from another protocol
  102. /// type.
  103. template <typename Protocol1>
  104. void converting_move_construct(implementation_type& impl,
  105. typename socket_acceptor_service<
  106. Protocol1>::implementation_type& other_impl,
  107. typename enable_if<is_convertible<
  108. Protocol1, Protocol>::value>::type* = 0)
  109. {
  110. service_impl_.template converting_move_construct<Protocol1>(
  111. impl, other_impl);
  112. }
  113. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  114. /// Destroy a socket acceptor implementation.
  115. void destroy(implementation_type& impl)
  116. {
  117. service_impl_.destroy(impl);
  118. }
  119. /// Open a new socket acceptor implementation.
  120. asio::error_code open(implementation_type& impl,
  121. const protocol_type& protocol, asio::error_code& ec)
  122. {
  123. return service_impl_.open(impl, protocol, ec);
  124. }
  125. /// Assign an existing native acceptor to a socket acceptor.
  126. asio::error_code assign(implementation_type& impl,
  127. const protocol_type& protocol, const native_handle_type& native_acceptor,
  128. asio::error_code& ec)
  129. {
  130. return service_impl_.assign(impl, protocol, native_acceptor, ec);
  131. }
  132. /// Determine whether the acceptor is open.
  133. bool is_open(const implementation_type& impl) const
  134. {
  135. return service_impl_.is_open(impl);
  136. }
  137. /// Cancel all asynchronous operations associated with the acceptor.
  138. asio::error_code cancel(implementation_type& impl,
  139. asio::error_code& ec)
  140. {
  141. return service_impl_.cancel(impl, ec);
  142. }
  143. /// Bind the socket acceptor to the specified local endpoint.
  144. asio::error_code bind(implementation_type& impl,
  145. const endpoint_type& endpoint, asio::error_code& ec)
  146. {
  147. return service_impl_.bind(impl, endpoint, ec);
  148. }
  149. /// Place the socket acceptor into the state where it will listen for new
  150. /// connections.
  151. asio::error_code listen(implementation_type& impl, int backlog,
  152. asio::error_code& ec)
  153. {
  154. return service_impl_.listen(impl, backlog, ec);
  155. }
  156. /// Close a socket acceptor implementation.
  157. asio::error_code close(implementation_type& impl,
  158. asio::error_code& ec)
  159. {
  160. return service_impl_.close(impl, ec);
  161. }
  162. /// (Deprecated: Use native_handle().) Get the native acceptor implementation.
  163. native_type native(implementation_type& impl)
  164. {
  165. return service_impl_.native_handle(impl);
  166. }
  167. /// Get the native acceptor implementation.
  168. native_handle_type native_handle(implementation_type& impl)
  169. {
  170. return service_impl_.native_handle(impl);
  171. }
  172. /// Set a socket option.
  173. template <typename SettableSocketOption>
  174. asio::error_code set_option(implementation_type& impl,
  175. const SettableSocketOption& option, asio::error_code& ec)
  176. {
  177. return service_impl_.set_option(impl, option, ec);
  178. }
  179. /// Get a socket option.
  180. template <typename GettableSocketOption>
  181. asio::error_code get_option(const implementation_type& impl,
  182. GettableSocketOption& option, asio::error_code& ec) const
  183. {
  184. return service_impl_.get_option(impl, option, ec);
  185. }
  186. /// Perform an IO control command on the socket.
  187. template <typename IoControlCommand>
  188. asio::error_code io_control(implementation_type& impl,
  189. IoControlCommand& command, asio::error_code& ec)
  190. {
  191. return service_impl_.io_control(impl, command, ec);
  192. }
  193. /// Gets the non-blocking mode of the acceptor.
  194. bool non_blocking(const implementation_type& impl) const
  195. {
  196. return service_impl_.non_blocking(impl);
  197. }
  198. /// Sets the non-blocking mode of the acceptor.
  199. asio::error_code non_blocking(implementation_type& impl,
  200. bool mode, asio::error_code& ec)
  201. {
  202. return service_impl_.non_blocking(impl, mode, ec);
  203. }
  204. /// Gets the non-blocking mode of the native acceptor implementation.
  205. bool native_non_blocking(const implementation_type& impl) const
  206. {
  207. return service_impl_.native_non_blocking(impl);
  208. }
  209. /// Sets the non-blocking mode of the native acceptor implementation.
  210. asio::error_code native_non_blocking(implementation_type& impl,
  211. bool mode, asio::error_code& ec)
  212. {
  213. return service_impl_.native_non_blocking(impl, mode, ec);
  214. }
  215. /// Get the local endpoint.
  216. endpoint_type local_endpoint(const implementation_type& impl,
  217. asio::error_code& ec) const
  218. {
  219. return service_impl_.local_endpoint(impl, ec);
  220. }
  221. /// Accept a new connection.
  222. template <typename Protocol1, typename SocketService>
  223. asio::error_code accept(implementation_type& impl,
  224. basic_socket<Protocol1, SocketService>& peer,
  225. endpoint_type* peer_endpoint, asio::error_code& ec,
  226. typename enable_if<is_convertible<Protocol, Protocol1>::value>::type* = 0)
  227. {
  228. return service_impl_.accept(impl, peer, peer_endpoint, ec);
  229. }
  230. /// Start an asynchronous accept.
  231. template <typename Protocol1, typename SocketService, typename AcceptHandler>
  232. ASIO_INITFN_RESULT_TYPE(AcceptHandler,
  233. void (asio::error_code))
  234. async_accept(implementation_type& impl,
  235. basic_socket<Protocol1, SocketService>& peer,
  236. endpoint_type* peer_endpoint,
  237. ASIO_MOVE_ARG(AcceptHandler) handler,
  238. typename enable_if<is_convertible<Protocol, Protocol1>::value>::type* = 0)
  239. {
  240. detail::async_result_init<
  241. AcceptHandler, void (asio::error_code)> init(
  242. ASIO_MOVE_CAST(AcceptHandler)(handler));
  243. service_impl_.async_accept(impl, peer, peer_endpoint, init.handler);
  244. return init.result.get();
  245. }
  246. private:
  247. // Destroy all user-defined handler objects owned by the service.
  248. void shutdown_service()
  249. {
  250. service_impl_.shutdown_service();
  251. }
  252. // The platform-specific implementation.
  253. service_impl_type service_impl_;
  254. };
  255. } // namespace asio
  256. #include "asio/detail/pop_options.hpp"
  257. #endif // ASIO_SOCKET_ACCEPTOR_SERVICE_HPP