42#include <boost/bind.hpp>
43#include <boost/asio.hpp>
58 template <
typename Protocol>
61 if (aProtocol == aProtocol.v4())
67 template <
typename Protocol>
70 if (aProtocolFamily &
IPv4)
71 return Protocol::v4();
73 return Protocol::v6();
76 template <
typename Protocol>
84 typedef typename protocol_type::endpoint endpoint_type;
85 typedef typename protocol_type::resolver resolver_type;
87 typedef typename resolver_type::iterator
iterator;
93 virtual void host_not_resolved(
const std::string& aHostName,
const boost::system::error_code& aError) = 0;
98 struct no_requester : std::logic_error {
no_requester() : std::logic_error(
"neolib::basic_resolver::request::no_requester") {} };
103 iParent(aParent), iOrphaned(false), iRequester(&aRequester), iHostName(aHostName), iProtocolFamily(aProtocolFamily)
117 return iProtocolFamily;
121 return iRequester !=
nullptr;
125 if (iRequester ==
nullptr)
131 iRequester =
nullptr;
136 iParent.handle_resolve(*
this, aError, aEndPointIterator);
141 requester_type* iRequester;
142 std::string iHostName;
155 iResolver(aIoTask.io_service().native_object())
160 for (
auto& r : iRequests)
171 iRequests.push_back(newRequest);
172 iResolver.async_resolve(
typename resolver_type::query{ aHostName,
"0" },
173 boost::bind(&
request::handle_resolve, *newRequest, boost::asio::placeholders::error, boost::asio::placeholders::iterator));
177 for (
auto&
request : iRequests)
184 void handle_resolve(request& aRequest,
const boost::system::error_code& aError,
iterator aEndPointIterator)
186 if (aRequest.has_requester())
190 bool foundGoodMatch =
false;
191 for (
iterator i = aEndPointIterator; !foundGoodMatch && i !=
iterator(); ++i)
193 endpoint_type endpoint = *i;
196 foundGoodMatch =
true;
197 aRequest.requester().host_resolved(aRequest.host_name(), i);
201 aRequest.requester().host_resolved(aRequest.host_name(), aEndPointIterator);
204 aRequest.requester().host_not_resolved(aRequest.host_name(), aError);
206 for (
auto i = iRequests.begin(); i != iRequests.end(); ++i)
207 if (&**i == &aRequest)
217 resolver_type iResolver;
request(basic_resolver< Protocol > &aParent, requester_type &aRequester, const std::string &aHostName, neolib::protocol_family aProtocolFamily)
void handle_resolve(const boost::system::error_code &aError, iterator aEndPointIterator)
requester_type & requester() const
bool has_requester() const
const std::string & host_name() const
neolib::protocol_family protocol_family() const
virtual void host_not_resolved(const std::string &aHostName, const boost::system::error_code &aError)=0
virtual void host_resolved(const std::string &aHostName, iterator aHost)=0
std::vector< request_pointer > request_list
resolver_type::iterator iterator
void resolve(requester &aRequester, const std::string &aHostName, protocol_family aProtocolFamily=IPv4orIPv6)
basic_resolver< Protocol > our_type
std::shared_ptr< request > request_pointer
basic_resolver(async_task &aIoTask)
void remove_requester(requester &aRequester)
basic_resolver< tcp_protocol > tcp_resolver
boost::asio::ip::tcp tcp_protocol
protocol_family to_protocol_family(Protocol aProtocol)
boost::asio::ip::udp udp_protocol
Protocol to_protocol(protocol_family aProtocolFamily)
basic_resolver< udp_protocol > udp_resolver