roar
Loading...
Searching...
No Matches
resolve.hpp
Go to the documentation of this file.
1#include <boost/asio/ip/tcp.hpp>
2#include <boost/system/error_code.hpp>
3
4#include <string>
5#include <stdexcept>
6#include <algorithm>
7#include <vector>
8
9namespace Roar::Dns
10{
23 template <typename Protocol = boost::asio::ip::tcp, typename FunctionT>
25 boost::asio::any_io_executor context,
26 std::string const& host,
27 std::string const& port,
28 FunctionT&& onResolveDone,
29 boost::asio::ip::resolver_base::flags flags = {})
30 {
31 using ResolverType = typename Protocol::resolver;
32 ResolverType resolver{context};
33
34 boost::system::error_code ec;
35 const auto start = resolver.resolve(host.c_str(), port.c_str(), flags, ec);
36 const auto end = decltype(start){};
37
38 if (start == end || ec)
39 throw std::runtime_error("Cannot resolve passed host.");
40
41 onResolveDone(start, end);
42 }
43
57 template <typename Protocol = boost::asio::ip::tcp, typename FunctionT>
59 boost::asio::any_io_executor context,
60 std::string const& host,
61 std::string const& port,
62 FunctionT&& onResolveDone,
63 boost::system::error_code& ec,
64 boost::asio::ip::resolver_base::flags flags = {})
65 {
66 using ResolverType = typename Protocol::resolver;
67 ResolverType resolver{context};
68
69 const auto start = resolver.resolve(host.c_str(), port.c_str(), flags, ec);
70 const auto end = decltype(start){};
71
72 if (start == end || ec)
73 return;
74
75 onResolveDone(start, end);
76 }
77
82 template <typename Protocol = boost::asio::ip::tcp, typename FunctionT>
84 boost::asio::any_io_executor context,
85 std::string const& host,
86 unsigned short port,
87 FunctionT&& onResolveDone,
88 boost::system::error_code& ec,
89 boost::asio::ip::resolver_base::flags flags = {})
90 {
92 context,
93 host,
94 port,
95 [&onResolveDone](auto begin, auto end) {
96 onResolveDone(typename Protocol::resolver::iterator{begin}, typename Protocol::resolver::iterator{end});
97 },
98 ec,
99 flags);
100 }
101
114 template <typename Protocol = boost::asio::ip::tcp>
116 boost::asio::any_io_executor context,
117 std::string const& host,
118 std::string const& port,
119 std::function<void(typename Protocol::resolver::iterator, typename Protocol::resolver::iterator)> onResolveDone,
120 boost::asio::ip::resolver_base::flags flags = {})
121 {
123 context,
124 host,
125 port,
126 [&onResolveDone](auto begin, auto end) {
127 onResolveDone(typename Protocol::resolver::iterator{begin}, typename Protocol::resolver::iterator{end});
128 },
129 flags);
130 }
131
147 template <typename ExecutorType, typename PickerFunctionT, typename Protocol = boost::asio::ip::tcp>
148 typename Protocol::resolver::endpoint_type resolveSelect(
149 ExecutorType&& executor,
150 std::string const& host,
151 std::string const& port,
152 PickerFunctionT&& picker,
153 boost::asio::ip::resolver_base::flags flags = {})
154 {
155 using ResolverType = typename Protocol::resolver;
156 ResolverType resolver{executor};
157
158 boost::system::error_code ec;
159 typename ResolverType::iterator end, start = resolver.resolve(host.c_str(), port.c_str(), flags, ec);
160 std::vector<typename ResolverType::endpoint_type> endpoints(start, end);
161
162 if (endpoints.empty())
163 throw std::runtime_error("Cannot resolve passed host.");
164
165 return picker(endpoints);
166 }
167
174 template <typename ExecutorType, typename PickerFunctionT, typename Protocol = boost::asio::ip::tcp>
175 typename Protocol::resolver::endpoint_type resolveSelect(
176 ExecutorType&& executor,
177 std::string const& host,
178 unsigned short port,
179 PickerFunctionT&& picker,
180 boost::asio::ip::resolver_base::flags flags = {})
181 {
182 return resolve<ExecutorType, PickerFunctionT, Protocol>(
183 std::forward<ExecutorType>(executor),
184 host,
185 std::to_string(port),
186 std::forward<PickerFunctionT>(picker),
187 flags);
188 }
189
202 template <typename ExecutorType, typename Protocol = boost::asio::ip::tcp>
203 typename Protocol::resolver::endpoint_type resolveSingle(
204 ExecutorType&& executor,
205 std::string const& host,
206 std::string const& port,
207 bool preferIpv4 = false,
208 boost::asio::ip::resolver_base::flags flags = {})
209 {
210 return resolveSelect(
211 std::forward<ExecutorType>(executor),
212 host,
213 port,
214 [&preferIpv4](auto& endpoints) {
215 std::partition(std::begin(endpoints), std::end(endpoints), [preferIpv4](auto const& lhs) {
216 return lhs.address().is_v4() == preferIpv4;
217 });
218 return endpoints[0];
219 },
220 flags);
221 }
222
227 template <typename ExecutorType, typename Protocol = boost::asio::ip::tcp>
228 typename Protocol::resolver::endpoint_type resolveSingle(
229 ExecutorType&& executor,
230 std::string const& host,
231 unsigned short port,
232 bool preferIpv4 = false,
233 boost::asio::ip::resolver_base::flags flags = {})
234 {
235 return resolveSingle<ExecutorType, Protocol>(
236 std::forward<ExecutorType>(executor), host, std::to_string(port), preferIpv4, flags);
237 }
238}
static constexpr int port
Definition main.cpp:16
Definition resolve.hpp:10
Protocol::resolver::endpoint_type resolveSelect(ExecutorType &&executor, std::string const &host, std::string const &port, PickerFunctionT &&picker, boost::asio::ip::resolver_base::flags flags={})
Resolves a host and port and calls the passed function with all results and then returns what picker ...
Definition resolve.hpp:148
Protocol::resolver::endpoint_type resolveSingle(ExecutorType &&executor, std::string const &host, std::string const &port, bool preferIpv4=false, boost::asio::ip::resolver_base::flags flags={})
Like resolve but picks one of the addresses. Will sort after ipv4/ipv6.
Definition resolve.hpp:203
void resolveAll(boost::asio::any_io_executor context, std::string const &host, std::string const &port, FunctionT &&onResolveDone, boost::asio::ip::resolver_base::flags flags={})
Resolves to a set of possible endpoints. Useful for clients that can try all and connect to one that ...
Definition resolve.hpp:24