示例#1
0
        private DiscoveredTivo HandleDnsAnswer(TivoAhoy.Common.Discovery.Message msg)
        {
            bool isTivoResponse = false;

            foreach (var answer in msg.Answers)
            {
                if (answer.Type == TivoAhoy.Common.Discovery.Type.PTR)
                {
                    var ptrData = (TivoAhoy.Common.Discovery.Ptr)answer.ResponseData;
                    var name    = ptrData.DomainName.ToString();

                    if (name.Contains(dnsProtocol))
                    {
                        isTivoResponse = true;
                    }
                }
            }

            if (!isTivoResponse)
            {
                AddDebugText("Non-TiVo response received: {0}", msg);
                return(null);
            }

            int?      port        = null;
            IPAddress tivoAddress = null;
            string    tivoName    = null;
            string    tivoTsn     = null;

            foreach (var additional in msg.Additionals)
            {
                if (additional.Type == TivoAhoy.Common.Discovery.Type.TXT)
                {
                    var txtData = (TivoAhoy.Common.Discovery.Txt)additional.ResponseData;

                    foreach (var property in txtData.Properties)
                    {
                        AddDebugText("TXT entry: {0} = {1}", property.Key, property.Value);
                        if (property.Key == "TSN")
                        {
                            tivoTsn = property.Value;
                        }
                    }
                }

                if (additional.Type == TivoAhoy.Common.Discovery.Type.A)
                {
                    var hostAddress = (TivoAhoy.Common.Discovery.HostAddress)additional.ResponseData;

                    tivoAddress = hostAddress.Address;
                    tivoName    = additional.DomainName[0];
                }

                if (additional.Type == TivoAhoy.Common.Discovery.Type.SRV)
                {
                    var srvData = (TivoAhoy.Common.Discovery.Srv)additional.ResponseData;

                    port = srvData.Port;
                }
            }

            AddDebugText("TiVo found at {0}:{1}", msg.From.Address, port);

            return(new DiscoveredTivo(tivoName, tivoAddress, tivoTsn));
        }