include/boost/corosio/native/detail/epoll/epoll_tcp_acceptor_service.hpp

76.2% Lines (112/147) 100.0% List of functions (20/20)
epoll_tcp_acceptor_service.hpp
f(x) Functions (20)
Function Calls Lines Blocks
boost::corosio::detail::epoll_tcp_acceptor_service::scheduler() const :75 152x 100.0% 100.0% boost::corosio::detail::epoll_accept_op::cancel() :92 6x 80.0% 75.0% boost::corosio::detail::epoll_accept_op::operator()() :101 4381x 100.0% 100.0% boost::corosio::detail::epoll_tcp_acceptor::epoll_tcp_acceptor(boost::corosio::detail::epoll_tcp_acceptor_service&) :106 80x 100.0% 100.0% boost::corosio::detail::epoll_tcp_acceptor::accept(std::__n4861::coroutine_handle<void>, boost::capy::executor_ref, std::stop_token, std::error_code*, boost::corosio::io_object::implementation**) :113 4381x 51.5% 47.0% boost::corosio::detail::epoll_tcp_acceptor::cancel() :228 2x 100.0% 100.0% boost::corosio::detail::epoll_tcp_acceptor::close_socket() :234 318x 100.0% 100.0% boost::corosio::detail::epoll_tcp_acceptor_service::epoll_tcp_acceptor_service(boost::capy::execution_context&) :239 320x 100.0% 83.0% boost::corosio::detail::epoll_tcp_acceptor_service::~epoll_tcp_acceptor_service() :248 640x 100.0% 100.0% boost::corosio::detail::epoll_tcp_acceptor_service::shutdown() :251 320x 80.0% 90.0% boost::corosio::detail::epoll_tcp_acceptor_service::construct() :264 80x 100.0% 78.0% boost::corosio::detail::epoll_tcp_acceptor_service::destroy(boost::corosio::io_object::implementation*) :277 80x 100.0% 83.0% boost::corosio::detail::epoll_tcp_acceptor_service::close(boost::corosio::io_object::handle&) :287 159x 100.0% 100.0% boost::corosio::detail::epoll_tcp_acceptor_service::open_acceptor_socket(boost::corosio::tcp_acceptor::implementation&, int, int, int) :293 79x 93.3% 93.0% boost::corosio::detail::epoll_tcp_acceptor_service::bind_acceptor(boost::corosio::tcp_acceptor::implementation&, boost::corosio::endpoint) :322 78x 100.0% 100.0% boost::corosio::detail::epoll_tcp_acceptor_service::listen_acceptor(boost::corosio::tcp_acceptor::implementation&, int) :329 75x 100.0% 100.0% boost::corosio::detail::epoll_tcp_acceptor_service::post(boost::corosio::detail::scheduler_op*) :336 11x 100.0% 100.0% boost::corosio::detail::epoll_tcp_acceptor_service::work_started() :342 4379x 100.0% 100.0% boost::corosio::detail::epoll_tcp_acceptor_service::work_finished() :348 9x 100.0% 100.0% boost::corosio::detail::epoll_tcp_acceptor_service::tcp_service() const :354 4372x 100.0% 78.0%
Line TLA Hits 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_EPOLL_EPOLL_TCP_ACCEPTOR_SERVICE_HPP
11 #define BOOST_COROSIO_NATIVE_DETAIL_EPOLL_EPOLL_TCP_ACCEPTOR_SERVICE_HPP
12
13 #include <boost/corosio/detail/platform.hpp>
14
15 #if BOOST_COROSIO_HAS_EPOLL
16
17 #include <boost/corosio/detail/config.hpp>
18 #include <boost/capy/ex/execution_context.hpp>
19 #include <boost/corosio/detail/tcp_acceptor_service.hpp>
20
21 #include <boost/corosio/native/detail/epoll/epoll_tcp_acceptor.hpp>
22 #include <boost/corosio/native/detail/epoll/epoll_tcp_service.hpp>
23 #include <boost/corosio/native/detail/epoll/epoll_scheduler.hpp>
24 #include <boost/corosio/native/detail/reactor/reactor_service_state.hpp>
25
26 #include <boost/corosio/native/detail/reactor/reactor_op_complete.hpp>
27
28 #include <memory>
29 #include <mutex>
30 #include <utility>
31
32 #include <errno.h>
33 #include <netinet/in.h>
34 #include <sys/epoll.h>
35 #include <sys/socket.h>
36 #include <unistd.h>
37
38 namespace boost::corosio::detail {
39
40 /// State for epoll acceptor service.
41 using epoll_tcp_acceptor_state =
42 reactor_service_state<epoll_scheduler, epoll_tcp_acceptor>;
43
44 /** epoll acceptor service implementation.
45
46 Inherits from tcp_acceptor_service to enable runtime polymorphism.
47 Uses key_type = tcp_acceptor_service for service lookup.
48 */
49 class BOOST_COROSIO_DECL epoll_tcp_acceptor_service final
50 : public tcp_acceptor_service
51 {
52 public:
53 explicit epoll_tcp_acceptor_service(capy::execution_context& ctx);
54 ~epoll_tcp_acceptor_service() override;
55
56 epoll_tcp_acceptor_service(epoll_tcp_acceptor_service const&) = delete;
57 epoll_tcp_acceptor_service&
58 operator=(epoll_tcp_acceptor_service const&) = delete;
59
60 void shutdown() override;
61
62 io_object::implementation* construct() override;
63 void destroy(io_object::implementation*) override;
64 void close(io_object::handle&) override;
65 std::error_code open_acceptor_socket(
66 tcp_acceptor::implementation& impl,
67 int family,
68 int type,
69 int protocol) override;
70 std::error_code
71 bind_acceptor(tcp_acceptor::implementation& impl, endpoint ep) override;
72 std::error_code
73 listen_acceptor(tcp_acceptor::implementation& impl, int backlog) override;
74
75 152x epoll_scheduler& scheduler() const noexcept
76 {
77 152x return state_->sched_;
78 }
79 void post(scheduler_op* op);
80 void work_started() noexcept;
81 void work_finished() noexcept;
82
83 /** Get the TCP service for creating peer sockets during accept. */
84 epoll_tcp_service* tcp_service() const noexcept;
85
86 private:
87 capy::execution_context& ctx_;
88 std::unique_ptr<epoll_tcp_acceptor_state> state_;
89 };
90
91 inline void
92 6x epoll_accept_op::cancel() noexcept
93 {
94 6x if (acceptor_impl_)
95 6x acceptor_impl_->cancel_single_op(*this);
96 else
97 request_cancel();
98 6x }
99
100 inline void
101 4381x epoll_accept_op::operator()()
102 {
103 4381x complete_accept_op<epoll_tcp_socket>(*this);
104 4381x }
105
106 80x inline epoll_tcp_acceptor::epoll_tcp_acceptor(
107 80x epoll_tcp_acceptor_service& svc) noexcept
108 80x : reactor_acceptor(svc)
109 {
110 80x }
111
112 inline std::coroutine_handle<>
113 4381x epoll_tcp_acceptor::accept(
114 std::coroutine_handle<> h,
115 capy::executor_ref ex,
116 std::stop_token token,
117 std::error_code* ec,
118 io_object::implementation** impl_out)
119 {
120 4381x auto& op = acc_;
121 4381x op.reset();
122 4381x op.h = h;
123 4381x op.ex = ex;
124 4381x op.ec_out = ec;
125 4381x op.impl_out = impl_out;
126 4381x op.fd = fd_;
127 4381x op.start(token, this);
128
129 4381x sockaddr_storage peer_storage{};
130 4381x socklen_t addrlen = sizeof(peer_storage);
131 int accepted;
132 do
133 {
134 4381x accepted = ::accept4(
135 fd_, reinterpret_cast<sockaddr*>(&peer_storage), &addrlen,
136 SOCK_NONBLOCK | SOCK_CLOEXEC);
137 }
138 4381x while (accepted < 0 && errno == EINTR);
139
140 4381x if (accepted >= 0)
141 {
142 {
143 2x std::lock_guard lock(desc_state_.mutex);
144 2x desc_state_.read_ready = false;
145 2x }
146
147 2x if (svc_.scheduler().try_consume_inline_budget())
148 {
149 auto* socket_svc = svc_.tcp_service();
150 if (socket_svc)
151 {
152 auto& impl =
153 static_cast<epoll_tcp_socket&>(*socket_svc->construct());
154 impl.set_socket(accepted);
155
156 impl.desc_state_.fd = accepted;
157 {
158 std::lock_guard lock(impl.desc_state_.mutex);
159 impl.desc_state_.read_op = nullptr;
160 impl.desc_state_.write_op = nullptr;
161 impl.desc_state_.connect_op = nullptr;
162 }
163 socket_svc->scheduler().register_descriptor(
164 accepted, &impl.desc_state_);
165
166 impl.set_endpoints(
167 local_endpoint_, from_sockaddr(peer_storage));
168
169 *ec = {};
170 if (impl_out)
171 *impl_out = &impl;
172 }
173 else
174 {
175 ::close(accepted);
176 *ec = make_err(ENOENT);
177 if (impl_out)
178 *impl_out = nullptr;
179 }
180 op.cont_op.cont.h = h;
181 return dispatch_coro(ex, op.cont_op.cont);
182 }
183
184 2x op.accepted_fd = accepted;
185 2x op.peer_storage = peer_storage;
186 2x op.complete(0, 0);
187 2x op.impl_ptr = shared_from_this();
188 2x svc_.post(&op);
189 2x return std::noop_coroutine();
190 }
191
192 4379x if (errno == EAGAIN || errno == EWOULDBLOCK)
193 {
194 4379x op.impl_ptr = shared_from_this();
195 4379x svc_.work_started();
196
197 4379x std::lock_guard lock(desc_state_.mutex);
198 4379x bool io_done = false;
199 4379x if (desc_state_.read_ready)
200 {
201 desc_state_.read_ready = false;
202 op.perform_io();
203 io_done = (op.errn != EAGAIN && op.errn != EWOULDBLOCK);
204 if (!io_done)
205 op.errn = 0;
206 }
207
208 4379x if (io_done || op.cancelled.load(std::memory_order_acquire))
209 {
210 svc_.post(&op);
211 svc_.work_finished();
212 }
213 else
214 {
215 4379x desc_state_.read_op = &op;
216 }
217 4379x return std::noop_coroutine();
218 4379x }
219
220 op.complete(errno, 0);
221 op.impl_ptr = shared_from_this();
222 svc_.post(&op);
223 // completion is always posted to scheduler queue, never inline.
224 return std::noop_coroutine();
225 }
226
227 inline void
228 2x epoll_tcp_acceptor::cancel() noexcept
229 {
230 2x do_cancel();
231 2x }
232
233 inline void
234 318x epoll_tcp_acceptor::close_socket() noexcept
235 {
236 318x do_close_socket();
237 318x }
238
239 320x inline epoll_tcp_acceptor_service::epoll_tcp_acceptor_service(
240 320x capy::execution_context& ctx)
241 320x : ctx_(ctx)
242 320x , state_(
243 std::make_unique<epoll_tcp_acceptor_state>(
244 320x ctx.use_service<epoll_scheduler>()))
245 {
246 320x }
247
248 640x inline epoll_tcp_acceptor_service::~epoll_tcp_acceptor_service() {}
249
250 inline void
251 320x epoll_tcp_acceptor_service::shutdown()
252 {
253 320x std::lock_guard lock(state_->mutex_);
254
255 320x while (auto* impl = state_->impl_list_.pop_front())
256 impl->close_socket();
257
258 // Don't clear impl_ptrs_ here — same rationale as
259 // epoll_tcp_service::shutdown(). Let ~state_ release ptrs
260 // after scheduler shutdown has drained all queued ops.
261 320x }
262
263 inline io_object::implementation*
264 80x epoll_tcp_acceptor_service::construct()
265 {
266 80x auto impl = std::make_shared<epoll_tcp_acceptor>(*this);
267 80x auto* raw = impl.get();
268
269 80x std::lock_guard lock(state_->mutex_);
270 80x state_->impl_ptrs_.emplace(raw, std::move(impl));
271 80x state_->impl_list_.push_back(raw);
272
273 80x return raw;
274 80x }
275
276 inline void
277 80x epoll_tcp_acceptor_service::destroy(io_object::implementation* impl)
278 {
279 80x auto* epoll_impl = static_cast<epoll_tcp_acceptor*>(impl);
280 80x epoll_impl->close_socket();
281 80x std::lock_guard lock(state_->mutex_);
282 80x state_->impl_list_.remove(epoll_impl);
283 80x state_->impl_ptrs_.erase(epoll_impl);
284 80x }
285
286 inline void
287 159x epoll_tcp_acceptor_service::close(io_object::handle& h)
288 {
289 159x static_cast<epoll_tcp_acceptor*>(h.get())->close_socket();
290 159x }
291
292 inline std::error_code
293 79x epoll_tcp_acceptor_service::open_acceptor_socket(
294 tcp_acceptor::implementation& impl, int family, int type, int protocol)
295 {
296 79x auto* epoll_impl = static_cast<epoll_tcp_acceptor*>(&impl);
297 79x epoll_impl->close_socket();
298
299 79x int fd = ::socket(family, type | SOCK_NONBLOCK | SOCK_CLOEXEC, protocol);
300 79x if (fd < 0)
301 return make_err(errno);
302
303 79x if (family == AF_INET6)
304 {
305 8x int val = 0; // dual-stack default
306 8x ::setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val));
307 }
308
309 79x epoll_impl->fd_ = fd;
310
311 // Set up descriptor state but do NOT register with epoll yet
312 79x epoll_impl->desc_state_.fd = fd;
313 {
314 79x std::lock_guard lock(epoll_impl->desc_state_.mutex);
315 79x epoll_impl->desc_state_.read_op = nullptr;
316 79x }
317
318 79x return {};
319 }
320
321 inline std::error_code
322 78x epoll_tcp_acceptor_service::bind_acceptor(
323 tcp_acceptor::implementation& impl, endpoint ep)
324 {
325 78x return static_cast<epoll_tcp_acceptor*>(&impl)->do_bind(ep);
326 }
327
328 inline std::error_code
329 75x epoll_tcp_acceptor_service::listen_acceptor(
330 tcp_acceptor::implementation& impl, int backlog)
331 {
332 75x return static_cast<epoll_tcp_acceptor*>(&impl)->do_listen(backlog);
333 }
334
335 inline void
336 11x epoll_tcp_acceptor_service::post(scheduler_op* op)
337 {
338 11x state_->sched_.post(op);
339 11x }
340
341 inline void
342 4379x epoll_tcp_acceptor_service::work_started() noexcept
343 {
344 4379x state_->sched_.work_started();
345 4379x }
346
347 inline void
348 9x epoll_tcp_acceptor_service::work_finished() noexcept
349 {
350 9x state_->sched_.work_finished();
351 9x }
352
353 inline epoll_tcp_service*
354 4372x epoll_tcp_acceptor_service::tcp_service() const noexcept
355 {
356 4372x auto* svc = ctx_.find_service<detail::tcp_service>();
357 4372x return svc ? dynamic_cast<epoll_tcp_service*>(svc) : nullptr;
358 }
359
360 } // namespace boost::corosio::detail
361
362 #endif // BOOST_COROSIO_HAS_EPOLL
363
364 #endif // BOOST_COROSIO_NATIVE_DETAIL_EPOLL_EPOLL_TCP_ACCEPTOR_SERVICE_HPP
365