引用dns_sd.h中的DNSSDObjects和MonoTouch中的DNSServiceResolve

时间:2012-01-08 16:16:43

标签: ios mono bluetooth xamarin.ios dns-sd

我想将DNSSDObjects的引用添加到MonoTouch中的项目,特别是DNSServiceResolve对象。

我想在MonoTouch项目中访问DNSServiceResolve,但无法在任何地方找到该类。

怎么办?

1 个答案:

答案 0 :(得分:0)

我从dns_sd.h获得了使用P / Invokes的函数。大多数定义已经在项目zeroconfignetservices [1]中完成,特别是在文件mDNSImports.cs中。不是引用dnssd.dll,而是iOS上的/usr/lib/system/libsystem_dnssd.dylib

例如,DNSServiceQueryRecord的定义是:

[DllImport("/usr/lib/system/libsystem_dnssd.dylib")]
public static extern DNSServiceErrorType DNSServiceQueryRecord(out IntPtr sdRef,
    DNSServiceFlags flags,
    UInt32 interfaceIndex,
    [MarshalAs(
            UnmanagedType.CustomMarshaler,
            MarshalTypeRef = typeof(Utf8Marshaler))] String fullname,
    DNSServiceType rrType,
    DNSServiceClass rrClass,
    DNSServiceQueryReply callBack,
    IntPtr context);

对SRV记录的查询如下:

public void DoDnsLookup()
{
    IntPtr sdRef;
    var result = DNSServiceQueryRecord(
        out sdRef,
        DNSServiceFlags.LongLivedQuery,
        0,
        "_xmpp-client._tcp.gmail.com",
        DNSServiceType.SRV,
        DNSServiceClass.IN,
        DnsServiceQueryReply,
        IntPtr.Zero
    );
    if (result == DNSServiceErrorType.NoError)
    {
        DNSServiceProcessResult(sdRef);
        DNSServiceRefDeallocate(sdRef);
    }
}

//see [2] why this method is static and the attribute
[MonoPInvokeCallback(typeof(DNSServiceQueryReply))]
public static void DnsServiceQueryReply(
    IntPtr sdRef,
    DNSServiceFlags flags,
    UInt32 interfaceIndex,
    DNSServiceErrorType errorCode,
    [MarshalAs(
        UnmanagedType.CustomMarshaler,
        MarshalTypeRef = typeof(Utf8Marshaler))] String fullname,
    DNSServiceType rrType,
    DNSServiceClass rrClass,
    UInt16 rdLength,
    [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 7)]byte[] rData,
    UInt32 ttl,
    IntPtr context)
{
    if (result == DNSServiceErrorType.NoError)
    {
        // process returned DNS data in rData
        // a useful library for this could be Bdev.Net.Dns [3]
    }
}

此处未定义的所有类,枚举等均来自[1]。

<强>参考文献:

  1. http://code.google.com/p/zeroconfignetservices
  2. http://docs.xamarin.com/ios/guides/advanced_topics/limitations#Reverse_Callbacks
  3. dnslookup.codeplex.com