Ejemplo n.º 1
0
        private IPAddressInfo GetNextIPAddressInfo()
        {
            IPHostEntry ipHostEntry;

            lock (this) {
                GlobalLog.Print("m_CurrentAddressInfoIndex = " + m_CurrentAddressInfoIndex);
                GlobalLog.Print("m_ConnectionsSinceDns = " + m_ConnectionsSinceDns);
                GlobalLog.Print("m_IPHostEntryInfos = " + m_IPHostEntryInfos);
                m_CurrentAddressInfoIndex++;
                if (m_ConnectionsSinceDns < 0 || (m_ConnectionsSinceDns > 0 && m_IPHostEntryInfos != null && m_IPHostEntryInfos.IPAddressInfoList != null && m_CurrentAddressInfoIndex == m_IPHostEntryInfos.IPAddressInfoList.Length))
                {
                    //
                    // it's either the first time we get here or we reached the end
                    // of the list of valid IPAddressInfo that we can connect to.
                    // in this second case, since at least one of the IPAddressInfo we got
                    // from DNS was valid, we'll query DNS again in case we just need to
                    // refresh our internal list. note that we don't need to assert DnsPermission
                    // since the internal method we're calling (Dns.InternalResolve()) doesn't Demand() any
                    //
                    m_ConnectionsSinceDns = 0;
                    try {
                        GlobalLog.Print("ServicePoint::GetNextIPAddressInfo() calling Dns.InternalResolve() for:" + m_Address.Host);
                        ipHostEntry = Dns.InternalResolve(m_Address.Host);
                        GlobalLog.Print("ServicePoint::GetNextIPAddressInfo() Dns.InternalResolve() returns:" + ValidationHelper.ToString(ipHostEntry));
                        if (ipHostEntry != null && ipHostEntry.AddressList != null && ipHostEntry.AddressList.Length > 0)
                        {
                            //
                            // this call will reset m_IPHostEntryInfos
                            //
                            m_CurrentAddressInfoIndex = 0;
                            m_IPHostEntryInfos        = SetAddressList(ipHostEntry);
                        }
                        else
                        {
                            GlobalLog.Print("Dns.InternalResolve() failed with null");
                            m_IPHostEntryInfos = null;
                        }
                    }
                    catch (Exception e) {
                        GlobalLog.Print("Dns.InternalResolve() failed with exception: " + e.Message + " " + e.StackTrace);
                        m_IPHostEntryInfos = null;
                    }
                }
                if (m_IPHostEntryInfos != null && m_IPHostEntryInfos.IPAddressInfoList != null && m_CurrentAddressInfoIndex < m_IPHostEntryInfos.IPAddressInfoList.Length)
                {
                    GlobalLog.Print("GetNextIPAddressInfo() returning " + m_IPHostEntryInfos.IPAddressInfoList[m_CurrentAddressInfoIndex]);
                    return(m_IPHostEntryInfos.IPAddressInfoList[m_CurrentAddressInfoIndex]);
                }
                // If we walked the whole list in failure, than reset and retry DNS
                if (m_IPHostEntryInfos == null || m_IPHostEntryInfos.IPAddressInfoList == null || m_CurrentAddressInfoIndex >= m_IPHostEntryInfos.IPAddressInfoList.Length)
                {
                    GlobalLog.Print("setting m_ConnectionsSinceDns to -1");
                    m_ConnectionsSinceDns = -1;
                }
            }
            GlobalLog.Print("GetNextIPAddressInfo() returning null");
            return(null);
        }