public void StartService() { if (ServiceStarted) { return; } ServiceStarted = true; MDNS.Start(); SD.ServiceInstanceDiscovered += (s, e) => MDNS.SendQuery(e.ServiceInstanceName, type: DnsType.SRV); MDNS.AnswerReceived += (s, e) => { var servers = e.Message.Answers.OfType <SRVRecord>(); foreach (var server in servers) { var target = server.Target.ToString(); var name = string.Join(".", server.Name.Labels.TakeLast(3)); lock (_dictLock) { if (TargetDictionary.ContainsKey(target)) { if (!TargetDictionary[target].Contains(name)) { TargetDictionary[target].Add(name); } } else { TargetDictionary.Add(target, new HashSet <string> { name }); } } Logger.LogInformation($"Receive service record: {name} = {target}"); MDNS.SendQuery(server.Target, type: DnsType.A); } var addresses = e.Message.Answers.OfType <AddressRecord>(); foreach (var address in addresses) { if (!DomainNameDictionary.TryAdd(address.Name.ToString(), address.Address)) { DomainNameDictionary[address.Name.ToString()] = address.Address; } Logger.LogInformation($"Receive address record: {address.Name} = {address.Address}"); if (TargetDictionary.ContainsKey(address.Name.ToString())) { TargetDictionary[address.Name.ToString()] .Where(t => EventDictionary.ContainsKey(t)) .Foreach(t => EventDictionary[t].Invoke(this, new IPAddressEventArgs(address.Address))); } } }; Logger.LogInformation("Service discovery started"); }
public EventProxy <IPAddressEventArgs> DiscoverService(string serviceName) { MDNS.SendQuery(serviceName, type: DnsType.PTR); lock (_dictLock) { EventDictionary.TryAdd(serviceName, new EventProxy <IPAddressEventArgs>()); } return(EventDictionary[serviceName]); }