MDNS client changed to avoid that multiple Avahi resolvers try to resolve the same name and domain simultaneously

This commit is contained in:
Andrea Bondavalli 2020-06-04 09:09:57 -07:00
parent e348400246
commit 0367dc33a0
2 changed files with 19 additions and 8 deletions

View File

@ -108,6 +108,8 @@ void MDNSClient::resolve_callback(AvahiServiceResolver* r,
break; break;
} }
/* remove the resolver from the active pool */
mdns.active_resolvers.erase(std::pair(name, domain));
avahi_service_resolver_free(r); avahi_service_resolver_free(r);
} }
@ -135,18 +137,24 @@ void MDNSClient::browse_callback(AvahiServiceBrowser* b,
BOOST_LOG_TRIVIAL(info) << "mdns_client:: (Browser) NEW: " BOOST_LOG_TRIVIAL(info) << "mdns_client:: (Browser) NEW: "
<< "service " << name << " of type " << type << "service " << name << " of type " << type
<< " in domain " << domain; << " in domain " << domain;
/* We ignore the returned resolver object. In the callback /* check if a resolver is already running for this name and domain */
function we free it. If the server is terminated before if (mdns.active_resolvers.find(std::pair(name, domain)) !=
the callback function is called the server will free mdns.active_resolvers.end()) {
the resolver for us. */ /* if already running we don't run a new resolver */
if (!(avahi_service_resolver_new(mdns.client_.get(), interface, protocol, BOOST_LOG_TRIVIAL(info)
name, type, domain, AVAHI_PROTO_UNSPEC, << "mdns_client:: (Browser): resolution already ongoing ...";
AVAHI_LOOKUP_NO_TXT, resolve_callback, }
&mdns))) { else if (!(avahi_service_resolver_new(mdns.client_.get(), interface,
protocol, name, type, domain,
AVAHI_PROTO_UNSPEC, AVAHI_LOOKUP_NO_TXT,
resolve_callback, &mdns))) {
BOOST_LOG_TRIVIAL(error) BOOST_LOG_TRIVIAL(error)
<< "mdns_client:: " << "mdns_client:: "
<< "Failed to resolve service " << name << " : " << "Failed to resolve service " << name << " : "
<< avahi_strerror(avahi_client_errno(mdns.client_.get())); << avahi_strerror(avahi_client_errno(mdns.client_.get()));
} else {
/* add the resolver to the active pool */
mdns.active_resolvers.insert(std::pair(name, domain));
} }
break; break;

View File

@ -96,6 +96,9 @@ class MDNSClient {
AvahiClientState state, AvahiClientState state,
void* userdata); void* userdata);
std::set<std::pair<std::string /*name*/, std::string /*domain */> >
active_resolvers;
#endif #endif
}; };