1#include <boost/asio/ip/tcp.hpp>
2#include <boost/system/error_code.hpp>
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 = {})
31 using ResolverType =
typename Protocol::resolver;
32 ResolverType resolver{context};
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){};
38 if (start == end || ec)
39 throw std::runtime_error(
"Cannot resolve passed host.");
41 onResolveDone(start, end);
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 = {})
66 using ResolverType =
typename Protocol::resolver;
67 ResolverType resolver{context};
69 const auto start = resolver.resolve(host.c_str(),
port.c_str(), flags, ec);
70 const auto end =
decltype(start){};
72 if (start == end || ec)
75 onResolveDone(start, end);
82 template <
typename Protocol = boost::asio::ip::tcp,
typename FunctionT>
84 boost::asio::any_io_executor context,
85 std::string
const& host,
87 FunctionT&& onResolveDone,
88 boost::system::error_code& ec,
89 boost::asio::ip::resolver_base::flags flags = {})
95 [&onResolveDone](
auto begin,
auto end) {
96 onResolveDone(
typename Protocol::resolver::iterator{begin},
typename Protocol::resolver::iterator{end});
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 = {})
126 [&onResolveDone](
auto begin,
auto end) {
127 onResolveDone(
typename Protocol::resolver::iterator{begin},
typename Protocol::resolver::iterator{end});
147 template <
typename ExecutorType,
typename PickerFunctionT,
typename Protocol = boost::asio::ip::tcp>
149 ExecutorType&& executor,
150 std::string
const& host,
151 std::string
const&
port,
152 PickerFunctionT&& picker,
153 boost::asio::ip::resolver_base::flags flags = {})
155 using ResolverType =
typename Protocol::resolver;
156 ResolverType resolver{executor};
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);
162 if (endpoints.empty())
163 throw std::runtime_error(
"Cannot resolve passed host.");
165 return picker(endpoints);
174 template <
typename ExecutorType,
typename PickerFunctionT,
typename Protocol = boost::asio::ip::tcp>
176 ExecutorType&& executor,
177 std::string
const& host,
179 PickerFunctionT&& picker,
180 boost::asio::ip::resolver_base::flags flags = {})
182 return resolve<ExecutorType, PickerFunctionT, Protocol>(
183 std::forward<ExecutorType>(executor),
185 std::to_string(
port),
186 std::forward<PickerFunctionT>(picker),
202 template <
typename ExecutorType,
typename Protocol = boost::asio::ip::tcp>
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 = {})
211 std::forward<ExecutorType>(executor),
214 [&preferIpv4](
auto& endpoints) {
215 std::partition(std::begin(endpoints), std::end(endpoints), [preferIpv4](
auto const& lhs) {
216 return lhs.address().is_v4() == preferIpv4;
227 template <
typename ExecutorType,
typename Protocol = boost::asio::ip::tcp>
229 ExecutorType&& executor,
230 std::string
const& host,
232 bool preferIpv4 =
false,
233 boost::asio::ip::resolver_base::flags flags = {})
235 return resolveSingle<ExecutorType, Protocol>(
236 std::forward<ExecutorType>(executor), host, std::to_string(
port), preferIpv4, flags);
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