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

89.2% Lines (214/240) 100.0% List of functions (25/25) 75.2% Branches (91/121)
win_resolver_service.hpp
f(x) Functions (25)
Function Calls Lines Branches Blocks
boost::corosio::detail::win_resolver_service::destroy(boost::corosio::io_object::implementation*) :49 32x 100.0% 100.0% boost::corosio::detail::win_resolver_service::pool() :85 10x 100.0% 100.0% boost::corosio::detail::resolver_detail::to_wide[abi:cxx11](std::basic_string_view<char, std::char_traits<char> >) :103 34x 83.3% 71.4% 73.1% boost::corosio::detail::resolver_detail::flags_to_hints(boost::corosio::resolve_flags) :123 17x 73.3% 66.7% 80.0% boost::corosio::detail::resolver_detail::flags_to_ni_flags(boost::corosio::reverse_flags) :145 10x 90.9% 87.5% 92.9% boost::corosio::detail::resolver_detail::from_wide[abi:cxx11](std::basic_string_view<wchar_t, std::char_traits<wchar_t> >) :163 18x 83.3% 71.4% 73.1% boost::corosio::detail::resolver_detail::convert_results(addrinfoexW*, std::basic_string_view<char, std::char_traits<char> >, std::basic_string_view<char, std::char_traits<char> >) :185 14x 100.0% 88.9% 73.1% boost::corosio::detail::resolve_op::completion(unsigned long, unsigned long, _OVERLAPPED*) :214 6x 100.0% 50.0% 83.3% boost::corosio::detail::resolve_op::resolve_op() :222 32x 100.0% 100.0% boost::corosio::detail::resolve_op::do_complete(void*, boost::corosio::detail::scheduler_op*, unsigned int, unsigned int) :225 17x 73.1% 68.0% 79.5% boost::corosio::detail::reverse_resolve_op::reverse_resolve_op() :279 32x 100.0% 100.0% boost::corosio::detail::reverse_resolve_op::do_complete(void*, boost::corosio::detail::scheduler_op*, unsigned int, unsigned int) :285 10x 83.3% 72.2% 88.6% boost::corosio::detail::win_resolver::win_resolver(boost::corosio::detail::win_resolver_service&) :324 32x 100.0% 100.0% boost::corosio::detail::win_resolver::resolve(std::__n4861::coroutine_handle<void>, boost::capy::executor_ref, std::basic_string_view<char, std::char_traits<char> >, std::basic_string_view<char, std::char_traits<char> >, boost::corosio::resolve_flags, std::stop_token, std::error_code*, boost::corosio::resolver_results*) :330 17x 100.0% 86.7% 89.2% boost::corosio::detail::win_resolver::reverse_resolve(std::__n4861::coroutine_handle<void>, boost::capy::executor_ref, boost::corosio::endpoint const&, boost::corosio::reverse_flags, std::stop_token, std::error_code*, boost::corosio::reverse_resolver_result*) :389 10x 81.0% 66.7% 80.0% boost::corosio::detail::win_resolver::cancel() :429 36x 83.3% 50.0% 80.0% boost::corosio::detail::win_resolver::do_reverse_resolve_work(boost::corosio::detail::pool_work_item*) :441 10x 100.0% 83.3% 100.0% boost::corosio::detail::win_resolver_service::win_resolver_service(boost::capy::execution_context&, boost::corosio::detail::scheduler&) :495 431x 100.0% 100.0% 64.3% boost::corosio::detail::win_resolver_service::~win_resolver_service() :502 862x 100.0% 100.0% boost::corosio::detail::win_resolver_service::shutdown() :505 431x 71.4% 50.0% 75.0% boost::corosio::detail::win_resolver_service::construct() :523 32x 100.0% 100.0% 70.6% boost::corosio::detail::win_resolver_service::destroy_impl(boost::corosio::detail::win_resolver&) :538 32x 100.0% 100.0% 66.7% boost::corosio::detail::win_resolver_service::post(boost::corosio::detail::overlapped_op*) :546 27x 100.0% 100.0% boost::corosio::detail::win_resolver_service::work_started() :552 27x 100.0% 100.0% boost::corosio::detail::win_resolver_service::work_finished() :558 27x 100.0% 100.0%
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2025 Vinnie Falco ([email protected])
3 // Copyright (c) 2026 Steve Gerbino
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Official repository: https://github.com/cppalliance/corosio
9 //
10
11 #ifndef BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_RESOLVER_SERVICE_HPP
12 #define BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_RESOLVER_SERVICE_HPP
13
14 #include <boost/corosio/detail/platform.hpp>
15
16 #if BOOST_COROSIO_HAS_IOCP
17
18 #include <boost/corosio/native/detail/iocp/win_resolver.hpp>
19 #include <boost/corosio/detail/thread_pool.hpp>
20
21 #include <unordered_map>
22
23 namespace boost::corosio::detail {
24
25 /** Windows IOCP resolver management service.
26
27 This service owns all resolver implementations and coordinates their
28 lifecycle. It provides:
29
30 - Resolver implementation allocation and deallocation
31 - Async DNS resolution via GetAddrInfoExW
32 - Graceful shutdown - destroys all implementations when io_context stops
33
34 @par Thread Safety
35 All public member functions are thread-safe.
36
37 @note Only available on Windows platforms with _WIN32_WINNT >= 0x0602.
38 */
39 class BOOST_COROSIO_DECL win_resolver_service final
40 : private win_wsa_init
41 , public capy::execution_context::service
42 , public io_object::io_service
43 {
44 public:
45 using key_type = win_resolver_service;
46
47 io_object::implementation* construct() override;
48
49 32x void destroy(io_object::implementation* p) override
50 {
51 32x auto& impl = static_cast<win_resolver&>(*p);
52 32x impl.cancel();
53 32x destroy_impl(impl);
54 32x }
55
56 /** Construct the resolver service.
57
58 @param ctx Reference to the owning execution_context.
59 @param sched Reference to the scheduler for posting completions.
60 */
61 win_resolver_service(capy::execution_context& ctx, scheduler& sched);
62
63 /** Destroy the resolver service. */
64 ~win_resolver_service();
65
66 win_resolver_service(win_resolver_service const&) = delete;
67 win_resolver_service& operator=(win_resolver_service const&) = delete;
68
69 /** Shut down the service. */
70 void shutdown() override;
71
72 /** Destroy a resolver implementation. */
73 void destroy_impl(win_resolver& impl);
74
75 /** Post an operation for completion. */
76 void post(overlapped_op* op);
77
78 /** Notify scheduler of pending I/O work. */
79 void work_started() noexcept;
80
81 /** Notify scheduler that I/O work completed. */
82 void work_finished() noexcept;
83
84 /** Return the resolver thread pool. */
85 10x thread_pool& pool() noexcept
86 {
87 10x return pool_;
88 }
89
90 private:
91 scheduler& sched_;
92 thread_pool& pool_;
93 win_mutex mutex_;
94 intrusive_list<win_resolver> resolver_list_;
95 std::unordered_map<win_resolver*, std::shared_ptr<win_resolver>>
96 resolver_ptrs_;
97 };
98
99 namespace resolver_detail {
100
101 // Convert narrow string to wide string
102 inline std::wstring
103 34x to_wide(std::string_view s)
104 {
105
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 34 times.
34x if (s.empty())
106 return {};
107
108
1/1
✓ Branch 7 → 8 taken 34 times.
34x int len = ::MultiByteToWideChar(
109 34x CP_UTF8, 0, s.data(), static_cast<int>(s.size()), nullptr, 0);
110
111
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 34 times.
34x if (len <= 0)
112 return {};
113
114
1/1
✓ Branch 12 → 13 taken 34 times.
34x std::wstring result(static_cast<std::size_t>(len), L'\0');
115
1/1
✓ Branch 17 → 18 taken 34 times.
68x ::MultiByteToWideChar(
116 34x CP_UTF8, 0, s.data(), static_cast<int>(s.size()), result.data(), len);
117
118 34x return result;
119 34x }
120
121 // Convert resolve_flags to ADDRINFOEXW hints
122 inline int
123 17x flags_to_hints(resolve_flags flags)
124 {
125 17x int hints = 0;
126
127
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 17 times.
17x if ((flags & resolve_flags::passive) != resolve_flags::none)
128 hints |= AI_PASSIVE;
129
2/2
✓ Branch 6 → 7 taken 11 times.
✓ Branch 6 → 8 taken 6 times.
17x if ((flags & resolve_flags::numeric_host) != resolve_flags::none)
130 11x hints |= AI_NUMERICHOST;
131
2/2
✓ Branch 9 → 10 taken 8 times.
✓ Branch 9 → 11 taken 9 times.
17x if ((flags & resolve_flags::numeric_service) != resolve_flags::none)
132 8x hints |= AI_NUMERICSERV;
133
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 17 times.
17x if ((flags & resolve_flags::address_configured) != resolve_flags::none)
134 hints |= AI_ADDRCONFIG;
135
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 17 times.
17x if ((flags & resolve_flags::v4_mapped) != resolve_flags::none)
136 hints |= AI_V4MAPPED;
137
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 17 times.
17x if ((flags & resolve_flags::all_matching) != resolve_flags::none)
138 hints |= AI_ALL;
139
140 17x return hints;
141 }
142
143 // Convert reverse_flags to getnameinfo NI_* flags
144 inline int
145 10x flags_to_ni_flags(reverse_flags flags)
146 {
147 10x int ni_flags = 0;
148
149
2/2
✓ Branch 3 → 4 taken 5 times.
✓ Branch 3 → 5 taken 5 times.
10x if ((flags & reverse_flags::numeric_host) != reverse_flags::none)
150 5x ni_flags |= NI_NUMERICHOST;
151
2/2
✓ Branch 6 → 7 taken 5 times.
✓ Branch 6 → 8 taken 5 times.
10x if ((flags & reverse_flags::numeric_service) != reverse_flags::none)
152 5x ni_flags |= NI_NUMERICSERV;
153
2/2
✓ Branch 9 → 10 taken 1 time.
✓ Branch 9 → 11 taken 9 times.
10x if ((flags & reverse_flags::name_required) != reverse_flags::none)
154 1x ni_flags |= NI_NAMEREQD;
155
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 10 times.
10x if ((flags & reverse_flags::datagram_service) != reverse_flags::none)
156 ni_flags |= NI_DGRAM;
157
158 10x return ni_flags;
159 }
160
161 // Convert wide string to UTF-8 string
162 inline std::string
163 18x from_wide(std::wstring_view s)
164 {
165
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 18 times.
18x if (s.empty())
166 return {};
167
168
1/1
✓ Branch 7 → 8 taken 18 times.
18x int len = ::WideCharToMultiByte(
169 18x CP_UTF8, 0, s.data(), static_cast<int>(s.size()), nullptr, 0, nullptr,
170 nullptr);
171
172
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 18 times.
18x if (len <= 0)
173 return {};
174
175
1/1
✓ Branch 12 → 13 taken 18 times.
18x std::string result(static_cast<std::size_t>(len), '\0');
176
1/1
✓ Branch 17 → 18 taken 18 times.
36x ::WideCharToMultiByte(
177 18x CP_UTF8, 0, s.data(), static_cast<int>(s.size()), result.data(), len,
178 nullptr, nullptr);
179
180 18x return result;
181 18x }
182
183 // Convert ADDRINFOEXW results to resolver_results
184 inline resolver_results
185 14x convert_results(
186 ADDRINFOEXW* ai, std::string_view host, std::string_view service)
187 {
188 14x std::vector<resolver_entry> entries;
189
190
2/2
✓ Branch 12 → 3 taken 19 times.
✓ Branch 12 → 13 taken 14 times.
33x for (auto* p = ai; p != nullptr; p = p->ai_next)
191 {
192
2/2
✓ Branch 3 → 4 taken 12 times.
✓ Branch 3 → 7 taken 7 times.
19x if (p->ai_family == AF_INET)
193 {
194 12x auto* addr = reinterpret_cast<sockaddr_in*>(p->ai_addr);
195 12x auto ep = from_sockaddr_in(*addr);
196
1/1
✓ Branch 5 → 6 taken 12 times.
12x entries.emplace_back(ep, host, service);
197 }
198
1/2
✓ Branch 7 → 8 taken 7 times.
✗ Branch 7 → 11 not taken.
7x else if (p->ai_family == AF_INET6)
199 {
200 7x auto* addr = reinterpret_cast<sockaddr_in6*>(p->ai_addr);
201 7x auto ep = from_sockaddr_in6(*addr);
202
1/1
✓ Branch 9 → 10 taken 7 times.
7x entries.emplace_back(ep, host, service);
203 }
204 }
205
206
1/1
✓ Branch 15 → 16 taken 14 times.
28x return resolver_results(std::move(entries));
207 14x }
208
209 } // namespace resolver_detail
210
211 // resolve_op
212
213 inline void CALLBACK
214 6x resolve_op::completion(DWORD dwError, DWORD /*bytes*/, OVERLAPPED* ov)
215 {
216
1/2
✓ Branch 2 → 3 taken 6 times.
✗ Branch 2 → 4 not taken.
6x auto* op = static_cast<resolve_op*>(ov);
217 6x op->dwError = dwError;
218 6x op->impl->svc_.work_finished();
219 6x op->impl->svc_.post(op);
220 6x }
221
222 32x inline resolve_op::resolve_op() noexcept : overlapped_op(&do_complete) {}
223
224 inline void
225 17x resolve_op::do_complete(
226 void* owner,
227 scheduler_op* base,
228 std::uint32_t /*bytes*/,
229 std::uint32_t /*error*/)
230 {
231 17x auto* op = static_cast<resolve_op*>(base);
232
233
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 8 taken 17 times.
17x if (!owner)
234 {
235 // Destroy path
236 op->stop_cb.reset();
237 if (op->results)
238 {
239 ::FreeAddrInfoExW(op->results);
240 op->results = nullptr;
241 }
242 op->cancel_handle = nullptr;
243 return;
244 }
245
246 17x op->stop_cb.reset();
247
248
1/2
✓ Branch 9 → 10 taken 17 times.
✗ Branch 9 → 18 not taken.
17x if (op->ec_out)
249 {
250
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 14 taken 17 times.
17x if (op->cancelled.load(std::memory_order_acquire))
251 *op->ec_out = capy::error::canceled;
252
2/2
✓ Branch 14 → 15 taken 3 times.
✓ Branch 14 → 16 taken 14 times.
17x else if (op->dwError != 0)
253 3x *op->ec_out = make_err(op->dwError);
254 else
255 14x *op->ec_out = {};
256 }
257
258
1/2
✓ Branch 20 → 21 taken 17 times.
✗ Branch 20 → 24 not taken.
17x if (op->out && !op->cancelled.load(std::memory_order_acquire) &&
259
6/8
✓ Branch 18 → 19 taken 17 times.
✗ Branch 18 → 24 not taken.
✓ Branch 21 → 22 taken 14 times.
✓ Branch 21 → 24 taken 3 times.
✓ Branch 22 → 23 taken 14 times.
✗ Branch 22 → 24 not taken.
✓ Branch 25 → 26 taken 14 times.
✓ Branch 25 → 32 taken 3 times.
34x op->dwError == 0 && op->results)
260 {
261
1/1
✓ Branch 28 → 29 taken 14 times.
28x *op->out = resolver_detail::convert_results(
262 14x op->results, op->host, op->service);
263 }
264
265
2/2
✓ Branch 32 → 33 taken 14 times.
✓ Branch 32 → 35 taken 3 times.
17x if (op->results)
266 {
267 14x ::FreeAddrInfoExW(op->results);
268 14x op->results = nullptr;
269 }
270
271 17x op->cancel_handle = nullptr;
272
273 17x op->cont_op.cont.h = op->h;
274
2/2
✓ Branch 35 → 36 taken 17 times.
✓ Branch 36 → 37 taken 17 times.
17x dispatch_coro(op->ex, op->cont_op.cont).resume();
275 }
276
277 // reverse_resolve_op
278
279 32x inline reverse_resolve_op::reverse_resolve_op() noexcept
280 32x : overlapped_op(&do_complete)
281 {
282 32x }
283
284 inline void
285 10x reverse_resolve_op::do_complete(
286 void* owner,
287 scheduler_op* base,
288 std::uint32_t /*bytes*/,
289 std::uint32_t /*error*/)
290 {
291 10x auto* op = static_cast<reverse_resolve_op*>(base);
292
293
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 10 times.
10x if (!owner)
294 {
295 op->stop_cb.reset();
296 return;
297 }
298
299 10x op->stop_cb.reset();
300
301
1/2
✓ Branch 6 → 7 taken 10 times.
✗ Branch 6 → 15 not taken.
10x if (op->ec_out)
302 {
303
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 11 taken 10 times.
10x if (op->cancelled.load(std::memory_order_acquire))
304 *op->ec_out = capy::error::canceled;
305
2/2
✓ Branch 11 → 12 taken 1 time.
✓ Branch 11 → 13 taken 9 times.
10x else if (op->gai_error != 0)
306 1x *op->ec_out = make_err(static_cast<DWORD>(op->gai_error));
307 else
308 9x *op->ec_out = {};
309 }
310
311
4/6
✓ Branch 15 → 16 taken 10 times.
✗ Branch 15 → 20 not taken.
✓ Branch 17 → 18 taken 10 times.
✗ Branch 17 → 20 not taken.
✓ Branch 21 → 22 taken 9 times.
✓ Branch 21 → 32 taken 1 time.
20x if (op->result_out && !op->cancelled.load(std::memory_order_acquire) &&
312
2/2
✓ Branch 18 → 19 taken 9 times.
✓ Branch 18 → 20 taken 1 time.
10x op->gai_error == 0)
313 {
314 9x *op->result_out = reverse_resolver_result(
315 18x op->ep, std::move(op->stored_host), std::move(op->stored_service));
316 }
317
318 10x op->cont_op.cont.h = op->h;
319
2/2
✓ Branch 32 → 33 taken 10 times.
✓ Branch 33 → 34 taken 10 times.
10x dispatch_coro(op->ex, op->cont_op.cont).resume();
320 }
321
322 // win_resolver
323
324 32x inline win_resolver::win_resolver(win_resolver_service& svc) noexcept
325 32x : svc_(svc)
326 {
327 32x }
328
329 inline std::coroutine_handle<>
330 17x win_resolver::resolve(
331 std::coroutine_handle<> h,
332 capy::executor_ref d,
333 std::string_view host,
334 std::string_view service,
335 resolve_flags flags,
336 std::stop_token token,
337 std::error_code* ec,
338 resolver_results* out)
339 {
340 17x auto& op = op_;
341 17x op.reset();
342 17x op.h = h;
343 17x op.ex = d;
344 17x op.ec_out = ec;
345 17x op.out = out;
346 17x op.impl = this;
347
1/1
✓ Branch 3 → 4 taken 17 times.
17x op.host = host;
348
1/1
✓ Branch 4 → 5 taken 17 times.
17x op.service = service;
349
1/1
✓ Branch 5 → 6 taken 17 times.
17x op.host_w = resolver_detail::to_wide(host);
350
1/1
✓ Branch 8 → 9 taken 17 times.
17x op.service_w = resolver_detail::to_wide(service);
351 17x op.start(token);
352
353 17x ADDRINFOEXW hints{};
354 17x hints.ai_family = AF_UNSPEC;
355 17x hints.ai_socktype = SOCK_STREAM;
356 17x hints.ai_flags = resolver_detail::flags_to_hints(flags);
357
358 // Keep io_context alive while resolution is pending
359 17x svc_.work_started();
360
361
3/5
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 19 taken 17 times.
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 17 times.
✓ Branch 24 → 25 taken 17 times.
51x int result = ::GetAddrInfoExW(
362 34x op.host_w.empty() ? nullptr : op.host_w.c_str(),
363 34x op.service_w.empty() ? nullptr : op.service_w.c_str(), NS_DNS, nullptr,
364 &hints, &op.results, nullptr, &op, &resolve_op::completion,
365 &op.cancel_handle);
366
367
2/2
✓ Branch 25 → 26 taken 11 times.
✓ Branch 25 → 32 taken 6 times.
17x if (result != WSA_IO_PENDING)
368 {
369 // Completed synchronously - callback won't be invoked
370 11x svc_.work_finished();
371
372
2/2
✓ Branch 27 → 28 taken 9 times.
✓ Branch 27 → 29 taken 2 times.
11x if (result == 0)
373 {
374 // Completed synchronously
375 9x op.dwError = 0;
376 }
377 else
378 {
379
1/1
✓ Branch 29 → 30 taken 2 times.
2x op.dwError = static_cast<DWORD>(::WSAGetLastError());
380 }
381
382
1/1
✓ Branch 31 → 32 taken 11 times.
11x svc_.post(&op);
383 }
384 // completion is always posted to scheduler queue, never inline.
385 17x return std::noop_coroutine();
386 }
387
388 inline std::coroutine_handle<>
389 10x win_resolver::reverse_resolve(
390 std::coroutine_handle<> h,
391 capy::executor_ref d,
392 endpoint const& ep,
393 reverse_flags flags,
394 std::stop_token token,
395 std::error_code* ec,
396 reverse_resolver_result* result_out)
397 {
398 10x auto& op = reverse_op_;
399 10x op.reset();
400 10x op.h = h;
401 10x op.ex = d;
402 10x op.ec_out = ec;
403 10x op.result_out = result_out;
404 10x op.impl = this;
405 10x op.ep = ep;
406 10x op.flags = flags;
407 10x op.start(token);
408
409 // Keep io_context alive while resolution is pending
410 10x svc_.work_started();
411
412 // Prevent impl destruction while work is in flight
413 10x reverse_pool_op_.resolver_ = this;
414
1/1
✓ Branch 7 → 8 taken 10 times.
10x reverse_pool_op_.ref_ = this->shared_from_this();
415 10x reverse_pool_op_.func_ = &win_resolver::do_reverse_resolve_work;
416
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 17 taken 10 times.
10x if (!svc_.pool().post(&reverse_pool_op_))
417 {
418 // Pool shut down — complete with cancellation
419 reverse_pool_op_.ref_.reset();
420 op.cancelled.store(true, std::memory_order_release);
421 svc_.work_finished();
422 svc_.post(&reverse_op_);
423 }
424 // completion is always posted to scheduler queue, never inline.
425 10x return std::noop_coroutine();
426 }
427
428 inline void
429 36x win_resolver::cancel() noexcept
430 {
431 36x op_.request_cancel();
432 36x reverse_op_.request_cancel();
433
434
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 36 times.
36x if (op_.cancel_handle)
435 {
436 ::GetAddrInfoExCancel(&op_.cancel_handle);
437 }
438 36x }
439
440 inline void
441 10x win_resolver::do_reverse_resolve_work(pool_work_item* w) noexcept
442 {
443 10x auto* pw = static_cast<pool_op*>(w);
444 10x auto* self = pw->resolver_;
445
446 10x sockaddr_storage ss{};
447 int ss_len;
448
449
2/2
✓ Branch 3 → 4 taken 8 times.
✓ Branch 3 → 6 taken 2 times.
10x if (self->reverse_op_.ep.is_v4())
450 {
451 8x auto sa = to_sockaddr_in(self->reverse_op_.ep);
452 8x std::memcpy(&ss, &sa, sizeof(sa));
453 8x ss_len = sizeof(sockaddr_in);
454 }
455 else
456 {
457 2x auto sa = to_sockaddr_in6(self->reverse_op_.ep);
458 2x std::memcpy(&ss, &sa, sizeof(sa));
459 2x ss_len = sizeof(sockaddr_in6);
460 }
461
462 wchar_t host[NI_MAXHOST];
463 wchar_t service[NI_MAXSERV];
464
465 10x int result = ::GetNameInfoW(
466 reinterpret_cast<sockaddr*>(&ss), ss_len, host, NI_MAXHOST, service,
467 NI_MAXSERV,
468 resolver_detail::flags_to_ni_flags(self->reverse_op_.flags));
469
470
1/2
✓ Branch 11 → 12 taken 10 times.
✗ Branch 11 → 23 not taken.
10x if (!self->reverse_op_.cancelled.load(std::memory_order_acquire))
471 {
472
2/2
✓ Branch 12 → 13 taken 9 times.
✓ Branch 12 → 22 taken 1 time.
10x if (result == 0)
473 {
474 9x self->reverse_op_.stored_host = resolver_detail::from_wide(host);
475 self->reverse_op_.stored_service =
476 9x resolver_detail::from_wide(service);
477 9x self->reverse_op_.gai_error = 0;
478 }
479 else
480 {
481 1x self->reverse_op_.gai_error = result;
482 }
483 }
484
485 10x self->svc_.work_finished();
486
487 // Move ref to stack before post — post may trigger destroy_impl
488 // which erases the last shared_ptr, destroying *self (and *pw)
489 10x auto ref = std::move(pw->ref_);
490 10x self->svc_.post(&self->reverse_op_);
491 10x }
492
493 // win_resolver_service
494
495 431x inline win_resolver_service::win_resolver_service(
496 431x capy::execution_context& ctx, scheduler& sched)
497 431x : sched_(sched)
498
2/2
✓ Branch 5 → 6 taken 431 times.
✓ Branch 6 → 7 taken 431 times.
431x , pool_(ctx.make_service<thread_pool>())
499 {
500 431x }
501
502 862x inline win_resolver_service::~win_resolver_service() {}
503
504 inline void
505 431x win_resolver_service::shutdown()
506 {
507 431x std::lock_guard<win_mutex> lock(mutex_);
508
509 // Cancel all resolvers (sets cancelled flag checked by pool threads)
510
1/2
✗ Branch 6 → 4 not taken.
✓ Branch 6 → 7 taken 431 times.
431x for (auto* impl = resolver_list_.pop_front(); impl != nullptr;
511 impl = resolver_list_.pop_front())
512 {
513 impl->cancel();
514 }
515
516 // Clear the map which releases shared_ptrs.
517 // The thread pool service shuts down separately via
518 // execution_context service ordering.
519 431x resolver_ptrs_.clear();
520 431x }
521
522 inline io_object::implementation*
523 32x win_resolver_service::construct()
524 {
525
1/1
✓ Branch 2 → 3 taken 32 times.
32x auto ptr = std::make_shared<win_resolver>(*this);
526 32x auto* impl = ptr.get();
527
528 {
529 32x std::lock_guard<win_mutex> lock(mutex_);
530 32x resolver_list_.push_back(impl);
531
1/1
✓ Branch 7 → 8 taken 32 times.
32x resolver_ptrs_[impl] = std::move(ptr);
532 32x }
533
534 32x return impl;
535 32x }
536
537 inline void
538 32x win_resolver_service::destroy_impl(win_resolver& impl)
539 {
540 32x std::lock_guard<win_mutex> lock(mutex_);
541 32x resolver_list_.remove(&impl);
542
1/1
✓ Branch 4 → 5 taken 32 times.
32x resolver_ptrs_.erase(&impl);
543 32x }
544
545 inline void
546 27x win_resolver_service::post(overlapped_op* op)
547 {
548 27x sched_.post(op);
549 27x }
550
551 inline void
552 27x win_resolver_service::work_started() noexcept
553 {
554 27x sched_.work_started();
555 27x }
556
557 inline void
558 27x win_resolver_service::work_finished() noexcept
559 {
560 27x sched_.work_finished();
561 27x }
562
563 } // namespace boost::corosio::detail
564
565 #endif // BOOST_COROSIO_HAS_IOCP
566
567 #endif // BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_RESOLVER_SERVICE_HPP
568