lib/avahi/Client: add method IsConnected()

Publish only if the client is really connected.
This commit is contained in:
Max Kellermann 2024-01-17 18:27:11 +01:00 committed by Max Kellermann
parent 6b43338e06
commit d28cb93835
3 changed files with 20 additions and 6 deletions

View File

@ -30,11 +30,15 @@ void
Client::Close() noexcept
{
if (client != nullptr) {
connected = false;
for (auto *l : listeners)
l->OnAvahiDisconnect();
avahi_client_free(client);
client = nullptr;
} else {
assert(!connected);
}
reconnect_timer.Cancel();
@ -45,6 +49,8 @@ Client::ClientCallback(AvahiClient *c, AvahiClientState state) noexcept
{
switch (state) {
case AVAHI_CLIENT_S_RUNNING:
connected = true;
for (auto *l : listeners)
l->OnAvahiConnect(c);
@ -73,12 +79,15 @@ Client::ClientCallback(AvahiClient *c, AvahiClientState state) noexcept
case AVAHI_CLIENT_S_COLLISION:
case AVAHI_CLIENT_S_REGISTERING:
assert(!connected);
for (auto *l : listeners)
l->OnAvahiChanged();
break;
case AVAHI_CLIENT_CONNECTING:
assert(!connected);
break;
}
}
@ -95,6 +104,7 @@ void
Client::OnReconnectTimer() noexcept
{
assert(client == nullptr);
assert(!connected);
int error;
client = avahi_client_new(&poll, AVAHI_CLIENT_NO_FAIL,

View File

@ -29,6 +29,8 @@ class Client final {
std::forward_list<ConnectionListener *> listeners;
bool connected = false;
public:
Client(EventLoop &event_loop, ErrorHandler &_error_handler) noexcept;
~Client() noexcept;
@ -42,6 +44,10 @@ public:
void Close() noexcept;
bool IsConnected() const noexcept {
return connected;
}
AvahiClient *GetClient() noexcept {
return client;
}

View File

@ -48,9 +48,8 @@ Publisher::Publisher(Client &_client, const char *_name,
client.AddListener(*this);
auto *c = client.GetClient();
if (c != nullptr)
RegisterServices(c);
if (client.IsConnected())
RegisterServices(client.GetClient());
}
Publisher::~Publisher() noexcept
@ -168,9 +167,8 @@ Publisher::ShowServices() noexcept
visible = true;
auto *c = client.GetClient();
if (c != nullptr)
RegisterServices(c);
if (client.IsConnected())
RegisterServices(client.GetClient());
}
void