使用 kDNSServiceFlagsShareConnection 共享 DNSServiceRef 会停止我的程序
我正在使用 Bonjour 的 dns-sd api 构建客户端。我注意到有一个名为 kDNSServiceFlagsShareConnection 的标志,它用于共享一个 DNSServiceRef 的连接。
苹果网站说
为了提高效率,执行许多并发操作的客户端可能希望使用与后台守护程序的单个 Unix 域套接字连接,而不是为每个独立操作使用单独的连接。要使用此模式,客户端首先调用 DNSServiceCreateConnection(&MainRef) 来初始化主 DNSServiceRef。对于共享同一连接的每个后续操作,客户端复制 MainRef,然后传递该副本的地址,设置 ShareConnection 标志以告诉库此 DNSServiceRef 不是典型的未初始化的 DNSServiceRef;它是现有 DNSServiceRef 的副本,应重用其连接信息。
甚至还有一个示例展示了如何使用该标志。我遇到的问题是,当我运行程序时,每当我调用带有标志的函数时,它就像在等待某些东西。这是代码:
DNSServiceErrorType error;
DNSServiceRef MainRef, BrowseRef;
error = DNSServiceCreateConnection(&MainRef);
BrowseRef = MainRef;
//I'm omitting when I check for errors
error = DNSServiceBrowse(&MainRef, kDNSServiceFlagsShareConnection, 0, "_http._tcp", "local", browse_reply, NULL);
// After this call the program stays waiting for I don't know what
//I'm omitting when I check for errors
error = DNSServiceBrowse(&BrowseRef, kDNSServiceFlagsShareConnection, 0, "_http._tcp", "local", browse_reply, NULL);
//I'm omitting when i check for errors
DNSServiceRefDeallocate(BrowseRef); // Terminate the browse operation
DNSServiceRefDeallocate(MainRef); // Terminate the shared connection
有什么想法吗?想法?建议?
I'm building a client using dns-sd api from Bonjour. I notice that there is a flag called kDNSServiceFlagsShareConnection that it is used to share the connection of one DNSServiceRef.
Apple site says
For efficiency, clients that perform many concurrent operations may want to use a single Unix Domain Socket connection with the background daemon, instead of having a separate connection for each independent operation. To use this mode, clients first call DNSServiceCreateConnection(&MainRef) to initialize the main DNSServiceRef. For each subsequent operation that is to share that same connection, the client copies the MainRef, and then passes the address of that copy, setting the ShareConnection flag to tell the library that this DNSServiceRef is not a typical uninitialized DNSServiceRef; it's a copy of an existing DNSServiceRef whose connection information should be reused.
There is even an example that shows how to use the flag. The problem i'm having is when I run the program it stays like waiting for something whenever I call a function with the flag. Here is the code:
DNSServiceErrorType error;
DNSServiceRef MainRef, BrowseRef;
error = DNSServiceCreateConnection(&MainRef);
BrowseRef = MainRef;
//I'm omitting when I check for errors
error = DNSServiceBrowse(&MainRef, kDNSServiceFlagsShareConnection, 0, "_http._tcp", "local", browse_reply, NULL);
// After this call the program stays waiting for I don't know what
//I'm omitting when I check for errors
error = DNSServiceBrowse(&BrowseRef, kDNSServiceFlagsShareConnection, 0, "_http._tcp", "local", browse_reply, NULL);
//I'm omitting when i check for errors
DNSServiceRefDeallocate(BrowseRef); // Terminate the browse operation
DNSServiceRefDeallocate(MainRef); // Terminate the shared connection
Any ideas? thoughts? suggestion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于存在相互矛盾的答案,我挖掘了来源 - 我的注释。
primary
字段在其他地方进行了解释:问题的问题是
DNSServiceBrowse
映射到ref->op==browse_request
,这会导致代码>kDNSServiceErr_BadReference。
看起来 kDNSServiceFlagsShareConnection 已经实现了一半,因为我也见过它起作用的情况 - 这个来源是通过追溯它不起作用时找到的。
Since there are conflicting answers, I dug up the source - annotations by me.
The
primary
fiels is explained elsewhere:The problem with the question is that
DNSServiceBrowse
maps toref->op==browse_request
which causes akDNSServiceErr_BadReference
.It looks like
kDNSServiceFlagsShareConnection
is half-implemented, because I've also seen cases in which it works - this source was found by tracing back when it didn't work.不幸的是,用于浏览和解析的服务引用可能不会被共享。请参阅
kDNSServiceFlagsShareConnection
标志的 Bonjour 文档。由于您只浏览两次,我只会让他们有单独的服务引用。因此,
DNSServiceBrowse()
和DNSServiceResolve()
都需要未分配的 service-ref 作为第一个参数。我无法解释为什么你的程序会卡住。示例中的第一个
DNSServiceBrowse()
调用应立即返回并带有错误代码。Service referenses for browsing and resolving may unfortunately not be shared. See the comments in the Bonjour documentation for the
kDNSServiceFlagsShareConnection
-flag. Since you only browse twice I would just let them have separate service-refs instead.So both
DNSServiceBrowse()
andDNSServiceResolve()
require an unallocated service-ref as first parameter.I can't explain why your program chokes though. The first
DNSServiceBrowse()
call in your example should return immediately with an error code.虽然是一个老问题,但它应该可以帮助人们现在寻找答案。
vidtige 的答案是不正确的,只要您将“kDNSServiceFlagsShareConnection”标志与参数一起传递,就可以为任何操作共享该答案。下面的示例 -
参考 - http://osxr.org/android/源/外部/mdnsresponder/mDNSShared/dns_sd.h#0267
Although an old question, but it should help people looking around for answers now.
The answer by vidtige is incorrect, the may be shared for any operation, provided you pass the 'kDNSServiceFlagsShareConnection' flag along with the arguments. Sample below -
Reference - http://osxr.org/android/source/external/mdnsresponder/mDNSShared/dns_sd.h#0267