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

100.0% Lines (7/7) 100.0% Functions (5/5) -% Branches (0/0)
libs/corosio/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::acceptor_impl
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 void release() override;
49
50 void accept(
51 std::coroutine_handle<>,
52 capy::executor_ref,
53 std::stop_token,
54 std::error_code*,
55 io_object::io_object_impl**) override;
56
57 int native_handle() const noexcept { return fd_; }
58 4 endpoint local_endpoint() const noexcept override { return local_endpoint_; }
59 bool is_open() const noexcept { return fd_ >= 0; }
60 void cancel() noexcept override;
61 void cancel_single_op(select_op& op) noexcept;
62 void close_socket() noexcept;
63 42 void set_local_endpoint(endpoint ep) noexcept { local_endpoint_ = ep; }
64
65 2071 select_acceptor_service& service() noexcept { return svc_; }
66
67 select_accept_op acc_;
68
69 private:
70 select_acceptor_service& svc_;
71 int fd_ = -1;
72 endpoint local_endpoint_;
73 };
74
75 /** State for select acceptor service. */
76 class select_acceptor_state
77 {
78 public:
79 120 explicit select_acceptor_state(select_scheduler& sched) noexcept
80 120 : sched_(sched)
81 {
82 120 }
83
84 select_scheduler& sched_;
85 std::mutex mutex_;
86 intrusive_list<select_acceptor_impl> acceptor_list_;
87 std::unordered_map<select_acceptor_impl*, std::shared_ptr<select_acceptor_impl>> acceptor_ptrs_;
88 };
89
90 /** select acceptor service implementation.
91
92 Inherits from acceptor_service to enable runtime polymorphism.
93 Uses key_type = acceptor_service for service lookup.
94 */
95 class select_acceptor_service : public acceptor_service
96 {
97 public:
98 explicit select_acceptor_service(capy::execution_context& ctx);
99 ~select_acceptor_service();
100
101 select_acceptor_service(select_acceptor_service const&) = delete;
102 select_acceptor_service& operator=(select_acceptor_service const&) = delete;
103
104 void shutdown() override;
105
106 tcp_acceptor::acceptor_impl& create_acceptor_impl() override;
107 void destroy_acceptor_impl(tcp_acceptor::acceptor_impl& impl) override;
108 std::error_code open_acceptor(
109 tcp_acceptor::acceptor_impl& impl,
110 endpoint ep,
111 int backlog) override;
112
113 2117 select_scheduler& scheduler() const noexcept { return state_->sched_; }
114 void post(select_op* op);
115 void work_started() noexcept;
116 void work_finished() noexcept;
117
118 /** Get the socket service for creating peer sockets during accept. */
119 select_socket_service* socket_service() const noexcept;
120
121 private:
122 capy::execution_context& ctx_;
123 std::unique_ptr<select_acceptor_state> state_;
124 };
125
126 } // namespace boost::corosio::detail
127
128 #endif // BOOST_COROSIO_HAS_SELECT
129
130 #endif // BOOST_COROSIO_DETAIL_SELECT_ACCEPTORS_HPP
131