Referring to DNSSDObjects in dns_sd.h and DNSServiceResolve in MonoTouch

China☆狼群 提交于 2019-12-24 08:58:31

问题


I want to add reference to DNSSDObjects to a project in MonoTouch, specifically DNSServiceResolve object.

I want to access DNSServiceResolve in a MonoTouch project but cant find that class anywhere.

How can that be done ?


回答1:


I got the functions from dns_sd.h working with P/Invokes. Most of the definitions were already done in the project zeroconfignetservices [1], specifically in the file mDNSImports.cs. Instead of referencing dnssd.dll, it is /usr/lib/system/libsystem_dnssd.dylib on iOS.

So for example, the definition for DNSServiceQueryRecord would be:

[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);

And a query for an SRV record would be as follows:

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]
    }
}

All the classes, enums, etc. not defined here are from [1].

References:

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


来源:https://stackoverflow.com/questions/8779081/referring-to-dnssdobjects-in-dns-sd-h-and-dnsserviceresolve-in-monotouch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!