diff --git a/src/lib/avahi/Publisher.cxx b/src/lib/avahi/Publisher.cxx index 97851faf3..b3ca6493d 100644 --- a/src/lib/avahi/Publisher.cxx +++ b/src/lib/avahi/Publisher.cxx @@ -42,18 +42,17 @@ Publisher::Publisher(Client &_client, const char *_name, ErrorHandler &_error_handler) noexcept :error_handler(_error_handler), name(MakePidName(_name)), - client(_client), services(std::move(_services)) + client(_client), + defer_register_services(client.GetEventLoop(), + BIND_THIS_METHOD(DeferredRegisterServices)), + services(std::move(_services)) { assert(!services.empty()); client.AddListener(*this); if (client.IsConnected()) - try { - RegisterServices(client.GetClient()); - } catch (...) { - error_handler.OnAvahiError(std::current_exception()); - } + defer_register_services.Schedule(); } Publisher::~Publisher() noexcept @@ -153,6 +152,19 @@ Publisher::RegisterServices(AvahiClient *c) RegisterServices(*group); } +void +Publisher::DeferredRegisterServices() noexcept +{ + assert(visible); + assert(client.IsConnected()); + + try { + RegisterServices(client.GetClient()); + } catch (...) { + error_handler.OnAvahiError(std::current_exception()); + } +} + void Publisher::HideServices() noexcept { @@ -161,6 +173,8 @@ Publisher::HideServices() noexcept visible = false; + defer_register_services.Cancel(); + if (group) avahi_entry_group_reset(group.get()); } @@ -174,11 +188,7 @@ Publisher::ShowServices() noexcept visible = true; if (client.IsConnected()) - try { - RegisterServices(client.GetClient()); - } catch (...) { - error_handler.OnAvahiError(std::current_exception()); - } + defer_register_services.Schedule(); } void diff --git a/src/lib/avahi/Publisher.hxx b/src/lib/avahi/Publisher.hxx index 37876b4cd..14bf6b6aa 100644 --- a/src/lib/avahi/Publisher.hxx +++ b/src/lib/avahi/Publisher.hxx @@ -6,6 +6,7 @@ #include "EntryGroup.hxx" #include "ConnectionListener.hxx" +#include "event/DeferEvent.hxx" #include @@ -31,6 +32,8 @@ class Publisher final : ConnectionListener { Client &client; + DeferEvent defer_register_services; + EntryGroupPtr group; const std::forward_list services; @@ -70,6 +73,7 @@ private: void RegisterServices(AvahiEntryGroup &g); void RegisterServices(AvahiClient *c); + void DeferredRegisterServices() noexcept; /* virtual methods from class AvahiConnectionListener */ void OnAvahiConnect(AvahiClient *client) noexcept override;