include/boost/corosio/native/detail/iocp/win_overlapped_op.hpp

100.0% Lines (71/71) 100.0% List of functions (10/10) 95.5% Branches (21/22)
win_overlapped_op.hpp
f(x) Functions (10)
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2025 Vinnie Falco ([email protected])
3 // Copyright (c) 2026 Steve Gerbino
4 // Copyright (c) 2026 Michael Vandeberg
5 //
6 // Distributed under the Boost Software License, Version 1.0. (See accompanying
7 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 //
9 // Official repository: https://github.com/cppalliance/corosio
10 //
11
12 #ifndef BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_OVERLAPPED_OP_HPP
13 #define BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_OVERLAPPED_OP_HPP
14
15 #include <boost/corosio/detail/platform.hpp>
16
17 #if BOOST_COROSIO_HAS_IOCP
18
19 #include <boost/corosio/detail/config.hpp>
20 #include <boost/capy/error.hpp>
21 #include <system_error>
22
23 #include <boost/corosio/native/detail/make_err.hpp>
24 #include <boost/corosio/detail/dispatch_coro.hpp>
25 #include <boost/corosio/native/detail/coro_op.hpp>
26 #include <boost/corosio/native/detail/coro_op_complete.hpp>
27
28 #include <atomic>
29 #include <coroutine>
30 #include <cstddef>
31
32 #include <boost/corosio/native/detail/iocp/win_windows.hpp>
33
34 namespace boost::corosio::detail {
35
36 /** Convert an IOCP completion's raw error to std::error_code, disambiguating
37 ERROR_NETNAME_DELETED by operation kind and surfacing the remote-connection
38 error family as portable std::errc conditions.
39
40 IOCP delivers ERROR_NETNAME_DELETED both when a local closesocket() cancels
41 a pending op (already handled by the cancelled flag before this is reached)
42 and when the peer hard-closes with a RST. A NETNAME_DELETED that survives
43 the cancelled check is therefore a genuine remote reset, and the correct
44 surface depends on the operation: connection_reset for stream read/write,
45 connection_aborted for accept (mirroring Asio's per-op mapping).
46
47 Why generic_category / std::errc rather than a native code: which raw
48 Windows code std::system_category maps to a given std::errc condition
49 depends on the standard library. MSVC's STL maps the WSA-range socket
50 codes (WSAECONNRESET 10054, WSAECONNREFUSED 10061, ...) to the std::errc
51 conditions but not their 12xx Win32 counterparts; libstdc++ (MinGW) does
52 the exact opposite -- it maps the Win32 12xx codes but not the WSA range.
53 So no single system_category code compares equal to e.g.
54 std::errc::connection_refused on both toolchains. Returning the condition
55 itself via std::make_error_code (generic_category) sidesteps the
56 divergence: it compares equal to the matching std::errc on every standard
57 library, mirroring what the POSIX backends yield from errno. The trade-off
58 is a generic (less Windows-specific) message string.
59
60 Both the WSA and Win32 forms are accepted because the delivered form
61 varies by operation: a WSASend/WSARecv completion surfaces the Winsock
62 code directly (e.g. WSAECONNRESET 10054), whereas a ConnectEx failure is
63 completed with an NTSTATUS and GetQueuedCompletionStatus reports the Win32
64 code RtlNtStatusToDosError derives from it (e.g. ERROR_CONNECTION_REFUSED
65 1225). This normalization lives here, in the IOCP layer, rather than in
66 the platform-neutral make_err. All other codes defer to make_err.
67
68 @param dwError The Windows error code (DWORD).
69 @param accept_path True on the accept completion path.
70 @return The corresponding std::error_code.
71 */
72 inline std::error_code
73 2748x iocp_make_err(DWORD dwError, bool accept_path) noexcept
74 {
75 // A pending op hit by a remote RST completes with ERROR_NETNAME_DELETED;
76 // its portable meaning depends on the operation (reset vs aborted).
77
2/2
✓ Branch 2 → 3 taken 5 times.
✓ Branch 2 → 8 taken 2743 times.
2748x if (dwError == ERROR_NETNAME_DELETED)
78
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 4 times.
5x return std::make_error_code(accept_path
79 ? std::errc::connection_aborted
80 5x : std::errc::connection_reset);
81
82
8/8
✓ Branch 8 → 9 taken 7 times.
✓ Branch 8 → 10 taken 21 times.
✓ Branch 8 → 11 taken 4 times.
✓ Branch 8 → 12 taken 2 times.
✓ Branch 8 → 13 taken 2 times.
✓ Branch 8 → 14 taken 2 times.
✓ Branch 8 → 15 taken 179 times.
✓ Branch 8 → 16 taken 2526 times.
2743x switch (dwError)
83 {
84 7x case WSAECONNRESET: // 10054
85 7x return std::make_error_code(std::errc::connection_reset);
86 21x case WSAECONNREFUSED: case ERROR_CONNECTION_REFUSED: // 10061 / 1225
87 21x return std::make_error_code(std::errc::connection_refused);
88 4x case WSAECONNABORTED: case ERROR_CONNECTION_ABORTED: // 10053 / 1236
89 4x return std::make_error_code(std::errc::connection_aborted);
90 2x case WSAENETUNREACH: case ERROR_NETWORK_UNREACHABLE: // 10051 / 1231
91 2x return std::make_error_code(std::errc::network_unreachable);
92 2x case WSAEHOSTUNREACH: case ERROR_HOST_UNREACHABLE: // 10065 / 1232
93 2x return std::make_error_code(std::errc::host_unreachable);
94 2x case WSAETIMEDOUT: case ERROR_SEM_TIMEOUT: // 10060 / 121
95 2x return std::make_error_code(std::errc::timed_out);
96 // Closed-object contract: MSVC maps ERROR_INVALID_HANDLE to
97 // invalid_argument and MinGW's WSAEBADF mapping is unreliable, so
98 // normalize both spellings of "dead handle" here.
99 179x case WSAEBADF: case ERROR_INVALID_HANDLE: // 10009 / 6
100 179x return std::make_error_code(std::errc::bad_file_descriptor);
101 2526x default:
102 2526x break;
103 }
104 2526x return make_err(dwError);
105 }
106
107 /** Base class for IOCP overlapped operations.
108
109 Derives from both OVERLAPPED (for Windows IOCP) and scheduler_op
110 (for queueing). Uses function pointer dispatch inherited from
111 scheduler_op - no virtual functions.
112
113 The OVERLAPPED structure is at the start so we can static_cast
114 between OVERLAPPED* and overlapped_op*.
115 */
116 struct overlapped_op
117 : OVERLAPPED
118 , coro_op
119 {
120 /** Function pointer type for cancellation hook. */
121 using cancel_func_type = void (*)(overlapped_op*) noexcept;
122
123 /** Completion handshake between the I/O initiator and the GQCS completer,
124 and the release/acquire barrier that publishes the payload: the plain
125 `dwError` / `bytes_transferred` fields are written before a release
126 store to `ready_` and read after an acquiring load, so they need no
127 atomicity of their own. The CAS protocol lives at the access sites in
128 win_scheduler.hpp. */
129 std::atomic<long> ready_{0};
130 DWORD dwError = 0;
131 DWORD bytes_transferred = 0;
132 cancel_func_type cancel_func_ = nullptr;
133
134 36062x explicit overlapped_op(func_type func) noexcept : coro_op(func)
135 {
136 36062x reset_overlapped();
137 36062x }
138
139 663899x void reset_overlapped() noexcept
140 {
141 663899x Internal = 0;
142 663899x InternalHigh = 0;
143 663899x Offset = 0;
144 663899x OffsetHigh = 0;
145 663899x hEvent = nullptr;
146 663899x }
147
148 627837x void reset() noexcept
149 {
150 627837x reset_overlapped();
151 627837x ready_.store(0, std::memory_order_relaxed);
152 627837x dwError = 0;
153 627837x bytes_transferred = 0;
154 627837x empty_buffer = false;
155 627837x is_read = false;
156 627837x cancelled.store(false, std::memory_order_relaxed);
157 627837x }
158
159 // coro_op::request_cancel() (set the cancelled flag) is inherited
160 // and used directly by close()/cancel() paths. The stop_token path
161 // additionally drives the kernel via on_cancel() below.
162
163 1427x void do_cancel() noexcept
164 {
165
2/2
✓ Branch 2 → 3 taken 1425 times.
✓ Branch 2 → 4 taken 2 times.
1427x if (cancel_func_)
166 1425x cancel_func_(this);
167 1427x }
168
169 /** IOCP cancellation hook (stop_token path): set the flag, then issue
170 the registered CancelIoEx / wait-reactor deregister via cancel_func_. */
171 1427x void on_cancel() noexcept override
172 {
173 1427x request_cancel();
174 1427x do_cancel();
175 1427x }
176
177 627553x void store_result(DWORD bytes, DWORD err) noexcept
178 {
179 627553x bytes_transferred = bytes;
180 627553x dwError = err;
181 627553x }
182
183 /** Write results to output parameters and resume coroutine. */
184 625276x void invoke_handler()
185 {
186 625276x stop_cb.reset();
187
188 1247829x decode_io_result(
189 ec_out,
190 625276x cancelled.load(std::memory_order_acquire),
191 2723x dwError != 0 ? iocp_make_err(dwError, /*accept_path=*/false)
192 : std::error_code{},
193 625276x is_read, static_cast<std::size_t>(bytes_transferred),
194
2/2
✓ Branch 3 → 4 taken 2723 times.
✓ Branch 3 → 5 taken 622553 times.
625276x empty_buffer);
195
196
2/2
✓ Branch 8 → 9 taken 622862 times.
✓ Branch 8 → 10 taken 2414 times.
625276x if (bytes_out)
197 622862x *bytes_out = static_cast<std::size_t>(bytes_transferred);
198
199 625276x cont.h = h;
200
2/2
✓ Branch 10 → 11 taken 625276 times.
✓ Branch 11 → 12 taken 625276 times.
625276x dispatch_coro(ex, cont).resume();
201 625276x }
202
203 /** Disarm cancellation and abandon the coroutine handle. */
204 4x void cleanup_only()
205 {
206 4x stop_cb.reset();
207 4x h = {};
208 4x }
209 };
210
211 /** Cast OVERLAPPED* to overlapped_op*.
212
213 Safe because overlapped_op has OVERLAPPED as first base class.
214 */
215 inline overlapped_op*
216 627804x overlapped_to_op(LPOVERLAPPED ov) noexcept
217 {
218
1/2
✓ Branch 2 → 3 taken 627804 times.
✗ Branch 2 → 4 not taken.
627804x return static_cast<overlapped_op*>(ov);
219 }
220
221 } // namespace boost::corosio::detail
222
223 #endif // BOOST_COROSIO_HAS_IOCP
224
225 #endif // BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_OVERLAPPED_OP_HPP
226