Chris@16: // Chris@16: // posix/basic_descriptor.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_POSIX_BASIC_DESCRIPTOR_HPP Chris@16: #define BOOST_ASIO_POSIX_BASIC_DESCRIPTOR_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: Chris@16: #if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) \ Chris@16: || defined(GENERATING_DOCUMENTATION) Chris@16: 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: namespace posix { Chris@16: Chris@16: /// Provides POSIX descriptor functionality. Chris@16: /** Chris@16: * The posix::basic_descriptor class template provides the ability to wrap a Chris@16: * POSIX descriptor. 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: template Chris@16: class basic_descriptor Chris@16: : public basic_io_object, Chris@16: public descriptor_base Chris@16: { Chris@16: public: Chris@16: /// (Deprecated: Use native_handle_type.) The native representation of a Chris@16: /// descriptor. Chris@16: typedef typename DescriptorService::native_handle_type native_type; Chris@16: Chris@16: /// The native representation of a descriptor. Chris@16: typedef typename DescriptorService::native_handle_type native_handle_type; Chris@16: Chris@16: /// A basic_descriptor is always the lowest layer. Chris@16: typedef basic_descriptor lowest_layer_type; Chris@16: Chris@16: /// Construct a basic_descriptor without opening it. Chris@16: /** Chris@16: * This constructor creates a descriptor without opening it. Chris@16: * Chris@16: * @param io_service The io_service object that the descriptor will use to Chris@16: * dispatch handlers for any asynchronous operations performed on the Chris@16: * descriptor. Chris@16: */ Chris@16: explicit basic_descriptor(boost::asio::io_service& io_service) Chris@16: : basic_io_object(io_service) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Construct a basic_descriptor on an existing native descriptor. Chris@16: /** Chris@16: * This constructor creates a descriptor object to hold an existing native Chris@16: * descriptor. Chris@16: * Chris@16: * @param io_service The io_service object that the descriptor will use to Chris@16: * dispatch handlers for any asynchronous operations performed on the Chris@16: * descriptor. Chris@16: * Chris@16: * @param native_descriptor A native descriptor. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: basic_descriptor(boost::asio::io_service& io_service, Chris@16: const native_handle_type& native_descriptor) 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: native_descriptor, 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_descriptor from another. Chris@16: /** Chris@16: * This constructor moves a descriptor from one object to another. Chris@16: * Chris@16: * @param other The other basic_descriptor object from which the move will Chris@16: * 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_descriptor(io_service&) constructor. Chris@16: */ Chris@16: basic_descriptor(basic_descriptor&& other) Chris@16: : basic_io_object( Chris@16: BOOST_ASIO_MOVE_CAST(basic_descriptor)(other)) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Move-assign a basic_descriptor from another. Chris@16: /** Chris@16: * This assignment operator moves a descriptor from one object to another. Chris@16: * Chris@16: * @param other The other basic_descriptor object from which the move will Chris@16: * 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_descriptor(io_service&) constructor. Chris@16: */ Chris@16: basic_descriptor& operator=(basic_descriptor&& other) Chris@16: { Chris@16: basic_io_object::operator=( Chris@16: BOOST_ASIO_MOVE_CAST(basic_descriptor)(other)); Chris@16: return *this; Chris@16: } Chris@16: #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) Chris@16: Chris@16: /// Get a reference to the lowest layer. Chris@16: /** Chris@16: * This function returns a reference to the lowest layer in a stack of Chris@16: * layers. Since a basic_descriptor cannot contain any further layers, it Chris@16: * simply returns a reference to itself. Chris@16: * Chris@16: * @return A reference to the lowest layer in the stack of layers. Ownership Chris@16: * is not transferred to the caller. Chris@16: */ Chris@16: lowest_layer_type& lowest_layer() Chris@16: { Chris@16: return *this; Chris@16: } Chris@16: Chris@16: /// Get a const reference to the lowest layer. Chris@16: /** Chris@16: * This function returns a const reference to the lowest layer in a stack of Chris@16: * layers. Since a basic_descriptor cannot contain any further layers, it Chris@16: * simply returns a reference to itself. Chris@16: * Chris@16: * @return A const reference to the lowest layer in the stack of layers. Chris@16: * Ownership is not transferred to the caller. Chris@16: */ Chris@16: const lowest_layer_type& lowest_layer() const Chris@16: { Chris@16: return *this; Chris@16: } Chris@16: Chris@16: /// Assign an existing native descriptor to the descriptor. Chris@16: /* Chris@16: * This function opens the descriptor to hold an existing native descriptor. Chris@16: * Chris@16: * @param native_descriptor A native descriptor. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: void assign(const native_handle_type& native_descriptor) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().assign(this->get_implementation(), Chris@16: native_descriptor, ec); Chris@16: boost::asio::detail::throw_error(ec, "assign"); Chris@16: } Chris@16: Chris@16: /// Assign an existing native descriptor to the descriptor. Chris@16: /* Chris@16: * This function opens the descriptor to hold an existing native descriptor. Chris@16: * Chris@16: * @param native_descriptor A native descriptor. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: */ Chris@16: boost::system::error_code assign(const native_handle_type& native_descriptor, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().assign( Chris@16: this->get_implementation(), native_descriptor, ec); Chris@16: } Chris@16: Chris@16: /// Determine whether the descriptor is open. Chris@16: bool is_open() const Chris@16: { Chris@16: return this->get_service().is_open(this->implementation); Chris@16: } Chris@16: Chris@16: /// Close the descriptor. Chris@16: /** Chris@16: * This function is used to close the descriptor. Any asynchronous read or Chris@16: * write operations will be cancelled immediately, and will complete with the Chris@16: * boost::asio::error::operation_aborted error. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Note that, even if Chris@16: * the function indicates an error, the underlying descriptor is closed. 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 descriptor. Chris@16: /** Chris@16: * This function is used to close the descriptor. Any asynchronous read or Chris@16: * write operations will be cancelled immediately, and will complete with the Chris@16: * boost::asio::error::operation_aborted error. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Note that, even if Chris@16: * the function indicates an error, the underlying descriptor is closed. 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 descriptor Chris@16: /// representation. Chris@16: /** Chris@16: * This function may be used to obtain the underlying representation of the Chris@16: * descriptor. This is intended to allow access to native descriptor Chris@16: * functionality that is not otherwise provided. Chris@16: */ Chris@16: native_type native() Chris@16: { Chris@16: return this->get_service().native_handle(this->implementation); Chris@16: } Chris@16: Chris@16: /// Get the native descriptor representation. Chris@16: /** Chris@16: * This function may be used to obtain the underlying representation of the Chris@16: * descriptor. This is intended to allow access to native descriptor Chris@16: * functionality 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->implementation); Chris@16: } Chris@16: Chris@16: /// Release ownership of the native descriptor implementation. Chris@16: /** Chris@16: * This function may be used to obtain the underlying representation of the Chris@16: * descriptor. After calling this function, @c is_open() returns false. The Chris@16: * caller is responsible for closing the descriptor. Chris@16: * Chris@16: * All outstanding asynchronous read or write operations will finish Chris@16: * immediately, and the handlers for cancelled operations will be passed the Chris@16: * boost::asio::error::operation_aborted error. Chris@16: */ Chris@16: native_handle_type release() Chris@16: { Chris@16: return this->get_service().release(this->implementation); Chris@16: } Chris@16: Chris@16: /// Cancel all asynchronous operations associated with the descriptor. Chris@16: /** Chris@16: * This function causes all outstanding asynchronous read or write operations Chris@16: * to finish immediately, and the handlers for cancelled operations will be Chris@16: * 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 descriptor. Chris@16: /** Chris@16: * This function causes all outstanding asynchronous read or write operations Chris@16: * to finish immediately, and the handlers for cancelled operations will be Chris@16: * 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: /// Perform an IO control command on the descriptor. Chris@16: /** Chris@16: * This function is used to execute an IO control command on the descriptor. Chris@16: * Chris@16: * @param command The IO control command to be performed on the descriptor. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: * Chris@16: * @sa IoControlCommand @n Chris@16: * boost::asio::posix::descriptor_base::bytes_readable @n Chris@16: * boost::asio::posix::descriptor_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::posix::stream_descriptor descriptor(io_service); Chris@16: * ... Chris@16: * boost::asio::posix::stream_descriptor::bytes_readable command; Chris@16: * descriptor.io_control(command); Chris@16: * std::size_t bytes_readable = command.get(); 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 descriptor. Chris@16: /** Chris@16: * This function is used to execute an IO control command on the descriptor. Chris@16: * Chris@16: * @param command The IO control command to be performed on the descriptor. 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::posix::descriptor_base::bytes_readable @n Chris@16: * boost::asio::posix::descriptor_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::posix::stream_descriptor descriptor(io_service); Chris@16: * ... Chris@16: * boost::asio::posix::stream_descriptor::bytes_readable command; Chris@16: * boost::system::error_code ec; Chris@16: * descriptor.io_control(command, ec); Chris@16: * if (ec) Chris@16: * { Chris@16: * // An error occurred. Chris@16: * } Chris@16: * std::size_t bytes_readable = command.get(); 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 descriptor. Chris@16: /** Chris@16: * @returns @c true if the descriptor'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->implementation); Chris@16: } Chris@16: Chris@16: /// Sets the non-blocking mode of the descriptor. Chris@16: /** Chris@16: * @param mode If @c true, the descriptor'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 descriptor. Chris@16: /** Chris@16: * @param mode If @c true, the descriptor'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 descriptor implementation. Chris@16: /** Chris@16: * This function is used to retrieve the non-blocking mode of the underlying Chris@16: * native descriptor. This mode has no effect on the behaviour of the Chris@16: * descriptor object's synchronous operations. Chris@16: * Chris@16: * @returns @c true if the underlying descriptor 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 descriptor object. Chris@16: * Consequently, the return value may be incorrect if the non-blocking mode Chris@16: * was set directly on the native descriptor. Chris@16: */ Chris@16: bool native_non_blocking() const Chris@16: { Chris@16: return this->get_service().native_non_blocking(this->implementation); Chris@16: } Chris@16: Chris@16: /// Sets the non-blocking mode of the native descriptor implementation. Chris@16: /** Chris@16: * This function is used to modify the non-blocking mode of the underlying Chris@16: * native descriptor. It has no effect on the behaviour of the descriptor Chris@16: * object's synchronous operations. Chris@16: * Chris@16: * @param mode If @c true, the underlying descriptor 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 descriptor implementation. Chris@16: /** Chris@16: * This function is used to modify the non-blocking mode of the underlying Chris@16: * native descriptor. It has no effect on the behaviour of the descriptor Chris@16: * object's synchronous operations. Chris@16: * Chris@16: * @param mode If @c true, the underlying descriptor 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: protected: Chris@16: /// Protected destructor to prevent deletion through this type. Chris@16: ~basic_descriptor() Chris@16: { Chris@16: } Chris@16: }; Chris@16: Chris@16: } // namespace posix Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) Chris@16: // || defined(GENERATING_DOCUMENTATION) Chris@16: Chris@16: #endif // BOOST_ASIO_POSIX_BASIC_DESCRIPTOR_HPP