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

100.0% Lines (70/70) 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 2453x 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 2440 times.
2453x 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
8/8
✓ Branch 8 → 9 taken 14 times.
✓ Branch 8 → 10 taken 23 times.
✓ Branch 8 → 11 taken 7 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 2211 times.
2440x switch (dwError)
83 {
84 14x case WSAECONNRESET: // 10054
85 14x return std::make_error_code(std::errc::connection_reset);
86 23x case WSAECONNREFUSED:
87 case ERROR_CONNECTION_REFUSED: // 10061 / 1225
88 23x return std::make_error_code(std::errc::connection_refused);
89 7x case WSAECONNABORTED:
90 case ERROR_CONNECTION_ABORTED: // 10053 / 1236
91 7x 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 179x case WSAEBADF:
105 case ERROR_INVALID_HANDLE: // 10009 / 6
106 179x return std::make_error_code(std::errc::bad_file_descriptor);
107 2211x default:
108 2211x break;
109 }
110 2211x return make_err(dwError);
111 }
112
113 /** Base class for IOCP overlapped operations.
114
115 Derives from both OVERLAPPED (for Windows IOCP) and scheduler_op
116 (for queueing). Uses function pointer dispatch inherited from
117 scheduler_op - no virtual functions.
118
119 The OVERLAPPED structure is at the start so we can static_cast
120 between OVERLAPPED* and overlapped_op*.
121 */
122 struct overlapped_op
123 : OVERLAPPED
124 , coro_op
125 {
126 /** Function pointer type for cancellation hook. */
127 using cancel_func_type = void (*)(overlapped_op*) noexcept;
128
129 /** Completion handshake between the I/O initiator and the GQCS completer,
130 and the release/acquire barrier that publishes the payload: the plain
131 `dwError` / `bytes_transferred` fields are written before a release
132 store to `ready_` and read after an acquiring load, so they need no
133 atomicity of their own. The CAS protocol lives at the access sites in
134 win_scheduler.hpp. */
135 std::atomic<long> ready_{0};
136 DWORD dwError = 0;
137 DWORD bytes_transferred = 0;
138 cancel_func_type cancel_func_ = nullptr;
139
140 31833x explicit overlapped_op(func_type func) noexcept : coro_op(func)
141 {
142 31833x reset_overlapped();
143 31833x }
144
145 552553x void reset_overlapped() noexcept
146 {
147 552553x Internal = 0;
148 552553x InternalHigh = 0;
149 552553x Offset = 0;
150 552553x OffsetHigh = 0;
151 552553x hEvent = nullptr;
152 552553x }
153
154 520720x void reset() noexcept
155 {
156 520720x reset_overlapped();
157 520720x ready_.store(0, std::memory_order_relaxed);
158 520720x dwError = 0;
159 520720x bytes_transferred = 0;
160 520720x empty_buffer = false;
161 520720x is_read = false;
162 // Release, not relaxed: the wait reactor decides whether a
163 // queued cancel request is stale by loading this flag, so the
164 // clear has to be ordered against the fields written above it
165 // rather than against whichever lock the caller happens to
166 // take next.
167 520720x cancelled.store(false, std::memory_order_release);
168 520720x }
169
170 // coro_op::request_cancel() (set the cancelled flag) is inherited
171 // and used directly by close()/cancel() paths. The stop_token path
172 // additionally drives the kernel via on_cancel() below.
173
174 1120x void do_cancel() noexcept
175 {
176
2/2
✓ Branch 2 → 3 taken 1117 times.
✓ Branch 2 → 4 taken 3 times.
1120x if (cancel_func_)
177 1117x cancel_func_(this);
178 1120x }
179
180 /** IOCP cancellation hook (stop_token path): set the flag, then issue
181 the registered CancelIoEx / wait-reactor deregister via cancel_func_. */
182 1120x void on_cancel() noexcept override
183 {
184 1120x request_cancel();
185 1120x do_cancel();
186 1120x }
187
188 520336x void store_result(DWORD bytes, DWORD err) noexcept
189 {
190 520336x bytes_transferred = bytes;
191 520336x dwError = err;
192 520336x }
193
194 /** Write results to output parameters and resume coroutine. */
195 518518x void invoke_handler()
196 {
197 518518x stop_cb.reset();
198
199 1034621x decode_io_result(
200 518518x ec_out, cancelled.load(std::memory_order_acquire),
201 2415x dwError != 0 ? iocp_make_err(dwError, /*accept_path=*/false)
202 : std::error_code{},
203
2/2
✓ Branch 3 → 4 taken 2415 times.
✓ Branch 3 → 5 taken 516103 times.
518518x is_read, static_cast<std::size_t>(bytes_transferred), empty_buffer);
204
205
2/2
✓ Branch 8 → 9 taken 516465 times.
✓ Branch 8 → 10 taken 2053 times.
518518x if (bytes_out)
206 516465x *bytes_out = static_cast<std::size_t>(bytes_transferred);
207
208 518518x cont.h = h;
209
2/2
✓ Branch 10 → 11 taken 518518 times.
✓ Branch 11 → 12 taken 518518 times.
518518x dispatch_coro(ex, cont).resume();
210 518518x }
211
212 /** Disarm cancellation and abandon the coroutine handle. */
213 24x void cleanup_only()
214 {
215 24x stop_cb.reset();
216 24x h = {};
217 24x }
218 };
219
220 /** Cast OVERLAPPED* to overlapped_op*.
221
222 Safe because overlapped_op has OVERLAPPED as first base class.
223 */
224 inline overlapped_op*
225 520672x overlapped_to_op(LPOVERLAPPED ov) noexcept
226 {
227
1/2
✓ Branch 2 → 3 taken 520672 times.
✗ Branch 2 → 4 not taken.
520672x return static_cast<overlapped_op*>(ov);
228 }
229
230 } // namespace boost::corosio::detail
231
232 #endif // BOOST_COROSIO_HAS_IOCP
233
234 #endif // BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_OVERLAPPED_OP_HPP
235