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_DETAIL_REACTOR_REACTOR_OP_COMPLETE_HPP
11 : #define BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_OP_COMPLETE_HPP
12 :
13 : #include <boost/corosio/detail/dispatch_coro.hpp>
14 : #include <boost/corosio/native/detail/endpoint_convert.hpp>
15 : #include <boost/corosio/native/detail/make_err.hpp>
16 : #include <boost/corosio/io/io_object.hpp>
17 :
18 : #include <coroutine>
19 : #include <mutex>
20 : #include <utility>
21 :
22 : #include <netinet/in.h>
23 : #include <sys/socket.h>
24 : #include <unistd.h>
25 :
26 : namespace boost::corosio::detail {
27 :
28 : /** Complete a base read/write operation.
29 :
30 : Translates the recorded errno and cancellation state into
31 : an error_code, stores the byte count, then resumes the
32 : caller via symmetric transfer.
33 :
34 : @tparam Op The concrete operation type.
35 : @param op The operation to complete.
36 : */
37 : template<typename Op>
38 : void
39 HIT 141769 : complete_io_op(Op& op)
40 : {
41 141769 : op.stop_cb.reset();
42 141769 : op.socket_impl_->desc_state_.scheduler_->reset_inline_budget();
43 :
44 141769 : if (op.cancelled.load(std::memory_order_acquire))
45 320 : *op.ec_out = capy::error::canceled;
46 141449 : else if (op.errn != 0)
47 MIS 0 : *op.ec_out = make_err(op.errn);
48 HIT 141449 : else if (op.is_read_operation() && op.bytes_transferred == 0)
49 MIS 0 : *op.ec_out = capy::error::eof;
50 : else
51 HIT 141449 : *op.ec_out = {};
52 :
53 141769 : *op.bytes_out = op.bytes_transferred;
54 :
55 141769 : op.cont_op.cont.h = op.h;
56 141769 : capy::executor_ref saved_ex(op.ex);
57 141769 : auto prevent = std::move(op.impl_ptr);
58 141769 : dispatch_coro(saved_ex, op.cont_op.cont).resume();
59 141769 : }
60 :
61 : /** Complete a connect operation with endpoint caching.
62 :
63 : On success, queries the local endpoint via getsockname and
64 : caches both endpoints in the socket impl. Then resumes the
65 : caller via symmetric transfer.
66 :
67 : @tparam Op The concrete connect operation type.
68 : @param op The operation to complete.
69 : */
70 : template<typename Op>
71 : void
72 8129 : complete_connect_op(Op& op)
73 : {
74 8129 : op.stop_cb.reset();
75 8129 : op.socket_impl_->desc_state_.scheduler_->reset_inline_budget();
76 :
77 8129 : bool success =
78 8129 : (op.errn == 0 && !op.cancelled.load(std::memory_order_acquire));
79 :
80 8129 : if (success && op.socket_impl_)
81 : {
82 : using ep_type = decltype(op.target_endpoint);
83 8124 : ep_type local_ep;
84 8124 : sockaddr_storage local_storage{};
85 8124 : socklen_t local_len = sizeof(local_storage);
86 8124 : if (::getsockname(
87 : op.fd, reinterpret_cast<sockaddr*>(&local_storage),
88 8124 : &local_len) == 0)
89 8120 : local_ep =
90 8124 : from_sockaddr_as(local_storage, local_len, ep_type{});
91 8124 : op.socket_impl_->set_endpoints(local_ep, op.target_endpoint);
92 : }
93 :
94 8129 : if (op.cancelled.load(std::memory_order_acquire))
95 MIS 0 : *op.ec_out = capy::error::canceled;
96 HIT 8129 : else if (op.errn != 0)
97 5 : *op.ec_out = make_err(op.errn);
98 : else
99 8124 : *op.ec_out = {};
100 :
101 8129 : op.cont_op.cont.h = op.h;
102 8129 : capy::executor_ref saved_ex(op.ex);
103 8129 : auto prevent = std::move(op.impl_ptr);
104 8129 : dispatch_coro(saved_ex, op.cont_op.cont).resume();
105 8129 : }
106 :
107 : /** Construct and register a peer socket from an accepted fd.
108 :
109 : Creates a new socket impl via the acceptor's associated
110 : socket service, registers it with the scheduler, and caches
111 : the local and remote endpoints.
112 :
113 : @tparam SocketImpl The concrete socket implementation type.
114 : @tparam AcceptorImpl The concrete acceptor implementation type.
115 : @param acceptor_impl The acceptor that accepted the connection.
116 : @param accepted_fd The accepted file descriptor (set to -1 on success).
117 : @param peer_storage The peer address from accept().
118 : @param impl_out Output pointer for the new socket impl.
119 : @param ec_out Output pointer for any error.
120 : @return True on success, false on failure.
121 : */
122 : template<typename SocketImpl, typename AcceptorImpl>
123 : bool
124 8114 : setup_accepted_socket(
125 : AcceptorImpl* acceptor_impl,
126 : int& accepted_fd,
127 : sockaddr_storage const& peer_storage,
128 : io_object::implementation** impl_out,
129 : std::error_code* ec_out)
130 : {
131 8114 : auto* socket_svc = acceptor_impl->service().stream_service();
132 8114 : if (!socket_svc)
133 : {
134 MIS 0 : *ec_out = make_err(ENOENT);
135 0 : return false;
136 : }
137 :
138 HIT 8114 : auto& impl = static_cast<SocketImpl&>(*socket_svc->construct());
139 8114 : impl.set_socket(accepted_fd);
140 :
141 8114 : impl.desc_state_.fd = accepted_fd;
142 : {
143 8114 : std::lock_guard lock(impl.desc_state_.mutex);
144 8114 : impl.desc_state_.read_op = nullptr;
145 8114 : impl.desc_state_.write_op = nullptr;
146 8114 : impl.desc_state_.connect_op = nullptr;
147 8114 : }
148 8114 : socket_svc->scheduler().register_descriptor(accepted_fd, &impl.desc_state_);
149 :
150 : using ep_type = decltype(acceptor_impl->local_endpoint());
151 8114 : impl.set_endpoints(
152 : acceptor_impl->local_endpoint(),
153 8114 : from_sockaddr_as(
154 : peer_storage,
155 : static_cast<socklen_t>(sizeof(peer_storage)),
156 : ep_type{}));
157 :
158 8114 : if (impl_out)
159 8114 : *impl_out = &impl;
160 8114 : accepted_fd = -1;
161 8114 : return true;
162 : }
163 :
164 : /** Complete an accept operation.
165 :
166 : Sets up the peer socket on success, or closes the accepted
167 : fd on failure. Then resumes the caller via symmetric transfer.
168 :
169 : @tparam SocketImpl The concrete socket implementation type.
170 : @tparam Op The concrete accept operation type.
171 : @param op The operation to complete.
172 : */
173 : template<typename SocketImpl, typename Op>
174 : void
175 8126 : complete_accept_op(Op& op)
176 : {
177 8126 : op.stop_cb.reset();
178 8126 : op.acceptor_impl_->desc_state_.scheduler_->reset_inline_budget();
179 :
180 8126 : bool success =
181 8126 : (op.errn == 0 && !op.cancelled.load(std::memory_order_acquire));
182 :
183 8126 : if (op.cancelled.load(std::memory_order_acquire))
184 12 : *op.ec_out = capy::error::canceled;
185 8114 : else if (op.errn != 0)
186 MIS 0 : *op.ec_out = make_err(op.errn);
187 : else
188 HIT 8114 : *op.ec_out = {};
189 :
190 8126 : if (success && op.accepted_fd >= 0 && op.acceptor_impl_)
191 : {
192 8114 : if (!setup_accepted_socket<SocketImpl>(
193 8114 : op.acceptor_impl_, op.accepted_fd, op.peer_storage, op.impl_out,
194 : op.ec_out))
195 MIS 0 : success = false;
196 : }
197 :
198 HIT 8126 : if (!success || !op.acceptor_impl_)
199 : {
200 12 : if (op.accepted_fd >= 0)
201 : {
202 MIS 0 : ::close(op.accepted_fd);
203 0 : op.accepted_fd = -1;
204 : }
205 HIT 12 : if (op.impl_out)
206 12 : *op.impl_out = nullptr;
207 : }
208 :
209 8126 : op.cont_op.cont.h = op.h;
210 8126 : capy::executor_ref saved_ex(op.ex);
211 8126 : auto prevent = std::move(op.impl_ptr);
212 8126 : dispatch_coro(saved_ex, op.cont_op.cont).resume();
213 8126 : }
214 :
215 : /** Complete a datagram operation (send_to or recv_from).
216 :
217 : For recv_from operations, writes the source endpoint from the
218 : recorded sockaddr_storage into the caller's endpoint pointer.
219 : Then resumes the caller via symmetric transfer.
220 :
221 : @tparam Op The concrete datagram operation type.
222 : @param op The operation to complete.
223 : @param source_out Optional pointer to store source endpoint
224 : (non-null for recv_from, null for send_to).
225 : */
226 : template<typename Op, typename Endpoint>
227 : void
228 18 : complete_datagram_op(Op& op, Endpoint* source_out)
229 : {
230 18 : op.stop_cb.reset();
231 18 : op.socket_impl_->desc_state_.scheduler_->reset_inline_budget();
232 :
233 18 : if (op.cancelled.load(std::memory_order_acquire))
234 6 : *op.ec_out = capy::error::canceled;
235 12 : else if (op.errn != 0)
236 MIS 0 : *op.ec_out = make_err(op.errn);
237 : else
238 HIT 12 : *op.ec_out = {};
239 :
240 18 : *op.bytes_out = op.bytes_transferred;
241 :
242 28 : if (source_out && !op.cancelled.load(std::memory_order_acquire) &&
243 10 : op.errn == 0)
244 20 : *source_out = from_sockaddr_as(
245 10 : op.source_storage,
246 : op.source_addrlen,
247 : Endpoint{});
248 :
249 18 : op.cont_op.cont.h = op.h;
250 18 : capy::executor_ref saved_ex(op.ex);
251 18 : auto prevent = std::move(op.impl_ptr);
252 18 : dispatch_coro(saved_ex, op.cont_op.cont).resume();
253 18 : }
254 :
255 : } // namespace boost::corosio::detail
256 :
257 : #endif // BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_OP_COMPLETE_HPP
|