Chris@16: // Chris@16: // basic_socket_acceptor.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_BASIC_SOCKET_ACCEPTOR_HPP Chris@16: #define BOOST_ASIO_BASIC_SOCKET_ACCEPTOR_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: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: Chris@16: /// Provides the ability to accept new connections. Chris@16: /** Chris@16: * The basic_socket_acceptor class template is used for accepting new socket Chris@16: * connections. Chris@16: * Chris@16: * @par Thread Safety Chris@16: * @e Distinct @e objects: Safe.@n Chris@16: * @e Shared @e objects: Unsafe. Chris@16: * Chris@16: * @par Example Chris@16: * Opening a socket acceptor with the SO_REUSEADDR option enabled: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), port); Chris@16: * acceptor.open(endpoint.protocol()); Chris@16: * acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); Chris@16: * acceptor.bind(endpoint); Chris@16: * acceptor.listen(); Chris@16: * @endcode Chris@16: */ Chris@16: template > Chris@16: class basic_socket_acceptor Chris@16: : public basic_io_object, Chris@16: public socket_base Chris@16: { Chris@16: public: Chris@16: /// (Deprecated: Use native_handle_type.) The native representation of an Chris@16: /// acceptor. Chris@16: typedef typename SocketAcceptorService::native_handle_type native_type; Chris@16: Chris@16: /// The native representation of an acceptor. Chris@16: typedef typename SocketAcceptorService::native_handle_type native_handle_type; 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::endpoint endpoint_type; Chris@16: Chris@16: /// Construct an acceptor without opening it. Chris@16: /** Chris@16: * This constructor creates an acceptor without opening it to listen for new Chris@16: * connections. The open() function must be called before the acceptor can Chris@16: * accept new socket connections. Chris@16: * Chris@16: * @param io_service The io_service object that the acceptor will use to Chris@16: * dispatch handlers for any asynchronous operations performed on the Chris@16: * acceptor. Chris@16: */ Chris@16: explicit basic_socket_acceptor(boost::asio::io_service& io_service) Chris@16: : basic_io_object(io_service) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Construct an open acceptor. Chris@16: /** Chris@16: * This constructor creates an acceptor and automatically opens it. Chris@16: * Chris@16: * @param io_service The io_service object that the acceptor will use to Chris@16: * dispatch handlers for any asynchronous operations performed on the Chris@16: * acceptor. Chris@16: * Chris@16: * @param protocol An object specifying protocol parameters to be used. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: basic_socket_acceptor(boost::asio::io_service& io_service, Chris@16: const protocol_type& protocol) Chris@16: : basic_io_object(io_service) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().open(this->get_implementation(), protocol, ec); Chris@16: boost::asio::detail::throw_error(ec, "open"); Chris@16: } Chris@16: Chris@16: /// Construct an acceptor opened on the given endpoint. Chris@16: /** Chris@16: * This constructor creates an acceptor and automatically opens it to listen Chris@16: * for new connections on the specified endpoint. Chris@16: * Chris@16: * @param io_service The io_service object that the acceptor will use to Chris@16: * dispatch handlers for any asynchronous operations performed on the Chris@16: * acceptor. Chris@16: * Chris@16: * @param endpoint An endpoint on the local machine on which the acceptor Chris@16: * will listen for new connections. Chris@16: * Chris@16: * @param reuse_addr Whether the constructor should set the socket option Chris@16: * socket_base::reuse_address. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: * Chris@16: * @note This constructor is equivalent to the following code: Chris@16: * @code Chris@16: * basic_socket_acceptor acceptor(io_service); Chris@16: * acceptor.open(endpoint.protocol()); Chris@16: * if (reuse_addr) Chris@16: * acceptor.set_option(socket_base::reuse_address(true)); Chris@16: * acceptor.bind(endpoint); Chris@16: * acceptor.listen(listen_backlog); Chris@16: * @endcode Chris@16: */ Chris@16: basic_socket_acceptor(boost::asio::io_service& io_service, Chris@16: const endpoint_type& endpoint, bool reuse_addr = true) Chris@16: : basic_io_object(io_service) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: const protocol_type protocol = endpoint.protocol(); Chris@16: this->get_service().open(this->get_implementation(), protocol, ec); Chris@16: boost::asio::detail::throw_error(ec, "open"); Chris@16: if (reuse_addr) Chris@16: { Chris@16: this->get_service().set_option(this->get_implementation(), Chris@16: socket_base::reuse_address(true), ec); Chris@16: boost::asio::detail::throw_error(ec, "set_option"); Chris@16: } Chris@16: this->get_service().bind(this->get_implementation(), endpoint, ec); Chris@16: boost::asio::detail::throw_error(ec, "bind"); Chris@16: this->get_service().listen(this->get_implementation(), Chris@16: socket_base::max_connections, ec); Chris@16: boost::asio::detail::throw_error(ec, "listen"); Chris@16: } Chris@16: Chris@16: /// Construct a basic_socket_acceptor on an existing native acceptor. Chris@16: /** Chris@16: * This constructor creates an acceptor object to hold an existing native Chris@16: * acceptor. Chris@16: * Chris@16: * @param io_service The io_service object that the acceptor will use to Chris@16: * dispatch handlers for any asynchronous operations performed on the Chris@16: * acceptor. Chris@16: * Chris@16: * @param protocol An object specifying protocol parameters to be used. Chris@16: * Chris@16: * @param native_acceptor A native acceptor. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: basic_socket_acceptor(boost::asio::io_service& io_service, Chris@16: const protocol_type& protocol, const native_handle_type& native_acceptor) Chris@16: : basic_io_object(io_service) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().assign(this->get_implementation(), Chris@16: protocol, native_acceptor, ec); Chris@16: boost::asio::detail::throw_error(ec, "assign"); Chris@16: } Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) Chris@16: /// Move-construct a basic_socket_acceptor from another. Chris@16: /** Chris@16: * This constructor moves an acceptor from one object to another. Chris@16: * Chris@16: * @param other The other basic_socket_acceptor object from which the move Chris@16: * will occur. Chris@16: * Chris@16: * @note Following the move, the moved-from object is in the same state as if Chris@16: * constructed using the @c basic_socket_acceptor(io_service&) constructor. Chris@16: */ Chris@16: basic_socket_acceptor(basic_socket_acceptor&& other) Chris@16: : basic_io_object( Chris@16: BOOST_ASIO_MOVE_CAST(basic_socket_acceptor)(other)) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Move-assign a basic_socket_acceptor from another. Chris@16: /** Chris@16: * This assignment operator moves an acceptor from one object to another. Chris@16: * Chris@16: * @param other The other basic_socket_acceptor object from which the move Chris@16: * will occur. Chris@16: * Chris@16: * @note Following the move, the moved-from object is in the same state as if Chris@16: * constructed using the @c basic_socket_acceptor(io_service&) constructor. Chris@16: */ Chris@16: basic_socket_acceptor& operator=(basic_socket_acceptor&& other) Chris@16: { Chris@16: basic_io_object::operator=( Chris@16: BOOST_ASIO_MOVE_CAST(basic_socket_acceptor)(other)); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // All socket acceptors have access to each other's implementations. Chris@16: template Chris@16: friend class basic_socket_acceptor; Chris@16: Chris@16: /// Move-construct a basic_socket_acceptor from an acceptor of another Chris@16: /// protocol type. Chris@16: /** Chris@16: * This constructor moves an acceptor from one object to another. Chris@16: * Chris@16: * @param other The other basic_socket_acceptor object from which the move Chris@16: * will occur. Chris@16: * Chris@16: * @note Following the move, the moved-from object is in the same state as if Chris@16: * constructed using the @c basic_socket(io_service&) constructor. Chris@16: */ Chris@16: template Chris@16: basic_socket_acceptor( Chris@16: basic_socket_acceptor&& other, Chris@16: typename enable_if::value>::type* = 0) Chris@16: : basic_io_object(other.get_io_service()) Chris@16: { Chris@16: this->get_service().template converting_move_construct( Chris@16: this->get_implementation(), other.get_implementation()); Chris@16: } Chris@16: Chris@16: /// Move-assign a basic_socket_acceptor from an acceptor of another protocol Chris@16: /// type. Chris@16: /** Chris@16: * This assignment operator moves an acceptor from one object to another. Chris@16: * Chris@16: * @param other The other basic_socket_acceptor object from which the move Chris@16: * will occur. Chris@16: * Chris@16: * @note Following the move, the moved-from object is in the same state as if Chris@16: * constructed using the @c basic_socket(io_service&) constructor. Chris@16: */ Chris@16: template Chris@16: typename enable_if::value, Chris@16: basic_socket_acceptor>::type& operator=( Chris@16: basic_socket_acceptor&& other) Chris@16: { Chris@16: basic_socket_acceptor tmp(BOOST_ASIO_MOVE_CAST2(basic_socket_acceptor< Chris@16: Protocol1, SocketAcceptorService1>)(other)); Chris@16: basic_io_object::operator=( Chris@16: BOOST_ASIO_MOVE_CAST(basic_socket_acceptor)(tmp)); Chris@16: return *this; Chris@16: } Chris@16: #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) Chris@16: Chris@16: /// Open the acceptor using the specified protocol. Chris@16: /** Chris@16: * This function opens the socket acceptor so that it will use the specified Chris@16: * protocol. Chris@16: * Chris@16: * @param protocol An object specifying which protocol is to be used. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: * Chris@16: * @par Example Chris@16: * @code Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * acceptor.open(boost::asio::ip::tcp::v4()); Chris@16: * @endcode Chris@16: */ Chris@16: void open(const protocol_type& protocol = protocol_type()) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().open(this->get_implementation(), protocol, ec); Chris@16: boost::asio::detail::throw_error(ec, "open"); Chris@16: } Chris@16: Chris@16: /// Open the acceptor using the specified protocol. Chris@16: /** Chris@16: * This function opens the socket acceptor so that it will use the specified Chris@16: * protocol. Chris@16: * Chris@16: * @param protocol An object specifying which protocol is to be used. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: * Chris@16: * @par Example Chris@16: * @code Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * boost::system::error_code ec; Chris@16: * acceptor.open(boost::asio::ip::tcp::v4(), ec); Chris@16: * if (ec) Chris@16: * { Chris@16: * // An error occurred. Chris@16: * } Chris@16: * @endcode Chris@16: */ Chris@16: boost::system::error_code open(const protocol_type& protocol, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().open(this->get_implementation(), protocol, ec); Chris@16: } Chris@16: Chris@16: /// Assigns an existing native acceptor to the acceptor. Chris@16: /* Chris@16: * This function opens the acceptor to hold an existing native acceptor. Chris@16: * Chris@16: * @param protocol An object specifying which protocol is to be used. Chris@16: * Chris@16: * @param native_acceptor A native acceptor. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: void assign(const protocol_type& protocol, Chris@16: const native_handle_type& native_acceptor) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().assign(this->get_implementation(), Chris@16: protocol, native_acceptor, ec); Chris@16: boost::asio::detail::throw_error(ec, "assign"); Chris@16: } Chris@16: Chris@16: /// Assigns an existing native acceptor to the acceptor. Chris@16: /* Chris@16: * This function opens the acceptor to hold an existing native acceptor. Chris@16: * Chris@16: * @param protocol An object specifying which protocol is to be used. Chris@16: * Chris@16: * @param native_acceptor A native acceptor. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: */ Chris@16: boost::system::error_code assign(const protocol_type& protocol, Chris@16: const native_handle_type& native_acceptor, boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().assign(this->get_implementation(), Chris@16: protocol, native_acceptor, ec); Chris@16: } Chris@16: Chris@16: /// Determine whether the acceptor is open. Chris@16: bool is_open() const Chris@16: { Chris@16: return this->get_service().is_open(this->get_implementation()); Chris@16: } Chris@16: Chris@16: /// Bind the acceptor to the given local endpoint. Chris@16: /** Chris@16: * This function binds the socket acceptor to the specified endpoint on the Chris@16: * local machine. Chris@16: * Chris@16: * @param endpoint An endpoint on the local machine to which the socket Chris@16: * acceptor will be bound. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: * Chris@16: * @par Example Chris@16: * @code Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 12345); Chris@16: * acceptor.open(endpoint.protocol()); Chris@16: * acceptor.bind(endpoint); Chris@16: * @endcode Chris@16: */ Chris@16: void bind(const endpoint_type& endpoint) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().bind(this->get_implementation(), endpoint, ec); Chris@16: boost::asio::detail::throw_error(ec, "bind"); Chris@16: } Chris@16: Chris@16: /// Bind the acceptor to the given local endpoint. Chris@16: /** Chris@16: * This function binds the socket acceptor to the specified endpoint on the Chris@16: * local machine. Chris@16: * Chris@16: * @param endpoint An endpoint on the local machine to which the socket Chris@16: * acceptor will be bound. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: * Chris@16: * @par Example Chris@16: * @code Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 12345); Chris@16: * acceptor.open(endpoint.protocol()); Chris@16: * boost::system::error_code ec; Chris@16: * acceptor.bind(endpoint, ec); Chris@16: * if (ec) Chris@16: * { Chris@16: * // An error occurred. Chris@16: * } Chris@16: * @endcode Chris@16: */ Chris@16: boost::system::error_code bind(const endpoint_type& endpoint, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().bind(this->get_implementation(), endpoint, ec); Chris@16: } Chris@16: Chris@16: /// Place the acceptor into the state where it will listen for new Chris@16: /// connections. Chris@16: /** Chris@16: * This function puts the socket acceptor into the state where it may accept Chris@16: * new connections. Chris@16: * Chris@16: * @param backlog The maximum length of the queue of pending connections. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: void listen(int backlog = socket_base::max_connections) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().listen(this->get_implementation(), backlog, ec); Chris@16: boost::asio::detail::throw_error(ec, "listen"); Chris@16: } Chris@16: Chris@16: /// Place the acceptor into the state where it will listen for new Chris@16: /// connections. Chris@16: /** Chris@16: * This function puts the socket acceptor into the state where it may accept Chris@16: * new connections. Chris@16: * Chris@16: * @param backlog The maximum length of the queue of pending connections. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: * Chris@16: * @par Example Chris@16: * @code Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * ... Chris@16: * boost::system::error_code ec; Chris@16: * acceptor.listen(boost::asio::socket_base::max_connections, ec); Chris@16: * if (ec) Chris@16: * { Chris@16: * // An error occurred. Chris@16: * } Chris@16: * @endcode Chris@16: */ Chris@16: boost::system::error_code listen(int backlog, boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().listen(this->get_implementation(), backlog, ec); Chris@16: } Chris@16: Chris@16: /// Close the acceptor. Chris@16: /** Chris@16: * This function is used to close the acceptor. Any asynchronous accept Chris@16: * operations will be cancelled immediately. Chris@16: * Chris@16: * A subsequent call to open() is required before the acceptor can again be Chris@16: * used to again perform socket accept operations. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: void close() Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().close(this->get_implementation(), ec); Chris@16: boost::asio::detail::throw_error(ec, "close"); Chris@16: } Chris@16: Chris@16: /// Close the acceptor. Chris@16: /** Chris@16: * This function is used to close the acceptor. Any asynchronous accept Chris@16: * operations will be cancelled immediately. Chris@16: * Chris@16: * A subsequent call to open() is required before the acceptor can again be Chris@16: * used to again perform socket accept operations. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: * Chris@16: * @par Example Chris@16: * @code Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * ... Chris@16: * boost::system::error_code ec; Chris@16: * acceptor.close(ec); Chris@16: * if (ec) Chris@16: * { Chris@16: * // An error occurred. Chris@16: * } Chris@16: * @endcode Chris@16: */ Chris@16: boost::system::error_code close(boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().close(this->get_implementation(), ec); Chris@16: } Chris@16: Chris@16: /// (Deprecated: Use native_handle().) Get the native acceptor representation. Chris@16: /** Chris@16: * This function may be used to obtain the underlying representation of the Chris@16: * acceptor. This is intended to allow access to native acceptor functionality Chris@16: * that is not otherwise provided. Chris@16: */ Chris@16: native_type native() Chris@16: { Chris@16: return this->get_service().native_handle(this->get_implementation()); Chris@16: } Chris@16: Chris@16: /// Get the native acceptor representation. Chris@16: /** Chris@16: * This function may be used to obtain the underlying representation of the Chris@16: * acceptor. This is intended to allow access to native acceptor functionality Chris@16: * that is not otherwise provided. Chris@16: */ Chris@16: native_handle_type native_handle() Chris@16: { Chris@16: return this->get_service().native_handle(this->get_implementation()); Chris@16: } Chris@16: Chris@16: /// Cancel all asynchronous operations associated with the acceptor. Chris@16: /** Chris@16: * This function causes all outstanding asynchronous connect, send and receive Chris@16: * operations to finish immediately, and the handlers for cancelled operations Chris@16: * will be passed the boost::asio::error::operation_aborted error. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: void cancel() Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().cancel(this->get_implementation(), ec); Chris@16: boost::asio::detail::throw_error(ec, "cancel"); Chris@16: } Chris@16: Chris@16: /// Cancel all asynchronous operations associated with the acceptor. Chris@16: /** Chris@16: * This function causes all outstanding asynchronous connect, send and receive Chris@16: * operations to finish immediately, and the handlers for cancelled operations Chris@16: * will be passed the boost::asio::error::operation_aborted error. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: */ Chris@16: boost::system::error_code cancel(boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().cancel(this->get_implementation(), ec); Chris@16: } Chris@16: Chris@16: /// Set an option on the acceptor. Chris@16: /** Chris@16: * This function is used to set an option on the acceptor. Chris@16: * Chris@16: * @param option The new option value to be set on the acceptor. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: * Chris@16: * @sa SettableSocketOption @n Chris@16: * boost::asio::socket_base::reuse_address Chris@16: * boost::asio::socket_base::enable_connection_aborted Chris@16: * Chris@16: * @par Example Chris@16: * Setting the SOL_SOCKET/SO_REUSEADDR option: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * ... Chris@16: * boost::asio::ip::tcp::acceptor::reuse_address option(true); Chris@16: * acceptor.set_option(option); Chris@16: * @endcode Chris@16: */ Chris@16: template Chris@16: void set_option(const SettableSocketOption& option) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().set_option(this->get_implementation(), option, ec); Chris@16: boost::asio::detail::throw_error(ec, "set_option"); Chris@16: } Chris@16: Chris@16: /// Set an option on the acceptor. Chris@16: /** Chris@16: * This function is used to set an option on the acceptor. Chris@16: * Chris@16: * @param option The new option value to be set on the acceptor. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: * Chris@16: * @sa SettableSocketOption @n Chris@16: * boost::asio::socket_base::reuse_address Chris@16: * boost::asio::socket_base::enable_connection_aborted Chris@16: * Chris@16: * @par Example Chris@16: * Setting the SOL_SOCKET/SO_REUSEADDR option: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * ... Chris@16: * boost::asio::ip::tcp::acceptor::reuse_address option(true); Chris@16: * boost::system::error_code ec; Chris@16: * acceptor.set_option(option, ec); Chris@16: * if (ec) Chris@16: * { Chris@16: * // An error occurred. Chris@16: * } Chris@16: * @endcode Chris@16: */ Chris@16: template Chris@16: boost::system::error_code set_option(const SettableSocketOption& option, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().set_option( Chris@16: this->get_implementation(), option, ec); Chris@16: } Chris@16: Chris@16: /// Get an option from the acceptor. Chris@16: /** Chris@16: * This function is used to get the current value of an option on the Chris@16: * acceptor. Chris@16: * Chris@16: * @param option The option value to be obtained from the acceptor. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: * Chris@16: * @sa GettableSocketOption @n Chris@16: * boost::asio::socket_base::reuse_address Chris@16: * Chris@16: * @par Example Chris@16: * Getting the value of the SOL_SOCKET/SO_REUSEADDR option: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * ... Chris@16: * boost::asio::ip::tcp::acceptor::reuse_address option; Chris@16: * acceptor.get_option(option); Chris@16: * bool is_set = option.get(); Chris@16: * @endcode Chris@16: */ Chris@16: template Chris@16: void get_option(GettableSocketOption& option) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().get_option(this->get_implementation(), option, ec); Chris@16: boost::asio::detail::throw_error(ec, "get_option"); Chris@16: } Chris@16: Chris@16: /// Get an option from the acceptor. Chris@16: /** Chris@16: * This function is used to get the current value of an option on the Chris@16: * acceptor. Chris@16: * Chris@16: * @param option The option value to be obtained from the acceptor. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: * Chris@16: * @sa GettableSocketOption @n Chris@16: * boost::asio::socket_base::reuse_address Chris@16: * Chris@16: * @par Example Chris@16: * Getting the value of the SOL_SOCKET/SO_REUSEADDR option: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * ... Chris@16: * boost::asio::ip::tcp::acceptor::reuse_address option; Chris@16: * boost::system::error_code ec; Chris@16: * acceptor.get_option(option, ec); Chris@16: * if (ec) Chris@16: * { Chris@16: * // An error occurred. Chris@16: * } Chris@16: * bool is_set = option.get(); Chris@16: * @endcode Chris@16: */ Chris@16: template Chris@16: boost::system::error_code get_option(GettableSocketOption& option, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().get_option( Chris@16: this->get_implementation(), option, ec); Chris@16: } Chris@16: Chris@16: /// Perform an IO control command on the acceptor. Chris@16: /** Chris@16: * This function is used to execute an IO control command on the acceptor. Chris@16: * Chris@16: * @param command The IO control command to be performed on the acceptor. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: * Chris@16: * @sa IoControlCommand @n Chris@16: * boost::asio::socket_base::non_blocking_io Chris@16: * Chris@16: * @par Example Chris@16: * Getting the number of bytes ready to read: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * ... Chris@16: * boost::asio::ip::tcp::acceptor::non_blocking_io command(true); Chris@16: * socket.io_control(command); Chris@16: * @endcode Chris@16: */ Chris@16: template Chris@16: void io_control(IoControlCommand& command) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().io_control(this->get_implementation(), command, ec); Chris@16: boost::asio::detail::throw_error(ec, "io_control"); Chris@16: } Chris@16: Chris@16: /// Perform an IO control command on the acceptor. Chris@16: /** Chris@16: * This function is used to execute an IO control command on the acceptor. Chris@16: * Chris@16: * @param command The IO control command to be performed on the acceptor. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: * Chris@16: * @sa IoControlCommand @n Chris@16: * boost::asio::socket_base::non_blocking_io Chris@16: * Chris@16: * @par Example Chris@16: * Getting the number of bytes ready to read: Chris@16: * @code Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * ... Chris@16: * boost::asio::ip::tcp::acceptor::non_blocking_io command(true); Chris@16: * boost::system::error_code ec; Chris@16: * socket.io_control(command, ec); Chris@16: * if (ec) Chris@16: * { Chris@16: * // An error occurred. Chris@16: * } Chris@16: * @endcode Chris@16: */ Chris@16: template Chris@16: boost::system::error_code io_control(IoControlCommand& command, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().io_control( Chris@16: this->get_implementation(), command, ec); Chris@16: } Chris@16: Chris@16: /// Gets the non-blocking mode of the acceptor. Chris@16: /** Chris@16: * @returns @c true if the acceptor's synchronous operations will fail with Chris@16: * boost::asio::error::would_block if they are unable to perform the requested Chris@16: * operation immediately. If @c false, synchronous operations will block Chris@16: * until complete. Chris@16: * Chris@16: * @note The non-blocking mode has no effect on the behaviour of asynchronous Chris@16: * operations. Asynchronous operations will never fail with the error Chris@16: * boost::asio::error::would_block. Chris@16: */ Chris@16: bool non_blocking() const Chris@16: { Chris@16: return this->get_service().non_blocking(this->get_implementation()); Chris@16: } Chris@16: Chris@16: /// Sets the non-blocking mode of the acceptor. Chris@16: /** Chris@16: * @param mode If @c true, the acceptor's synchronous operations will fail Chris@16: * with boost::asio::error::would_block if they are unable to perform the Chris@16: * requested operation immediately. If @c false, synchronous operations will Chris@16: * block until complete. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: * Chris@16: * @note The non-blocking mode has no effect on the behaviour of asynchronous Chris@16: * operations. Asynchronous operations will never fail with the error Chris@16: * boost::asio::error::would_block. Chris@16: */ Chris@16: void non_blocking(bool mode) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().non_blocking(this->get_implementation(), mode, ec); Chris@16: boost::asio::detail::throw_error(ec, "non_blocking"); Chris@16: } Chris@16: Chris@16: /// Sets the non-blocking mode of the acceptor. Chris@16: /** Chris@16: * @param mode If @c true, the acceptor's synchronous operations will fail Chris@16: * with boost::asio::error::would_block if they are unable to perform the Chris@16: * requested operation immediately. If @c false, synchronous operations will Chris@16: * block until complete. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: * Chris@16: * @note The non-blocking mode has no effect on the behaviour of asynchronous Chris@16: * operations. Asynchronous operations will never fail with the error Chris@16: * boost::asio::error::would_block. Chris@16: */ Chris@16: boost::system::error_code non_blocking( Chris@16: bool mode, boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().non_blocking( Chris@16: this->get_implementation(), mode, ec); Chris@16: } Chris@16: Chris@16: /// Gets the non-blocking mode of the native acceptor implementation. Chris@16: /** Chris@16: * This function is used to retrieve the non-blocking mode of the underlying Chris@16: * native acceptor. This mode has no effect on the behaviour of the acceptor Chris@16: * object's synchronous operations. Chris@16: * Chris@16: * @returns @c true if the underlying acceptor is in non-blocking mode and Chris@16: * direct system calls may fail with boost::asio::error::would_block (or the Chris@16: * equivalent system error). Chris@16: * Chris@16: * @note The current non-blocking mode is cached by the acceptor object. Chris@16: * Consequently, the return value may be incorrect if the non-blocking mode Chris@16: * was set directly on the native acceptor. Chris@16: */ Chris@16: bool native_non_blocking() const Chris@16: { Chris@16: return this->get_service().native_non_blocking(this->get_implementation()); Chris@16: } Chris@16: Chris@16: /// Sets the non-blocking mode of the native acceptor implementation. Chris@16: /** Chris@16: * This function is used to modify the non-blocking mode of the underlying Chris@16: * native acceptor. It has no effect on the behaviour of the acceptor object's Chris@16: * synchronous operations. Chris@16: * Chris@16: * @param mode If @c true, the underlying acceptor is put into non-blocking Chris@16: * mode and direct system calls may fail with boost::asio::error::would_block Chris@16: * (or the equivalent system error). Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. If the @c mode is Chris@16: * @c false, but the current value of @c non_blocking() is @c true, this Chris@16: * function fails with boost::asio::error::invalid_argument, as the Chris@16: * combination does not make sense. Chris@16: */ Chris@16: void native_non_blocking(bool mode) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().native_non_blocking( Chris@16: this->get_implementation(), mode, ec); Chris@16: boost::asio::detail::throw_error(ec, "native_non_blocking"); Chris@16: } Chris@16: Chris@16: /// Sets the non-blocking mode of the native acceptor implementation. Chris@16: /** Chris@16: * This function is used to modify the non-blocking mode of the underlying Chris@16: * native acceptor. It has no effect on the behaviour of the acceptor object's Chris@16: * synchronous operations. Chris@16: * Chris@16: * @param mode If @c true, the underlying acceptor is put into non-blocking Chris@16: * mode and direct system calls may fail with boost::asio::error::would_block Chris@16: * (or the equivalent system error). Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. If the @c mode is Chris@16: * @c false, but the current value of @c non_blocking() is @c true, this Chris@16: * function fails with boost::asio::error::invalid_argument, as the Chris@16: * combination does not make sense. Chris@16: */ Chris@16: boost::system::error_code native_non_blocking( Chris@16: bool mode, boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().native_non_blocking( Chris@16: this->get_implementation(), mode, ec); Chris@16: } Chris@16: Chris@16: /// Get the local endpoint of the acceptor. Chris@16: /** Chris@16: * This function is used to obtain the locally bound endpoint of the acceptor. Chris@16: * Chris@16: * @returns An object that represents the local endpoint of the acceptor. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: * Chris@16: * @par Example Chris@16: * @code Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * ... Chris@16: * boost::asio::ip::tcp::endpoint endpoint = acceptor.local_endpoint(); Chris@16: * @endcode Chris@16: */ Chris@16: endpoint_type local_endpoint() const Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: endpoint_type ep = this->get_service().local_endpoint( Chris@16: this->get_implementation(), ec); Chris@16: boost::asio::detail::throw_error(ec, "local_endpoint"); Chris@16: return ep; Chris@16: } Chris@16: Chris@16: /// Get the local endpoint of the acceptor. Chris@16: /** Chris@16: * This function is used to obtain the locally bound endpoint of the acceptor. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: * Chris@16: * @returns An object that represents the local endpoint of the acceptor. Chris@16: * Returns a default-constructed endpoint object if an error occurred and the Chris@16: * error handler did not throw an exception. Chris@16: * Chris@16: * @par Example Chris@16: * @code Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * ... Chris@16: * boost::system::error_code ec; Chris@16: * boost::asio::ip::tcp::endpoint endpoint = acceptor.local_endpoint(ec); Chris@16: * if (ec) Chris@16: * { Chris@16: * // An error occurred. Chris@16: * } Chris@16: * @endcode Chris@16: */ Chris@16: endpoint_type local_endpoint(boost::system::error_code& ec) const Chris@16: { Chris@16: return this->get_service().local_endpoint(this->get_implementation(), ec); Chris@16: } Chris@16: Chris@16: /// Accept a new connection. Chris@16: /** Chris@16: * This function is used to accept a new connection from a peer into the Chris@16: * given socket. The function call will block until a new connection has been Chris@16: * accepted successfully or an error occurs. Chris@16: * Chris@16: * @param peer The socket into which the new connection will be accepted. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: * Chris@16: * @par Example Chris@16: * @code Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * ... Chris@16: * boost::asio::ip::tcp::socket socket(io_service); Chris@16: * acceptor.accept(socket); Chris@16: * @endcode Chris@16: */ Chris@16: template Chris@16: void accept(basic_socket& peer, Chris@16: typename enable_if::value>::type* = 0) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().accept(this->get_implementation(), Chris@16: peer, static_cast(0), ec); Chris@16: boost::asio::detail::throw_error(ec, "accept"); Chris@16: } Chris@16: Chris@16: /// Accept a new connection. Chris@16: /** Chris@16: * This function is used to accept a new connection from a peer into the Chris@16: * given socket. The function call will block until a new connection has been Chris@16: * accepted successfully or an error occurs. Chris@16: * Chris@16: * @param peer The socket into which the new connection will be accepted. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: * Chris@16: * @par Example Chris@16: * @code Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * ... Chris@16: * boost::asio::ip::tcp::soocket socket(io_service); Chris@16: * boost::system::error_code ec; Chris@16: * acceptor.accept(socket, ec); Chris@16: * if (ec) Chris@16: * { Chris@16: * // An error occurred. Chris@16: * } Chris@16: * @endcode Chris@16: */ Chris@16: template Chris@16: boost::system::error_code accept( Chris@16: basic_socket& peer, Chris@16: boost::system::error_code& ec, Chris@16: typename enable_if::value>::type* = 0) Chris@16: { Chris@16: return this->get_service().accept(this->get_implementation(), Chris@16: peer, static_cast(0), ec); Chris@16: } Chris@16: Chris@16: /// Start an asynchronous accept. Chris@16: /** Chris@16: * This function is used to asynchronously accept a new connection into a Chris@16: * socket. The function call always returns immediately. Chris@16: * Chris@16: * @param peer The socket into which the new connection will be accepted. Chris@16: * Ownership of the peer object is retained by the caller, which must Chris@16: * guarantee that it is valid until the handler is called. Chris@16: * Chris@16: * @param handler The handler to be called when the accept operation Chris@16: * completes. Copies will be made of the handler as required. The function Chris@16: * signature of the handler must be: Chris@16: * @code void handler( Chris@16: * const boost::system::error_code& error // Result of operation. Chris@16: * ); @endcode Chris@16: * Regardless of whether the asynchronous operation completes immediately or Chris@16: * not, the handler will not be invoked from within this function. Invocation Chris@16: * of the handler will be performed in a manner equivalent to using Chris@16: * boost::asio::io_service::post(). Chris@16: * Chris@16: * @par Example Chris@16: * @code Chris@16: * void accept_handler(const boost::system::error_code& error) Chris@16: * { Chris@16: * if (!error) Chris@16: * { Chris@16: * // Accept succeeded. Chris@16: * } Chris@16: * } Chris@16: * Chris@16: * ... Chris@16: * Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * ... Chris@16: * boost::asio::ip::tcp::socket socket(io_service); Chris@16: * acceptor.async_accept(socket, accept_handler); Chris@16: * @endcode Chris@16: */ Chris@16: template Chris@16: BOOST_ASIO_INITFN_RESULT_TYPE(AcceptHandler, Chris@16: void (boost::system::error_code)) Chris@16: async_accept(basic_socket& peer, Chris@16: BOOST_ASIO_MOVE_ARG(AcceptHandler) handler, Chris@16: typename enable_if::value>::type* = 0) Chris@16: { Chris@16: // If you get an error on the following line it means that your handler does Chris@16: // not meet the documented type requirements for a AcceptHandler. Chris@16: BOOST_ASIO_ACCEPT_HANDLER_CHECK(AcceptHandler, handler) type_check; Chris@16: Chris@16: return this->get_service().async_accept(this->get_implementation(), Chris@16: peer, static_cast(0), Chris@16: BOOST_ASIO_MOVE_CAST(AcceptHandler)(handler)); Chris@16: } Chris@16: Chris@16: /// Accept a new connection and obtain the endpoint of the peer Chris@16: /** Chris@16: * This function is used to accept a new connection from a peer into the Chris@16: * given socket, and additionally provide the endpoint of the remote peer. Chris@16: * The function call will block until a new connection has been accepted Chris@16: * successfully or an error occurs. Chris@16: * Chris@16: * @param peer The socket into which the new connection will be accepted. Chris@16: * Chris@16: * @param peer_endpoint An endpoint object which will receive the endpoint of Chris@16: * the remote peer. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: * Chris@16: * @par Example Chris@16: * @code Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * ... Chris@16: * boost::asio::ip::tcp::socket socket(io_service); Chris@16: * boost::asio::ip::tcp::endpoint endpoint; Chris@16: * acceptor.accept(socket, endpoint); Chris@16: * @endcode Chris@16: */ Chris@16: template Chris@16: void accept(basic_socket& peer, Chris@16: endpoint_type& peer_endpoint) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().accept(this->get_implementation(), Chris@16: peer, &peer_endpoint, ec); Chris@16: boost::asio::detail::throw_error(ec, "accept"); Chris@16: } Chris@16: Chris@16: /// Accept a new connection and obtain the endpoint of the peer Chris@16: /** Chris@16: * This function is used to accept a new connection from a peer into the Chris@16: * given socket, and additionally provide the endpoint of the remote peer. Chris@16: * The function call will block until a new connection has been accepted Chris@16: * successfully or an error occurs. Chris@16: * Chris@16: * @param peer The socket into which the new connection will be accepted. Chris@16: * Chris@16: * @param peer_endpoint An endpoint object which will receive the endpoint of Chris@16: * the remote peer. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: * Chris@16: * @par Example Chris@16: * @code Chris@16: * boost::asio::ip::tcp::acceptor acceptor(io_service); Chris@16: * ... Chris@16: * boost::asio::ip::tcp::socket socket(io_service); Chris@16: * boost::asio::ip::tcp::endpoint endpoint; Chris@16: * boost::system::error_code ec; Chris@16: * acceptor.accept(socket, endpoint, ec); Chris@16: * if (ec) Chris@16: * { Chris@16: * // An error occurred. Chris@16: * } Chris@16: * @endcode Chris@16: */ Chris@16: template Chris@16: boost::system::error_code accept( Chris@16: basic_socket& peer, Chris@16: endpoint_type& peer_endpoint, boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().accept( Chris@16: this->get_implementation(), peer, &peer_endpoint, ec); Chris@16: } Chris@16: Chris@16: /// Start an asynchronous accept. Chris@16: /** Chris@16: * This function is used to asynchronously accept a new connection into a Chris@16: * socket, and additionally obtain the endpoint of the remote peer. The Chris@16: * function call always returns immediately. Chris@16: * Chris@16: * @param peer The socket into which the new connection will be accepted. Chris@16: * Ownership of the peer object is retained by the caller, which must Chris@16: * guarantee that it is valid until the handler is called. Chris@16: * Chris@16: * @param peer_endpoint An endpoint object into which the endpoint of the Chris@16: * remote peer will be written. Ownership of the peer_endpoint object is Chris@16: * retained by the caller, which must guarantee that it is valid until the Chris@16: * handler is called. Chris@16: * Chris@16: * @param handler The handler to be called when the accept operation Chris@16: * completes. Copies will be made of the handler as required. The function Chris@16: * signature of the handler must be: Chris@16: * @code void handler( Chris@16: * const boost::system::error_code& error // Result of operation. Chris@16: * ); @endcode Chris@16: * Regardless of whether the asynchronous operation completes immediately or Chris@16: * not, the handler will not be invoked from within this function. Invocation Chris@16: * of the handler will be performed in a manner equivalent to using Chris@16: * boost::asio::io_service::post(). Chris@16: */ Chris@16: template Chris@16: BOOST_ASIO_INITFN_RESULT_TYPE(AcceptHandler, Chris@16: void (boost::system::error_code)) Chris@16: async_accept(basic_socket& peer, Chris@16: endpoint_type& peer_endpoint, BOOST_ASIO_MOVE_ARG(AcceptHandler) handler) Chris@16: { Chris@16: // If you get an error on the following line it means that your handler does Chris@16: // not meet the documented type requirements for a AcceptHandler. Chris@16: BOOST_ASIO_ACCEPT_HANDLER_CHECK(AcceptHandler, handler) type_check; Chris@16: Chris@16: return this->get_service().async_accept(this->get_implementation(), peer, Chris@16: &peer_endpoint, BOOST_ASIO_MOVE_CAST(AcceptHandler)(handler)); Chris@16: } Chris@16: }; Chris@16: Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_ASIO_BASIC_SOCKET_ACCEPTOR_HPP