socket_acceptor_service.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // socket_acceptor_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 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/push_options.hpp>
  16. #include <boost/asio/basic_socket.hpp>
  17. #include <boost/asio/error.hpp>
  18. #include <boost/asio/io_service.hpp>
  19. #include <boost/asio/detail/epoll_reactor.hpp>
  20. #include <boost/asio/detail/kqueue_reactor.hpp>
  21. #include <boost/asio/detail/select_reactor.hpp>
  22. #include <boost/asio/detail/service_base.hpp>
  23. #include <boost/asio/detail/reactive_socket_service.hpp>
  24. #include <boost/asio/detail/win_iocp_socket_service.hpp>
  25. namespace boost {
  26. namespace asio {
  27. /// Default service implementation for a socket acceptor.
  28. template <typename Protocol>
  29. class socket_acceptor_service
  30. #if defined(GENERATING_DOCUMENTATION)
  31. : public boost::asio::io_service::service
  32. #else
  33. : public boost::asio::detail::service_base<socket_acceptor_service<Protocol> >
  34. #endif
  35. {
  36. public:
  37. #if defined(GENERATING_DOCUMENTATION)
  38. /// The unique service identifier.
  39. static boost::asio::io_service::id id;
  40. #endif
  41. /// The protocol type.
  42. typedef Protocol protocol_type;
  43. /// The endpoint type.
  44. typedef typename protocol_type::endpoint endpoint_type;
  45. private:
  46. // The type of the platform-specific implementation.
  47. #if defined(BOOST_ASIO_HAS_IOCP)
  48. typedef detail::win_iocp_socket_service<Protocol> service_impl_type;
  49. #elif defined(BOOST_ASIO_HAS_EPOLL)
  50. typedef detail::reactive_socket_service<
  51. Protocol, detail::epoll_reactor<false> > service_impl_type;
  52. #elif defined(BOOST_ASIO_HAS_KQUEUE)
  53. typedef detail::reactive_socket_service<
  54. Protocol, detail::kqueue_reactor<false> > service_impl_type;
  55. #elif defined(BOOST_ASIO_HAS_DEV_POLL)
  56. typedef detail::reactive_socket_service<
  57. Protocol, detail::dev_poll_reactor<false> > service_impl_type;
  58. #else
  59. typedef detail::reactive_socket_service<
  60. Protocol, detail::select_reactor<false> > service_impl_type;
  61. #endif
  62. public:
  63. /// The native type of the socket acceptor.
  64. #if defined(GENERATING_DOCUMENTATION)
  65. typedef implementation_defined implementation_type;
  66. #else
  67. typedef typename service_impl_type::implementation_type implementation_type;
  68. #endif
  69. /// The native acceptor type.
  70. #if defined(GENERATING_DOCUMENTATION)
  71. typedef implementation_defined native_type;
  72. #else
  73. typedef typename service_impl_type::native_type native_type;
  74. #endif
  75. /// Construct a new socket acceptor service for the specified io_service.
  76. explicit socket_acceptor_service(boost::asio::io_service& io_service)
  77. : boost::asio::detail::service_base<
  78. socket_acceptor_service<Protocol> >(io_service),
  79. service_impl_(boost::asio::use_service<service_impl_type>(io_service))
  80. {
  81. }
  82. /// Destroy all user-defined handler objects owned by the service.
  83. void shutdown_service()
  84. {
  85. }
  86. /// Construct a new socket acceptor implementation.
  87. void construct(implementation_type& impl)
  88. {
  89. service_impl_.construct(impl);
  90. }
  91. /// Destroy a socket acceptor implementation.
  92. void destroy(implementation_type& impl)
  93. {
  94. service_impl_.destroy(impl);
  95. }
  96. /// Open a new socket acceptor implementation.
  97. boost::system::error_code open(implementation_type& impl,
  98. const protocol_type& protocol, boost::system::error_code& ec)
  99. {
  100. return service_impl_.open(impl, protocol, ec);
  101. }
  102. /// Assign an existing native acceptor to a socket acceptor.
  103. boost::system::error_code assign(implementation_type& impl,
  104. const protocol_type& protocol, const native_type& native_acceptor,
  105. boost::system::error_code& ec)
  106. {
  107. return service_impl_.assign(impl, protocol, native_acceptor, ec);
  108. }
  109. /// Determine whether the acceptor is open.
  110. bool is_open(const implementation_type& impl) const
  111. {
  112. return service_impl_.is_open(impl);
  113. }
  114. /// Cancel all asynchronous operations associated with the acceptor.
  115. boost::system::error_code cancel(implementation_type& impl,
  116. boost::system::error_code& ec)
  117. {
  118. return service_impl_.cancel(impl, ec);
  119. }
  120. /// Bind the socket acceptor to the specified local endpoint.
  121. boost::system::error_code bind(implementation_type& impl,
  122. const endpoint_type& endpoint, boost::system::error_code& ec)
  123. {
  124. return service_impl_.bind(impl, endpoint, ec);
  125. }
  126. /// Place the socket acceptor into the state where it will listen for new
  127. /// connections.
  128. boost::system::error_code listen(implementation_type& impl, int backlog,
  129. boost::system::error_code& ec)
  130. {
  131. return service_impl_.listen(impl, backlog, ec);
  132. }
  133. /// Close a socket acceptor implementation.
  134. boost::system::error_code close(implementation_type& impl,
  135. boost::system::error_code& ec)
  136. {
  137. return service_impl_.close(impl, ec);
  138. }
  139. /// Get the native acceptor implementation.
  140. native_type native(implementation_type& impl)
  141. {
  142. return service_impl_.native(impl);
  143. }
  144. /// Set a socket option.
  145. template <typename SettableSocketOption>
  146. boost::system::error_code set_option(implementation_type& impl,
  147. const SettableSocketOption& option, boost::system::error_code& ec)
  148. {
  149. return service_impl_.set_option(impl, option, ec);
  150. }
  151. /// Get a socket option.
  152. template <typename GettableSocketOption>
  153. boost::system::error_code get_option(const implementation_type& impl,
  154. GettableSocketOption& option, boost::system::error_code& ec) const
  155. {
  156. return service_impl_.get_option(impl, option, ec);
  157. }
  158. /// Perform an IO control command on the socket.
  159. template <typename IoControlCommand>
  160. boost::system::error_code io_control(implementation_type& impl,
  161. IoControlCommand& command, boost::system::error_code& ec)
  162. {
  163. return service_impl_.io_control(impl, command, ec);
  164. }
  165. /// Get the local endpoint.
  166. endpoint_type local_endpoint(const implementation_type& impl,
  167. boost::system::error_code& ec) const
  168. {
  169. return service_impl_.local_endpoint(impl, ec);
  170. }
  171. /// Accept a new connection.
  172. template <typename SocketService>
  173. boost::system::error_code accept(implementation_type& impl,
  174. basic_socket<protocol_type, SocketService>& peer,
  175. endpoint_type* peer_endpoint, boost::system::error_code& ec)
  176. {
  177. return service_impl_.accept(impl, peer, peer_endpoint, ec);
  178. }
  179. /// Start an asynchronous accept.
  180. template <typename SocketService, typename AcceptHandler>
  181. void async_accept(implementation_type& impl,
  182. basic_socket<protocol_type, SocketService>& peer,
  183. endpoint_type* peer_endpoint, AcceptHandler handler)
  184. {
  185. service_impl_.async_accept(impl, peer, peer_endpoint, handler);
  186. }
  187. private:
  188. // The service that provides the platform-specific implementation.
  189. service_impl_type& service_impl_;
  190. };
  191. } // namespace asio
  192. } // namespace boost
  193. #include <boost/asio/detail/pop_options.hpp>
  194. #endif // BOOST_ASIO_SOCKET_ACCEPTOR_SERVICE_HPP