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
: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

View File

@ -6,6 +6,7 @@
#include "EntryGroup.hxx"
#include "ConnectionListener.hxx"
#include "event/DeferEvent.hxx"
#include <avahi-client/publish.h>
@ -31,6 +32,8 @@ class Publisher final : ConnectionListener {
Client &client;
DeferEvent defer_register_services;
EntryGroupPtr group;
const std::forward_list<Service> 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;