socket_acceptor_service.hpp 9.5 KB

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