src/corosio/src/socket_option.cpp

93.1% Lines (54/58) 90.9% Functions (20/22)
src/corosio/src/socket_option.cpp
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 #include <boost/corosio/socket_option.hpp>
11 #include <boost/corosio/native/native_socket_option.hpp>
12
13 #include <cstring>
14
15 namespace boost::corosio::socket_option {
16
17 // no_delay
18
19 int
20 42 no_delay::level() noexcept
21 {
22 42 return native_socket_option::no_delay::level();
23 }
24 int
25 42 no_delay::name() noexcept
26 {
27 42 return native_socket_option::no_delay::name();
28 }
29
30 // keep_alive
31
32 int
33 16 keep_alive::level() noexcept
34 {
35 16 return native_socket_option::keep_alive::level();
36 }
37 int
38 16 keep_alive::name() noexcept
39 {
40 16 return native_socket_option::keep_alive::name();
41 }
42
43 // v6_only
44
45 int
46 14 v6_only::level() noexcept
47 {
48 14 return native_socket_option::v6_only::level();
49 }
50 int
51 14 v6_only::name() noexcept
52 {
53 14 return native_socket_option::v6_only::name();
54 }
55
56 // reuse_address
57
58 int
59 135 reuse_address::level() noexcept
60 {
61 135 return native_socket_option::reuse_address::level();
62 }
63 int
64 135 reuse_address::name() noexcept
65 {
66 135 return native_socket_option::reuse_address::name();
67 }
68
69 // reuse_port
70
71 #ifdef SO_REUSEPORT
72 int
73 reuse_port::level() noexcept
74 {
75 return native_socket_option::reuse_port::level();
76 }
77 int
78 reuse_port::name() noexcept
79 {
80 return native_socket_option::reuse_port::name();
81 }
82 #else
83 int
84 reuse_port::level() noexcept
85 {
86 return SOL_SOCKET;
87 }
88 int
89 reuse_port::name() noexcept
90 {
91 return -1;
92 }
93 #endif
94
95 // receive_buffer_size
96
97 int
98 16 receive_buffer_size::level() noexcept
99 {
100 16 return native_socket_option::receive_buffer_size::level();
101 }
102 int
103 16 receive_buffer_size::name() noexcept
104 {
105 16 return native_socket_option::receive_buffer_size::name();
106 }
107
108 // send_buffer_size
109
110 int
111 8 send_buffer_size::level() noexcept
112 {
113 8 return native_socket_option::send_buffer_size::level();
114 }
115 int
116 8 send_buffer_size::name() noexcept
117 {
118 8 return native_socket_option::send_buffer_size::name();
119 }
120
121 // linger
122
123 16 linger::linger(bool enabled, int timeout) noexcept
124 {
125 16 native_socket_option::linger native(enabled, timeout);
126 static_assert(
127 sizeof(native) <= sizeof(storage_),
128 "platform linger exceeds socket_option::linger storage");
129 16 std::memcpy(storage_, native.data(), native.size());
130 16 }
131
132 bool
133 14 linger::enabled() const noexcept
134 {
135 14 native_socket_option::linger native;
136 14 std::memcpy(native.data(), storage_, native.size());
137 14 return native.enabled();
138 }
139
140 void
141 2 linger::enabled(bool e) noexcept
142 {
143 2 native_socket_option::linger native;
144 2 std::memcpy(native.data(), storage_, native.size());
145 2 native.enabled(e);
146 2 std::memcpy(storage_, native.data(), native.size());
147 2 }
148
149 int
150 12 linger::timeout() const noexcept
151 {
152 12 native_socket_option::linger native;
153 12 std::memcpy(native.data(), storage_, native.size());
154 12 return native.timeout();
155 }
156
157 void
158 2 linger::timeout(int t) noexcept
159 {
160 2 native_socket_option::linger native;
161 2 std::memcpy(native.data(), storage_, native.size());
162 2 native.timeout(t);
163 2 std::memcpy(storage_, native.data(), native.size());
164 2 }
165
166 int
167 28 linger::level() noexcept
168 {
169 28 return native_socket_option::linger::level();
170 }
171 int
172 28 linger::name() noexcept
173 {
174 28 return native_socket_option::linger::name();
175 }
176
177 std::size_t
178 28 linger::size() const noexcept
179 {
180 28 return native_socket_option::linger{}.size();
181 }
182
183 } // namespace boost::corosio::socket_option
184