src/corosio/src/detail/select/acceptors.hpp

100.0% Lines (8/8) 100.0% Functions (6/6) -% Branches (0/0)
src/corosio/src/detail/select/acceptors.hpp
Line 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_DETAIL_SELECT_ACCEPTORS_HPP
11 #define BOOST_COROSIO_DETAIL_SELECT_ACCEPTORS_HPP
12
13 #include <boost/corosio/detail/platform.hpp>
14
15 #if BOOST_COROSIO_HAS_SELECT
16
17 #include <boost/corosio/detail/config.hpp>
18 #include <boost/corosio/tcp_acceptor.hpp>
19 #include <boost/capy/ex/executor_ref.hpp>
20 #include <boost/capy/ex/execution_context.hpp>
21 #include "src/detail/intrusive.hpp"
22 #include "src/detail/socket_service.hpp"
23
24 #include "src/detail/select/op.hpp"
25 #include "src/detail/select/scheduler.hpp"
26
27 #include <memory>
28 #include <mutex>
29 #include <unordered_map>
30
31 namespace boost::corosio::detail {
32
33 class select_acceptor_service;
34 class select_acceptor_impl;
35 class select_socket_service;
36
37 /// Acceptor implementation for select backend.
38 class select_acceptor_impl
39 : public tcp_acceptor::implementation
40 , public std::enable_shared_from_this<select_acceptor_impl>
41 , public intrusive_list<select_acceptor_impl>::node
42 {
43 friend class select_acceptor_service;
44
45 public:
46 explicit select_acceptor_impl(select_acceptor_service& svc) noexcept;
47
48 std::coroutine_handle<> accept(
49 std::coroutine_handle<>,
50 capy::executor_ref,
51 std::stop_token,
52 std::error_code*,
53 io_object::implementation**) override;
54
55 int native_handle() const noexcept { return fd_; }
56 39 endpoint local_endpoint() const noexcept override { return local_endpoint_; }
57 3613 bool is_open() const noexcept override { return fd_ >= 0; }
58 void cancel() noexcept override;
59 void cancel_single_op(select_op& op) noexcept;
60 void close_socket() noexcept;
61 42 void set_local_endpoint(endpoint ep) noexcept { local_endpoint_ = ep; }
62
63 3434 select_acceptor_service& service() noexcept { return svc_; }
64
65 select_accept_op acc_;
66
67 private:
68 select_acceptor_service& svc_;
69 int fd_ = -1;
70 endpoint local_endpoint_;
71 };
72
73 /** State for select acceptor service. */
74 class select_acceptor_state
75 {
76 public:
77 133 explicit select_acceptor_state(select_scheduler& sched) noexcept
78 133 : sched_(sched)
79 {
80 133 }
81
82 select_scheduler& sched_;
83 std::mutex mutex_;
84 intrusive_list<select_acceptor_impl> acceptor_list_;
85 std::unordered_map<select_acceptor_impl*, std::shared_ptr<select_acceptor_impl>> acceptor_ptrs_;
86 };
87
88 /** select acceptor service implementation.
89
90 Inherits from acceptor_service to enable runtime polymorphism.
91 Uses key_type = acceptor_service for service lookup.
92 */
93 class select_acceptor_service : public acceptor_service
94 {
95 public:
96 explicit select_acceptor_service(capy::execution_context& ctx);
97 ~select_acceptor_service();
98
99 select_acceptor_service(select_acceptor_service const&) = delete;
100 select_acceptor_service& operator=(select_acceptor_service const&) = delete;
101
102 void shutdown() override;
103
104 io_object::implementation* construct() override;
105 void destroy(io_object::implementation*) override;
106 void close(io_object::handle&) override;
107 std::error_code open_acceptor(
108 tcp_acceptor::implementation& impl,
109 endpoint ep,
110 int backlog) override;
111
112 3480 select_scheduler& scheduler() const noexcept { return state_->sched_; }
113 void post(select_op* op);
114 void work_started() noexcept;
115 void work_finished() noexcept;
116
117 /** Get the socket service for creating peer sockets during accept. */
118 select_socket_service* socket_service() const noexcept;
119
120 private:
121 capy::execution_context& ctx_;
122 std::unique_ptr<select_acceptor_state> state_;
123 };
124
125 } // namespace boost::corosio::detail
126
127 #endif // BOOST_COROSIO_HAS_SELECT
128
129 #endif // BOOST_COROSIO_DETAIL_SELECT_ACCEPTORS_HPP
130