100.00% Lines (87/87)
100.00% Functions (23/23)
| TLA | Baseline | Branch | ||||||
|---|---|---|---|---|---|---|---|---|
| Line | Hits | Code | Line | Hits | Code | |||
| 1 | // | 1 | // | |||||
| 2 | // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) | 2 | // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) | |||||
| 3 | // Copyright (c) 2026 Steve Gerbino | 3 | // Copyright (c) 2026 Steve Gerbino | |||||
| 4 | // Copyright (c) 2026 Michael Vandeberg | 4 | // Copyright (c) 2026 Michael Vandeberg | |||||
| 5 | // | 5 | // | |||||
| 6 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | 6 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |||||
| 7 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | 7 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |||||
| 8 | // | 8 | // | |||||
| 9 | // Official repository: https://github.com/cppalliance/corosio | 9 | // Official repository: https://github.com/cppalliance/corosio | |||||
| 10 | // | 10 | // | |||||
| 11 | 11 | |||||||
| 12 | #ifndef BOOST_COROSIO_TCP_ACCEPTOR_HPP | 12 | #ifndef BOOST_COROSIO_TCP_ACCEPTOR_HPP | |||||
| 13 | #define BOOST_COROSIO_TCP_ACCEPTOR_HPP | 13 | #define BOOST_COROSIO_TCP_ACCEPTOR_HPP | |||||
| 14 | 14 | |||||||
| 15 | #include <boost/corosio/family.hpp> | 15 | #include <boost/corosio/family.hpp> | |||||
| 16 | #include <boost/corosio/detail/config.hpp> | 16 | #include <boost/corosio/detail/config.hpp> | |||||
| 17 | #include <boost/corosio/detail/native_handle.hpp> | 17 | #include <boost/corosio/detail/native_handle.hpp> | |||||
| 18 | #include <boost/corosio/detail/op_base.hpp> | 18 | #include <boost/corosio/detail/op_base.hpp> | |||||
| 19 | #include <boost/corosio/error.hpp> | 19 | #include <boost/corosio/error.hpp> | |||||
| 20 | #include <boost/corosio/wait_type.hpp> | 20 | #include <boost/corosio/wait_type.hpp> | |||||
| 21 | #include <boost/corosio/io/io_object.hpp> | 21 | #include <boost/corosio/io/io_object.hpp> | |||||
| 22 | #include <boost/capy/results.hpp> | 22 | #include <boost/capy/results.hpp> | |||||
| 23 | #include <boost/corosio/endpoint.hpp> | 23 | #include <boost/corosio/endpoint.hpp> | |||||
| 24 | #include <boost/corosio/tcp_socket.hpp> | 24 | #include <boost/corosio/tcp_socket.hpp> | |||||
| 25 | #include <boost/capy/ex/executor_ref.hpp> | 25 | #include <boost/capy/ex/executor_ref.hpp> | |||||
| 26 | #include <boost/capy/ex/execution_context.hpp> | 26 | #include <boost/capy/ex/execution_context.hpp> | |||||
| 27 | #include <boost/capy/ex/await_context.hpp> | 27 | #include <boost/capy/ex/await_context.hpp> | |||||
| 28 | #include <boost/capy/concept/executor.hpp> | 28 | #include <boost/capy/concept/executor.hpp> | |||||
| 29 | 29 | |||||||
| 30 | #include <system_error> | 30 | #include <system_error> | |||||
| 31 | 31 | |||||||
| 32 | #include <concepts> | 32 | #include <concepts> | |||||
| 33 | #include <boost/capy/continuation.hpp> | 33 | #include <boost/capy/continuation.hpp> | |||||
| 34 | 34 | |||||||
| 35 | #include <coroutine> | 35 | #include <coroutine> | |||||
| 36 | #include <cstddef> | 36 | #include <cstddef> | |||||
| 37 | #include <stop_token> | 37 | #include <stop_token> | |||||
| 38 | #include <type_traits> | 38 | #include <type_traits> | |||||
| 39 | #include <utility> | 39 | #include <utility> | |||||
| 40 | 40 | |||||||
| 41 | namespace boost::corosio { | 41 | namespace boost::corosio { | |||||
| 42 | 42 | |||||||
| 43 | /** Accepts inbound TCP connections, from a coroutine. | 43 | /** Accepts inbound TCP connections, from a coroutine. | |||||
| 44 | 44 | |||||||
| 45 | This class provides asynchronous TCP accept operations that return | 45 | This class provides asynchronous TCP accept operations that return | |||||
| 46 | awaitable types. The acceptor binds to a local endpoint and listens | 46 | awaitable types. The acceptor binds to a local endpoint and listens | |||||
| 47 | for incoming connections. | 47 | for incoming connections. | |||||
| 48 | 48 | |||||||
| 49 | Each accept operation participates in the affine awaitable protocol, | 49 | Each accept operation participates in the affine awaitable protocol, | |||||
| 50 | ensuring coroutines resume on the correct executor. | 50 | ensuring coroutines resume on the correct executor. | |||||
| 51 | 51 | |||||||
| 52 | @par Thread Safety | 52 | @par Thread Safety | |||||
| 53 | Distinct objects: Safe.@n | 53 | Distinct objects: Safe.@n | |||||
| 54 | Shared objects: Unsafe. An acceptor must not have concurrent accept | 54 | Shared objects: Unsafe. An acceptor must not have concurrent accept | |||||
| 55 | operations. | 55 | operations. | |||||
| 56 | 56 | |||||||
| 57 | @par Semantics | 57 | @par Semantics | |||||
| 58 | Wraps the platform TCP listener. Operations dispatch to | 58 | Wraps the platform TCP listener. Operations dispatch to | |||||
| 59 | OS accept APIs via the `io_context` reactor. | 59 | OS accept APIs via the `io_context` reactor. | |||||
| 60 | 60 | |||||||
| 61 | @par Example | 61 | @par Example | |||||
| 62 | @par !example convenience_construction | 62 | @par !example convenience_construction | |||||
| 63 | 63 | |||||||
| 64 | @par Example | 64 | @par Example | |||||
| 65 | @par !example fine_grained_setup | 65 | @par !example fine_grained_setup | |||||
| 66 | */ | 66 | */ | |||||
| 67 | class BOOST_COROSIO_DECL tcp_acceptor : public io_object | 67 | class BOOST_COROSIO_DECL tcp_acceptor : public io_object | |||||
| 68 | { | 68 | { | |||||
| 69 | struct wait_awaitable : detail::void_op_base<wait_awaitable> | 69 | struct wait_awaitable : detail::void_op_base<wait_awaitable> | |||||
| 70 | { | 70 | { | |||||
| 71 | private: | 71 | private: | |||||
| 72 | friend tcp_acceptor; | 72 | friend tcp_acceptor; | |||||
| 73 | 73 | |||||||
| HITCBC | 74 | 31 | wait_awaitable(tcp_acceptor& acc, wait_type w) noexcept | 74 | 31 | wait_awaitable(tcp_acceptor& acc, wait_type w) noexcept | ||
| HITCBC | 75 | 62 | : acc_(acc) | 75 | 62 | : acc_(acc) | ||
| HITCBC | 76 | 31 | , w_(w) | 76 | 31 | , w_(w) | ||
| 77 | { | 77 | { | |||||
| HITCBC | 78 | 31 | } | 78 | 31 | } | ||
| 79 | 79 | |||||||
| 80 | friend detail::void_op_base<wait_awaitable>; | 80 | friend detail::void_op_base<wait_awaitable>; | |||||
| 81 | 81 | |||||||
| 82 | tcp_acceptor& acc_; | 82 | tcp_acceptor& acc_; | |||||
| 83 | wait_type w_; | 83 | wait_type w_; | |||||
| 84 | 84 | |||||||
| 85 | std::coroutine_handle<> | 85 | std::coroutine_handle<> | |||||
| HITCBC | 86 | 27 | dispatch(capy::continuation& cont, capy::executor_ref ex) const | 86 | 27 | dispatch(capy::continuation& cont, capy::executor_ref ex) const | ||
| 87 | { | 87 | { | |||||
| HITCBC | 88 | 27 | return acc_.get().wait(cont, ex, w_, token_, &ec_); | 88 | 27 | return acc_.get().wait(cont, ex, w_, token_, &ec_); | ||
| 89 | } | 89 | } | |||||
| 90 | }; | 90 | }; | |||||
| 91 | 91 | |||||||
| 92 | struct accept_awaitable : detail::void_op_base<accept_awaitable> | 92 | struct accept_awaitable : detail::void_op_base<accept_awaitable> | |||||
| 93 | { | 93 | { | |||||
| 94 | private: | 94 | private: | |||||
| 95 | friend tcp_acceptor; | 95 | friend tcp_acceptor; | |||||
| 96 | friend detail::void_op_base<accept_awaitable>; | 96 | friend detail::void_op_base<accept_awaitable>; | |||||
| 97 | 97 | |||||||
| 98 | tcp_acceptor& acc_; | 98 | tcp_acceptor& acc_; | |||||
| 99 | tcp_socket& peer_; | 99 | tcp_socket& peer_; | |||||
| 100 | mutable io_object::implementation* peer_impl_ = nullptr; | 100 | mutable io_object::implementation* peer_impl_ = nullptr; | |||||
| 101 | 101 | |||||||
| 102 | 102 | |||||||
| HITCBC | 103 | 7210 | accept_awaitable(tcp_acceptor& acc, tcp_socket& peer) noexcept | 103 | 7399 | accept_awaitable(tcp_acceptor& acc, tcp_socket& peer) noexcept | ||
| HITCBC | 104 | 14420 | : acc_(acc) | 104 | 14798 | : acc_(acc) | ||
| HITCBC | 105 | 7210 | , peer_(peer) | 105 | 7399 | , peer_(peer) | ||
| 106 | { | 106 | { | |||||
| HITCBC | 107 | 7210 | } | 107 | 7399 | } | ||
| 108 | 108 | |||||||
| 109 | std::coroutine_handle<> | 109 | std::coroutine_handle<> | |||||
| HITCBC | 110 | 7201 | dispatch(capy::continuation& cont, capy::executor_ref ex) const | 110 | 7389 | dispatch(capy::continuation& cont, capy::executor_ref ex) const | ||
| 111 | { | 111 | { | |||||
| HITCBC | 112 | 21603 | return acc_.get().accept( | 112 | 22167 | return acc_.get().accept( | ||
| HITCBC | 113 | 21603 | cont, ex, this->token_, &this->ec_, &peer_impl_); | 113 | 22167 | cont, ex, this->token_, &this->ec_, &peer_impl_); | ||
| 114 | } | 114 | } | |||||
| 115 | 115 | |||||||
| 116 | public: | 116 | public: | |||||
| 117 | // A peer accepted for an awaiter that never resumed is closed | 117 | // A peer accepted for an awaiter that never resumed is closed | |||||
| 118 | // here, or nothing would own it. | 118 | // here, or nothing would own it. | |||||
| HITCBC | 119 | 14420 | ~accept_awaitable() | 119 | 14798 | ~accept_awaitable() | ||
| 120 | { | 120 | { | |||||
| HITCBC | 121 | 14420 | if (peer_impl_) | 121 | 14798 | if (peer_impl_) | ||
| HITCBC | 122 | 2 | discard_peer(acc_, peer_impl_); | 122 | 2 | discard_peer(acc_, peer_impl_); | ||
| HITCBC | 123 | 14420 | } | 123 | 14798 | } | ||
| 124 | 124 | |||||||
| HITCBC | 125 | 6398 | [[nodiscard]] capy::results<> await_resume() const noexcept | 125 | 6587 | [[nodiscard]] capy::results<> await_resume() const noexcept | ||
| 126 | { | 126 | { | |||||
| HITCBC | 127 | 6398 | if (!this->ec_ && peer_impl_) | 127 | 6587 | if (!this->ec_ && peer_impl_) | ||
| HITCBC | 128 | 6232 | peer_.h_.reset(std::exchange(peer_impl_, nullptr)); | 128 | 6426 | peer_.h_.reset(std::exchange(peer_impl_, nullptr)); | ||
| HITCBC | 129 | 6398 | return {this->ec_}; | 129 | 6587 | return {this->ec_}; | ||
| 130 | } | 130 | } | |||||
| 131 | }; | 131 | }; | |||||
| 132 | 132 | |||||||
| 133 | struct accept_value_awaitable : detail::void_op_base<accept_value_awaitable> | 133 | struct accept_value_awaitable : detail::void_op_base<accept_value_awaitable> | |||||
| 134 | { | 134 | { | |||||
| 135 | private: | 135 | private: | |||||
| 136 | friend tcp_acceptor; | 136 | friend tcp_acceptor; | |||||
| 137 | friend detail::void_op_base<accept_value_awaitable>; | 137 | friend detail::void_op_base<accept_value_awaitable>; | |||||
| 138 | 138 | |||||||
| 139 | tcp_acceptor& acc_; | 139 | tcp_acceptor& acc_; | |||||
| 140 | mutable io_object::implementation* peer_impl_ = nullptr; | 140 | mutable io_object::implementation* peer_impl_ = nullptr; | |||||
| 141 | 141 | |||||||
| 142 | 142 | |||||||
| HITCBC | 143 | 41 | explicit accept_value_awaitable(tcp_acceptor& acc) noexcept : acc_(acc) | 143 | 41 | explicit accept_value_awaitable(tcp_acceptor& acc) noexcept : acc_(acc) | ||
| 144 | { | 144 | { | |||||
| HITCBC | 145 | 41 | } | 145 | 41 | } | ||
| 146 | 146 | |||||||
| 147 | std::coroutine_handle<> | 147 | std::coroutine_handle<> | |||||
| HITCBC | 148 | 37 | dispatch(capy::continuation& cont, capy::executor_ref ex) const | 148 | 37 | dispatch(capy::continuation& cont, capy::executor_ref ex) const | ||
| 149 | { | 149 | { | |||||
| HITCBC | 150 | 111 | return acc_.get().accept( | 150 | 111 | return acc_.get().accept( | ||
| HITCBC | 151 | 111 | cont, ex, this->token_, &this->ec_, &peer_impl_); | 151 | 111 | cont, ex, this->token_, &this->ec_, &peer_impl_); | ||
| 152 | } | 152 | } | |||||
| 153 | 153 | |||||||
| 154 | public: | 154 | public: | |||||
| 155 | // A peer accepted for an awaiter that never resumed is closed | 155 | // A peer accepted for an awaiter that never resumed is closed | |||||
| 156 | // here, or nothing would own it. | 156 | // here, or nothing would own it. | |||||
| HITCBC | 157 | 82 | ~accept_value_awaitable() | 157 | 82 | ~accept_value_awaitable() | ||
| 158 | { | 158 | { | |||||
| HITCBC | 159 | 82 | if (peer_impl_) | 159 | 82 | if (peer_impl_) | ||
| HITCBC | 160 | 2 | discard_peer(acc_, peer_impl_); | 160 | 2 | discard_peer(acc_, peer_impl_); | ||
| HITCBC | 161 | 82 | } | 161 | 82 | } | ||
| 162 | 162 | |||||||
| HITCBC | 163 | 31 | [[nodiscard]] capy::results<tcp_socket> await_resume() noexcept | 163 | 31 | [[nodiscard]] capy::results<tcp_socket> await_resume() noexcept | ||
| 164 | { | 164 | { | |||||
| HITCBC | 165 | 31 | return take_peer(acc_, this->ec_, peer_impl_); | 165 | 31 | return take_peer(acc_, this->ec_, peer_impl_); | ||
| 166 | } | 166 | } | |||||
| 167 | }; | 167 | }; | |||||
| 168 | 168 | |||||||
| 169 | public: | 169 | public: | |||||
| 170 | /** Closes the acceptor if open, cancelling any pending operations. | 170 | /** Closes the acceptor if open, cancelling any pending operations. | |||||
| 171 | */ | 171 | */ | |||||
| 172 | ~tcp_acceptor() override; | 172 | ~tcp_acceptor() override; | |||||
| 173 | 173 | |||||||
| 174 | /** Construct an acceptor from an execution context. | 174 | /** Construct an acceptor from an execution context. | |||||
| 175 | 175 | |||||||
| 176 | @param ctx The execution context that owns this acceptor. | 176 | @param ctx The execution context that owns this acceptor. | |||||
| 177 | */ | 177 | */ | |||||
| 178 | explicit tcp_acceptor(capy::execution_context& ctx); | 178 | explicit tcp_acceptor(capy::execution_context& ctx); | |||||
| 179 | 179 | |||||||
| 180 | /** Convenience constructor: open + configure + bind + listen. | 180 | /** Convenience constructor: open + configure + bind + listen. | |||||
| 181 | 181 | |||||||
| 182 | Creates a fully bound listening acceptor in a single | 182 | Creates a fully bound listening acceptor in a single | |||||
| 183 | expression, throwing the codes the piecewise `open()` + | 183 | expression, throwing the codes the piecewise `open()` + | |||||
| 184 | `set_option()` + `bind()` + `listen()` path reports. The | 184 | `set_option()` + `bind()` + `listen()` path reports. The | |||||
| 185 | address family is deduced from @p ep. | 185 | address family is deduced from @p ep. | |||||
| 186 | 186 | |||||||
| 187 | Before binding, the constructor configures address reuse so a | 187 | Before binding, the constructor configures address reuse so a | |||||
| 188 | server can rebind its port immediately after a restart. It | 188 | server can rebind its port immediately after a restart. It | |||||
| 189 | sets `SO_REUSEADDR` on POSIX and `SO_EXCLUSIVEADDRUSE` on | 189 | sets `SO_REUSEADDR` on POSIX and `SO_EXCLUSIVEADDRUSE` on | |||||
| 190 | Windows. Windows does not use `SO_REUSEADDR` because it | 190 | Windows. Windows does not use `SO_REUSEADDR` because it | |||||
| 191 | instead grants other sockets bind-over rights. A second | 191 | instead grants other sockets bind-over rights. A second | |||||
| 192 | listener on an occupied endpoint therefore throws | 192 | listener on an occupied endpoint therefore throws | |||||
| 193 | `errc::address_in_use` on every platform. | 193 | `errc::address_in_use` on every platform. | |||||
| 194 | 194 | |||||||
| 195 | @param ctx The execution context that owns this acceptor. | 195 | @param ctx The execution context that owns this acceptor. | |||||
| 196 | @param ep The local endpoint to bind to. | 196 | @param ep The local endpoint to bind to. | |||||
| 197 | @param backlog The maximum pending connection queue length. | 197 | @param backlog The maximum pending connection queue length. | |||||
| 198 | 198 | |||||||
| 199 | @throws std::system_error on open, configuration, bind, or | 199 | @throws std::system_error on open, configuration, bind, or | |||||
| 200 | listen failure. | 200 | listen failure. | |||||
| 201 | */ | 201 | */ | |||||
| 202 | tcp_acceptor(capy::execution_context& ctx, endpoint ep, int backlog = 128); | 202 | tcp_acceptor(capy::execution_context& ctx, endpoint ep, int backlog = 128); | |||||
| 203 | 203 | |||||||
| 204 | /** Construct an acceptor from an executor. | 204 | /** Construct an acceptor from an executor. | |||||
| 205 | 205 | |||||||
| 206 | The acceptor is associated with the executor's context. `Ex` | 206 | The acceptor is associated with the executor's context. `Ex` | |||||
| 207 | must satisfy `capy::Executor`. | 207 | must satisfy `capy::Executor`. | |||||
| 208 | 208 | |||||||
| 209 | @param ex The executor whose context owns the acceptor. | 209 | @param ex The executor whose context owns the acceptor. | |||||
| 210 | */ | 210 | */ | |||||
| 211 | template<class Ex> | 211 | template<class Ex> | |||||
| 212 | requires(!std::same_as<std::remove_cvref_t<Ex>, tcp_acceptor>) && | 212 | requires(!std::same_as<std::remove_cvref_t<Ex>, tcp_acceptor>) && | |||||
| 213 | capy::Executor<Ex> | 213 | capy::Executor<Ex> | |||||
| HITCBC | 214 | 1 | explicit tcp_acceptor(Ex const& ex) : tcp_acceptor(ex.context()) | 214 | 1 | explicit tcp_acceptor(Ex const& ex) : tcp_acceptor(ex.context()) | ||
| 215 | { | 215 | { | |||||
| HITCBC | 216 | 1 | } | 216 | 1 | } | ||
| 217 | 217 | |||||||
| 218 | /** Convenience constructor from an executor. | 218 | /** Convenience constructor from an executor. | |||||
| 219 | 219 | |||||||
| 220 | Creates a fully bound listening acceptor in a single | 220 | Creates a fully bound listening acceptor in a single | |||||
| 221 | expression, throwing the codes the piecewise `open()` + | 221 | expression, throwing the codes the piecewise `open()` + | |||||
| 222 | `set_option()` + `bind()` + `listen()` path reports. The | 222 | `set_option()` + `bind()` + `listen()` path reports. The | |||||
| 223 | address family is deduced from @p ep. | 223 | address family is deduced from @p ep. | |||||
| 224 | 224 | |||||||
| 225 | Before binding, the constructor configures address reuse so a | 225 | Before binding, the constructor configures address reuse so a | |||||
| 226 | server can rebind its port immediately after a restart. It | 226 | server can rebind its port immediately after a restart. It | |||||
| 227 | sets `SO_REUSEADDR` on POSIX and `SO_EXCLUSIVEADDRUSE` on | 227 | sets `SO_REUSEADDR` on POSIX and `SO_EXCLUSIVEADDRUSE` on | |||||
| 228 | Windows. Windows does not use `SO_REUSEADDR` because it | 228 | Windows. Windows does not use `SO_REUSEADDR` because it | |||||
| 229 | instead grants other sockets bind-over rights. A second | 229 | instead grants other sockets bind-over rights. A second | |||||
| 230 | listener on an occupied endpoint therefore throws | 230 | listener on an occupied endpoint therefore throws | |||||
| 231 | `errc::address_in_use` on every platform. | 231 | `errc::address_in_use` on every platform. | |||||
| 232 | 232 | |||||||
| 233 | `Ex` must satisfy `capy::Executor`. | 233 | `Ex` must satisfy `capy::Executor`. | |||||
| 234 | 234 | |||||||
| 235 | @param ex The executor whose context owns the acceptor. | 235 | @param ex The executor whose context owns the acceptor. | |||||
| 236 | @param ep The local endpoint to bind to. | 236 | @param ep The local endpoint to bind to. | |||||
| 237 | @param backlog The maximum pending connection queue length. | 237 | @param backlog The maximum pending connection queue length. | |||||
| 238 | 238 | |||||||
| 239 | @throws std::system_error on open, configuration, bind, or | 239 | @throws std::system_error on open, configuration, bind, or | |||||
| 240 | listen failure. | 240 | listen failure. | |||||
| 241 | */ | 241 | */ | |||||
| 242 | template<class Ex> | 242 | template<class Ex> | |||||
| 243 | requires capy::Executor<Ex> | 243 | requires capy::Executor<Ex> | |||||
| 244 | tcp_acceptor(Ex const& ex, endpoint ep, int backlog = 128) | 244 | tcp_acceptor(Ex const& ex, endpoint ep, int backlog = 128) | |||||
| 245 | : tcp_acceptor(ex.context(), ep, backlog) | 245 | : tcp_acceptor(ex.context(), ep, backlog) | |||||
| 246 | { | 246 | { | |||||
| 247 | } | 247 | } | |||||
| 248 | 248 | |||||||
| 249 | /** Transfers ownership of the acceptor resources. | 249 | /** Transfers ownership of the acceptor resources. | |||||
| 250 | 250 | |||||||
| 251 | @param other The acceptor to move from. | 251 | @param other The acceptor to move from. | |||||
| 252 | 252 | |||||||
| 253 | @pre No awaitables returned by @p other's methods exist. | 253 | @pre No awaitables returned by @p other's methods exist. | |||||
| 254 | @pre The execution context associated with @p other must | 254 | @pre The execution context associated with @p other must | |||||
| 255 | outlive this acceptor. | 255 | outlive this acceptor. | |||||
| 256 | */ | 256 | */ | |||||
| HITCBC | 257 | 182 | tcp_acceptor(tcp_acceptor&& other) noexcept : io_object(std::move(other)) {} | 257 | 182 | tcp_acceptor(tcp_acceptor&& other) noexcept : io_object(std::move(other)) {} | ||
| 258 | 258 | |||||||
| 259 | /** Closes any existing acceptor and transfers ownership. | 259 | /** Closes any existing acceptor and transfers ownership. | |||||
| 260 | 260 | |||||||
| 261 | @param other The acceptor to move from. | 261 | @param other The acceptor to move from. | |||||
| 262 | 262 | |||||||
| 263 | @pre No awaitables returned by either `*this` or @p other's | 263 | @pre No awaitables returned by either `*this` or @p other's | |||||
| 264 | methods exist. | 264 | methods exist. | |||||
| 265 | @pre The execution context associated with @p other must | 265 | @pre The execution context associated with @p other must | |||||
| 266 | outlive this acceptor. | 266 | outlive this acceptor. | |||||
| 267 | 267 | |||||||
| 268 | @return Reference to this acceptor. | 268 | @return Reference to this acceptor. | |||||
| 269 | */ | 269 | */ | |||||
| HITCBC | 270 | 3 | tcp_acceptor& operator=(tcp_acceptor&& other) noexcept | 270 | 3 | tcp_acceptor& operator=(tcp_acceptor&& other) noexcept | ||
| 271 | { | 271 | { | |||||
| HITCBC | 272 | 3 | if (this != &other) | 272 | 3 | if (this != &other) | ||
| 273 | { | 273 | { | |||||
| HITCBC | 274 | 3 | close(); | 274 | 3 | close(); | ||
| HITCBC | 275 | 3 | h_ = std::move(other.h_); | 275 | 3 | h_ = std::move(other.h_); | ||
| 276 | } | 276 | } | |||||
| HITCBC | 277 | 3 | return *this; | 277 | 3 | return *this; | ||
| 278 | } | 278 | } | |||||
| 279 | 279 | |||||||
| 280 | /// Copy construction is disabled; the handle is uniquely owned. | 280 | /// Copy construction is disabled; the handle is uniquely owned. | |||||
| 281 | tcp_acceptor(tcp_acceptor const&) = delete; | 281 | tcp_acceptor(tcp_acceptor const&) = delete; | |||||
| 282 | /// Copy assignment is disabled; the handle is uniquely owned. | 282 | /// Copy assignment is disabled; the handle is uniquely owned. | |||||
| 283 | tcp_acceptor& operator=(tcp_acceptor const&) = delete; | 283 | tcp_acceptor& operator=(tcp_acceptor const&) = delete; | |||||
| 284 | 284 | |||||||
| 285 | /** Create the acceptor socket without binding or listening. | 285 | /** Create the acceptor socket without binding or listening. | |||||
| 286 | 286 | |||||||
| 287 | Creates a TCP socket with dual-stack enabled for IPv6. | 287 | Creates a TCP socket with dual-stack enabled for IPv6. | |||||
| 288 | Does not set SO_REUSEADDR. Call `set_option` explicitly | 288 | Does not set SO_REUSEADDR. Call `set_option` explicitly | |||||
| 289 | if needed. | 289 | if needed. | |||||
| 290 | 290 | |||||||
| 291 | If the acceptor is already open, this function is a no-op. | 291 | If the acceptor is already open, this function is a no-op. | |||||
| 292 | 292 | |||||||
| 293 | Failures such as descriptor exhaustion are normal runtime | 293 | Failures such as descriptor exhaustion are normal runtime | |||||
| 294 | conditions and are reported through the returned error code. | 294 | conditions and are reported through the returned error code. | |||||
| 295 | 295 | |||||||
| 296 | @param f The address family (IPv4 or IPv6). Defaults to | 296 | @param f The address family (IPv4 or IPv6). Defaults to | |||||
| 297 | `family::v4`. | 297 | `family::v4`. | |||||
| 298 | 298 | |||||||
| 299 | @par Example | 299 | @par Example | |||||
| 300 | @par !example open | 300 | @par !example open | |||||
| 301 | 301 | |||||||
| 302 | @see @ref bind, @ref listen | 302 | @see @ref bind, @ref listen | |||||
| 303 | 303 | |||||||
| 304 | @return The error code, empty on success. | 304 | @return The error code, empty on success. | |||||
| 305 | */ | 305 | */ | |||||
| 306 | [[nodiscard]] std::error_code open(family f = family::v4) noexcept; | 306 | [[nodiscard]] std::error_code open(family f = family::v4) noexcept; | |||||
| 307 | 307 | |||||||
| 308 | /** Bind to a local endpoint. | 308 | /** Bind to a local endpoint. | |||||
| 309 | 309 | |||||||
| 310 | The acceptor must be open. Binds the socket to @p ep and | 310 | The acceptor must be open. Binds the socket to @p ep and | |||||
| 311 | caches the resolved local endpoint (useful when port 0 is | 311 | caches the resolved local endpoint (useful when port 0 is | |||||
| 312 | used to request an ephemeral port). | 312 | used to request an ephemeral port). | |||||
| 313 | 313 | |||||||
| 314 | @param ep The local endpoint to bind to. | 314 | @param ep The local endpoint to bind to. | |||||
| 315 | 315 | |||||||
| 316 | @return An error code indicating success or the reason for | 316 | @return An error code indicating success or the reason for | |||||
| 317 | failure. | 317 | failure. | |||||
| 318 | 318 | |||||||
| 319 | @par Error Conditions | 319 | @par Error Conditions | |||||
| 320 | @li `errc::address_in_use`: The endpoint is already in use. | 320 | @li `errc::address_in_use`: The endpoint is already in use. | |||||
| 321 | @li `errc::address_not_available`: The address is not available | 321 | @li `errc::address_not_available`: The address is not available | |||||
| 322 | on any local interface. | 322 | on any local interface. | |||||
| 323 | @li `errc::permission_denied`: Insufficient privileges to bind | 323 | @li `errc::permission_denied`: Insufficient privileges to bind | |||||
| 324 | to the endpoint (e.g., privileged port). | 324 | to the endpoint (e.g., privileged port). | |||||
| 325 | @li `errc::bad_file_descriptor`: The acceptor is not open. | 325 | @li `errc::bad_file_descriptor`: The acceptor is not open. | |||||
| 326 | */ | 326 | */ | |||||
| 327 | [[nodiscard]] std::error_code bind(endpoint ep) noexcept; | 327 | [[nodiscard]] std::error_code bind(endpoint ep) noexcept; | |||||
| 328 | 328 | |||||||
| 329 | /** Start listening for incoming connections. | 329 | /** Start listening for incoming connections. | |||||
| 330 | 330 | |||||||
| 331 | The acceptor must be open and bound. Registers the acceptor | 331 | The acceptor must be open and bound. Registers the acceptor | |||||
| 332 | with the platform reactor. | 332 | with the platform reactor. | |||||
| 333 | 333 | |||||||
| 334 | @param backlog The maximum length of the queue of pending | 334 | @param backlog The maximum length of the queue of pending | |||||
| 335 | connections. Defaults to 128. | 335 | connections. Defaults to 128. | |||||
| 336 | 336 | |||||||
| 337 | @return An error code indicating success or the reason for | 337 | @return An error code indicating success or the reason for | |||||
| 338 | failure. | 338 | failure. | |||||
| 339 | 339 | |||||||
| 340 | A closed acceptor reports `errc::bad_file_descriptor`. | 340 | A closed acceptor reports `errc::bad_file_descriptor`. | |||||
| 341 | */ | 341 | */ | |||||
| 342 | [[nodiscard]] std::error_code listen(int backlog = 128) noexcept; | 342 | [[nodiscard]] std::error_code listen(int backlog = 128) noexcept; | |||||
| 343 | 343 | |||||||
| 344 | /** Close the acceptor. | 344 | /** Close the acceptor. | |||||
| 345 | 345 | |||||||
| 346 | Releases acceptor resources. Any pending operations complete | 346 | Releases acceptor resources. Any pending operations complete | |||||
| 347 | with `errc::operation_canceled`. | 347 | with `errc::operation_canceled`. | |||||
| 348 | */ | 348 | */ | |||||
| 349 | void close() noexcept; | 349 | void close() noexcept; | |||||
| 350 | 350 | |||||||
| 351 | /** Check if the acceptor is listening. | 351 | /** Check if the acceptor is listening. | |||||
| 352 | 352 | |||||||
| 353 | @return `true` if the acceptor is open and listening. | 353 | @return `true` if the acceptor is open and listening. | |||||
| 354 | */ | 354 | */ | |||||
| HITCBC | 355 | 20977 | bool is_open() const noexcept | 355 | 21166 | bool is_open() const noexcept | ||
| 356 | { | 356 | { | |||||
| HITCBC | 357 | 20977 | return h_ && get().is_open(); | 357 | 21166 | return h_ && get().is_open(); | ||
| 358 | } | 358 | } | |||||
| 359 | 359 | |||||||
| 360 | /** Initiate an asynchronous accept operation. | 360 | /** Initiate an asynchronous accept operation. | |||||
| 361 | 361 | |||||||
| 362 | Accepts an incoming connection and initializes the provided | 362 | Accepts an incoming connection and initializes the provided | |||||
| 363 | socket with the new connection. The acceptor must be listening | 363 | socket with the new connection. The acceptor must be listening | |||||
| 364 | before calling this function. | 364 | before calling this function. | |||||
| 365 | 365 | |||||||
| 366 | The operation supports cancellation via `std::stop_token` through | 366 | The operation supports cancellation via `std::stop_token` through | |||||
| 367 | the affine awaitable protocol. If the associated stop token is | 367 | the affine awaitable protocol. If the associated stop token is | |||||
| 368 | triggered, the operation completes immediately with | 368 | triggered, the operation completes immediately with | |||||
| 369 | `errc::operation_canceled`. | 369 | `errc::operation_canceled`. | |||||
| 370 | 370 | |||||||
| 371 | @param peer The socket to receive the accepted connection. Any | 371 | @param peer The socket to receive the accepted connection. Any | |||||
| 372 | existing connection on this socket is closed. | 372 | existing connection on this socket is closed. | |||||
| 373 | 373 | |||||||
| 374 | @return An awaitable that completes with `results<>`. | 374 | @return An awaitable that completes with `results<>`. | |||||
| 375 | Returns success on successful accept, or an error code on | 375 | Returns success on successful accept, or an error code on | |||||
| 376 | failure including: | 376 | failure including: | |||||
| 377 | - `operation_canceled`: Cancelled via stop_token or cancel(). | 377 | - `operation_canceled`: Cancelled via stop_token or cancel(). | |||||
| 378 | Check `ec == cond::canceled` for portable comparison. | 378 | Check `ec == cond::canceled` for portable comparison. | |||||
| 379 | 379 | |||||||
| 380 | A closed acceptor completes with `errc::bad_file_descriptor`. | 380 | A closed acceptor completes with `errc::bad_file_descriptor`. | |||||
| 381 | 381 | |||||||
| 382 | @pre The peer socket must be associated with the same execution context. | 382 | @pre The peer socket must be associated with the same execution context. | |||||
| 383 | 383 | |||||||
| 384 | Both this acceptor and @p peer must outlive the returned | 384 | Both this acceptor and @p peer must outlive the returned | |||||
| 385 | awaitable. | 385 | awaitable. | |||||
| 386 | 386 | |||||||
| 387 | @par Example | 387 | @par Example | |||||
| 388 | @par !example accept_into_a_reused_socket | 388 | @par !example accept_into_a_reused_socket | |||||
| 389 | 389 | |||||||
| 390 | @see @ref accept() | 390 | @see @ref accept() | |||||
| 391 | */ | 391 | */ | |||||
| HITCBC | 392 | 7210 | [[nodiscard]] auto accept(tcp_socket& peer) | 392 | 7399 | [[nodiscard]] auto accept(tcp_socket& peer) | ||
| 393 | { | 393 | { | |||||
| HITCBC | 394 | 7210 | accept_awaitable aw(*this, peer); | 394 | 7399 | accept_awaitable aw(*this, peer); | ||
| HITCBC | 395 | 7210 | if (!is_open()) | 395 | 7399 | if (!is_open()) | ||
| HITCBC | 396 | 2 | aw.ec_ = make_error_code(std::errc::bad_file_descriptor); | 396 | 2 | aw.ec_ = make_error_code(std::errc::bad_file_descriptor); | ||
| HITCBC | 397 | 7210 | return aw; | 397 | 7399 | return aw; | ||
| 398 | } | 398 | } | |||||
| 399 | 399 | |||||||
| 400 | /** Initiate an asynchronous accept operation, returning the peer. | 400 | /** Initiate an asynchronous accept operation, returning the peer. | |||||
| 401 | 401 | |||||||
| 402 | Accepts an incoming connection and returns a newly constructed | 402 | Accepts an incoming connection and returns a newly constructed | |||||
| 403 | socket for it, associated with this acceptor's execution context. | 403 | socket for it, associated with this acceptor's execution context. | |||||
| 404 | The acceptor must be listening before calling this function. | 404 | The acceptor must be listening before calling this function. | |||||
| 405 | 405 | |||||||
| 406 | The caller does not pre-construct the peer socket. The returned | 406 | The caller does not pre-construct the peer socket. The returned | |||||
| 407 | socket shares this acceptor's execution context. | 407 | socket shares this acceptor's execution context. | |||||
| 408 | 408 | |||||||
| 409 | The operation supports cancellation via `std::stop_token` through | 409 | The operation supports cancellation via `std::stop_token` through | |||||
| 410 | the affine awaitable protocol. If the associated stop token is | 410 | the affine awaitable protocol. If the associated stop token is | |||||
| 411 | triggered, the operation completes immediately with | 411 | triggered, the operation completes immediately with | |||||
| 412 | `errc::operation_canceled`. | 412 | `errc::operation_canceled`. | |||||
| 413 | 413 | |||||||
| 414 | @return An awaitable that completes with `results<tcp_socket>`. | 414 | @return An awaitable that completes with `results<tcp_socket>`. | |||||
| 415 | On success the payload is the connected peer socket; on failure | 415 | On success the payload is the connected peer socket; on failure | |||||
| 416 | (including cancellation) the error code is set and the payload | 416 | (including cancellation) the error code is set and the payload | |||||
| 417 | socket is unconnected. Errors include: | 417 | socket is unconnected. Errors include: | |||||
| 418 | - `operation_canceled`: Cancelled via stop_token or cancel(). | 418 | - `operation_canceled`: Cancelled via stop_token or cancel(). | |||||
| 419 | Check `ec == cond::canceled` for portable comparison. | 419 | Check `ec == cond::canceled` for portable comparison. | |||||
| 420 | 420 | |||||||
| 421 | A closed acceptor completes with `errc::bad_file_descriptor`. | 421 | A closed acceptor completes with `errc::bad_file_descriptor`. | |||||
| 422 | On failure the returned socket is default-constructed and not | 422 | On failure the returned socket is default-constructed and not | |||||
| 423 | open. It has no context, so it may only be destroyed, assigned, | 423 | open. It has no context, so it may only be destroyed, assigned, | |||||
| 424 | or checked with `is_open()`. | 424 | or checked with `is_open()`. | |||||
| 425 | 425 | |||||||
| 426 | @pre This acceptor must outlive the returned awaitable. | 426 | @pre This acceptor must outlive the returned awaitable. | |||||
| 427 | 427 | |||||||
| 428 | @par Example | 428 | @par Example | |||||
| 429 | @par !example accept_returning_a_new_socket | 429 | @par !example accept_returning_a_new_socket | |||||
| 430 | 430 | |||||||
| 431 | @see @ref accept(tcp_socket&) | 431 | @see @ref accept(tcp_socket&) | |||||
| 432 | */ | 432 | */ | |||||
| HITCBC | 433 | 41 | [[nodiscard]] auto accept() | 433 | 41 | [[nodiscard]] auto accept() | ||
| 434 | { | 434 | { | |||||
| HITCBC | 435 | 41 | accept_value_awaitable aw(*this); | 435 | 41 | accept_value_awaitable aw(*this); | ||
| HITCBC | 436 | 41 | if (!is_open()) | 436 | 41 | if (!is_open()) | ||
| HITCBC | 437 | 4 | aw.ec_ = make_error_code(std::errc::bad_file_descriptor); | 437 | 4 | aw.ec_ = make_error_code(std::errc::bad_file_descriptor); | ||
| HITCBC | 438 | 41 | return aw; | 438 | 41 | return aw; | ||
| 439 | } | 439 | } | |||||
| 440 | 440 | |||||||
| 441 | /** Wait for an incoming connection or readiness condition. | 441 | /** Wait for an incoming connection or readiness condition. | |||||
| 442 | 442 | |||||||
| 443 | Suspends until the listen socket is ready in the | 443 | Suspends until the listen socket is ready in the | |||||
| 444 | requested direction, or an error condition is reported. | 444 | requested direction, or an error condition is reported. | |||||
| 445 | For `wait_type::read`, completion signals that a | 445 | For `wait_type::read`, completion signals that a | |||||
| 446 | subsequent @ref accept succeeds without blocking. A | 446 | subsequent @ref accept succeeds without blocking. A | |||||
| 447 | connection already queued when the wait begins completes | 447 | connection already queued when the wait begins completes | |||||
| 448 | it immediately. No connection is consumed. | 448 | it immediately. No connection is consumed. | |||||
| 449 | 449 | |||||||
| 450 | @note `wait_type::write` is not usable on an acceptor: | 450 | @note `wait_type::write` is not usable on an acceptor: | |||||
| 451 | writability carries no meaning for a listening socket, so | 451 | writability carries no meaning for a listening socket, so | |||||
| 452 | the wait fails with `errc::operation_not_supported` on | 452 | the wait fails with `errc::operation_not_supported` on | |||||
| 453 | every backend. | 453 | every backend. | |||||
| 454 | 454 | |||||||
| 455 | @param w The wait direction. | 455 | @param w The wait direction. | |||||
| 456 | 456 | |||||||
| 457 | @return An awaitable that completes with `results<>`. | 457 | @return An awaitable that completes with `results<>`. | |||||
| 458 | 458 | |||||||
| 459 | A closed acceptor completes with `errc::bad_file_descriptor`. | 459 | A closed acceptor completes with `errc::bad_file_descriptor`. | |||||
| 460 | 460 | |||||||
| 461 | @pre This acceptor must outlive the returned awaitable. | 461 | @pre This acceptor must outlive the returned awaitable. | |||||
| 462 | */ | 462 | */ | |||||
| HITCBC | 463 | 31 | [[nodiscard]] auto wait(wait_type w) | 463 | 31 | [[nodiscard]] auto wait(wait_type w) | ||
| 464 | { | 464 | { | |||||
| HITCBC | 465 | 31 | wait_awaitable aw(*this, w); | 465 | 31 | wait_awaitable aw(*this, w); | ||
| HITCBC | 466 | 31 | if (!is_open()) | 466 | 31 | if (!is_open()) | ||
| HITCBC | 467 | 2 | aw.ec_ = make_error_code(std::errc::bad_file_descriptor); | 467 | 2 | aw.ec_ = make_error_code(std::errc::bad_file_descriptor); | ||
| HITCBC | 468 | 31 | return aw; | 468 | 31 | return aw; | ||
| 469 | } | 469 | } | |||||
| 470 | 470 | |||||||
| 471 | /** Cancel any pending asynchronous operations. | 471 | /** Cancel any pending asynchronous operations. | |||||
| 472 | 472 | |||||||
| 473 | Accept and wait transfer no bytes, so a cancellation always wins: | 473 | Accept and wait transfer no bytes, so a cancellation always wins: | |||||
| 474 | an operation reports `errc::operation_canceled` even when it had | 474 | an operation reports `errc::operation_canceled` even when it had | |||||
| 475 | already succeeded when the cancellation landed. Check | 475 | already succeeded when the cancellation landed. Check | |||||
| 476 | `ec == cond::canceled` for portable comparison. | 476 | `ec == cond::canceled` for portable comparison. | |||||
| 477 | */ | 477 | */ | |||||
| 478 | void cancel() noexcept; | 478 | void cancel() noexcept; | |||||
| 479 | 479 | |||||||
| 480 | /** Get the native socket handle. | 480 | /** Get the native socket handle. | |||||
| 481 | 481 | |||||||
| 482 | Returns the underlying platform-specific socket descriptor. | 482 | Returns the underlying platform-specific socket descriptor. | |||||
| 483 | On POSIX systems this is an `int` file descriptor. | 483 | On POSIX systems this is an `int` file descriptor. | |||||
| 484 | On Windows this is a `SOCKET` handle. | 484 | On Windows this is a `SOCKET` handle. | |||||
| 485 | 485 | |||||||
| 486 | @return The native socket handle, or -1/INVALID_SOCKET if not open. | 486 | @return The native socket handle, or -1/INVALID_SOCKET if not open. | |||||
| 487 | 487 | |||||||
| 488 | @pre None. May be called on closed acceptors. | 488 | @pre None. May be called on closed acceptors. | |||||
| 489 | */ | 489 | */ | |||||
| 490 | native_handle_type native_handle() const noexcept; | 490 | native_handle_type native_handle() const noexcept; | |||||
| 491 | 491 | |||||||
| 492 | /** Assign an existing native socket to this acceptor. | 492 | /** Assign an existing native socket to this acceptor. | |||||
| 493 | 493 | |||||||
| 494 | Adopts a listening socket created outside the library. The | 494 | Adopts a listening socket created outside the library. The | |||||
| 495 | socket may come from a service manager, be inherited, or be | 495 | socket may come from a service manager, be inherited, or be | |||||
| 496 | created natively. Adoption registers the socket with the | 496 | created natively. Adoption registers the socket with the | |||||
| 497 | backend. The socket must be a listening stream socket in the | 497 | backend. The socket must be a listening stream socket in the | |||||
| 498 | `AF_INET` or `AF_INET6` family. | 498 | `AF_INET` or `AF_INET6` family. | |||||
| 499 | Adoption never alters the descriptor's flags or options: on | 499 | Adoption never alters the descriptor's flags or options: on | |||||
| 500 | POSIX the fd must already be non-blocking, and on Windows the | 500 | POSIX the fd must already be non-blocking, and on Windows the | |||||
| 501 | socket must be overlapped-capable. | 501 | socket must be overlapped-capable. | |||||
| 502 | 502 | |||||||
| 503 | Adoption does not verify listen state; @ref accept reports the | 503 | Adoption does not verify listen state; @ref accept reports the | |||||
| 504 | error if the socket is not listening. | 504 | error if the socket is not listening. | |||||
| 505 | 505 | |||||||
| 506 | The object must be closed. To replace a held socket, `close()` | 506 | The object must be closed. To replace a held socket, `close()` | |||||
| 507 | or `release()` it first. | 507 | or `release()` it first. | |||||
| 508 | 508 | |||||||
| 509 | @par Exception Safety | 509 | @par Exception Safety | |||||
| 510 | Throws nothing. On failure the object is unchanged and the | 510 | Throws nothing. On failure the object is unchanged and the | |||||
| 511 | caller retains ownership of `fd`. | 511 | caller retains ownership of `fd`. | |||||
| 512 | 512 | |||||||
| 513 | @param fd The native socket to adopt. On success the object | 513 | @param fd The native socket to adopt. On success the object | |||||
| 514 | owns it and closes it. | 514 | owns it and closes it. | |||||
| 515 | 515 | |||||||
| 516 | @return `error::already_open` if this object is open. | 516 | @return `error::already_open` if this object is open. | |||||
| 517 | Otherwise the error code, empty on success. Validation and | 517 | Otherwise the error code, empty on success. Validation and | |||||
| 518 | registration failures are normal runtime conditions when | 518 | registration failures are normal runtime conditions when | |||||
| 519 | adopting foreign descriptors. | 519 | adopting foreign descriptors. | |||||
| 520 | */ | 520 | */ | |||||
| 521 | [[nodiscard]] std::error_code assign(native_handle_type fd) noexcept; | 521 | [[nodiscard]] std::error_code assign(native_handle_type fd) noexcept; | |||||
| 522 | 522 | |||||||
| 523 | /** Release ownership of the native socket handle. | 523 | /** Release ownership of the native socket handle. | |||||
| 524 | 524 | |||||||
| 525 | Deregisters the socket from the backend and cancels pending | 525 | Deregisters the socket from the backend and cancels pending | |||||
| 526 | operations without closing the descriptor. The caller takes | 526 | operations without closing the descriptor. The caller takes | |||||
| 527 | ownership of the returned handle. | 527 | ownership of the returned handle. | |||||
| 528 | 528 | |||||||
| 529 | A closed object reports `errc::bad_file_descriptor` and the | 529 | A closed object reports `errc::bad_file_descriptor` and the | |||||
| 530 | returned handle is the invalid sentinel ( -1, or | 530 | returned handle is the invalid sentinel ( -1, or | |||||
| 531 | `INVALID_SOCKET` on Windows ), never zero. | 531 | `INVALID_SOCKET` on Windows ), never zero. | |||||
| 532 | 532 | |||||||
| 533 | @note With the IOCP backend, a socket released while operations | 533 | @note With the IOCP backend, a socket released while operations | |||||
| 534 | on it are in flight stays bound to this context's completion | 534 | on it are in flight stays bound to this context's completion | |||||
| 535 | port for good. Windows refuses to unbind a handle with I/O | 535 | port for good. Windows refuses to unbind a handle with I/O | |||||
| 536 | outstanding, so no context, this one included, can assign it | 536 | outstanding, so no context, this one included, can assign it | |||||
| 537 | afterwards. Its cancelled operations still complete normally. | 537 | afterwards. Its cancelled operations still complete normally. | |||||
| 538 | 538 | |||||||
| 539 | @return The error code and the native handle. | 539 | @return The error code and the native handle. | |||||
| 540 | 540 | |||||||
| 541 | @post is_open() == false | 541 | @post is_open() == false | |||||
| 542 | */ | 542 | */ | |||||
| 543 | [[nodiscard]] capy::results<native_handle_type> release() noexcept; | 543 | [[nodiscard]] capy::results<native_handle_type> release() noexcept; | |||||
| 544 | 544 | |||||||
| 545 | /** Get the local endpoint of the acceptor. | 545 | /** Get the local endpoint of the acceptor. | |||||
| 546 | 546 | |||||||
| 547 | Returns the local address and port to which the acceptor is bound. | 547 | Returns the local address and port to which the acceptor is bound. | |||||
| 548 | This is useful when binding to port 0 (ephemeral port) to discover | 548 | This is useful when binding to port 0 (ephemeral port) to discover | |||||
| 549 | the OS-assigned port number. The endpoint is cached when bind() | 549 | the OS-assigned port number. The endpoint is cached when bind() | |||||
| 550 | is called. | 550 | is called. | |||||
| 551 | 551 | |||||||
| 552 | @return The local endpoint, or a default endpoint (0.0.0.0:0) if | 552 | @return The local endpoint, or a default endpoint (0.0.0.0:0) if | |||||
| 553 | the acceptor is not open. | 553 | the acceptor is not open. | |||||
| 554 | 554 | |||||||
| 555 | @par Thread Safety | 555 | @par Thread Safety | |||||
| 556 | The cached endpoint value is set during bind() and cleared | 556 | The cached endpoint value is set during bind() and cleared | |||||
| 557 | during close(). This function may be called concurrently with | 557 | during close(). This function may be called concurrently with | |||||
| 558 | accept operations, but must not be called concurrently with | 558 | accept operations, but must not be called concurrently with | |||||
| 559 | bind() or close(). | 559 | bind() or close(). | |||||
| 560 | */ | 560 | */ | |||||
| 561 | endpoint local_endpoint() const noexcept; | 561 | endpoint local_endpoint() const noexcept; | |||||
| 562 | 562 | |||||||
| 563 | /** Set a socket option on the acceptor. | 563 | /** Set a socket option on the acceptor. | |||||
| 564 | 564 | |||||||
| 565 | Applies a type-safe socket option to the underlying listening | 565 | Applies a type-safe socket option to the underlying listening | |||||
| 566 | socket. The socket must be open (via `open()` or `listen()`). | 566 | socket. The socket must be open (via `open()` or `listen()`). | |||||
| 567 | This is useful for setting options between `open()` and | 567 | This is useful for setting options between `open()` and | |||||
| 568 | `listen()`, such as `socket_option::reuse_port`. | 568 | `listen()`, such as `socket_option::reuse_port`. | |||||
| 569 | 569 | |||||||
| 570 | Fails with `errc::bad_file_descriptor` if the acceptor is not | 570 | Fails with `errc::bad_file_descriptor` if the acceptor is not | |||||
| 571 | open, or with the error the operating system reports. | 571 | open, or with the error the operating system reports. | |||||
| 572 | 572 | |||||||
| 573 | @par Example | 573 | @par Example | |||||
| 574 | @par !example set_option | 574 | @par !example set_option | |||||
| 575 | 575 | |||||||
| 576 | @param opt The option to set. | 576 | @param opt The option to set. | |||||
| 577 | 577 | |||||||
| 578 | @return The error code, empty on success. | 578 | @return The error code, empty on success. | |||||
| 579 | */ | 579 | */ | |||||
| 580 | template<class Option> | 580 | template<class Option> | |||||
| HITCBC | 581 | 1430 | [[nodiscard]] std::error_code set_option(Option const& opt) noexcept | 581 | 1430 | [[nodiscard]] std::error_code set_option(Option const& opt) noexcept | ||
| 582 | { | 582 | { | |||||
| HITCBC | 583 | 1430 | if (!is_open()) | 583 | 1430 | if (!is_open()) | ||
| HITCBC | 584 | 2 | return make_error_code(std::errc::bad_file_descriptor); | 584 | 2 | return make_error_code(std::errc::bad_file_descriptor); | ||
| HITCBC | 585 | 1428 | auto const fam = get().family(); | 585 | 1428 | auto const fam = get().family(); | ||
| HITCBC | 586 | 1428 | return get().set_option( | 586 | 1428 | return get().set_option( | ||
| HITCBC | 587 | 1428 | opt.level(fam), opt.name(fam), opt.data(fam), opt.size(fam)); | 587 | 1428 | opt.level(fam), opt.name(fam), opt.data(fam), opt.size(fam)); | ||
| 588 | } | 588 | } | |||||
| 589 | 589 | |||||||
| 590 | /** Get a socket option from the acceptor. | 590 | /** Get a socket option from the acceptor. | |||||
| 591 | 591 | |||||||
| 592 | Retrieves the current value of a type-safe socket option. | 592 | Retrieves the current value of a type-safe socket option. | |||||
| 593 | 593 | |||||||
| 594 | Fails with `errc::bad_file_descriptor` if the acceptor is not | 594 | Fails with `errc::bad_file_descriptor` if the acceptor is not | |||||
| 595 | open, or with the error the operating system reports. On | 595 | open, or with the error the operating system reports. On | |||||
| 596 | failure the option is value-initialized. | 596 | failure the option is value-initialized. | |||||
| 597 | 597 | |||||||
| 598 | @par Example | 598 | @par Example | |||||
| 599 | @par !example get_option | 599 | @par !example get_option | |||||
| 600 | 600 | |||||||
| 601 | @return The error code and the option value. | 601 | @return The error code and the option value. | |||||
| 602 | */ | 602 | */ | |||||
| 603 | template<class Option> | 603 | template<class Option> | |||||
| HITCBC | 604 | 23 | [[nodiscard]] capy::results<Option> get_option() const noexcept | 604 | 23 | [[nodiscard]] capy::results<Option> get_option() const noexcept | ||
| 605 | { | 605 | { | |||||
| HITCBC | 606 | 23 | if (!is_open()) | 606 | 23 | if (!is_open()) | ||
| HITCBC | 607 | 2 | return {make_error_code(std::errc::bad_file_descriptor), Option{}}; | 607 | 2 | return {make_error_code(std::errc::bad_file_descriptor), Option{}}; | ||
| HITCBC | 608 | 21 | Option opt{}; | 608 | 21 | Option opt{}; | ||
| HITCBC | 609 | 21 | auto const fam = get().family(); | 609 | 21 | auto const fam = get().family(); | ||
| HITCBC | 610 | 21 | std::size_t sz = opt.size(fam); | 610 | 21 | std::size_t sz = opt.size(fam); | ||
| HITCBC | 611 | 21 | if (auto ec = get().get_option( | 611 | 21 | if (auto ec = get().get_option( | ||
| 612 | opt.level(fam), opt.name(fam), opt.data(fam), &sz)) | 612 | opt.level(fam), opt.name(fam), opt.data(fam), &sz)) | |||||
| HITCBC | 613 | 8 | return {ec, Option{}}; | 613 | 8 | return {ec, Option{}}; | ||
| HITCBC | 614 | 13 | opt.resize(fam, sz); | 614 | 13 | opt.resize(fam, sz); | ||
| HITCBC | 615 | 13 | return {std::error_code{}, std::move(opt)}; | 615 | 13 | return {std::error_code{}, std::move(opt)}; | ||
| 616 | } | 616 | } | |||||
| 617 | 617 | |||||||
| 618 | /** Define backend hooks for TCP acceptor operations. | 618 | /** Define backend hooks for TCP acceptor operations. | |||||
| 619 | 619 | |||||||
| 620 | Platform backends derive from this to implement | 620 | Platform backends derive from this to implement | |||||
| 621 | accept, endpoint query, open-state checks, cancellation, | 621 | accept, endpoint query, open-state checks, cancellation, | |||||
| 622 | and socket-option management. | 622 | and socket-option management. | |||||
| 623 | */ | 623 | */ | |||||
| 624 | struct implementation : io_object::implementation | 624 | struct implementation : io_object::implementation | |||||
| 625 | { | 625 | { | |||||
| 626 | /** Initiate an asynchronous accept operation. | 626 | /** Initiate an asynchronous accept operation. | |||||
| 627 | 627 | |||||||
| 628 | @param cont Continuation to resume on completion; it lives in | 628 | @param cont Continuation to resume on completion; it lives in | |||||
| 629 | the awaiting frame until then. | 629 | the awaiting frame until then. | |||||
| 630 | @param ex Executor for dispatching the completion. | 630 | @param ex Executor for dispatching the completion. | |||||
| 631 | @param token Stop token for cancellation. | 631 | @param token Stop token for cancellation. | |||||
| 632 | @param ec Output error code. | 632 | @param ec Output error code. | |||||
| 633 | @param impl_out Output implementation for the accepted peer. | 633 | @param impl_out Output implementation for the accepted peer. | |||||
| 634 | 634 | |||||||
| 635 | @return Coroutine handle to resume immediately. | 635 | @return Coroutine handle to resume immediately. | |||||
| 636 | */ | 636 | */ | |||||
| 637 | virtual std::coroutine_handle<> accept( | 637 | virtual std::coroutine_handle<> accept( | |||||
| 638 | capy::continuation& cont, | 638 | capy::continuation& cont, | |||||
| 639 | capy::executor_ref ex, | 639 | capy::executor_ref ex, | |||||
| 640 | std::stop_token token, | 640 | std::stop_token token, | |||||
| 641 | std::error_code* ec, | 641 | std::error_code* ec, | |||||
| 642 | io_object::implementation** impl_out) = 0; | 642 | io_object::implementation** impl_out) = 0; | |||||
| 643 | 643 | |||||||
| 644 | /** Initiate an asynchronous wait for acceptor readiness. | 644 | /** Initiate an asynchronous wait for acceptor readiness. | |||||
| 645 | 645 | |||||||
| 646 | Completes when the listen socket becomes ready for | 646 | Completes when the listen socket becomes ready for | |||||
| 647 | the specified direction (typically `wait_type::read` | 647 | the specified direction (typically `wait_type::read` | |||||
| 648 | for an incoming connection), or an error condition is | 648 | for an incoming connection), or an error condition is | |||||
| 649 | reported. No connection is consumed. | 649 | reported. No connection is consumed. | |||||
| 650 | 650 | |||||||
| 651 | @param cont Continuation to resume on completion; it lives in | 651 | @param cont Continuation to resume on completion; it lives in | |||||
| 652 | the awaiting frame until then. | 652 | the awaiting frame until then. | |||||
| 653 | @param ex Executor for dispatching the completion. | 653 | @param ex Executor for dispatching the completion. | |||||
| 654 | @param w The direction to wait on. | 654 | @param w The direction to wait on. | |||||
| 655 | @param token Stop token for cancellation. | 655 | @param token Stop token for cancellation. | |||||
| 656 | @param ec Output error code. | 656 | @param ec Output error code. | |||||
| 657 | 657 | |||||||
| 658 | @return Coroutine handle to resume immediately. | 658 | @return Coroutine handle to resume immediately. | |||||
| 659 | */ | 659 | */ | |||||
| 660 | virtual std::coroutine_handle<> wait( | 660 | virtual std::coroutine_handle<> wait( | |||||
| 661 | capy::continuation& cont, | 661 | capy::continuation& cont, | |||||
| 662 | capy::executor_ref ex, | 662 | capy::executor_ref ex, | |||||
| 663 | wait_type w, | 663 | wait_type w, | |||||
| 664 | std::stop_token token, | 664 | std::stop_token token, | |||||
| 665 | std::error_code* ec) = 0; | 665 | std::error_code* ec) = 0; | |||||
| 666 | 666 | |||||||
| 667 | /** Returns the cached local endpoint. | 667 | /** Returns the cached local endpoint. | |||||
| 668 | 668 | |||||||
| 669 | @return The cached local endpoint. | 669 | @return The cached local endpoint. | |||||
| 670 | */ | 670 | */ | |||||
| 671 | virtual endpoint local_endpoint() const noexcept = 0; | 671 | virtual endpoint local_endpoint() const noexcept = 0; | |||||
| 672 | 672 | |||||||
| 673 | /** Return true if the acceptor has a kernel resource open. | 673 | /** Return true if the acceptor has a kernel resource open. | |||||
| 674 | 674 | |||||||
| 675 | @return true if the acceptor has a kernel resource open. | 675 | @return true if the acceptor has a kernel resource open. | |||||
| 676 | */ | 676 | */ | |||||
| 677 | virtual bool is_open() const noexcept = 0; | 677 | virtual bool is_open() const noexcept = 0; | |||||
| 678 | 678 | |||||||
| 679 | /** Return the native handle, or the platform sentinel if closed. | 679 | /** Return the native handle, or the platform sentinel if closed. | |||||
| 680 | 680 | |||||||
| 681 | @return The native handle, or the platform sentinel if closed. | 681 | @return The native handle, or the platform sentinel if closed. | |||||
| 682 | */ | 682 | */ | |||||
| 683 | virtual native_handle_type native_handle() const noexcept = 0; | 683 | virtual native_handle_type native_handle() const noexcept = 0; | |||||
| 684 | 684 | |||||||
| 685 | /** Return the socket's address family. | 685 | /** Return the socket's address family. | |||||
| 686 | 686 | |||||||
| 687 | Socket options render for this family. | 687 | Socket options render for this family. | |||||
| 688 | 688 | |||||||
| 689 | @return The socket's address family. | 689 | @return The socket's address family. | |||||
| 690 | */ | 690 | */ | |||||
| 691 | virtual corosio::family family() const noexcept = 0; | 691 | virtual corosio::family family() const noexcept = 0; | |||||
| 692 | 692 | |||||||
| 693 | /** Release and return the native handle without closing. | 693 | /** Release and return the native handle without closing. | |||||
| 694 | 694 | |||||||
| 695 | @return The native handle. | 695 | @return The native handle. | |||||
| 696 | */ | 696 | */ | |||||
| 697 | virtual native_handle_type release_socket() noexcept = 0; | 697 | virtual native_handle_type release_socket() noexcept = 0; | |||||
| 698 | 698 | |||||||
| 699 | /** Cancel any pending asynchronous operations. | 699 | /** Cancel any pending asynchronous operations. | |||||
| 700 | 700 | |||||||
| 701 | Accept and wait transfer no bytes, so a cancellation always | 701 | Accept and wait transfer no bytes, so a cancellation always | |||||
| 702 | wins: an operation reports `operation_canceled` even when it | 702 | wins: an operation reports `operation_canceled` even when it | |||||
| 703 | had already succeeded when the cancellation landed. | 703 | had already succeeded when the cancellation landed. | |||||
| 704 | */ | 704 | */ | |||||
| 705 | virtual void cancel() noexcept = 0; | 705 | virtual void cancel() noexcept = 0; | |||||
| 706 | 706 | |||||||
| 707 | /** Set a socket option. | 707 | /** Set a socket option. | |||||
| 708 | 708 | |||||||
| 709 | @param level The protocol level. | 709 | @param level The protocol level. | |||||
| 710 | @param optname The option name. | 710 | @param optname The option name. | |||||
| 711 | @param data Pointer to the option value. | 711 | @param data Pointer to the option value. | |||||
| 712 | @param size Size of the option value in bytes. | 712 | @param size Size of the option value in bytes. | |||||
| 713 | @return Error code on failure, empty on success. | 713 | @return Error code on failure, empty on success. | |||||
| 714 | */ | 714 | */ | |||||
| 715 | virtual std::error_code set_option( | 715 | virtual std::error_code set_option( | |||||
| 716 | int level, | 716 | int level, | |||||
| 717 | int optname, | 717 | int optname, | |||||
| 718 | void const* data, | 718 | void const* data, | |||||
| 719 | std::size_t size) noexcept = 0; | 719 | std::size_t size) noexcept = 0; | |||||
| 720 | 720 | |||||||
| 721 | /** Get a socket option. | 721 | /** Get a socket option. | |||||
| 722 | 722 | |||||||
| 723 | @param level The protocol level. | 723 | @param level The protocol level. | |||||
| 724 | @param optname The option name. | 724 | @param optname The option name. | |||||
| 725 | @param data Pointer to receive the option value. | 725 | @param data Pointer to receive the option value. | |||||
| 726 | @param size On entry, the size of the buffer. On exit, | 726 | @param size On entry, the size of the buffer. On exit, | |||||
| 727 | the size of the option value. | 727 | the size of the option value. | |||||
| 728 | @return Error code on failure, empty on success. | 728 | @return Error code on failure, empty on success. | |||||
| 729 | */ | 729 | */ | |||||
| 730 | virtual std::error_code | 730 | virtual std::error_code | |||||
| 731 | get_option(int level, int optname, void* data, std::size_t* size) | 731 | get_option(int level, int optname, void* data, std::size_t* size) | |||||
| 732 | const noexcept = 0; | 732 | const noexcept = 0; | |||||
| 733 | }; | 733 | }; | |||||
| 734 | 734 | |||||||
| 735 | protected: | 735 | protected: | |||||
| 736 | /** Adopt an existing handle. | 736 | /** Adopt an existing handle. | |||||
| 737 | 737 | |||||||
| 738 | @param h The handle the acceptor takes ownership of. | 738 | @param h The handle the acceptor takes ownership of. | |||||
| 739 | */ | 739 | */ | |||||
| HITCBC | 740 | 47 | explicit tcp_acceptor(handle h) noexcept : io_object(std::move(h)) {} | 740 | 47 | explicit tcp_acceptor(handle h) noexcept : io_object(std::move(h)) {} | ||
| 741 | 741 | |||||||
| 742 | /** Adopt an existing handle, then open, configure, bind, and listen. | 742 | /** Adopt an existing handle, then open, configure, bind, and listen. | |||||
| 743 | 743 | |||||||
| 744 | Lets a derived acceptor offer the convenience constructor while | 744 | Lets a derived acceptor offer the convenience constructor while | |||||
| 745 | supplying its own service. | 745 | supplying its own service. | |||||
| 746 | 746 | |||||||
| 747 | @param h The handle the acceptor takes ownership of. | 747 | @param h The handle the acceptor takes ownership of. | |||||
| 748 | @param ep The local endpoint to bind to. | 748 | @param ep The local endpoint to bind to. | |||||
| 749 | @param backlog The maximum pending connection queue length. | 749 | @param backlog The maximum pending connection queue length. | |||||
| 750 | 750 | |||||||
| 751 | @throws std::system_error on open, configuration, bind, or | 751 | @throws std::system_error on open, configuration, bind, or | |||||
| 752 | listen failure. | 752 | listen failure. | |||||
| 753 | */ | 753 | */ | |||||
| 754 | tcp_acceptor(handle h, endpoint ep, int backlog); | 754 | tcp_acceptor(handle h, endpoint ep, int backlog); | |||||
| 755 | 755 | |||||||
| 756 | /** Transfer the accepted peer implementation to the peer socket. | 756 | /** Transfer the accepted peer implementation to the peer socket. | |||||
| 757 | 757 | |||||||
| 758 | @param peer The socket that receives the transferred implementation. | 758 | @param peer The socket that receives the transferred implementation. | |||||
| 759 | @param impl The accepted peer implementation, or null to do nothing. | 759 | @param impl The accepted peer implementation, or null to do nothing. | |||||
| 760 | */ | 760 | */ | |||||
| 761 | static void | 761 | static void | |||||
| HITCBC | 762 | 15 | reset_peer_impl(tcp_socket& peer, io_object::implementation* impl) noexcept | 762 | 15 | reset_peer_impl(tcp_socket& peer, io_object::implementation* impl) noexcept | ||
| 763 | { | 763 | { | |||||
| HITCBC | 764 | 15 | if (impl) | 764 | 15 | if (impl) | ||
| HITCBC | 765 | 15 | peer.h_.reset(impl); | 765 | 15 | peer.h_.reset(impl); | ||
| HITCBC | 766 | 15 | } | 766 | 15 | } | ||
| 767 | 767 | |||||||
| 768 | /** Return the result of an accept that yields the peer. | 768 | /** Return the result of an accept that yields the peer. | |||||
| 769 | 769 | |||||||
| 770 | The peer socket is built only on success, so a failed accept | 770 | The peer socket is built only on success, so a failed accept | |||||
| 771 | never touches the acceptor's context, which a moved-from | 771 | never touches the acceptor's context, which a moved-from | |||||
| 772 | acceptor lacks. On failure the peer is default-constructed. | 772 | acceptor lacks. On failure the peer is default-constructed. | |||||
| 773 | 773 | |||||||
| 774 | @param acc The acceptor that performed the accept. | 774 | @param acc The acceptor that performed the accept. | |||||
| 775 | @param ec The completion code. | 775 | @param ec The completion code. | |||||
| 776 | @param impl The accepted peer implementation; taken on success. | 776 | @param impl The accepted peer implementation; taken on success. | |||||
| 777 | 777 | |||||||
| 778 | @return The completion code and the peer. | 778 | @return The completion code and the peer. | |||||
| 779 | */ | 779 | */ | |||||
| HITCBC | 780 | 39 | static capy::results<tcp_socket> take_peer( | 780 | 39 | static capy::results<tcp_socket> take_peer( | ||
| 781 | tcp_acceptor& acc, | 781 | tcp_acceptor& acc, | |||||
| 782 | std::error_code ec, | 782 | std::error_code ec, | |||||
| 783 | io_object::implementation*& impl) noexcept | 783 | io_object::implementation*& impl) noexcept | |||||
| 784 | { | 784 | { | |||||
| HITCBC | 785 | 39 | if (ec || !impl) | 785 | 39 | if (ec || !impl) | ||
| HITCBC | 786 | 12 | return {ec, tcp_socket()}; | 786 | 12 | return {ec, tcp_socket()}; | ||
| 787 | 787 | |||||||
| HITCBC | 788 | 27 | tcp_socket peer(acc.context()); | 788 | 27 | tcp_socket peer(acc.context()); | ||
| HITCBC | 789 | 27 | peer.h_.reset(std::exchange(impl, nullptr)); | 789 | 27 | peer.h_.reset(std::exchange(impl, nullptr)); | ||
| HITCBC | 790 | 27 | return {ec, std::move(peer)}; | 790 | 27 | return {ec, std::move(peer)}; | ||
| HITCBC | 791 | 27 | } | 791 | 27 | } | ||
| 792 | 792 | |||||||
| 793 | /// Close and release a non-null accepted peer no socket took over. | 793 | /// Close and release a non-null accepted peer no socket took over. | |||||
| 794 | static void | 794 | static void | |||||
| 795 | discard_peer(tcp_acceptor& acc, io_object::implementation* impl) noexcept; | 795 | discard_peer(tcp_acceptor& acc, io_object::implementation* impl) noexcept; | |||||
| 796 | 796 | |||||||
| 797 | private: | 797 | private: | |||||
| HITCBC | 798 | 32374 | inline implementation& get() const noexcept | 798 | 32751 | inline implementation& get() const noexcept | ||
| 799 | { | 799 | { | |||||
| HITCBC | 800 | 32374 | return *static_cast<implementation*>(h_.get()); | 800 | 32751 | return *static_cast<implementation*>(h_.get()); | ||
| 801 | } | 801 | } | |||||
| 802 | }; | 802 | }; | |||||
| 803 | 803 | |||||||
| 804 | } // namespace boost::corosio | 804 | } // namespace boost::corosio | |||||
| 805 | 805 | |||||||
| 806 | #endif | 806 | #endif | |||||