c-如何将结构传递到browse_callback,然后在avahi中进一步传递到resolve_callback



每当在avahi中成功解析服务时,我都想填充一个小部件。由于填充所需的大多数信息都可以在resolve_callback()中获得,所以我需要这个小部件出现在其中。它由avahi_service_resolver_new()自动调用。

static void resolve_callback(
AvahiServiceResolver *r,
AVAHI_GCC_UNUSED AvahiIfIndex interface,
AVAHI_GCC_UNUSED AvahiProtocol protocol,
AvahiResolverEvent event,
const char *name,
const char *type,
const char *domain,
const char *host_name,
const AvahiAddress *address,
uint16_t port,
AvahiStringList *txt,
AvahiLookupResultFlags flags,
PpDnsWindow *self)
{
assert(r);
/* Called whenever a service has been resolved successfully or timed out */
switch (event)
{
case AVAHI_RESOLVER_FAILURE:
fprintf(stderr, "(Resolver) Failed to resolve service '%s' of type '%s' in domain '%s': %sn", name, type, domain, avahi_strerror(avahi_client_errno(avahi_service_resolver_get_client(r))));
break;
case AVAHI_RESOLVER_FOUND:
{
// here I'll be using the self struct
char a[AVAHI_ADDRESS_STR_MAX], *t;
fprintf(stderr, "Service '%s' of type '%s' in domain '%s':n", name, type, domain);
avahi_address_snprint(a, sizeof(a), address);
t = avahi_string_list_to_string(txt);
fprintf(stderr,
"t%s:%u (%s)n"
"tTXT=%sn"
"tcookie is %un"
"tis_local: %in"
"tour_own: %in"
"twide_area: %in"
"tmulticast: %in"
"tcached: %in",
host_name, port, a,
t,
avahi_string_list_get_service_cookie(txt),
!!(flags & AVAHI_LOOKUP_RESULT_LOCAL),
!!(flags & AVAHI_LOOKUP_RESULT_OUR_OWN),
!!(flags & AVAHI_LOOKUP_RESULT_WIDE_AREA),
!!(flags & AVAHI_LOOKUP_RESULT_MULTICAST),
!!(flags & AVAHI_LOOKUP_RESULT_CACHED));
avahi_free(t);
}
}
avahi_service_resolver_free(r);
}
static void browse_callback(
AvahiServiceBrowser *b,
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiBrowserEvent event,
const char *name,
const char *type,
const char *domain,
AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
PpDnsWindow *self)
{
AvahiClient *c = self->client;
assert(b);
/* Called whenever a new services becomes available on the LAN or is removed from the LAN */
switch (event)
{
case AVAHI_BROWSER_FAILURE:
fprintf(stderr, "(Browser) %sn", avahi_strerror(avahi_client_errno(avahi_service_browser_get_client(b))));
return;
case AVAHI_BROWSER_NEW:
fprintf(stderr, "(Browser) NEW: service '%s' of type '%s' in domain '%s'n", name, type, domain);
/* We ignore the returned resolver object. In the callback
function we free it. If the server is terminated before
the callback function is called the server will free
the resolver for us. */
if (!(avahi_service_resolver_new(c, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, resolve_callback, self)))
fprintf(stderr, "Failed to resolve service '%s': %sn", name, avahi_strerror(avahi_client_errno(c)));
break;
case AVAHI_BROWSER_REMOVE:
fprintf(stderr, "(Browser) REMOVE: service '%s' of type '%s' in domain '%s'n", name, type, domain);
break;
case AVAHI_BROWSER_ALL_FOR_NOW:
case AVAHI_BROWSER_CACHE_EXHAUSTED:
fprintf(stderr, "(Browser) %sn", event == AVAHI_BROWSER_CACHE_EXHAUSTED ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW");
break;
}
}

我尝试在此函数定义中将AVAHI_GCC_UNUSED void* userdata修改为PpDnsWindow* self,然后将PpDnsWindow向下传递这些函数,如so-

/* Create the service browser */
if (!(sb = avahi_service_browser_new(self->client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_ipp._tcp", NULL, 0, browse_callback, self)))
{
fprintf(stderr, "Failed to create service browser: %sn", avahi_strerror(avahi_client_errno(self->client)));
destroy(self);
}

但这导致了用AVAHI_RESOLVER_FAILURE解决故障。

我不想让小部件全球化,因为全球化通常是不鼓励的。有什么办法我可以做到这一点吗?

在更改resolve_callback()的函数签名时,我还应该在PpDnsWindow *self之前添加AVAHI_GCC_UNUSED,如下所示。但对于browse_callback(),这不是必需的。

static void resolve_callback(
AvahiServiceResolver *r,
AVAHI_GCC_UNUSED AvahiIfIndex interface,
AVAHI_GCC_UNUSED AvahiProtocol protocol,
AvahiResolverEvent event,
const char *name,
const char *type,
const char *domain,
const char *host_name,
const AvahiAddress *address,
uint16_t port,
AvahiStringList *txt,
AvahiLookupResultFlags flags,
AVAHI_GCC_UNUSED PpDnsWindow *self)

我在网上查了AVAHI_GCC_UNUSED,但找不到任何实质性的东西。如果有人解释一下它的意义,我将不胜感激。

相关内容

  • 没有找到相关文章

最新更新