2012-01-08 8 views

答えて

0

私はdns_sd.hの関数をP/Invokesで処理しています。ほとんどの定義はすでにzeroconfignetservicesプロジェクト[1]、特にmDNSImports.csファイルで行われています。 dnssd.dllを参照する代わりに、iOSでは/usr/lib/system/libsystem_dnssd.dylibとなります。

[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レコードのクエリは、次のようになります:

したがって、たとえば、DNSServiceQueryRecordの定義は以下のようになり

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