Chris@16: // Chris@16: // socket_acceptor_service.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_SOCKET_ACCEPTOR_SERVICE_HPP Chris@16: #define BOOST_ASIO_SOCKET_ACCEPTOR_SERVICE_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #if defined(BOOST_ASIO_WINDOWS_RUNTIME) Chris@16: # include Chris@16: #elif defined(BOOST_ASIO_HAS_IOCP) Chris@16: # include Chris@16: #else Chris@16: # include Chris@16: #endif Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: Chris@16: /// Default service implementation for a socket acceptor. Chris@16: template Chris@16: class socket_acceptor_service Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: : public boost::asio::io_service::service Chris@16: #else Chris@16: : public boost::asio::detail::service_base > Chris@16: #endif Chris@16: { Chris@16: public: Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: /// The unique service identifier. Chris@16: static boost::asio::io_service::id id; Chris@16: #endif Chris@16: Chris@16: /// The protocol type. Chris@16: typedef Protocol protocol_type; Chris@16: Chris@16: /// The endpoint type. Chris@16: typedef typename protocol_type::endpoint endpoint_type; Chris@16: Chris@16: private: Chris@16: // The type of the platform-specific implementation. Chris@16: #if defined(BOOST_ASIO_WINDOWS_RUNTIME) Chris@16: typedef detail::null_socket_service service_impl_type; Chris@16: #elif defined(BOOST_ASIO_HAS_IOCP) Chris@16: typedef detail::win_iocp_socket_service service_impl_type; Chris@16: #else Chris@16: typedef detail::reactive_socket_service service_impl_type; Chris@16: #endif Chris@16: Chris@16: public: Chris@16: /// The native type of the socket acceptor. Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined implementation_type; Chris@16: #else Chris@16: typedef typename service_impl_type::implementation_type implementation_type; Chris@16: #endif Chris@16: Chris@16: /// (Deprecated: Use native_handle_type.) The native acceptor type. Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined native_type; Chris@16: #else Chris@16: typedef typename service_impl_type::native_handle_type native_type; Chris@16: #endif Chris@16: Chris@16: /// The native acceptor type. Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined native_handle_type; Chris@16: #else Chris@16: typedef typename service_impl_type::native_handle_type native_handle_type; Chris@16: #endif Chris@16: Chris@16: /// Construct a new socket acceptor service for the specified io_service. Chris@16: explicit socket_acceptor_service(boost::asio::io_service& io_service) Chris@16: : boost::asio::detail::service_base< Chris@16: socket_acceptor_service >(io_service), Chris@16: service_impl_(io_service) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Construct a new socket acceptor implementation. Chris@16: void construct(implementation_type& impl) Chris@16: { Chris@16: service_impl_.construct(impl); Chris@16: } Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) Chris@16: /// Move-construct a new socket acceptor implementation. Chris@16: void move_construct(implementation_type& impl, Chris@16: implementation_type& other_impl) Chris@16: { Chris@16: service_impl_.move_construct(impl, other_impl); Chris@16: } Chris@16: Chris@16: /// Move-assign from another socket acceptor implementation. Chris@16: void move_assign(implementation_type& impl, Chris@16: socket_acceptor_service& other_service, Chris@16: implementation_type& other_impl) Chris@16: { Chris@16: service_impl_.move_assign(impl, other_service.service_impl_, other_impl); Chris@16: } Chris@16: Chris@16: /// Move-construct a new socket acceptor implementation from another protocol Chris@16: /// type. Chris@16: template Chris@16: void converting_move_construct(implementation_type& impl, Chris@16: typename socket_acceptor_service< Chris@16: Protocol1>::implementation_type& other_impl, Chris@16: typename enable_if::value>::type* = 0) Chris@16: { Chris@16: service_impl_.template converting_move_construct( Chris@16: impl, other_impl); Chris@16: } Chris@16: #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) Chris@16: Chris@16: /// Destroy a socket acceptor implementation. Chris@16: void destroy(implementation_type& impl) Chris@16: { Chris@16: service_impl_.destroy(impl); Chris@16: } Chris@16: Chris@16: /// Open a new socket acceptor implementation. Chris@16: boost::system::error_code open(implementation_type& impl, Chris@16: const protocol_type& protocol, boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.open(impl, protocol, ec); Chris@16: } Chris@16: Chris@16: /// Assign an existing native acceptor to a socket acceptor. Chris@16: boost::system::error_code assign(implementation_type& impl, Chris@16: const protocol_type& protocol, const native_handle_type& native_acceptor, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.assign(impl, protocol, native_acceptor, ec); Chris@16: } Chris@16: Chris@16: /// Determine whether the acceptor is open. Chris@16: bool is_open(const implementation_type& impl) const Chris@16: { Chris@16: return service_impl_.is_open(impl); Chris@16: } Chris@16: Chris@16: /// Cancel all asynchronous operations associated with the acceptor. Chris@16: boost::system::error_code cancel(implementation_type& impl, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.cancel(impl, ec); Chris@16: } Chris@16: Chris@16: /// Bind the socket acceptor to the specified local endpoint. Chris@16: boost::system::error_code bind(implementation_type& impl, Chris@16: const endpoint_type& endpoint, boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.bind(impl, endpoint, ec); Chris@16: } Chris@16: Chris@16: /// Place the socket acceptor into the state where it will listen for new Chris@16: /// connections. Chris@16: boost::system::error_code listen(implementation_type& impl, int backlog, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.listen(impl, backlog, ec); Chris@16: } Chris@16: Chris@16: /// Close a socket acceptor implementation. Chris@16: boost::system::error_code close(implementation_type& impl, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.close(impl, ec); Chris@16: } Chris@16: Chris@16: /// (Deprecated: Use native_handle().) Get the native acceptor implementation. Chris@16: native_type native(implementation_type& impl) Chris@16: { Chris@16: return service_impl_.native_handle(impl); Chris@16: } Chris@16: Chris@16: /// Get the native acceptor implementation. Chris@16: native_handle_type native_handle(implementation_type& impl) Chris@16: { Chris@16: return service_impl_.native_handle(impl); Chris@16: } Chris@16: Chris@16: /// Set a socket option. Chris@16: template Chris@16: boost::system::error_code set_option(implementation_type& impl, Chris@16: const SettableSocketOption& option, boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.set_option(impl, option, ec); Chris@16: } Chris@16: Chris@16: /// Get a socket option. Chris@16: template Chris@16: boost::system::error_code get_option(const implementation_type& impl, Chris@16: GettableSocketOption& option, boost::system::error_code& ec) const Chris@16: { Chris@16: return service_impl_.get_option(impl, option, ec); Chris@16: } Chris@16: Chris@16: /// Perform an IO control command on the socket. Chris@16: template Chris@16: boost::system::error_code io_control(implementation_type& impl, Chris@16: IoControlCommand& command, boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.io_control(impl, command, ec); Chris@16: } Chris@16: Chris@16: /// Gets the non-blocking mode of the acceptor. Chris@16: bool non_blocking(const implementation_type& impl) const Chris@16: { Chris@16: return service_impl_.non_blocking(impl); Chris@16: } Chris@16: Chris@16: /// Sets the non-blocking mode of the acceptor. Chris@16: boost::system::error_code non_blocking(implementation_type& impl, Chris@16: bool mode, boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.non_blocking(impl, mode, ec); Chris@16: } Chris@16: Chris@16: /// Gets the non-blocking mode of the native acceptor implementation. Chris@16: bool native_non_blocking(const implementation_type& impl) const Chris@16: { Chris@16: return service_impl_.native_non_blocking(impl); Chris@16: } Chris@16: Chris@16: /// Sets the non-blocking mode of the native acceptor implementation. Chris@16: boost::system::error_code native_non_blocking(implementation_type& impl, Chris@16: bool mode, boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.native_non_blocking(impl, mode, ec); Chris@16: } Chris@16: Chris@16: /// Get the local endpoint. Chris@16: endpoint_type local_endpoint(const implementation_type& impl, Chris@16: boost::system::error_code& ec) const Chris@16: { Chris@16: return service_impl_.local_endpoint(impl, ec); Chris@16: } Chris@16: Chris@16: /// Accept a new connection. Chris@16: template Chris@16: boost::system::error_code accept(implementation_type& impl, Chris@16: basic_socket& peer, Chris@16: endpoint_type* peer_endpoint, boost::system::error_code& ec, Chris@16: typename enable_if::value>::type* = 0) Chris@16: { Chris@16: return service_impl_.accept(impl, peer, peer_endpoint, ec); Chris@16: } Chris@16: Chris@16: /// Start an asynchronous accept. Chris@16: template Chris@16: BOOST_ASIO_INITFN_RESULT_TYPE(AcceptHandler, Chris@16: void (boost::system::error_code)) Chris@16: async_accept(implementation_type& impl, Chris@16: basic_socket& peer, Chris@16: endpoint_type* peer_endpoint, Chris@16: BOOST_ASIO_MOVE_ARG(AcceptHandler) handler, Chris@16: typename enable_if::value>::type* = 0) Chris@16: { Chris@16: detail::async_result_init< Chris@16: AcceptHandler, void (boost::system::error_code)> init( Chris@16: BOOST_ASIO_MOVE_CAST(AcceptHandler)(handler)); Chris@16: Chris@16: service_impl_.async_accept(impl, peer, peer_endpoint, init.handler); Chris@16: Chris@16: return init.result.get(); Chris@16: } Chris@16: Chris@16: private: Chris@16: // Destroy all user-defined handler objects owned by the service. Chris@16: void shutdown_service() Chris@16: { Chris@16: service_impl_.shutdown_service(); Chris@16: } Chris@16: Chris@16: // The platform-specific implementation. Chris@16: service_impl_type service_impl_; Chris@16: }; Chris@16: Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_ASIO_SOCKET_ACCEPTOR_SERVICE_HPP