95.45% Lines (21/22) 100.00% Functions (7/7)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2026 Steve Gerbino 2   // Copyright (c) 2026 Steve Gerbino
  3 + // Copyright (c) 2026 Michael Vandeberg
3   // 4   //
4   // Distributed under the Boost Software License, Version 1.0. (See accompanying 5   // Distributed under the Boost Software License, Version 1.0. (See accompanying
5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6   // 7   //
7   // Official repository: https://github.com/cppalliance/corosio 8   // Official repository: https://github.com/cppalliance/corosio
8   // 9   //
9   10  
10   #ifndef BOOST_COROSIO_NATIVE_NATIVE_RESOLVER_HPP 11   #ifndef BOOST_COROSIO_NATIVE_NATIVE_RESOLVER_HPP
11   #define BOOST_COROSIO_NATIVE_NATIVE_RESOLVER_HPP 12   #define BOOST_COROSIO_NATIVE_NATIVE_RESOLVER_HPP
12   13  
13   #include <boost/corosio/resolver.hpp> 14   #include <boost/corosio/resolver.hpp>
14   #include <boost/corosio/backend.hpp> 15   #include <boost/corosio/backend.hpp>
15   16  
16   #ifndef BOOST_COROSIO_MRDOCS 17   #ifndef BOOST_COROSIO_MRDOCS
17   #if BOOST_COROSIO_HAS_EPOLL || BOOST_COROSIO_HAS_SELECT || \ 18   #if BOOST_COROSIO_HAS_EPOLL || BOOST_COROSIO_HAS_SELECT || \
18   BOOST_COROSIO_HAS_KQUEUE 19   BOOST_COROSIO_HAS_KQUEUE
19   #include <boost/corosio/native/detail/posix/posix_resolver_service.hpp> 20   #include <boost/corosio/native/detail/posix/posix_resolver_service.hpp>
20   #endif 21   #endif
21   22  
22   #if BOOST_COROSIO_HAS_IOCP 23   #if BOOST_COROSIO_HAS_IOCP
23   #include <boost/corosio/native/detail/iocp/win_resolver_service.hpp> 24   #include <boost/corosio/native/detail/iocp/win_resolver_service.hpp>
24   #endif 25   #endif
25   #endif // !BOOST_COROSIO_MRDOCS 26   #endif // !BOOST_COROSIO_MRDOCS
26   27  
27   namespace boost::corosio { 28   namespace boost::corosio {
28   29  
29   /** An asynchronous DNS resolver with devirtualized operations. 30   /** An asynchronous DNS resolver with devirtualized operations.
30   31  
31   This class template inherits from @ref resolver and shadows 32   This class template inherits from @ref resolver and shadows
32   the `resolve` operations with versions that call the backend 33   the `resolve` operations with versions that call the backend
33   implementation directly, allowing the compiler to inline 34   implementation directly, allowing the compiler to inline
34   through the entire call chain. 35   through the entire call chain.
35   36  
36   Non-async operations (`cancel`) remain unchanged and dispatch 37   Non-async operations (`cancel`) remain unchanged and dispatch
37   through the compiled library. 38   through the compiled library.
38   39  
39   A `native_resolver` IS-A `resolver` and can be passed to any 40   A `native_resolver` IS-A `resolver` and can be passed to any
40   function expecting `resolver&`. 41   function expecting `resolver&`.
41   42  
42   @tparam Backend A backend tag value (e.g., `epoll`). 43   @tparam Backend A backend tag value (e.g., `epoll`).
43   44  
44   @par Thread Safety 45   @par Thread Safety
45   Same as @ref resolver. 46   Same as @ref resolver.
46   47  
47   @see resolver, epoll_t, iocp_t 48   @see resolver, epoll_t, iocp_t
48   */ 49   */
49   template<auto Backend> 50   template<auto Backend>
50   class native_resolver : public resolver 51   class native_resolver : public resolver
51   { 52   {
52   using backend_type = decltype(Backend); 53   using backend_type = decltype(Backend);
53   using impl_type = typename backend_type::resolver_type; 54   using impl_type = typename backend_type::resolver_type;
54   55  
HITCBC 55   2 impl_type& get_impl() noexcept 56   2 impl_type& get_impl() noexcept
56   { 57   {
HITCBC 57   2 return *static_cast<impl_type*>(h_.get()); 58   2 return *static_cast<impl_type*>(h_.get());
58   } 59   }
59   60  
60   struct native_resolve_awaitable 61   struct native_resolve_awaitable
61   { 62   {
62   native_resolver& self_; 63   native_resolver& self_;
63   std::string host_; 64   std::string host_;
64   std::string service_; 65   std::string service_;
65   resolve_flags flags_; 66   resolve_flags flags_;
66   std::stop_token token_; 67   std::stop_token token_;
67   mutable std::error_code ec_; 68   mutable std::error_code ec_;
68   mutable resolver_results results_; 69   mutable resolver_results results_;
69   70  
HITCBC 70   2 native_resolve_awaitable( 71   2 native_resolve_awaitable(
71   native_resolver& self, 72   native_resolver& self,
72   std::string_view host, 73   std::string_view host,
73   std::string_view service, 74   std::string_view service,
74   resolve_flags flags) noexcept 75   resolve_flags flags) noexcept
HITCBC 75   2 : self_(self) 76   2 : self_(self)
HITCBC 76   4 , host_(host) 77   4 , host_(host)
HITCBC 77   4 , service_(service) 78   4 , service_(service)
HITCBC 78   2 , flags_(flags) 79   2 , flags_(flags)
79   { 80   {
HITCBC 80   2 } 81   2 }
81   82  
HITCBC 82   2 bool await_ready() const noexcept 83   2 bool await_ready() const noexcept
83   { 84   {
HITCBC 84   2 return token_.stop_requested(); 85   2 return token_.stop_requested();
85   } 86   }
86   87  
HITCBC 87   2 capy::io_result<resolver_results> await_resume() const noexcept 88   2 capy::io_result<resolver_results> await_resume() const noexcept
88   { 89   {
HITCBC 89   2 if (token_.stop_requested()) 90   2 if (token_.stop_requested())
MISUBC 90   return {make_error_code(std::errc::operation_canceled), {}}; 91   return {make_error_code(std::errc::operation_canceled), {}};
HITCBC 91   2 return {ec_, std::move(results_)}; 92   2 return {ec_, std::move(results_)};
92   } 93   }
93   94  
HITCBC 94   2 auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) 95   2 auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
95   -> std::coroutine_handle<> 96   -> std::coroutine_handle<>
96   { 97   {
HITCBC 97   2 token_ = env->stop_token; 98   2 token_ = env->stop_token;
HITCBC 98   6 return self_.get_impl().resolve( 99   6 return self_.get_impl().resolve(
HITCBC 99   2 h, env->executor, host_, service_, flags_, token_, &ec_, 100   2 h, env->executor, host_, service_, flags_, token_, &ec_,
HITCBC 100   4 &results_); 101   4 &results_);
101   } 102   }
102   }; 103   };
103   104  
104   struct native_reverse_awaitable 105   struct native_reverse_awaitable
105   { 106   {
106   native_resolver& self_; 107   native_resolver& self_;
107   endpoint ep_; 108   endpoint ep_;
108   reverse_flags flags_; 109   reverse_flags flags_;
109   std::stop_token token_; 110   std::stop_token token_;
110   mutable std::error_code ec_; 111   mutable std::error_code ec_;
111   mutable reverse_resolver_result result_; 112   mutable reverse_resolver_result result_;
112   113  
113   native_reverse_awaitable( 114   native_reverse_awaitable(
114   native_resolver& self, 115   native_resolver& self,
115   endpoint const& ep, 116   endpoint const& ep,
116   reverse_flags flags) noexcept 117   reverse_flags flags) noexcept
117   : self_(self) 118   : self_(self)
118   , ep_(ep) 119   , ep_(ep)
119   , flags_(flags) 120   , flags_(flags)
120   { 121   {
121   } 122   }
122   123  
123   bool await_ready() const noexcept 124   bool await_ready() const noexcept
124   { 125   {
125   return token_.stop_requested(); 126   return token_.stop_requested();
126   } 127   }
127   128  
128   capy::io_result<reverse_resolver_result> await_resume() const noexcept 129   capy::io_result<reverse_resolver_result> await_resume() const noexcept
129   { 130   {
130   if (token_.stop_requested()) 131   if (token_.stop_requested())
131   return {make_error_code(std::errc::operation_canceled), {}}; 132   return {make_error_code(std::errc::operation_canceled), {}};
132   return {ec_, std::move(result_)}; 133   return {ec_, std::move(result_)};
133   } 134   }
134   135  
135   auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) 136   auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
136   -> std::coroutine_handle<> 137   -> std::coroutine_handle<>
137   { 138   {
138   token_ = env->stop_token; 139   token_ = env->stop_token;
139   return self_.get_impl().reverse_resolve( 140   return self_.get_impl().reverse_resolve(
140   h, env->executor, ep_, flags_, token_, &ec_, &result_); 141   h, env->executor, ep_, flags_, token_, &ec_, &result_);
141   } 142   }
142   }; 143   };
143   144  
144   public: 145   public:
145   /** Construct a native resolver from an execution context. 146   /** Construct a native resolver from an execution context.
146   147  
147   @param ctx The execution context that will own this resolver. 148   @param ctx The execution context that will own this resolver.
148   */ 149   */
HITCBC 149   6 explicit native_resolver(capy::execution_context& ctx) : resolver(ctx) {} 150   6 explicit native_resolver(capy::execution_context& ctx) : resolver(ctx) {}
150   151  
151   /** Construct a native resolver from an executor. 152   /** Construct a native resolver from an executor.
152   153  
153   @param ex The executor whose context will own the resolver. 154   @param ex The executor whose context will own the resolver.
154   */ 155   */
155   template<class Ex> 156   template<class Ex>
156   requires(!std::same_as<std::remove_cvref_t<Ex>, native_resolver>) && 157   requires(!std::same_as<std::remove_cvref_t<Ex>, native_resolver>) &&
157   capy::Executor<Ex> 158   capy::Executor<Ex>
158   explicit native_resolver(Ex const& ex) : native_resolver(ex.context()) 159   explicit native_resolver(Ex const& ex) : native_resolver(ex.context())
159   { 160   {
160   } 161   }
161   162  
162   /** Move construct. 163   /** Move construct.
163   164  
164   @pre No awaitables returned by @p other's `resolve` methods 165   @pre No awaitables returned by @p other's `resolve` methods
165   exist. 166   exist.
166   @pre The execution context associated with @p other must 167   @pre The execution context associated with @p other must
167   outlive this resolver. 168   outlive this resolver.
168   */ 169   */
169   native_resolver(native_resolver&&) noexcept = default; 170   native_resolver(native_resolver&&) noexcept = default;
170   171  
171   /** Move assign. 172   /** Move assign.
172   173  
173   @pre No awaitables returned by either `*this` or the source's 174   @pre No awaitables returned by either `*this` or the source's
174   `resolve` methods exist. 175   `resolve` methods exist.
175   @pre The execution context associated with the source must 176   @pre The execution context associated with the source must
176   outlive this resolver. 177   outlive this resolver.
177   */ 178   */
178   native_resolver& operator=(native_resolver&&) noexcept = default; 179   native_resolver& operator=(native_resolver&&) noexcept = default;
179   180  
180   native_resolver(native_resolver const&) = delete; 181   native_resolver(native_resolver const&) = delete;
181   native_resolver& operator=(native_resolver const&) = delete; 182   native_resolver& operator=(native_resolver const&) = delete;
182   183  
183   /** Asynchronously resolve a host and service to endpoints. 184   /** Asynchronously resolve a host and service to endpoints.
184   185  
185   Calls the backend implementation directly, bypassing virtual 186   Calls the backend implementation directly, bypassing virtual
186   dispatch. Otherwise identical to @ref resolver::resolve. 187   dispatch. Otherwise identical to @ref resolver::resolve.
187   188  
188   This resolver must outlive the returned awaitable. 189   This resolver must outlive the returned awaitable.
189   190  
190   @param host The host name or address string. 191   @param host The host name or address string.
191   @param service The service name or port string. 192   @param service The service name or port string.
192   193  
193   @return An awaitable yielding `io_result<resolver_results>`. 194   @return An awaitable yielding `io_result<resolver_results>`.
  195 +
  196 + @note `resolver_results` is an alias for `std::vector<resolver_entry>`;
  197 + copying it deep-copies every entry. See @ref resolver::resolve.
194   */ 198   */
HITCBC 195   2 auto resolve(std::string_view host, std::string_view service) 199   2 auto resolve(std::string_view host, std::string_view service)
196   { 200   {
197   return native_resolve_awaitable( 201   return native_resolve_awaitable(
HITCBC 198   2 *this, host, service, resolve_flags::none); 202   2 *this, host, service, resolve_flags::none);
199   } 203   }
200   204  
201   /** Asynchronously resolve a host and service with flags. 205   /** Asynchronously resolve a host and service with flags.
202   206  
203   This resolver must outlive the returned awaitable. 207   This resolver must outlive the returned awaitable.
204   208  
205   @param host The host name or address string. 209   @param host The host name or address string.
206   @param service The service name or port string. 210   @param service The service name or port string.
207   @param flags Flags controlling resolution behavior. 211   @param flags Flags controlling resolution behavior.
208   212  
209   @return An awaitable yielding `io_result<resolver_results>`. 213   @return An awaitable yielding `io_result<resolver_results>`.
210   */ 214   */
211   auto resolve( 215   auto resolve(
212   std::string_view host, std::string_view service, resolve_flags flags) 216   std::string_view host, std::string_view service, resolve_flags flags)
213   { 217   {
214   return native_resolve_awaitable(*this, host, service, flags); 218   return native_resolve_awaitable(*this, host, service, flags);
215   } 219   }
216   220  
217   /** Asynchronously reverse-resolve an endpoint. 221   /** Asynchronously reverse-resolve an endpoint.
218   222  
219   Calls the backend implementation directly, bypassing virtual 223   Calls the backend implementation directly, bypassing virtual
220   dispatch. Otherwise identical to the endpoint overload of 224   dispatch. Otherwise identical to the endpoint overload of
221   @ref resolver::resolve. 225   @ref resolver::resolve.
222   226  
223   This resolver must outlive the returned awaitable. 227   This resolver must outlive the returned awaitable.
224   228  
225   @param ep The endpoint to resolve. 229   @param ep The endpoint to resolve.
226   230  
227   @return An awaitable yielding 231   @return An awaitable yielding
228   `io_result<reverse_resolver_result>`. 232   `io_result<reverse_resolver_result>`.
229   */ 233   */
230   auto resolve(endpoint const& ep) 234   auto resolve(endpoint const& ep)
231   { 235   {
232   return native_reverse_awaitable(*this, ep, reverse_flags::none); 236   return native_reverse_awaitable(*this, ep, reverse_flags::none);
233   } 237   }
234   238  
235   /** Asynchronously reverse-resolve an endpoint with flags. 239   /** Asynchronously reverse-resolve an endpoint with flags.
236   240  
237   This resolver must outlive the returned awaitable. 241   This resolver must outlive the returned awaitable.
238   242  
239   @param ep The endpoint to resolve. 243   @param ep The endpoint to resolve.
240   @param flags Flags controlling resolution behavior. 244   @param flags Flags controlling resolution behavior.
241   245  
242   @return An awaitable yielding 246   @return An awaitable yielding
243   `io_result<reverse_resolver_result>`. 247   `io_result<reverse_resolver_result>`.
244   */ 248   */
245   auto resolve(endpoint const& ep, reverse_flags flags) 249   auto resolve(endpoint const& ep, reverse_flags flags)
246   { 250   {
247   return native_reverse_awaitable(*this, ep, flags); 251   return native_reverse_awaitable(*this, ep, flags);
248   } 252   }
249   }; 253   };
250   254  
251   } // namespace boost::corosio 255   } // namespace boost::corosio
252   256  
253   #endif 257   #endif