Chops Net IP
Loading...
Searching...
No Matches
endpoints_resolver.hpp
Go to the documentation of this file.
1
16#ifndef ENDPOINTS_RESOLVER_HPP_INCLUDED
17#define ENDPOINTS_RESOLVER_HPP_INCLUDED
18
19#include "asio/ip/basic_resolver.hpp"
20#include "asio/io_context.hpp"
21
22#include <string_view>
23#include <string>
24#include <utility> // std::move, std::forward
25
26#include "nonstd/expected.hpp"
27
28namespace chops {
29namespace net {
30
52template <typename Protocol>
54private:
55 asio::ip::basic_resolver<Protocol> m_resolver;
56
57public:
58
65 explicit endpoints_resolver(asio::io_context& ioc) : m_resolver(ioc) { }
66
95 template <typename F>
96 void make_endpoints(bool local, std::string_view host_or_intf_name,
97 std::string_view service_or_port, F&& func) {
98
99 // Note - std::move used instead of std::forward since an explicit move or copy
100 // is needed to prevent worry about dangling references
101 if (local) {
102 m_resolver.async_resolve(host_or_intf_name, service_or_port,
103 asio::ip::resolver_base::flags(asio::ip::resolver_base::passive |
104 asio::ip::resolver_base::address_configured),
105 std::move(func));
106 }
107 else {
108 m_resolver.async_resolve(host_or_intf_name, service_or_port,
109 std::move(func));
110 }
111 return;
112 }
113
117 void cancel() {
118 m_resolver.cancel();
119 }
120
134 auto make_endpoints(bool local, std::string_view host_or_intf_name, std::string_view service_or_port) ->
135 nonstd::expected<asio::ip::basic_resolver_results<Protocol>, std::error_code> {
136
137 std::error_code ec;
138 auto res = m_resolver.resolve(host_or_intf_name, service_or_port,
139 (local ? asio::ip::resolver_base::flags(asio::ip::resolver_base::passive |
140 asio::ip::resolver_base::address_configured) :
141 asio::ip::resolver_base::flags()), ec);
142 if (ec) {
143 return nonstd::make_unexpected(ec);
144 }
145 return res;
146
147 }
148
149};
150
151} // end net namespace
152} // end chops namespace
153
154#endif
155
Convenience class for resolving names to endpoints suitable for use within the Chops Net IP library (...
Definition endpoints_resolver.hpp:53
void cancel()
Cancel any outstanding async operations.
Definition endpoints_resolver.hpp:117
void make_endpoints(bool local, std::string_view host_or_intf_name, std::string_view service_or_port, F &&func)
Create a sequence of endpoints and return them in a function object callback.
Definition endpoints_resolver.hpp:96
auto make_endpoints(bool local, std::string_view host_or_intf_name, std::string_view service_or_port) -> nonstd::expected< asio::ip::basic_resolver_results< Protocol >, std::error_code >
Create a sequence of endpoints and return them immediately in a container.
Definition endpoints_resolver.hpp:134
endpoints_resolver(asio::io_context &ioc)
Construct with an io_context.
Definition endpoints_resolver.hpp:65