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

100.0% Lines (76/76) 100.0% List of functions (11/11) 91.3% Branches (21/23)
win_overlapped_op.hpp
f(x) Functions (11)
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 2471x 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 13 times.
✓ Branch 2 → 8 taken 2458 times.
2471x if (dwError == ERROR_NETNAME_DELETED)
78
2/2
✓ Branch 3 → 4 taken 3 times.
✓ Branch 3 → 5 taken 10 times.
13x return std::make_error_code(
79 accept_path ? std::errc::connection_aborted
80 13x : std::errc::connection_reset);
81
82
9/9
✓ Branch 8 → 9 taken 17 times.
✓ Branch 8 → 10 taken 21 times.
✓ Branch 8 → 11 taken 8 times.
✓ Branch 8 → 12 taken 2 times.
✓ Branch 8 → 13 taken 2 times.
✓ Branch 8 → 14 taken 2 times.
✓ Branch 8 → 15 taken 259 times.
✓ Branch 8 → 16 taken 5 times.
✓ Branch 8 → 17 taken 2142 times.
2458x switch (dwError)
83 {
84 17x case WSAECONNRESET: // 10054
85 17x return std::make_error_code(std::errc::connection_reset);
86 21x case WSAECONNREFUSED:
87 case ERROR_CONNECTION_REFUSED: // 10061 / 1225
88 21x return std::make_error_code(std::errc::connection_refused);
89 8x case WSAECONNABORTED:
90 case ERROR_CONNECTION_ABORTED: // 10053 / 1236
91 8x return std::make_error_code(std::errc::connection_aborted);
92 2x case WSAENETUNREACH:
93 case ERROR_NETWORK_UNREACHABLE: // 10051 / 1231
94 2x return std::make_error_code(std::errc::network_unreachable);
95 2x case WSAEHOSTUNREACH:
96 case ERROR_HOST_UNREACHABLE: // 10065 / 1232
97 2x return std::make_error_code(std::errc::host_unreachable);
98 2x case WSAETIMEDOUT:
99 case ERROR_SEM_TIMEOUT: // 10060 / 121
100 2x return std::make_error_code(std::errc::timed_out);
101 // Closed-object contract: MSVC maps ERROR_INVALID_HANDLE to
102 // invalid_argument and MinGW's WSAEBADF mapping is unreliable, so
103 // normalize both spellings of "dead handle" here.
104 259x case WSAEBADF:
105 case ERROR_INVALID_HANDLE: // 10009 / 6
106 259x return std::make_error_code(std::errc::bad_file_descriptor);
107 // A write to a pipe whose reader closed. The server end of a named
108 // pipe reports ERROR_NO_DATA ("the pipe is being closed"), and a
109 // client whose server disconnected reports ERROR_PIPE_NOT_CONNECTED.
110 5x case ERROR_BROKEN_PIPE:
111 case ERROR_NO_DATA:
112 case ERROR_PIPE_NOT_CONNECTED: // 109 / 232 / 233
113 5x return std::make_error_code(std::errc::broken_pipe);
114 2142x default:
115 2142x break;
116 }
117 2142x return make_err(dwError);
118 }
119
120 /** Map a failed zero-byte `WSARecv` read wait to readiness.
121
122 A read wait that the kernel failed with a connection error reports
123 readiness; the read that follows names the error, which Windows
124 keeps reporting (`WSAECONNRESET` on every later call). A reset
125 arriving before the wait fails it synchronously with
126 `WSAECONNRESET`, one arriving during it completes with
127 `ERROR_NETNAME_DELETED`. Cancellation and a closed socket pass
128 through.
129 */
130 inline DWORD
131 23x normalize_read_wait_error(DWORD err) noexcept
132 {
133
2/2
✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 4 taken 21 times.
23x switch (err)
134 {
135 2x case ERROR_NETNAME_DELETED: // 64, a reset through IOCP
136 case ERROR_CONNECTION_ABORTED: // 1236
137 case WSAECONNRESET:
138 case WSAECONNABORTED:
139 case WSAENETRESET:
140 case WSAESHUTDOWN:
141 2x return 0;
142 21x default:
143 21x return err;
144 }
145 }
146
147 /** Base class for IOCP overlapped operations.
148
149 Derives from both OVERLAPPED (for Windows IOCP) and scheduler_op
150 (for queueing). Uses function pointer dispatch inherited from
151 scheduler_op - no virtual functions.
152
153 The OVERLAPPED structure is at the start so we can static_cast
154 between OVERLAPPED* and overlapped_op*.
155 */
156 struct overlapped_op
157 : OVERLAPPED
158 , coro_op
159 {
160 /** Function pointer type for cancellation hook. */
161 using cancel_func_type = void (*)(overlapped_op*) noexcept;
162
163 /** Completion handshake between the I/O initiator and the GQCS completer,
164 and the release/acquire barrier that publishes the payload: the plain
165 `dwError` / `bytes_transferred` fields are written before a release
166 store to `ready_` and read after an acquiring load, so they need no
167 atomicity of their own. The CAS protocol lives at the access sites in
168 win_scheduler.hpp. */
169 std::atomic<long> ready_{0};
170 DWORD dwError = 0;
171 DWORD bytes_transferred = 0;
172 cancel_func_type cancel_func_ = nullptr;
173
174 33955x explicit overlapped_op(func_type func) noexcept : coro_op(func)
175 {
176 33955x reset_overlapped();
177 33955x }
178
179 461004x void reset_overlapped() noexcept
180 {
181 461004x Internal = 0;
182 461004x InternalHigh = 0;
183 461004x Offset = 0;
184 461004x OffsetHigh = 0;
185 461004x hEvent = nullptr;
186 461004x }
187
188 427049x void reset() noexcept
189 {
190 427049x reset_overlapped();
191 427049x ready_.store(0, std::memory_order_relaxed);
192 427049x dwError = 0;
193 427049x bytes_transferred = 0;
194 427049x empty_buffer = false;
195 427049x is_read = false;
196 // Release, not relaxed: the wait reactor decides whether a
197 // queued cancel request is stale by loading this flag, so the
198 // clear has to be ordered against the fields written above it
199 // rather than against whichever lock the caller happens to
200 // take next.
201 427049x cancelled.store(false, std::memory_order_release);
202 427049x }
203
204 // coro_op::request_cancel() (set the cancelled flag) is inherited
205 // and used directly by close()/cancel() paths. The stop_token path
206 // additionally drives the kernel via on_cancel() below.
207
208 1111x void do_cancel() noexcept
209 {
210
1/2
✓ Branch 2 → 3 taken 1111 times.
✗ Branch 2 → 4 not taken.
1111x if (cancel_func_)
211 1111x cancel_func_(this);
212 1111x }
213
214 /** IOCP cancellation hook (stop_token path): set the flag, then issue
215 the registered CancelIoEx / wait-reactor deregister via cancel_func_. */
216 1111x void on_cancel() noexcept override
217 {
218 1111x request_cancel();
219 1111x do_cancel();
220 1111x }
221
222 423545x void store_result(DWORD bytes, DWORD err) noexcept
223 {
224 423545x bytes_transferred = bytes;
225 423545x dwError = err;
226 423545x }
227
228 /** Write results to output parameters and resume coroutine. */
229 421827x void invoke_handler()
230 {
231 421827x stop_cb.reset();
232
233 841228x decode_io_result(
234 421827x ec_out, bytes_out, cancelled.load(std::memory_order_acquire),
235 2426x dwError != 0 ? iocp_make_err(dwError, /*accept_path=*/false)
236 : std::error_code{},
237
2/2
✓ Branch 3 → 4 taken 2426 times.
✓ Branch 3 → 5 taken 419401 times.
421827x is_read, static_cast<std::size_t>(bytes_transferred), empty_buffer);
238
239 421827x cont.h = h;
240
2/2
✓ Branch 8 → 9 taken 421827 times.
✓ Branch 9 → 10 taken 421827 times.
421827x dispatch_coro(ex, cont).resume();
241 421827x }
242
243 /** Disarm cancellation and abandon the coroutine handle. */
244 33x void cleanup_only()
245 {
246 33x stop_cb.reset();
247 33x h = {};
248 33x }
249 };
250
251 /** Cast OVERLAPPED* to overlapped_op*.
252
253 Safe because overlapped_op has OVERLAPPED as first base class.
254 */
255 inline overlapped_op*
256 427000x overlapped_to_op(LPOVERLAPPED ov) noexcept
257 {
258
1/2
✓ Branch 2 → 3 taken 427000 times.
✗ Branch 2 → 4 not taken.
427000x return static_cast<overlapped_op*>(ov);
259 }
260
261 } // namespace boost::corosio::detail
262
263 #endif // BOOST_COROSIO_HAS_IOCP
264
265 #endif // BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_OVERLAPPED_OP_HPP
266