include/boost/corosio/native/detail/iocp/win_resolver_service.hpp
89.1% Lines (212/238)
100.0% List of functions (25/25)
75.2% Branches (91/121)
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
72.0%
68.0%
79.5%
boost::corosio::detail::reverse_resolve_op::reverse_resolve_op()
:278
32x
100.0%
–
100.0%
boost::corosio::detail::reverse_resolve_op::do_complete(void*, boost::corosio::detail::scheduler_op*, unsigned int, unsigned int)
:284
10x
82.4%
72.2%
88.6%
boost::corosio::detail::win_resolver::win_resolver(boost::corosio::detail::win_resolver_service&)
:322
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*)
:328
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*)
:387
10x
81.0%
66.7%
80.0%
boost::corosio::detail::win_resolver::cancel()
:427
36x
83.3%
50.0%
80.0%
boost::corosio::detail::win_resolver::do_reverse_resolve_work(boost::corosio::detail::pool_work_item*)
:439
10x
100.0%
83.3%
100.0%
boost::corosio::detail::win_resolver_service::win_resolver_service(boost::capy::execution_context&, boost::corosio::detail::scheduler&)
:493
369x
100.0%
100.0%
64.3%
boost::corosio::detail::win_resolver_service::~win_resolver_service()
:500
738x
100.0%
–
100.0%
boost::corosio::detail::win_resolver_service::shutdown()
:503
369x
71.4%
50.0%
75.0%
boost::corosio::detail::win_resolver_service::construct()
:521
32x
100.0%
100.0%
70.6%
boost::corosio::detail::win_resolver_service::destroy_impl(boost::corosio::detail::win_resolver&)
:536
32x
100.0%
100.0%
66.7%
boost::corosio::detail::win_resolver_service::post(boost::corosio::detail::overlapped_op*)
:544
27x
100.0%
–
100.0%
boost::corosio::detail::win_resolver_service::work_started()
:550
27x
100.0%
–
100.0%
boost::corosio::detail::win_resolver_service::work_finished()
:556
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 |
2/2✓ Branch 35 → 36 taken 17 times.
✓ Branch 36 → 37 taken 17 times.
|
17x | dispatch_coro(op->ex, op->h).resume(); | |
| 274 | } | |||
| 275 | ||||
| 276 | // reverse_resolve_op | |||
| 277 | ||||
| 278 | 32x | inline reverse_resolve_op::reverse_resolve_op() noexcept | ||
| 279 | 32x | : overlapped_op(&do_complete) | ||
| 280 | { | |||
| 281 | 32x | } | ||
| 282 | ||||
| 283 | inline void | |||
| 284 | 10x | reverse_resolve_op::do_complete( | ||
| 285 | void* owner, | |||
| 286 | scheduler_op* base, | |||
| 287 | std::uint32_t /*bytes*/, | |||
| 288 | std::uint32_t /*error*/) | |||
| 289 | { | |||
| 290 | 10x | auto* op = static_cast<reverse_resolve_op*>(base); | ||
| 291 | ||||
| 292 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 10 times.
|
10x | if (!owner) | |
| 293 | { | |||
| 294 | ✗ | op->stop_cb.reset(); | ||
| 295 | ✗ | return; | ||
| 296 | } | |||
| 297 | ||||
| 298 | 10x | op->stop_cb.reset(); | ||
| 299 | ||||
| 300 |
1/2✓ Branch 6 → 7 taken 10 times.
✗ Branch 6 → 15 not taken.
|
10x | if (op->ec_out) | |
| 301 | { | |||
| 302 |
1/2✗ Branch 8 → 9 not taken.
✓ Branch 8 → 11 taken 10 times.
|
10x | if (op->cancelled.load(std::memory_order_acquire)) | |
| 303 | ✗ | *op->ec_out = capy::error::canceled; | ||
| 304 |
2/2✓ Branch 11 → 12 taken 1 time.
✓ Branch 11 → 13 taken 9 times.
|
10x | else if (op->gai_error != 0) | |
| 305 | 1x | *op->ec_out = make_err(static_cast<DWORD>(op->gai_error)); | ||
| 306 | else | |||
| 307 | 9x | *op->ec_out = {}; | ||
| 308 | } | |||
| 309 | ||||
| 310 |
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) && | |
| 311 |
2/2✓ Branch 18 → 19 taken 9 times.
✓ Branch 18 → 20 taken 1 time.
|
10x | op->gai_error == 0) | |
| 312 | { | |||
| 313 | 9x | *op->result_out = reverse_resolver_result( | ||
| 314 | 18x | op->ep, std::move(op->stored_host), std::move(op->stored_service)); | ||
| 315 | } | |||
| 316 | ||||
| 317 |
2/2✓ Branch 32 → 33 taken 10 times.
✓ Branch 33 → 34 taken 10 times.
|
10x | dispatch_coro(op->ex, op->h).resume(); | |
| 318 | } | |||
| 319 | ||||
| 320 | // win_resolver | |||
| 321 | ||||
| 322 | 32x | inline win_resolver::win_resolver(win_resolver_service& svc) noexcept | ||
| 323 | 32x | : svc_(svc) | ||
| 324 | { | |||
| 325 | 32x | } | ||
| 326 | ||||
| 327 | inline std::coroutine_handle<> | |||
| 328 | 17x | win_resolver::resolve( | ||
| 329 | std::coroutine_handle<> h, | |||
| 330 | capy::executor_ref d, | |||
| 331 | std::string_view host, | |||
| 332 | std::string_view service, | |||
| 333 | resolve_flags flags, | |||
| 334 | std::stop_token token, | |||
| 335 | std::error_code* ec, | |||
| 336 | resolver_results* out) | |||
| 337 | { | |||
| 338 | 17x | auto& op = op_; | ||
| 339 | 17x | op.reset(); | ||
| 340 | 17x | op.h = h; | ||
| 341 | 17x | op.ex = d; | ||
| 342 | 17x | op.ec_out = ec; | ||
| 343 | 17x | op.out = out; | ||
| 344 | 17x | op.impl = this; | ||
| 345 |
1/1✓ Branch 3 → 4 taken 17 times.
|
17x | op.host = host; | |
| 346 |
1/1✓ Branch 4 → 5 taken 17 times.
|
17x | op.service = service; | |
| 347 |
1/1✓ Branch 5 → 6 taken 17 times.
|
17x | op.host_w = resolver_detail::to_wide(host); | |
| 348 |
1/1✓ Branch 8 → 9 taken 17 times.
|
17x | op.service_w = resolver_detail::to_wide(service); | |
| 349 | 17x | op.start(token); | ||
| 350 | ||||
| 351 | 17x | ADDRINFOEXW hints{}; | ||
| 352 | 17x | hints.ai_family = AF_UNSPEC; | ||
| 353 | 17x | hints.ai_socktype = SOCK_STREAM; | ||
| 354 | 17x | hints.ai_flags = resolver_detail::flags_to_hints(flags); | ||
| 355 | ||||
| 356 | // Keep io_context alive while resolution is pending | |||
| 357 | 17x | svc_.work_started(); | ||
| 358 | ||||
| 359 |
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( | |
| 360 | 34x | op.host_w.empty() ? nullptr : op.host_w.c_str(), | ||
| 361 | 34x | op.service_w.empty() ? nullptr : op.service_w.c_str(), NS_DNS, nullptr, | ||
| 362 | &hints, &op.results, nullptr, &op, &resolve_op::completion, | |||
| 363 | &op.cancel_handle); | |||
| 364 | ||||
| 365 |
2/2✓ Branch 25 → 26 taken 11 times.
✓ Branch 25 → 32 taken 6 times.
|
17x | if (result != WSA_IO_PENDING) | |
| 366 | { | |||
| 367 | // Completed synchronously - callback won't be invoked | |||
| 368 | 11x | svc_.work_finished(); | ||
| 369 | ||||
| 370 |
2/2✓ Branch 27 → 28 taken 9 times.
✓ Branch 27 → 29 taken 2 times.
|
11x | if (result == 0) | |
| 371 | { | |||
| 372 | // Completed synchronously | |||
| 373 | 9x | op.dwError = 0; | ||
| 374 | } | |||
| 375 | else | |||
| 376 | { | |||
| 377 |
1/1✓ Branch 29 → 30 taken 2 times.
|
2x | op.dwError = static_cast<DWORD>(::WSAGetLastError()); | |
| 378 | } | |||
| 379 | ||||
| 380 |
1/1✓ Branch 31 → 32 taken 11 times.
|
11x | svc_.post(&op); | |
| 381 | } | |||
| 382 | // completion is always posted to scheduler queue, never inline. | |||
| 383 | 17x | return std::noop_coroutine(); | ||
| 384 | } | |||
| 385 | ||||
| 386 | inline std::coroutine_handle<> | |||
| 387 | 10x | win_resolver::reverse_resolve( | ||
| 388 | std::coroutine_handle<> h, | |||
| 389 | capy::executor_ref d, | |||
| 390 | endpoint const& ep, | |||
| 391 | reverse_flags flags, | |||
| 392 | std::stop_token token, | |||
| 393 | std::error_code* ec, | |||
| 394 | reverse_resolver_result* result_out) | |||
| 395 | { | |||
| 396 | 10x | auto& op = reverse_op_; | ||
| 397 | 10x | op.reset(); | ||
| 398 | 10x | op.h = h; | ||
| 399 | 10x | op.ex = d; | ||
| 400 | 10x | op.ec_out = ec; | ||
| 401 | 10x | op.result_out = result_out; | ||
| 402 | 10x | op.impl = this; | ||
| 403 | 10x | op.ep = ep; | ||
| 404 | 10x | op.flags = flags; | ||
| 405 | 10x | op.start(token); | ||
| 406 | ||||
| 407 | // Keep io_context alive while resolution is pending | |||
| 408 | 10x | svc_.work_started(); | ||
| 409 | ||||
| 410 | // Prevent impl destruction while work is in flight | |||
| 411 | 10x | reverse_pool_op_.resolver_ = this; | ||
| 412 |
1/1✓ Branch 7 → 8 taken 10 times.
|
10x | reverse_pool_op_.ref_ = this->shared_from_this(); | |
| 413 | 10x | reverse_pool_op_.func_ = &win_resolver::do_reverse_resolve_work; | ||
| 414 |
1/2✗ Branch 12 → 13 not taken.
✓ Branch 12 → 17 taken 10 times.
|
10x | if (!svc_.pool().post(&reverse_pool_op_)) | |
| 415 | { | |||
| 416 | // Pool shut down — complete with cancellation | |||
| 417 | ✗ | reverse_pool_op_.ref_.reset(); | ||
| 418 | ✗ | op.cancelled.store(true, std::memory_order_release); | ||
| 419 | ✗ | svc_.work_finished(); | ||
| 420 | ✗ | svc_.post(&reverse_op_); | ||
| 421 | } | |||
| 422 | // completion is always posted to scheduler queue, never inline. | |||
| 423 | 10x | return std::noop_coroutine(); | ||
| 424 | } | |||
| 425 | ||||
| 426 | inline void | |||
| 427 | 36x | win_resolver::cancel() noexcept | ||
| 428 | { | |||
| 429 | 36x | op_.request_cancel(); | ||
| 430 | 36x | reverse_op_.request_cancel(); | ||
| 431 | ||||
| 432 |
1/2✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 36 times.
|
36x | if (op_.cancel_handle) | |
| 433 | { | |||
| 434 | ✗ | ::GetAddrInfoExCancel(&op_.cancel_handle); | ||
| 435 | } | |||
| 436 | 36x | } | ||
| 437 | ||||
| 438 | inline void | |||
| 439 | 10x | win_resolver::do_reverse_resolve_work(pool_work_item* w) noexcept | ||
| 440 | { | |||
| 441 | 10x | auto* pw = static_cast<pool_op*>(w); | ||
| 442 | 10x | auto* self = pw->resolver_; | ||
| 443 | ||||
| 444 | 10x | sockaddr_storage ss{}; | ||
| 445 | int ss_len; | |||
| 446 | ||||
| 447 |
2/2✓ Branch 3 → 4 taken 8 times.
✓ Branch 3 → 6 taken 2 times.
|
10x | if (self->reverse_op_.ep.is_v4()) | |
| 448 | { | |||
| 449 | 8x | auto sa = to_sockaddr_in(self->reverse_op_.ep); | ||
| 450 | 8x | std::memcpy(&ss, &sa, sizeof(sa)); | ||
| 451 | 8x | ss_len = sizeof(sockaddr_in); | ||
| 452 | } | |||
| 453 | else | |||
| 454 | { | |||
| 455 | 2x | auto sa = to_sockaddr_in6(self->reverse_op_.ep); | ||
| 456 | 2x | std::memcpy(&ss, &sa, sizeof(sa)); | ||
| 457 | 2x | ss_len = sizeof(sockaddr_in6); | ||
| 458 | } | |||
| 459 | ||||
| 460 | wchar_t host[NI_MAXHOST]; | |||
| 461 | wchar_t service[NI_MAXSERV]; | |||
| 462 | ||||
| 463 | 10x | int result = ::GetNameInfoW( | ||
| 464 | reinterpret_cast<sockaddr*>(&ss), ss_len, host, NI_MAXHOST, service, | |||
| 465 | NI_MAXSERV, | |||
| 466 | resolver_detail::flags_to_ni_flags(self->reverse_op_.flags)); | |||
| 467 | ||||
| 468 |
1/2✓ Branch 11 → 12 taken 10 times.
✗ Branch 11 → 23 not taken.
|
10x | if (!self->reverse_op_.cancelled.load(std::memory_order_acquire)) | |
| 469 | { | |||
| 470 |
2/2✓ Branch 12 → 13 taken 9 times.
✓ Branch 12 → 22 taken 1 time.
|
10x | if (result == 0) | |
| 471 | { | |||
| 472 | 9x | self->reverse_op_.stored_host = resolver_detail::from_wide(host); | ||
| 473 | self->reverse_op_.stored_service = | |||
| 474 | 9x | resolver_detail::from_wide(service); | ||
| 475 | 9x | self->reverse_op_.gai_error = 0; | ||
| 476 | } | |||
| 477 | else | |||
| 478 | { | |||
| 479 | 1x | self->reverse_op_.gai_error = result; | ||
| 480 | } | |||
| 481 | } | |||
| 482 | ||||
| 483 | 10x | self->svc_.work_finished(); | ||
| 484 | ||||
| 485 | // Move ref to stack before post — post may trigger destroy_impl | |||
| 486 | // which erases the last shared_ptr, destroying *self (and *pw) | |||
| 487 | 10x | auto ref = std::move(pw->ref_); | ||
| 488 | 10x | self->svc_.post(&self->reverse_op_); | ||
| 489 | 10x | } | ||
| 490 | ||||
| 491 | // win_resolver_service | |||
| 492 | ||||
| 493 | 369x | inline win_resolver_service::win_resolver_service( | ||
| 494 | 369x | capy::execution_context& ctx, scheduler& sched) | ||
| 495 | 369x | : sched_(sched) | ||
| 496 |
2/2✓ Branch 5 → 6 taken 369 times.
✓ Branch 6 → 7 taken 369 times.
|
369x | , pool_(ctx.make_service<thread_pool>()) | |
| 497 | { | |||
| 498 | 369x | } | ||
| 499 | ||||
| 500 | 738x | inline win_resolver_service::~win_resolver_service() {} | ||
| 501 | ||||
| 502 | inline void | |||
| 503 | 369x | win_resolver_service::shutdown() | ||
| 504 | { | |||
| 505 | 369x | std::lock_guard<win_mutex> lock(mutex_); | ||
| 506 | ||||
| 507 | // Cancel all resolvers (sets cancelled flag checked by pool threads) | |||
| 508 |
1/2✗ Branch 6 → 4 not taken.
✓ Branch 6 → 7 taken 369 times.
|
369x | for (auto* impl = resolver_list_.pop_front(); impl != nullptr; | |
| 509 | ✗ | impl = resolver_list_.pop_front()) | ||
| 510 | { | |||
| 511 | ✗ | impl->cancel(); | ||
| 512 | } | |||
| 513 | ||||
| 514 | // Clear the map which releases shared_ptrs. | |||
| 515 | // The thread pool service shuts down separately via | |||
| 516 | // execution_context service ordering. | |||
| 517 | 369x | resolver_ptrs_.clear(); | ||
| 518 | 369x | } | ||
| 519 | ||||
| 520 | inline io_object::implementation* | |||
| 521 | 32x | win_resolver_service::construct() | ||
| 522 | { | |||
| 523 |
1/1✓ Branch 2 → 3 taken 32 times.
|
32x | auto ptr = std::make_shared<win_resolver>(*this); | |
| 524 | 32x | auto* impl = ptr.get(); | ||
| 525 | ||||
| 526 | { | |||
| 527 | 32x | std::lock_guard<win_mutex> lock(mutex_); | ||
| 528 | 32x | resolver_list_.push_back(impl); | ||
| 529 |
1/1✓ Branch 7 → 8 taken 32 times.
|
32x | resolver_ptrs_[impl] = std::move(ptr); | |
| 530 | 32x | } | ||
| 531 | ||||
| 532 | 32x | return impl; | ||
| 533 | 32x | } | ||
| 534 | ||||
| 535 | inline void | |||
| 536 | 32x | win_resolver_service::destroy_impl(win_resolver& impl) | ||
| 537 | { | |||
| 538 | 32x | std::lock_guard<win_mutex> lock(mutex_); | ||
| 539 | 32x | resolver_list_.remove(&impl); | ||
| 540 |
1/1✓ Branch 4 → 5 taken 32 times.
|
32x | resolver_ptrs_.erase(&impl); | |
| 541 | 32x | } | ||
| 542 | ||||
| 543 | inline void | |||
| 544 | 27x | win_resolver_service::post(overlapped_op* op) | ||
| 545 | { | |||
| 546 | 27x | sched_.post(op); | ||
| 547 | 27x | } | ||
| 548 | ||||
| 549 | inline void | |||
| 550 | 27x | win_resolver_service::work_started() noexcept | ||
| 551 | { | |||
| 552 | 27x | sched_.work_started(); | ||
| 553 | 27x | } | ||
| 554 | ||||
| 555 | inline void | |||
| 556 | 27x | win_resolver_service::work_finished() noexcept | ||
| 557 | { | |||
| 558 | 27x | sched_.work_finished(); | ||
| 559 | 27x | } | ||
| 560 | ||||
| 561 | } // namespace boost::corosio::detail | |||
| 562 | ||||
| 563 | #endif // BOOST_COROSIO_HAS_IOCP | |||
| 564 | ||||
| 565 | #endif // BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_RESOLVER_SERVICE_HPP | |||
| 566 |