2 // socket_acceptor_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_SOCKET_ACCEPTOR_SERVICE_HPP
12 #define BOOST_ASIO_SOCKET_ACCEPTOR_SERVICE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/basic_socket.hpp>
20 #include <boost/asio/error.hpp>
21 #include <boost/asio/io_service.hpp>
23 #if defined(BOOST_ASIO_HAS_IOCP)
24 # include <boost/asio/detail/win_iocp_socket_service.hpp>
26 # include <boost/asio/detail/reactive_socket_service.hpp>
29 #include <boost/asio/detail/push_options.hpp>
34 /// Default service implementation for a socket acceptor.
35 template <typename Protocol>
36 class socket_acceptor_service
37 #if defined(GENERATING_DOCUMENTATION)
38 : public boost::asio::io_service::service
40 : public boost::asio::detail::service_base<socket_acceptor_service<Protocol> >
44 #if defined(GENERATING_DOCUMENTATION)
45 /// The unique service identifier.
46 static boost::asio::io_service::id id;
49 /// The protocol type.
50 typedef Protocol protocol_type;
52 /// The endpoint type.
53 typedef typename protocol_type::endpoint endpoint_type;
56 // The type of the platform-specific implementation.
57 #if defined(BOOST_ASIO_HAS_IOCP)
58 typedef detail::win_iocp_socket_service<Protocol> service_impl_type;
60 typedef detail::reactive_socket_service<Protocol> service_impl_type;
64 /// The native type of the socket acceptor.
65 #if defined(GENERATING_DOCUMENTATION)
66 typedef implementation_defined implementation_type;
68 typedef typename service_impl_type::implementation_type implementation_type;
71 /// (Deprecated: Use native_handle_type.) The native acceptor type.
72 #if defined(GENERATING_DOCUMENTATION)
73 typedef implementation_defined native_type;
75 typedef typename service_impl_type::native_handle_type native_type;
78 /// The native acceptor type.
79 #if defined(GENERATING_DOCUMENTATION)
80 typedef implementation_defined native_handle_type;
82 typedef typename service_impl_type::native_handle_type native_handle_type;
85 /// Construct a new socket acceptor service for the specified io_service.
86 explicit socket_acceptor_service(boost::asio::io_service& io_service)
87 : boost::asio::detail::service_base<
88 socket_acceptor_service<Protocol> >(io_service),
89 service_impl_(io_service)
93 /// Construct a new socket acceptor implementation.
94 void construct(implementation_type& impl)
96 service_impl_.construct(impl);
99 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
100 /// Move-construct a new socket acceptor implementation.
101 void move_construct(implementation_type& impl,
102 implementation_type& other_impl)
104 service_impl_.move_construct(impl, other_impl);
107 /// Move-assign from another socket acceptor implementation.
108 void move_assign(implementation_type& impl,
109 socket_acceptor_service& other_service,
110 implementation_type& other_impl)
112 service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
114 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
116 /// Destroy a socket acceptor implementation.
117 void destroy(implementation_type& impl)
119 service_impl_.destroy(impl);
122 /// Open a new socket acceptor implementation.
123 boost::system::error_code open(implementation_type& impl,
124 const protocol_type& protocol, boost::system::error_code& ec)
126 return service_impl_.open(impl, protocol, ec);
129 /// Assign an existing native acceptor to a socket acceptor.
130 boost::system::error_code assign(implementation_type& impl,
131 const protocol_type& protocol, const native_handle_type& native_acceptor,
132 boost::system::error_code& ec)
134 return service_impl_.assign(impl, protocol, native_acceptor, ec);
137 /// Determine whether the acceptor is open.
138 bool is_open(const implementation_type& impl) const
140 return service_impl_.is_open(impl);
143 /// Cancel all asynchronous operations associated with the acceptor.
144 boost::system::error_code cancel(implementation_type& impl,
145 boost::system::error_code& ec)
147 return service_impl_.cancel(impl, ec);
150 /// Bind the socket acceptor to the specified local endpoint.
151 boost::system::error_code bind(implementation_type& impl,
152 const endpoint_type& endpoint, boost::system::error_code& ec)
154 return service_impl_.bind(impl, endpoint, ec);
157 /// Place the socket acceptor into the state where it will listen for new
159 boost::system::error_code listen(implementation_type& impl, int backlog,
160 boost::system::error_code& ec)
162 return service_impl_.listen(impl, backlog, ec);
165 /// Close a socket acceptor implementation.
166 boost::system::error_code close(implementation_type& impl,
167 boost::system::error_code& ec)
169 return service_impl_.close(impl, ec);
172 /// (Deprecated: Use native_handle().) Get the native acceptor implementation.
173 native_type native(implementation_type& impl)
175 return service_impl_.native_handle(impl);
178 /// Get the native acceptor implementation.
179 native_handle_type native_handle(implementation_type& impl)
181 return service_impl_.native_handle(impl);
184 /// Set a socket option.
185 template <typename SettableSocketOption>
186 boost::system::error_code set_option(implementation_type& impl,
187 const SettableSocketOption& option, boost::system::error_code& ec)
189 return service_impl_.set_option(impl, option, ec);
192 /// Get a socket option.
193 template <typename GettableSocketOption>
194 boost::system::error_code get_option(const implementation_type& impl,
195 GettableSocketOption& option, boost::system::error_code& ec) const
197 return service_impl_.get_option(impl, option, ec);
200 /// Perform an IO control command on the socket.
201 template <typename IoControlCommand>
202 boost::system::error_code io_control(implementation_type& impl,
203 IoControlCommand& command, boost::system::error_code& ec)
205 return service_impl_.io_control(impl, command, ec);
208 /// Gets the non-blocking mode of the acceptor.
209 bool non_blocking(const implementation_type& impl) const
211 return service_impl_.non_blocking(impl);
214 /// Sets the non-blocking mode of the acceptor.
215 boost::system::error_code non_blocking(implementation_type& impl,
216 bool mode, boost::system::error_code& ec)
218 return service_impl_.non_blocking(impl, mode, ec);
221 /// Gets the non-blocking mode of the native acceptor implementation.
222 bool native_non_blocking(const implementation_type& impl) const
224 return service_impl_.native_non_blocking(impl);
227 /// Sets the non-blocking mode of the native acceptor implementation.
228 boost::system::error_code native_non_blocking(implementation_type& impl,
229 bool mode, boost::system::error_code& ec)
231 return service_impl_.native_non_blocking(impl, mode, ec);
234 /// Get the local endpoint.
235 endpoint_type local_endpoint(const implementation_type& impl,
236 boost::system::error_code& ec) const
238 return service_impl_.local_endpoint(impl, ec);
241 /// Accept a new connection.
242 template <typename SocketService>
243 boost::system::error_code accept(implementation_type& impl,
244 basic_socket<protocol_type, SocketService>& peer,
245 endpoint_type* peer_endpoint, boost::system::error_code& ec)
247 return service_impl_.accept(impl, peer, peer_endpoint, ec);
250 /// Start an asynchronous accept.
251 template <typename SocketService, typename AcceptHandler>
252 void async_accept(implementation_type& impl,
253 basic_socket<protocol_type, SocketService>& peer,
254 endpoint_type* peer_endpoint,
255 BOOST_ASIO_MOVE_ARG(AcceptHandler) handler)
257 service_impl_.async_accept(impl, peer, peer_endpoint,
258 BOOST_ASIO_MOVE_CAST(AcceptHandler)(handler));
262 // Destroy all user-defined handler objects owned by the service.
263 void shutdown_service()
265 service_impl_.shutdown_service();
268 // The platform-specific implementation.
269 service_impl_type service_impl_;
275 #include <boost/asio/detail/pop_options.hpp>
277 #endif // BOOST_ASIO_SOCKET_ACCEPTOR_SERVICE_HPP