diff --git a/src/ServiceDiscovery.cs b/src/ServiceDiscovery.cs index 7a8413b..a11f266 100644 --- a/src/ServiceDiscovery.cs +++ b/src/ServiceDiscovery.cs @@ -198,6 +198,37 @@ public void Advertise(ServiceProfile service) } } + /// + /// Sends a goodbye message for the provided + /// profile and removes its pointer from the name sever. + /// + /// The profile to send a goodbye message for. + public void Unadvertise(ServiceProfile profile) + { + var message = new Message { QR = true }; + var ptrRecord = new PTRRecord { Name = profile.QualifiedServiceName, DomainName = profile.FullyQualifiedName }; + ptrRecord.TTL = TimeSpan.Zero; + + message.Answers.Add(ptrRecord); + profile.Resources.ForEach((resource) => + { + resource.TTL = TimeSpan.Zero; + message.AdditionalRecords.Add(resource); + }); + + Mdns.SendAnswer(message); + + NameServer.Catalog.TryRemove(profile.QualifiedServiceName, out Node _); + } + + /// + /// Sends a goodbye message for each anounced service. + /// + public void Unadvertise() + { + profiles.ForEach(profile => Unadvertise(profile)); + } + void OnAnswer(object sender, MessageEventArgs e) { var msg = e.Message;