diff options
Diffstat (limited to '3rdparty/asio/include/asio/posix/basic_descriptor.hpp')
-rw-r--r-- | 3rdparty/asio/include/asio/posix/basic_descriptor.hpp | 355 |
1 files changed, 273 insertions, 82 deletions
diff --git a/3rdparty/asio/include/asio/posix/basic_descriptor.hpp b/3rdparty/asio/include/asio/posix/basic_descriptor.hpp index 27db872c338..fa47d6a0949 100644 --- a/3rdparty/asio/include/asio/posix/basic_descriptor.hpp +++ b/3rdparty/asio/include/asio/posix/basic_descriptor.hpp @@ -2,7 +2,7 @@ // posix/basic_descriptor.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~ // -// Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com) +// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) @@ -17,16 +17,26 @@ #include "asio/detail/config.hpp" -#if defined(ASIO_ENABLE_OLD_SERVICES) - #if defined(ASIO_HAS_POSIX_STREAM_DESCRIPTOR) \ || defined(GENERATING_DOCUMENTATION) -#include "asio/basic_io_object.hpp" +#include <utility> +#include "asio/any_io_executor.hpp" +#include "asio/async_result.hpp" +#include "asio/detail/handler_type_requirements.hpp" +#include "asio/detail/io_object_impl.hpp" +#include "asio/detail/non_const_lvalue.hpp" #include "asio/detail/throw_error.hpp" #include "asio/error.hpp" +#include "asio/execution_context.hpp" #include "asio/posix/descriptor_base.hpp" +#if defined(ASIO_HAS_IO_URING_AS_DEFAULT) +# include "asio/detail/io_uring_descriptor_service.hpp" +#else // defined(ASIO_HAS_IO_URING_AS_DEFAULT) +# include "asio/detail/reactive_descriptor_service.hpp" +#endif // defined(ASIO_HAS_IO_URING_AS_DEFAULT) + #include "asio/detail/push_options.hpp" namespace asio { @@ -41,37 +51,76 @@ namespace posix { * @e Distinct @e objects: Safe.@n * @e Shared @e objects: Unsafe. */ -template <typename DescriptorService> +template <typename Executor = any_io_executor> class basic_descriptor - : public basic_io_object<DescriptorService>, - public descriptor_base + : public descriptor_base { +private: + class initiate_async_wait; + public: - /// The native representation of a descriptor. - typedef typename DescriptorService::native_handle_type native_handle_type; + /// The type of the executor associated with the object. + typedef Executor executor_type; - /// A basic_descriptor is always the lowest layer. - typedef basic_descriptor<DescriptorService> lowest_layer_type; + /// Rebinds the descriptor type to another executor. + template <typename Executor1> + struct rebind_executor + { + /// The descriptor type when rebound to the specified executor. + typedef basic_descriptor<Executor1> other; + }; - /// Construct a basic_descriptor without opening it. + /// The native representation of a descriptor. +#if defined(GENERATING_DOCUMENTATION) + typedef implementation_defined native_handle_type; +#elif defined(ASIO_HAS_IO_URING_AS_DEFAULT) + typedef detail::io_uring_descriptor_service::native_handle_type + native_handle_type; +#else // defined(ASIO_HAS_IO_URING_AS_DEFAULT) + typedef detail::reactive_descriptor_service::native_handle_type + native_handle_type; +#endif // defined(ASIO_HAS_IO_URING_AS_DEFAULT) + + /// A descriptor is always the lowest layer. + typedef basic_descriptor lowest_layer_type; + + /// Construct a descriptor without opening it. /** * This constructor creates a descriptor without opening it. * - * @param io_context The io_context object that the descriptor will use to + * @param ex The I/O executor that the descriptor will use, by default, to * dispatch handlers for any asynchronous operations performed on the * descriptor. */ - explicit basic_descriptor(asio::io_context& io_context) - : basic_io_object<DescriptorService>(io_context) + explicit basic_descriptor(const executor_type& ex) + : impl_(0, ex) { } - /// Construct a basic_descriptor on an existing native descriptor. + /// Construct a descriptor without opening it. + /** + * This constructor creates a descriptor without opening it. + * + * @param context An execution context which provides the I/O executor that + * the descriptor will use, by default, to dispatch handlers for any + * asynchronous operations performed on the descriptor. + */ + template <typename ExecutionContext> + explicit basic_descriptor(ExecutionContext& context, + constraint_t< + is_convertible<ExecutionContext&, execution_context&>::value, + defaulted_constraint + > = defaulted_constraint()) + : impl_(0, 0, context) + { + } + + /// Construct a descriptor on an existing native descriptor. /** * This constructor creates a descriptor object to hold an existing native * descriptor. * - * @param io_context The io_context object that the descriptor will use to + * @param ex The I/O executor that the descriptor will use, by default, to * dispatch handlers for any asynchronous operations performed on the * descriptor. * @@ -79,18 +128,82 @@ public: * * @throws asio::system_error Thrown on failure. */ - basic_descriptor(asio::io_context& io_context, + basic_descriptor(const executor_type& ex, const native_handle_type& native_descriptor) - : basic_io_object<DescriptorService>(io_context) + : impl_(0, ex) + { + asio::error_code ec; + impl_.get_service().assign(impl_.get_implementation(), + native_descriptor, ec); + asio::detail::throw_error(ec, "assign"); + } + + /// Construct a descriptor on an existing native descriptor. + /** + * This constructor creates a descriptor object to hold an existing native + * descriptor. + * + * @param context An execution context which provides the I/O executor that + * the descriptor will use, by default, to dispatch handlers for any + * asynchronous operations performed on the descriptor. + * + * @param native_descriptor A native descriptor. + * + * @throws asio::system_error Thrown on failure. + */ + template <typename ExecutionContext> + basic_descriptor(ExecutionContext& context, + const native_handle_type& native_descriptor, + constraint_t< + is_convertible<ExecutionContext&, execution_context&>::value + > = 0) + : impl_(0, 0, context) { asio::error_code ec; - this->get_service().assign(this->get_implementation(), + impl_.get_service().assign(impl_.get_implementation(), native_descriptor, ec); asio::detail::throw_error(ec, "assign"); } -#if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) - /// Move-construct a basic_descriptor from another. + /// Move-construct a descriptor from another. + /** + * This constructor moves a descriptor from one object to another. + * + * @param other The other descriptor object from which the move will + * occur. + * + * @note Following the move, the moved-from object is in the same state as if + * constructed using the @c basic_descriptor(const executor_type&) + * constructor. + */ + basic_descriptor(basic_descriptor&& other) noexcept + : impl_(std::move(other.impl_)) + { + } + + /// Move-assign a descriptor from another. + /** + * This assignment operator moves a descriptor from one object to another. + * + * @param other The other descriptor object from which the move will + * occur. + * + * @note Following the move, the moved-from object is in the same state as if + * constructed using the @c basic_descriptor(const executor_type&) + * constructor. + */ + basic_descriptor& operator=(basic_descriptor&& other) + { + impl_ = std::move(other.impl_); + return *this; + } + + // All descriptors have access to each other's implementations. + template <typename Executor1> + friend class basic_descriptor; + + /// Move-construct a basic_descriptor from a descriptor of another executor + /// type. /** * This constructor moves a descriptor from one object to another. * @@ -98,15 +211,20 @@ public: * occur. * * @note Following the move, the moved-from object is in the same state as if - * constructed using the @c basic_descriptor(io_context&) constructor. + * constructed using the @c basic_descriptor(const executor_type&) + * constructor. */ - basic_descriptor(basic_descriptor&& other) - : basic_io_object<DescriptorService>( - ASIO_MOVE_CAST(basic_descriptor)(other)) + template <typename Executor1> + basic_descriptor(basic_descriptor<Executor1>&& other, + constraint_t< + is_convertible<Executor1, Executor>::value, + defaulted_constraint + > = defaulted_constraint()) + : impl_(std::move(other.impl_)) { } - /// Move-assign a basic_descriptor from another. + /// Move-assign a basic_descriptor from a descriptor of another executor type. /** * This assignment operator moves a descriptor from one object to another. * @@ -114,20 +232,30 @@ public: * occur. * * @note Following the move, the moved-from object is in the same state as if - * constructed using the @c basic_descriptor(io_context&) constructor. + * constructed using the @c basic_descriptor(const executor_type&) + * constructor. */ - basic_descriptor& operator=(basic_descriptor&& other) + template <typename Executor1> + constraint_t< + is_convertible<Executor1, Executor>::value, + basic_descriptor& + > operator=(basic_descriptor<Executor1> && other) { - basic_io_object<DescriptorService>::operator=( - ASIO_MOVE_CAST(basic_descriptor)(other)); + basic_descriptor tmp(std::move(other)); + impl_ = std::move(tmp.impl_); return *this; } -#endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) + + /// Get the executor associated with the object. + const executor_type& get_executor() noexcept + { + return impl_.get_executor(); + } /// Get a reference to the lowest layer. /** * This function returns a reference to the lowest layer in a stack of - * layers. Since a basic_descriptor cannot contain any further layers, it + * layers. Since a descriptor cannot contain any further layers, it * simply returns a reference to itself. * * @return A reference to the lowest layer in the stack of layers. Ownership @@ -141,7 +269,7 @@ public: /// Get a const reference to the lowest layer. /** * This function returns a const reference to the lowest layer in a stack of - * layers. Since a basic_descriptor cannot contain any further layers, it + * layers. Since a descriptor cannot contain any further layers, it * simply returns a reference to itself. * * @return A const reference to the lowest layer in the stack of layers. @@ -163,7 +291,7 @@ public: void assign(const native_handle_type& native_descriptor) { asio::error_code ec; - this->get_service().assign(this->get_implementation(), + impl_.get_service().assign(impl_.get_implementation(), native_descriptor, ec); asio::detail::throw_error(ec, "assign"); } @@ -179,15 +307,15 @@ public: ASIO_SYNC_OP_VOID assign(const native_handle_type& native_descriptor, asio::error_code& ec) { - this->get_service().assign( - this->get_implementation(), native_descriptor, ec); + impl_.get_service().assign( + impl_.get_implementation(), native_descriptor, ec); ASIO_SYNC_OP_VOID_RETURN(ec); } /// Determine whether the descriptor is open. bool is_open() const { - return this->get_service().is_open(this->get_implementation()); + return impl_.get_service().is_open(impl_.get_implementation()); } /// Close the descriptor. @@ -202,7 +330,7 @@ public: void close() { asio::error_code ec; - this->get_service().close(this->get_implementation(), ec); + impl_.get_service().close(impl_.get_implementation(), ec); asio::detail::throw_error(ec, "close"); } @@ -217,7 +345,7 @@ public: */ ASIO_SYNC_OP_VOID close(asio::error_code& ec) { - this->get_service().close(this->get_implementation(), ec); + impl_.get_service().close(impl_.get_implementation(), ec); ASIO_SYNC_OP_VOID_RETURN(ec); } @@ -229,7 +357,7 @@ public: */ native_handle_type native_handle() { - return this->get_service().native_handle(this->get_implementation()); + return impl_.get_service().native_handle(impl_.get_implementation()); } /// Release ownership of the native descriptor implementation. @@ -244,7 +372,7 @@ public: */ native_handle_type release() { - return this->get_service().release(this->get_implementation()); + return impl_.get_service().release(impl_.get_implementation()); } /// Cancel all asynchronous operations associated with the descriptor. @@ -258,7 +386,7 @@ public: void cancel() { asio::error_code ec; - this->get_service().cancel(this->get_implementation(), ec); + impl_.get_service().cancel(impl_.get_implementation(), ec); asio::detail::throw_error(ec, "cancel"); } @@ -272,7 +400,7 @@ public: */ ASIO_SYNC_OP_VOID cancel(asio::error_code& ec) { - this->get_service().cancel(this->get_implementation(), ec); + impl_.get_service().cancel(impl_.get_implementation(), ec); ASIO_SYNC_OP_VOID_RETURN(ec); } @@ -291,7 +419,7 @@ public: * @par Example * Getting the number of bytes ready to read: * @code - * asio::posix::stream_descriptor descriptor(io_context); + * asio::posix::stream_descriptor descriptor(my_context); * ... * asio::posix::stream_descriptor::bytes_readable command; * descriptor.io_control(command); @@ -302,7 +430,7 @@ public: void io_control(IoControlCommand& command) { asio::error_code ec; - this->get_service().io_control(this->get_implementation(), command, ec); + impl_.get_service().io_control(impl_.get_implementation(), command, ec); asio::detail::throw_error(ec, "io_control"); } @@ -321,7 +449,7 @@ public: * @par Example * Getting the number of bytes ready to read: * @code - * asio::posix::stream_descriptor descriptor(io_context); + * asio::posix::stream_descriptor descriptor(my_context); * ... * asio::posix::stream_descriptor::bytes_readable command; * asio::error_code ec; @@ -337,7 +465,7 @@ public: ASIO_SYNC_OP_VOID io_control(IoControlCommand& command, asio::error_code& ec) { - this->get_service().io_control(this->get_implementation(), command, ec); + impl_.get_service().io_control(impl_.get_implementation(), command, ec); ASIO_SYNC_OP_VOID_RETURN(ec); } @@ -354,7 +482,7 @@ public: */ bool non_blocking() const { - return this->get_service().non_blocking(this->get_implementation()); + return impl_.get_service().non_blocking(impl_.get_implementation()); } /// Sets the non-blocking mode of the descriptor. @@ -373,7 +501,7 @@ public: void non_blocking(bool mode) { asio::error_code ec; - this->get_service().non_blocking(this->get_implementation(), mode, ec); + impl_.get_service().non_blocking(impl_.get_implementation(), mode, ec); asio::detail::throw_error(ec, "non_blocking"); } @@ -393,7 +521,7 @@ public: ASIO_SYNC_OP_VOID non_blocking( bool mode, asio::error_code& ec) { - this->get_service().non_blocking(this->get_implementation(), mode, ec); + impl_.get_service().non_blocking(impl_.get_implementation(), mode, ec); ASIO_SYNC_OP_VOID_RETURN(ec); } @@ -413,8 +541,8 @@ public: */ bool native_non_blocking() const { - return this->get_service().native_non_blocking( - this->get_implementation()); + return impl_.get_service().native_non_blocking( + impl_.get_implementation()); } /// Sets the non-blocking mode of the native descriptor implementation. @@ -435,8 +563,8 @@ public: void native_non_blocking(bool mode) { asio::error_code ec; - this->get_service().native_non_blocking( - this->get_implementation(), mode, ec); + impl_.get_service().native_non_blocking( + impl_.get_implementation(), mode, ec); asio::detail::throw_error(ec, "native_non_blocking"); } @@ -458,8 +586,8 @@ public: ASIO_SYNC_OP_VOID native_non_blocking( bool mode, asio::error_code& ec) { - this->get_service().native_non_blocking( - this->get_implementation(), mode, ec); + impl_.get_service().native_non_blocking( + impl_.get_implementation(), mode, ec); ASIO_SYNC_OP_VOID_RETURN(ec); } @@ -474,7 +602,7 @@ public: * @par Example * Waiting for a descriptor to become readable. * @code - * asio::posix::stream_descriptor descriptor(io_context); + * asio::posix::stream_descriptor descriptor(my_context); * ... * descriptor.wait(asio::posix::stream_descriptor::wait_read); * @endcode @@ -482,7 +610,7 @@ public: void wait(wait_type w) { asio::error_code ec; - this->get_service().wait(this->get_implementation(), w, ec); + impl_.get_service().wait(impl_.get_implementation(), w, ec); asio::detail::throw_error(ec, "wait"); } @@ -499,7 +627,7 @@ public: * @par Example * Waiting for a descriptor to become readable. * @code - * asio::posix::stream_descriptor descriptor(io_context); + * asio::posix::stream_descriptor descriptor(my_context); * ... * asio::error_code ec; * descriptor.wait(asio::posix::stream_descriptor::wait_read, ec); @@ -507,7 +635,7 @@ public: */ ASIO_SYNC_OP_VOID wait(wait_type w, asio::error_code& ec) { - this->get_service().wait(this->get_implementation(), w, ec); + impl_.get_service().wait(impl_.get_implementation(), w, ec); ASIO_SYNC_OP_VOID_RETURN(ec); } @@ -515,20 +643,27 @@ public: /// write, or to have pending error conditions. /** * This function is used to perform an asynchronous wait for a descriptor to - * enter a ready to read, write or error condition state. + * enter a ready to read, write or error condition state. It is an initiating + * function for an @ref asynchronous_operation, and always returns + * immediately. * * @param w Specifies the desired descriptor state. * - * @param handler The handler to be called when the wait operation completes. - * Copies will be made of the handler as required. The function signature of - * the handler must be: + * @param token The @ref completion_token that will be used to produce a + * completion handler, which will be called when the wait completes. + * Potential completion tokens include @ref use_future, @ref use_awaitable, + * @ref yield_context, or a function object with the correct completion + * signature. The function signature of the completion handler must be: * @code void handler( - * const asio::error_code& error // Result of operation + * const asio::error_code& error // Result of operation. * ); @endcode * Regardless of whether the asynchronous operation completes immediately or - * not, the handler will not be invoked from within this function. Invocation - * of the handler will be performed in a manner equivalent to using - * asio::io_context::post(). + * not, the completion handler will not be invoked from within this function. + * On immediate completion, invocation of the handler will be performed in a + * manner equivalent to using asio::post(). + * + * @par Completion Signature + * @code void(asio::error_code) @endcode * * @par Example * @code @@ -542,31 +677,89 @@ public: * * ... * - * asio::posix::stream_descriptor descriptor(io_context); + * asio::posix::stream_descriptor descriptor(my_context); * ... * descriptor.async_wait( * asio::posix::stream_descriptor::wait_read, * wait_handler); * @endcode + * + * @par Per-Operation Cancellation + * This asynchronous operation supports cancellation for the following + * asio::cancellation_type values: + * + * @li @c cancellation_type::terminal + * + * @li @c cancellation_type::partial + * + * @li @c cancellation_type::total */ - template <typename WaitHandler> - ASIO_INITFN_RESULT_TYPE(WaitHandler, - void (asio::error_code)) - async_wait(wait_type w, ASIO_MOVE_ARG(WaitHandler) handler) + template < + ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code)) + WaitToken = default_completion_token_t<executor_type>> + auto async_wait(wait_type w, + WaitToken&& token = default_completion_token_t<executor_type>()) + -> decltype( + async_initiate<WaitToken, void (asio::error_code)>( + declval<initiate_async_wait>(), token, w)) { - // If you get an error on the following line it means that your handler does - // not meet the documented type requirements for a WaitHandler. - ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check; - - return this->get_service().async_wait(this->get_implementation(), - w, ASIO_MOVE_CAST(WaitHandler)(handler)); + return async_initiate<WaitToken, void (asio::error_code)>( + initiate_async_wait(this), token, w); } protected: /// Protected destructor to prevent deletion through this type. + /** + * This function destroys the descriptor, cancelling any outstanding + * asynchronous wait operations associated with the descriptor as if by + * calling @c cancel. + */ ~basic_descriptor() { } + +#if defined(ASIO_HAS_IO_URING_AS_DEFAULT) + detail::io_object_impl<detail::io_uring_descriptor_service, Executor> impl_; +#else // defined(ASIO_HAS_IO_URING_AS_DEFAULT) + detail::io_object_impl<detail::reactive_descriptor_service, Executor> impl_; +#endif // defined(ASIO_HAS_IO_URING_AS_DEFAULT) + +private: + // Disallow copying and assignment. + basic_descriptor(const basic_descriptor&) = delete; + basic_descriptor& operator=(const basic_descriptor&) = delete; + + class initiate_async_wait + { + public: + typedef Executor executor_type; + + explicit initiate_async_wait(basic_descriptor* self) + : self_(self) + { + } + + const executor_type& get_executor() const noexcept + { + return self_->get_executor(); + } + + template <typename WaitHandler> + void operator()(WaitHandler&& handler, wait_type w) const + { + // If you get an error on the following line it means that your handler + // does not meet the documented type requirements for a WaitHandler. + ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check; + + detail::non_const_lvalue<WaitHandler> handler2(handler); + self_->impl_.get_service().async_wait( + self_->impl_.get_implementation(), w, + handler2.value, self_->impl_.get_executor()); + } + + private: + basic_descriptor* self_; + }; }; } // namespace posix @@ -577,6 +770,4 @@ protected: #endif // defined(ASIO_HAS_POSIX_STREAM_DESCRIPTOR) // || defined(GENERATING_DOCUMENTATION) -#endif // defined(ASIO_ENABLE_OLD_SERVICES) - #endif // ASIO_POSIX_BASIC_DESCRIPTOR_HPP |