+typedef struct {
+ DBusConnection *connection;
+ const AvahiPoll *poll_api;
+ AvahiTimeout *dispatch_timeout;
+ int ref;
+} ConnectionData;
+
+static ConnectionData *connection_data_ref(ConnectionData *d) {
+ assert(d);
+ assert(d->ref >= 1);
+
+ d->ref++;
+ return d;
+}
+
+static void connection_data_unref(ConnectionData *d) {
+ assert(d);
+ assert(d->ref >= 1);
+
+ if (--d->ref <= 0) {
+ d->poll_api->timeout_free(d->dispatch_timeout);
+ avahi_free(d);
+ }
+}
+
+static void request_dispatch(ConnectionData *d, int enable) {
+ static const struct timeval tv = { 0, 0 };
+ assert(d);
+
+ if (enable) {
+ assert(dbus_connection_get_dispatch_status(d->connection) == DBUS_DISPATCH_DATA_REMAINS);
+ d->poll_api->timeout_update(d->dispatch_timeout, &tv);
+ } else
+ d->poll_api->timeout_update(d->dispatch_timeout, NULL);
+}
+
+static void dispatch_timeout_callback(AvahiTimeout *t, void *userdata) {
+ ConnectionData *d = userdata;
+ assert(t);
+ assert(d);
+
+ connection_data_ref(d);
+ dbus_connection_ref(d->connection);
+
+ if (dbus_connection_dispatch(d->connection) == DBUS_DISPATCH_DATA_REMAINS)
+ /* If there's still data, request that this handler is called again */
+ request_dispatch(d, 1);
+ else
+ request_dispatch(d, 0);
+
+ dbus_connection_unref(d->connection);
+ connection_data_unref(d);
+}
+
+static void watch_callback(AvahiWatch *avahi_watch, AVAHI_GCC_UNUSED int fd, AvahiWatchEvent events, void *userdata) {