2 // socket_acceptor_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 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/detail/type_traits.hpp>
21 #include <boost/asio/error.hpp>
22 #include <boost/asio/io_service.hpp>
24 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
25 # include <boost/asio/detail/null_socket_service.hpp>
26 #elif defined(BOOST_ASIO_HAS_IOCP)
27 # include <boost/asio/detail/win_iocp_socket_service.hpp>
29 # include <boost/asio/detail/reactive_socket_service.hpp>
32 #include <boost/asio/detail/push_options.hpp>
37 /// Default service implementation for a socket acceptor.
38 template <typename Protocol>
39 class socket_acceptor_service
40 #if defined(GENERATING_DOCUMENTATION)
41 : public boost::asio::io_service::service
43 : public boost::asio::detail::service_base<socket_acceptor_service<Protocol> >
47 #if defined(GENERATING_DOCUMENTATION)
48 /// The unique service identifier.
49 static boost::asio::io_service::id id;
52 /// The protocol type.
53 typedef Protocol protocol_type;
55 /// The endpoint type.
56 typedef typename protocol_type::endpoint endpoint_type;
59 // The type of the platform-specific implementation.
60 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
61 typedef detail::null_socket_service<Protocol> service_impl_type;
62 #elif defined(BOOST_ASIO_HAS_IOCP)
63 typedef detail::win_iocp_socket_service<Protocol> service_impl_type;
65 typedef detail::reactive_socket_service<Protocol> service_impl_type;
69 /// The native type of the socket acceptor.
70 #if defined(GENERATING_DOCUMENTATION)
71 typedef implementation_defined implementation_type;
73 typedef typename service_impl_type::implementation_type implementation_type;
76 /// (Deprecated: Use native_handle_type.) The native acceptor type.
77 #if defined(GENERATING_DOCUMENTATION)
78 typedef implementation_defined native_type;
80 typedef typename service_impl_type::native_handle_type native_type;
83 /// The native acceptor type.
84 #if defined(GENERATING_DOCUMENTATION)
85 typedef implementation_defined native_handle_type;
87 typedef typename service_impl_type::native_handle_type native_handle_type;
90 /// Construct a new socket acceptor service for the specified io_service.
91 explicit socket_acceptor_service(boost::asio::io_service& io_service)
92 : boost::asio::detail::service_base<
93 socket_acceptor_service<Protocol> >(io_service),
94 service_impl_(io_service)
98 /// Construct a new socket acceptor implementation.
99 void construct(implementation_type& impl)
101 service_impl_.construct(impl);
104 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
105 /// Move-construct a new socket acceptor implementation.
106 void move_construct(implementation_type& impl,
107 implementation_type& other_impl)
109 service_impl_.move_construct(impl, other_impl);
112 /// Move-assign from another socket acceptor implementation.
113 void move_assign(implementation_type& impl,
114 socket_acceptor_service& other_service,
115 implementation_type& other_impl)
117 service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
120 /// Move-construct a new socket acceptor implementation from another protocol
122 template <typename Protocol1>
123 void converting_move_construct(implementation_type& impl,
124 typename socket_acceptor_service<
125 Protocol1>::implementation_type& other_impl,
126 typename enable_if<is_convertible<
127 Protocol1, Protocol>::value>::type* = 0)
129 service_impl_.template converting_move_construct<Protocol1>(
132 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
134 /// Destroy a socket acceptor implementation.
135 void destroy(implementation_type& impl)
137 service_impl_.destroy(impl);
140 /// Open a new socket acceptor implementation.
141 boost::system::error_code open(implementation_type& impl,
142 const protocol_type& protocol, boost::system::error_code& ec)
144 return service_impl_.open(impl, protocol, ec);
147 /// Assign an existing native acceptor to a socket acceptor.
148 boost::system::error_code assign(implementation_type& impl,
149 const protocol_type& protocol, const native_handle_type& native_acceptor,
150 boost::system::error_code& ec)
152 return service_impl_.assign(impl, protocol, native_acceptor, ec);
155 /// Determine whether the acceptor is open.
156 bool is_open(const implementation_type& impl) const
158 return service_impl_.is_open(impl);
161 /// Cancel all asynchronous operations associated with the acceptor.
162 boost::system::error_code cancel(implementation_type& impl,
163 boost::system::error_code& ec)
165 return service_impl_.cancel(impl, ec);
168 /// Bind the socket acceptor to the specified local endpoint.
169 boost::system::error_code bind(implementation_type& impl,
170 const endpoint_type& endpoint, boost::system::error_code& ec)
172 return service_impl_.bind(impl, endpoint, ec);
175 /// Place the socket acceptor into the state where it will listen for new
177 boost::system::error_code listen(implementation_type& impl, int backlog,
178 boost::system::error_code& ec)
180 return service_impl_.listen(impl, backlog, ec);
183 /// Close a socket acceptor implementation.
184 boost::system::error_code close(implementation_type& impl,
185 boost::system::error_code& ec)
187 return service_impl_.close(impl, ec);
190 /// (Deprecated: Use native_handle().) Get the native acceptor implementation.
191 native_type native(implementation_type& impl)
193 return service_impl_.native_handle(impl);
196 /// Get the native acceptor implementation.
197 native_handle_type native_handle(implementation_type& impl)
199 return service_impl_.native_handle(impl);
202 /// Set a socket option.
203 template <typename SettableSocketOption>
204 boost::system::error_code set_option(implementation_type& impl,
205 const SettableSocketOption& option, boost::system::error_code& ec)
207 return service_impl_.set_option(impl, option, ec);
210 /// Get a socket option.
211 template <typename GettableSocketOption>
212 boost::system::error_code get_option(const implementation_type& impl,
213 GettableSocketOption& option, boost::system::error_code& ec) const
215 return service_impl_.get_option(impl, option, ec);
218 /// Perform an IO control command on the socket.
219 template <typename IoControlCommand>
220 boost::system::error_code io_control(implementation_type& impl,
221 IoControlCommand& command, boost::system::error_code& ec)
223 return service_impl_.io_control(impl, command, ec);
226 /// Gets the non-blocking mode of the acceptor.
227 bool non_blocking(const implementation_type& impl) const
229 return service_impl_.non_blocking(impl);
232 /// Sets the non-blocking mode of the acceptor.
233 boost::system::error_code non_blocking(implementation_type& impl,
234 bool mode, boost::system::error_code& ec)
236 return service_impl_.non_blocking(impl, mode, ec);
239 /// Gets the non-blocking mode of the native acceptor implementation.
240 bool native_non_blocking(const implementation_type& impl) const
242 return service_impl_.native_non_blocking(impl);
245 /// Sets the non-blocking mode of the native acceptor implementation.
246 boost::system::error_code native_non_blocking(implementation_type& impl,
247 bool mode, boost::system::error_code& ec)
249 return service_impl_.native_non_blocking(impl, mode, ec);
252 /// Get the local endpoint.
253 endpoint_type local_endpoint(const implementation_type& impl,
254 boost::system::error_code& ec) const
256 return service_impl_.local_endpoint(impl, ec);
259 /// Accept a new connection.
260 template <typename Protocol1, typename SocketService>
261 boost::system::error_code accept(implementation_type& impl,
262 basic_socket<Protocol1, SocketService>& peer,
263 endpoint_type* peer_endpoint, boost::system::error_code& ec,
264 typename enable_if<is_convertible<Protocol, Protocol1>::value>::type* = 0)
266 return service_impl_.accept(impl, peer, peer_endpoint, ec);
269 /// Start an asynchronous accept.
270 template <typename Protocol1, typename SocketService, typename AcceptHandler>
271 BOOST_ASIO_INITFN_RESULT_TYPE(AcceptHandler,
272 void (boost::system::error_code))
273 async_accept(implementation_type& impl,
274 basic_socket<Protocol1, SocketService>& peer,
275 endpoint_type* peer_endpoint,
276 BOOST_ASIO_MOVE_ARG(AcceptHandler) handler,
277 typename enable_if<is_convertible<Protocol, Protocol1>::value>::type* = 0)
279 detail::async_result_init<
280 AcceptHandler, void (boost::system::error_code)> init(
281 BOOST_ASIO_MOVE_CAST(AcceptHandler)(handler));
283 service_impl_.async_accept(impl, peer, peer_endpoint, init.handler);
285 return init.result.get();
289 // Destroy all user-defined handler objects owned by the service.
290 void shutdown_service()
292 service_impl_.shutdown_service();
295 // The platform-specific implementation.
296 service_impl_type service_impl_;
302 #include <boost/asio/detail/pop_options.hpp>
304 #endif // BOOST_ASIO_SOCKET_ACCEPTOR_SERVICE_HPP