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

85.5% Lines (59/69) 90.0% List of functions (9/10) 76.2% Branches (16/21)
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 1904x 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 3 times.
✓ Branch 2 → 8 taken 1901 times.
1904x if (dwError == ERROR_NETNAME_DELETED)
78
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 3 times.
3x return std::make_error_code(accept_path
79 ? std::errc::connection_aborted
80 3x : std::errc::connection_reset);
81
82
4/7
✓ Branch 8 → 9 taken 4 times.
✓ Branch 8 → 10 taken 10 times.
✓ Branch 8 → 11 taken 2 times.
✗ Branch 8 → 12 not taken.
✗ Branch 8 → 13 not taken.
✗ Branch 8 → 14 not taken.
✓ Branch 8 → 15 taken 1885 times.
1901x switch (dwError)
83 {
84 4x case WSAECONNRESET: // 10054
85 4x return std::make_error_code(std::errc::connection_reset);
86 10x case WSAECONNREFUSED: case ERROR_CONNECTION_REFUSED: // 10061 / 1225
87 10x return std::make_error_code(std::errc::connection_refused);
88 2x case WSAECONNABORTED: case ERROR_CONNECTION_ABORTED: // 10053 / 1236
89 2x return std::make_error_code(std::errc::connection_aborted);
90 case WSAENETUNREACH: case ERROR_NETWORK_UNREACHABLE: // 10051 / 1231
91 return std::make_error_code(std::errc::network_unreachable);
92 case WSAEHOSTUNREACH: case ERROR_HOST_UNREACHABLE: // 10065 / 1232
93 return std::make_error_code(std::errc::host_unreachable);
94 case WSAETIMEDOUT: case ERROR_SEM_TIMEOUT: // 10060 / 121
95 return std::make_error_code(std::errc::timed_out);
96 1885x default:
97 1885x break;
98 }
99 1885x return make_err(dwError);
100 }
101
102 /** Base class for IOCP overlapped operations.
103
104 Derives from both OVERLAPPED (for Windows IOCP) and scheduler_op
105 (for queueing). Uses function pointer dispatch inherited from
106 scheduler_op - no virtual functions.
107
108 The OVERLAPPED structure is at the start so we can static_cast
109 between OVERLAPPED* and overlapped_op*.
110 */
111 struct overlapped_op
112 : OVERLAPPED
113 , coro_op
114 {
115 /** Function pointer type for cancellation hook. */
116 using cancel_func_type = void (*)(overlapped_op*) noexcept;
117
118 /** Completion handshake between the I/O initiator and the GQCS completer,
119 and the release/acquire barrier that publishes the payload: the plain
120 `dwError` / `bytes_transferred` fields are written before a release
121 store to `ready_` and read after an acquiring load, so they need no
122 atomicity of their own. The CAS protocol lives at the access sites in
123 win_scheduler.hpp. */
124 std::atomic<long> ready_{0};
125 DWORD dwError = 0;
126 DWORD bytes_transferred = 0;
127 cancel_func_type cancel_func_ = nullptr;
128
129 19119x explicit overlapped_op(func_type func) noexcept : coro_op(func)
130 {
131 19119x reset_overlapped();
132 19119x }
133
134 516556x void reset_overlapped() noexcept
135 {
136 516556x Internal = 0;
137 516556x InternalHigh = 0;
138 516556x Offset = 0;
139 516556x OffsetHigh = 0;
140 516556x hEvent = nullptr;
141 516556x }
142
143 497437x void reset() noexcept
144 {
145 497437x reset_overlapped();
146 497437x ready_.store(0, std::memory_order_relaxed);
147 497437x dwError = 0;
148 497437x bytes_transferred = 0;
149 497437x empty_buffer = false;
150 497437x is_read = false;
151 497437x cancelled.store(false, std::memory_order_relaxed);
152 497437x }
153
154 // coro_op::request_cancel() (set the cancelled flag) is inherited
155 // and used directly by close()/cancel() paths. The stop_token path
156 // additionally drives the kernel via on_cancel() below.
157
158 789x void do_cancel() noexcept
159 {
160
2/2
✓ Branch 2 → 3 taken 787 times.
✓ Branch 2 → 4 taken 2 times.
789x if (cancel_func_)
161 787x cancel_func_(this);
162 789x }
163
164 /** IOCP cancellation hook (stop_token path): set the flag, then issue
165 the registered CancelIoEx / wait-reactor deregister via cancel_func_. */
166 789x void on_cancel() noexcept override
167 {
168 789x request_cancel();
169 789x do_cancel();
170 789x }
171
172 497347x void store_result(DWORD bytes, DWORD err) noexcept
173 {
174 497347x bytes_transferred = bytes;
175 497347x dwError = err;
176 497347x }
177
178 /** Write results to output parameters and resume coroutine. */
179 495985x void invoke_handler()
180 {
181 495985x stop_cb.reset();
182
183 990066x decode_io_result(
184 ec_out,
185 495985x cancelled.load(std::memory_order_acquire),
186 1904x dwError != 0 ? iocp_make_err(dwError, /*accept_path=*/false)
187 : std::error_code{},
188 495985x is_read, static_cast<std::size_t>(bytes_transferred),
189
2/2
✓ Branch 3 → 4 taken 1904 times.
✓ Branch 3 → 5 taken 494081 times.
495985x empty_buffer);
190
191
2/2
✓ Branch 8 → 9 taken 494689 times.
✓ Branch 8 → 10 taken 1296 times.
495985x if (bytes_out)
192 494689x *bytes_out = static_cast<std::size_t>(bytes_transferred);
193
194 495985x cont.h = h;
195
2/2
✓ Branch 10 → 11 taken 495985 times.
✓ Branch 11 → 12 taken 495985 times.
495985x dispatch_coro(ex, cont).resume();
196 495985x }
197
198 /** Disarm cancellation and abandon the coroutine handle. */
199 void cleanup_only()
200 {
201 stop_cb.reset();
202 h = {};
203 }
204 };
205
206 /** Cast OVERLAPPED* to overlapped_op*.
207
208 Safe because overlapped_op has OVERLAPPED as first base class.
209 */
210 inline overlapped_op*
211 497406x overlapped_to_op(LPOVERLAPPED ov) noexcept
212 {
213
1/2
✓ Branch 2 → 3 taken 497406 times.
✗ Branch 2 → 4 not taken.
497406x return static_cast<overlapped_op*>(ov);
214 }
215
216 } // namespace boost::corosio::detail
217
218 #endif // BOOST_COROSIO_HAS_IOCP
219
220 #endif // BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_OVERLAPPED_OP_HPP
221