如何将结构传递到 browse_callback,然后进一步向下传递到 avahi 中的 resolve_callback?
How do I pass a struct into the browse_callback and then further down to resolve_callback in avahi?
我想在 avahi 中成功解析服务时填充小部件。由于填充所需的大部分信息都在 resolve_callback() 中可用,因此我需要将此小部件显示在其中。但是 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': %s\n", 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=%s\n"
"\tcookie is %u\n"
"\tis_local: %i\n"
"\tour_own: %i\n"
"\twide_area: %i\n"
"\tmulticast: %i\n"
"\tcached: %i\n",
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) %s\n", 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': %s\n", 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) %s\n", 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: %s\n", 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 但找不到任何实质性的东西。如果有人解释它的重要性,我将不胜感激。
我想在 avahi 中成功解析服务时填充小部件。由于填充所需的大部分信息都在 resolve_callback() 中可用,因此我需要将此小部件显示在其中。但是 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': %s\n", 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=%s\n"
"\tcookie is %u\n"
"\tis_local: %i\n"
"\tour_own: %i\n"
"\twide_area: %i\n"
"\tmulticast: %i\n"
"\tcached: %i\n",
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) %s\n", 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': %s\n", 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) %s\n", 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: %s\n", 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 但找不到任何实质性的东西。如果有人解释它的重要性,我将不胜感激。