include/boost/corosio/native/detail/reactor/reactor_backend.hpp

87.5% Lines (63/0/72) 100.0% List of functions (1/1/2)
reactor_backend.hpp
f(x) Functions (2)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Michael Vandeberg
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_DETAIL_REACTOR_REACTOR_BACKEND_HPP
11 #define BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_BACKEND_HPP
12
13 /* Reactor backend: acceptor accept() implementation.
14
15 Contains the accept() method body for reactor_acceptor_impl,
16 which needs all socket/service types to be complete. Included
17 by per-backend type files (epoll_types.hpp, etc.) after all
18 named types are defined.
19 */
20
21 #include <boost/corosio/native/detail/reactor/reactor_service_finals.hpp>
22 #include <boost/corosio/native/detail/reactor/reactor_op_complete.hpp>
23 #include <boost/corosio/native/detail/endpoint_convert.hpp>
24 #include <boost/corosio/detail/dispatch_coro.hpp>
25
26 #include <mutex>
27
28 namespace boost::corosio::detail {
29
30 // ============================================================
31 // Acceptor accept() implementation
32 // ============================================================
33
34 template<
35 class Derived,
36 class Traits,
37 class Service,
38 class SocketFinal,
39 class AccImplBase,
40 class Endpoint>
41 std::coroutine_handle<>
42 4638x reactor_acceptor_impl<
43 Derived,
44 Traits,
45 Service,
46 SocketFinal,
47 AccImplBase,
48 Endpoint>::
49 accept(
50 std::coroutine_handle<> h,
51 capy::executor_ref ex,
52 std::stop_token token,
53 std::error_code* ec,
54 io_object::implementation** impl_out)
55 {
56 4638x auto& op = this->acc_;
57 4638x op.reset();
58 4638x op.h = h;
59 4638x op.ex = ex;
60 4638x op.ec_out = ec;
61 4638x op.impl_out = impl_out;
62 4638x op.fd = this->fd_;
63 4638x op.start(token, static_cast<Derived*>(this));
64
65 4638x sockaddr_storage peer_storage{};
66 4638x socklen_t peer_addrlen = 0;
67
68 int accepted =
69 4638x Traits::accept_policy::do_accept(this->fd_, peer_storage, peer_addrlen);
70
71 4638x if (accepted >= 0)
72 {
73 {
74 44x std::lock_guard lock(this->desc_state_.mutex);
75 44x this->desc_state_.read_ready = false;
76 44x }
77
78 44x if (this->svc_.scheduler().try_consume_inline_budget())
79 {
80 15x auto* socket_svc = this->svc_.stream_service();
81 15x if (socket_svc)
82 {
83 auto& impl =
84 15x static_cast<SocketFinal&>(*socket_svc->construct());
85 15x impl.set_socket(accepted);
86
87 15x impl.desc_state_.fd = accepted;
88 {
89 15x std::lock_guard lock(impl.desc_state_.mutex);
90 15x impl.desc_state_.read_op = nullptr;
91 15x impl.desc_state_.write_op = nullptr;
92 15x impl.desc_state_.connect_op = nullptr;
93 15x }
94 15x auto reg_ec = socket_svc->scheduler().register_descriptor(
95 accepted, &impl.desc_state_);
96 15x if (reg_ec)
97 {
98 // destroy() closes the fd the impl already owns.
99 1x socket_svc->destroy(&impl);
100 1x *ec = reg_ec;
101 1x if (impl_out)
102 1x *impl_out = nullptr;
103 }
104 else
105 {
106 14x impl.set_endpoints(
107 this->local_endpoint_,
108 14x from_sockaddr_as(
109 peer_storage, peer_addrlen, Endpoint{}));
110
111 14x *ec = {};
112 14x if (impl_out)
113 14x *impl_out = &impl;
114 }
115 }
116 else
117 {
118 ::close(accepted);
119 *ec = make_err(ENOENT);
120 if (impl_out)
121 *impl_out = nullptr;
122 }
123 15x op.cont.h = h;
124 15x return dispatch_coro(ex, op.cont);
125 }
126
127 29x op.accepted_fd = accepted;
128 29x op.peer_storage = peer_storage;
129 29x op.peer_addrlen = peer_addrlen;
130 29x op.complete(0, 0);
131 29x op.impl_ptr = this->shared_from_this();
132 29x this->svc_.post(&op);
133 29x return std::noop_coroutine();
134 }
135
136 4594x if (errno == EAGAIN || errno == EWOULDBLOCK)
137 {
138 4584x op.impl_ptr = this->shared_from_this();
139 4584x this->svc_.work_started();
140
141 4584x std::lock_guard lock(this->desc_state_.mutex);
142 4584x bool io_done = false;
143 4584x if (this->desc_state_.read_ready)
144 {
145 this->desc_state_.read_ready = false;
146 op.perform_io();
147 io_done = (op.errn != EAGAIN && op.errn != EWOULDBLOCK);
148 if (!io_done)
149 op.errn = 0;
150 }
151
152 4584x if (io_done || op.cancelled.load(std::memory_order_acquire))
153 {
154 10x this->svc_.post(&op);
155 10x this->svc_.work_finished();
156 }
157 else
158 {
159 4574x this->desc_state_.read_op = &op;
160 }
161 4584x return std::noop_coroutine();
162 4584x }
163
164 10x op.complete(errno, 0);
165 10x op.impl_ptr = this->shared_from_this();
166 10x this->svc_.post(&op);
167 10x return std::noop_coroutine();
168 }
169
170 } // namespace boost::corosio::detail
171
172 #endif // BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_BACKEND_HPP
173