lib/avahi/Publisher: use DeferEvent for RegisterServices()

Prepares making the service list dyammic; the DeferEvent not only
moves the operation out of the current stack frame, but also allows
combining several AddService() calls.
This commit is contained in:
Max Kellermann 2024-01-17 18:45:11 +01:00 committed by Max Kellermann
parent 4a14248004
commit 5023432802
2 changed files with 25 additions and 11 deletions

View File

@ -42,18 +42,17 @@ Publisher::Publisher(Client &_client, const char *_name,
ErrorHandler &_error_handler) noexcept ErrorHandler &_error_handler) noexcept
:error_handler(_error_handler), :error_handler(_error_handler),
name(MakePidName(_name)), 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()); assert(!services.empty());
client.AddListener(*this); client.AddListener(*this);
if (client.IsConnected()) if (client.IsConnected())
try { defer_register_services.Schedule();
RegisterServices(client.GetClient());
} catch (...) {
error_handler.OnAvahiError(std::current_exception());
}
} }
Publisher::~Publisher() noexcept Publisher::~Publisher() noexcept
@ -153,6 +152,19 @@ Publisher::RegisterServices(AvahiClient *c)
RegisterServices(*group); RegisterServices(*group);
} }
void
Publisher::DeferredRegisterServices() noexcept
{
assert(visible);
assert(client.IsConnected());
try {
RegisterServices(client.GetClient());
} catch (...) {
error_handler.OnAvahiError(std::current_exception());
}
}
void void
Publisher::HideServices() noexcept Publisher::HideServices() noexcept
{ {
@ -161,6 +173,8 @@ Publisher::HideServices() noexcept
visible = false; visible = false;
defer_register_services.Cancel();
if (group) if (group)
avahi_entry_group_reset(group.get()); avahi_entry_group_reset(group.get());
} }
@ -174,11 +188,7 @@ Publisher::ShowServices() noexcept
visible = true; visible = true;
if (client.IsConnected()) if (client.IsConnected())
try { defer_register_services.Schedule();
RegisterServices(client.GetClient());
} catch (...) {
error_handler.OnAvahiError(std::current_exception());
}
} }
void void

View File

@ -6,6 +6,7 @@
#include "EntryGroup.hxx" #include "EntryGroup.hxx"
#include "ConnectionListener.hxx" #include "ConnectionListener.hxx"
#include "event/DeferEvent.hxx"
#include <avahi-client/publish.h> #include <avahi-client/publish.h>
@ -31,6 +32,8 @@ class Publisher final : ConnectionListener {
Client &client; Client &client;
DeferEvent defer_register_services;
EntryGroupPtr group; EntryGroupPtr group;
const std::forward_list<Service> services; const std::forward_list<Service> services;
@ -70,6 +73,7 @@ private:
void RegisterServices(AvahiEntryGroup &g); void RegisterServices(AvahiEntryGroup &g);
void RegisterServices(AvahiClient *c); void RegisterServices(AvahiClient *c);
void DeferredRegisterServices() noexcept;
/* virtual methods from class AvahiConnectionListener */ /* virtual methods from class AvahiConnectionListener */
void OnAvahiConnect(AvahiClient *client) noexcept override; void OnAvahiConnect(AvahiClient *client) noexcept override;