include/boost/corosio/native/detail/kqueue/kqueue_socket.hpp

100.0% Lines (13/13) 100.0% Functions (5/5) -% Branches (0/0)
include/boost/corosio/native/detail/kqueue/kqueue_socket.hpp
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Michael Vandeberg
3 // Copyright (c) 2026 Steve Gerbino
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Official repository: https://github.com/cppalliance/corosio
9 //
10
11 #ifndef BOOST_COROSIO_NATIVE_DETAIL_KQUEUE_KQUEUE_SOCKET_HPP
12 #define BOOST_COROSIO_NATIVE_DETAIL_KQUEUE_KQUEUE_SOCKET_HPP
13
14 #include <boost/corosio/detail/platform.hpp>
15
16 #if BOOST_COROSIO_HAS_KQUEUE
17
18 #include <boost/corosio/tcp_socket.hpp>
19 #include <boost/capy/ex/executor_ref.hpp>
20 #include <boost/corosio/detail/intrusive.hpp>
21
22 #include <boost/corosio/native/detail/kqueue/kqueue_op.hpp>
23
24 #include <memory>
25
26 namespace boost::corosio::detail {
27
28 class kqueue_socket_service;
29
30 /// Socket implementation for kqueue backend.
31 class kqueue_socket final
32 : public tcp_socket::implementation
33 , public std::enable_shared_from_this<kqueue_socket>
34 , public intrusive_list<kqueue_socket>::node
35 {
36 friend class kqueue_socket_service;
37
38 public:
39 explicit kqueue_socket(kqueue_socket_service& svc) noexcept;
40 ~kqueue_socket();
41
42 std::coroutine_handle<> connect(
43 std::coroutine_handle<>,
44 capy::executor_ref,
45 endpoint,
46 std::stop_token,
47 std::error_code*) override;
48
49 std::coroutine_handle<> read_some(
50 std::coroutine_handle<>,
51 capy::executor_ref,
52 io_buffer_param,
53 std::stop_token,
54 std::error_code*,
55 std::size_t*) override;
56
57 std::coroutine_handle<> write_some(
58 std::coroutine_handle<>,
59 capy::executor_ref,
60 io_buffer_param,
61 std::stop_token,
62 std::error_code*,
63 std::size_t*) override;
64
65 std::error_code shutdown(tcp_socket::shutdown_type what) noexcept override;
66
67 36021 native_handle_type native_handle() const noexcept override
68 {
69 36021 return fd_;
70 }
71
72 // Socket options
73 std::error_code set_no_delay(bool value) noexcept override;
74 bool no_delay(std::error_code& ec) const noexcept override;
75
76 std::error_code set_keep_alive(bool value) noexcept override;
77 bool keep_alive(std::error_code& ec) const noexcept override;
78
79 std::error_code set_receive_buffer_size(int size) noexcept override;
80 int receive_buffer_size(std::error_code& ec) const noexcept override;
81
82 std::error_code set_send_buffer_size(int size) noexcept override;
83 int send_buffer_size(std::error_code& ec) const noexcept override;
84
85 std::error_code set_linger(bool enabled, int timeout) noexcept override;
86 tcp_socket::linger_options
87 linger(std::error_code& ec) const noexcept override;
88
89 16 endpoint local_endpoint() const noexcept override
90 {
91 16 return local_endpoint_;
92 }
93 16 endpoint remote_endpoint() const noexcept override
94 {
95 16 return remote_endpoint_;
96 }
97 bool is_open() const noexcept
98 {
99 return fd_ >= 0;
100 }
101 void cancel() noexcept override;
102 void cancel_single_op(kqueue_op& op) noexcept;
103 void close_socket() noexcept;
104 5436 void set_socket(int fd) noexcept
105 {
106 5436 fd_ = fd;
107 5436 }
108 10872 void set_endpoints(endpoint local, endpoint remote) noexcept
109 {
110 10872 local_endpoint_ = local;
111 10872 remote_endpoint_ = remote;
112 10872 }
113
114 // Public for internal integration with the scheduler and reactor —
115 // not part of the external API. The descriptor_state is accessed by
116 // the reactor thread (lock-free atomics) and by op completion under
117 // desc_state_.mutex; the op slots and initiators are only touched
118 // by the thread that owns the current I/O call.
119 kqueue_connect_op conn_;
120 kqueue_read_op rd_;
121 kqueue_write_op wr_;
122 descriptor_state desc_state_;
123
124 void register_op(
125 kqueue_op& op,
126 kqueue_op*& desc_slot,
127 bool& ready_flag,
128 bool& cancel_flag) noexcept;
129
130 private:
131 kqueue_socket_service& svc_;
132 int fd_ = -1;
133 bool user_set_linger_ = false;
134 endpoint local_endpoint_;
135 endpoint remote_endpoint_;
136 };
137
138 } // namespace boost::corosio::detail
139
140 #endif // BOOST_COROSIO_HAS_KQUEUE
141
142 #endif // BOOST_COROSIO_NATIVE_DETAIL_KQUEUE_KQUEUE_SOCKET_HPP
143