Esempio n. 1
0
        /// <summary>
        /// Called with the result of the call to DNSServiceResolve().
        ///
        /// If this object instance is configured with an <see cref="DNSService.InvokeableObject">InvokeableObject</see>,
        /// this method is called in a thread safe manner. Typically, this means it's called on the application main loop.
        /// </summary>
        /// <param name="sdRef">
        ///		The DNSServiceRef initialized by DNSServiceResolve().
        /// </param>
        /// <param name="flags">
        ///		Currently unused, reserved for future use.
        /// </param>
        /// <param name="interfaceIndex">
        ///		The interface on which the service was resolved.
        /// </param>
        /// <param name="errorCode">
        ///		Will be NoError (0) on success, otherwise will indicate the failure that occurred.
        ///		Other parameters are undefined if the errorCode is nonzero.
        /// </param>
        /// <param name="fullname">
        ///		The full service domain name, in the form [servicename].[protocol].[domain].
        ///		(This name is escaped following standard DNS rules, making it suitable for
        ///		passing to standard system DNS APIs such as res_query(), or to the
        ///		special-purpose functions included in this API that take fullname parameters.)
        /// </param>
        /// <param name="hosttarget">
        ///		The target hostname of the machine providing the service.  This name can
        ///		be passed to functions like gethostbyname() to identify the host's IP address.
        /// </param>
        /// <param name="port">
        ///		The port, in network byte order, on which connections are accepted for this service.
        /// </param>
        /// <param name="txtLen">
        ///		The length of the txt record, in bytes.
        /// </param>
        /// <param name="txtRecord">
        ///		The service's primary txt record, in standard txt record format.
        /// </param>
        /// <param name="context">
        ///		The context pointer that was passed to the callout.
        ///	</param>
        private void ResolveReply(IntPtr sdRef,
                                  DNSServiceFlags flags,
                                  UInt32 interfaceIndex,
                                  DNSServiceErrorType errorCode,
                                  String fullname,
                                  String hosttarget,
                                  UInt16 port,
                                  UInt16 txtLen,
                                  byte[] txtRecord,
                                  IntPtr context)
        {
            if (errorCode == DNSServiceErrorType.NoError)
            {
                // Update internal variables
                mHostName = hosttarget;
                mPort     = ((int)System.Net.IPAddress.NetworkToHostOrder((short)port) & 0x0000ffff);

                // We may want to update the txt record.
                // The service may not have a txt record yet if it's never been monitored or resolved before.
                // Also, if it's not currently being monitored, then the returned txt record may include updates
                if (mTXTRecordData == null || !NetService.ByteArrayCompare(mTXTRecordData, txtRecord))
                {
                    mTXTRecordData = txtRecord;

                    // Invoke delegate if set
                    if (DidUpdateTXT != null)
                    {
                        DidUpdateTXT(this);
                    }
                }

                // At this point we have a host name, but we don't have the actual IP address.
                // We could use the Windows API's (System.Net.Dns.BeginGetHostEntry) to
                // convert from host name to IP, but they're painfully slow.
                // According to the following website (and my own personal testing),
                // using DNSServiceQueryRecord is much faster:
                // http://lists.apple.com/archives/Bonjour-dev/2006/Jan/msg00008.html

                //AsyncCallback cb = new AsyncCallback(c.AsyncGetHostEntryCallback);
                //IAsyncResult ar = System.Net.Dns.BeginGetHostEntry(hosttarget, cb, c);

                // Begin the process of looking up the IP address(es)
                IPLookup();
            }
            else
            {
                Stop();
                if (DidNotResolveService != null)
                {
                    DNSServiceException exception = new DNSServiceException("DNSServiceResolve", errorCode);
                    DidNotResolveService(this, exception);
                }
            }
        }