From aef2c5dc142b455274f62367d2874c58cc08bba5 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 18 Jan 2024 16:04:40 +0100 Subject: [PATCH] lib/avahi/Service: add field `visible` Now individual services can be hidden or shown at any time. --- src/lib/avahi/Publisher.cxx | 18 +++++++++++------- src/lib/avahi/Publisher.hxx | 6 ++++++ src/lib/avahi/Service.hxx | 7 +++++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/lib/avahi/Publisher.cxx b/src/lib/avahi/Publisher.cxx index 64cfd43ae..e9dd95202 100644 --- a/src/lib/avahi/Publisher.cxx +++ b/src/lib/avahi/Publisher.cxx @@ -55,22 +55,25 @@ Publisher::~Publisher() noexcept client.RemoveListener(*this); } +void +Publisher::UpdateServices() noexcept +{ + if (visible && client.IsConnected()) + defer_register_services.Schedule(); +} + void Publisher::AddService(Service &service) noexcept { services.push_back(service); - - if (visible && client.IsConnected()) - defer_register_services.Schedule(); + UpdateServices(); } void Publisher::RemoveService(Service &service) noexcept { services.erase(services.iterator_to(service)); - - if (visible && client.IsConnected()) - defer_register_services.Schedule(); + UpdateServices(); } inline void @@ -141,7 +144,8 @@ AddServices(AvahiEntryGroup &group, const IntrusiveList &services, const char *name) { for (const auto &i : services) - AddService(group, i, name); + if (i.visible) + AddService(group, i, name); } void diff --git a/src/lib/avahi/Publisher.hxx b/src/lib/avahi/Publisher.hxx index cabdcbaeb..dcd3f3e8c 100644 --- a/src/lib/avahi/Publisher.hxx +++ b/src/lib/avahi/Publisher.hxx @@ -59,6 +59,12 @@ public: Publisher(const Publisher &) = delete; Publisher &operator=(const Publisher &) = delete; + /** + * Call this after a #Service field was modified to publish + * the changes. + */ + void UpdateServices() noexcept; + /** * Publish another service. * diff --git a/src/lib/avahi/Service.hxx b/src/lib/avahi/Service.hxx index 432d374c1..abc26d05e 100644 --- a/src/lib/avahi/Service.hxx +++ b/src/lib/avahi/Service.hxx @@ -22,6 +22,13 @@ struct Service : IntrusiveListHook<> { std::string type; uint16_t port; + /** + * If this is false, then the service is not published. You + * can change this field at any time and then call + * Publisher::UpdateServices() to publish the change. + */ + bool visible = true; + Service(AvahiIfIndex _interface, AvahiProtocol _protocol, const char *_type, uint16_t _port) noexcept :interface(_interface), protocol(_protocol),