lib/avahi/Publisher: allow RegisterServices() to throw

This commit is contained in:
Max Kellermann 2024-01-17 18:52:07 +01:00 committed by Max Kellermann
parent 09d1ab7abe
commit 1b241fc97a
2 changed files with 27 additions and 19 deletions

View File

@ -49,7 +49,11 @@ Publisher::Publisher(Client &_client, const char *_name,
client.AddListener(*this); client.AddListener(*this);
if (client.IsConnected()) if (client.IsConnected())
RegisterServices(client.GetClient()); try {
RegisterServices(client.GetClient());
} catch (...) {
error_handler.OnAvahiError(std::current_exception());
}
} }
Publisher::~Publisher() noexcept Publisher::~Publisher() noexcept
@ -126,25 +130,21 @@ AddServices(AvahiEntryGroup &group,
} }
void void
Publisher::RegisterServices(AvahiClient *c) noexcept Publisher::RegisterServices(AvahiClient *c)
{ {
assert(visible); assert(visible);
try { if (!group) {
if (!group) { group.reset(avahi_entry_group_new(c, GroupCallback, this));
group.reset(avahi_entry_group_new(c, GroupCallback, this)); if (!group)
if (!group) throw MakeError(*c, "Failed to create Avahi service group");
throw MakeError(*c, "Failed to create Avahi service group");
}
AddServices(*group, services, name.c_str());
if (int error = avahi_entry_group_commit(group.get());
error != AVAHI_OK)
throw MakeError(error, "Failed to commit Avahi service group");
} catch (...) {
error_handler.OnAvahiError(std::current_exception());
} }
AddServices(*group, services, name.c_str());
if (int error = avahi_entry_group_commit(group.get());
error != AVAHI_OK)
throw MakeError(error, "Failed to commit Avahi service group");
} }
void void
@ -168,14 +168,22 @@ Publisher::ShowServices() noexcept
visible = true; visible = true;
if (client.IsConnected()) if (client.IsConnected())
RegisterServices(client.GetClient()); try {
RegisterServices(client.GetClient());
} catch (...) {
error_handler.OnAvahiError(std::current_exception());
}
} }
void void
Publisher::OnAvahiConnect(AvahiClient *c) noexcept Publisher::OnAvahiConnect(AvahiClient *c) noexcept
{ {
if (visible) if (visible)
RegisterServices(c); try {
RegisterServices(c);
} catch (...) {
error_handler.OnAvahiError(std::current_exception());
}
} }
void void

View File

@ -68,7 +68,7 @@ private:
AvahiEntryGroupState state, AvahiEntryGroupState state,
void *userdata) noexcept; void *userdata) noexcept;
void RegisterServices(AvahiClient *c) noexcept; void RegisterServices(AvahiClient *c);
/* virtual methods from class AvahiConnectionListener */ /* virtual methods from class AvahiConnectionListener */
void OnAvahiConnect(AvahiClient *client) noexcept override; void OnAvahiConnect(AvahiClient *client) noexcept override;