TLA Line data Source code
1 : //
2 : // Copyright (c) 2026 Steve Gerbino
3 : //
4 : // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 : // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 : //
7 : // Official repository: https://github.com/cppalliance/corosio
8 : //
9 :
10 : #ifndef BOOST_COROSIO_NATIVE_NATIVE_TCP_ACCEPTOR_HPP
11 : #define BOOST_COROSIO_NATIVE_NATIVE_TCP_ACCEPTOR_HPP
12 :
13 : #include <boost/corosio/tcp_acceptor.hpp>
14 : #include <boost/corosio/backend.hpp>
15 :
16 : #ifndef BOOST_COROSIO_MRDOCS
17 : #if BOOST_COROSIO_HAS_IOCP
18 : #include <boost/corosio/native/detail/iocp/win_tcp_acceptor_service.hpp>
19 : #endif
20 : #endif // !BOOST_COROSIO_MRDOCS
21 :
22 : namespace boost::corosio {
23 :
24 : /** An asynchronous TCP acceptor with devirtualized accept operations.
25 :
26 : This class template inherits from @ref tcp_acceptor and shadows
27 : the `accept` operation with a version that calls the backend
28 : implementation directly, allowing the compiler to inline through
29 : the entire call chain.
30 :
31 : Non-async operations (`listen`, `close`, `cancel`) remain
32 : unchanged and dispatch through the compiled library.
33 :
34 : A `native_tcp_acceptor` IS-A `tcp_acceptor` and can be passed
35 : to any function expecting `tcp_acceptor&`.
36 :
37 : @tparam Backend A backend tag value (e.g., `epoll`).
38 :
39 : @par Thread Safety
40 : Same as @ref tcp_acceptor.
41 :
42 : @see tcp_acceptor, epoll_t, iocp_t
43 : */
44 : template<auto Backend>
45 : class native_tcp_acceptor : public tcp_acceptor
46 : {
47 : using backend_type = decltype(Backend);
48 : using impl_type = typename backend_type::tcp_acceptor_type;
49 : using service_type = typename backend_type::tcp_acceptor_service_type;
50 :
51 HIT 4 : impl_type& get_impl() noexcept
52 : {
53 4 : return *static_cast<impl_type*>(h_.get());
54 : }
55 :
56 : struct native_accept_awaitable
57 : {
58 : native_tcp_acceptor& acc_;
59 : tcp_socket& peer_;
60 : std::stop_token token_;
61 : mutable std::error_code ec_;
62 : mutable io_object::implementation* peer_impl_ = nullptr;
63 :
64 4 : native_accept_awaitable(
65 : native_tcp_acceptor& acc, tcp_socket& peer) noexcept
66 4 : : acc_(acc)
67 4 : , peer_(peer)
68 : {
69 4 : }
70 :
71 4 : bool await_ready() const noexcept
72 : {
73 4 : return token_.stop_requested();
74 : }
75 :
76 4 : capy::io_result<> await_resume() const noexcept
77 : {
78 4 : if (token_.stop_requested())
79 MIS 0 : return {make_error_code(std::errc::operation_canceled)};
80 HIT 4 : if (!ec_)
81 4 : acc_.reset_peer_impl(peer_, peer_impl_);
82 4 : return {ec_};
83 : }
84 :
85 4 : auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
86 : -> std::coroutine_handle<>
87 : {
88 4 : token_ = env->stop_token;
89 12 : return acc_.get_impl().accept(
90 12 : h, env->executor, token_, &ec_, &peer_impl_);
91 : }
92 : };
93 :
94 : public:
95 : /** Construct a native acceptor from an execution context.
96 :
97 : @param ctx The execution context that will own this acceptor.
98 : */
99 4 : explicit native_tcp_acceptor(capy::execution_context& ctx)
100 4 : : tcp_acceptor(create_handle<service_type>(ctx))
101 : {
102 4 : }
103 :
104 : /** Construct a native acceptor from an executor.
105 :
106 : @param ex The executor whose context will own the acceptor.
107 : */
108 : template<class Ex>
109 : requires(!std::same_as<std::remove_cvref_t<Ex>, native_tcp_acceptor>) &&
110 : capy::Executor<Ex>
111 : explicit native_tcp_acceptor(Ex const& ex)
112 : : native_tcp_acceptor(ex.context())
113 : {
114 : }
115 :
116 : /** Move construct.
117 :
118 : @param other The acceptor to move from.
119 :
120 : @pre No awaitables returned by @p other's methods exist.
121 : @pre The execution context associated with @p other must
122 : outlive this acceptor.
123 : */
124 : native_tcp_acceptor(native_tcp_acceptor&&) noexcept = default;
125 :
126 : /** Move assign.
127 :
128 : @param other The acceptor to move from.
129 :
130 : @pre No awaitables returned by either `*this` or @p other's
131 : methods exist.
132 : @pre The execution context associated with @p other must
133 : outlive this acceptor.
134 : */
135 : native_tcp_acceptor& operator=(native_tcp_acceptor&&) noexcept = default;
136 :
137 : native_tcp_acceptor(native_tcp_acceptor const&) = delete;
138 : native_tcp_acceptor& operator=(native_tcp_acceptor const&) = delete;
139 :
140 : /** Asynchronously accept an incoming connection.
141 :
142 : Calls the backend implementation directly, bypassing virtual
143 : dispatch. Otherwise identical to @ref tcp_acceptor::accept.
144 :
145 : @param peer The socket to receive the accepted connection.
146 :
147 : @return An awaitable yielding `io_result<>`.
148 :
149 : @throws std::logic_error if the acceptor is not listening.
150 :
151 : Both this acceptor and @p peer must outlive the returned
152 : awaitable.
153 : */
154 4 : auto accept(tcp_socket& peer)
155 : {
156 4 : if (!is_open())
157 MIS 0 : detail::throw_logic_error("accept: acceptor not listening");
158 HIT 4 : return native_accept_awaitable(*this, peer);
159 : }
160 : };
161 :
162 : } // namespace boost::corosio
163 :
164 : #endif
|