diff --git a/src/lib/avahi/Client.cxx b/src/lib/avahi/Client.cxx index 3f14c3473..955641449 100644 --- a/src/lib/avahi/Client.cxx +++ b/src/lib/avahi/Client.cxx @@ -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, diff --git a/src/lib/avahi/Client.hxx b/src/lib/avahi/Client.hxx index d405893e3..509300e1c 100644 --- a/src/lib/avahi/Client.hxx +++ b/src/lib/avahi/Client.hxx @@ -29,6 +29,8 @@ class Client final { std::forward_list 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; } diff --git a/src/lib/avahi/Publisher.cxx b/src/lib/avahi/Publisher.cxx index 416fa3fde..ea88cb4c3 100644 --- a/src/lib/avahi/Publisher.cxx +++ b/src/lib/avahi/Publisher.cxx @@ -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