socket_acceptor_service.hpp 6.1 KB

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