Exemple #1
0
        public static Dictionary <string, IPHostEntry> LoadDnsCacheFromFile()
        {
            Dictionary <string, IPHostEntry> dictionary = new Dictionary <string, IPHostEntry>();
            string dnsCacheFileName = Utils.GetDnsCacheFileName();

            try
            {
                if (File.Exists(dnsCacheFileName))
                {
                    string text = File.ReadAllText(dnsCacheFileName);
                    Logger.DEBUG("Dns cache: " + text);
                    Dictionary <string, object> dictionary2 = Json.Deserialize(text) as Dictionary <string, object>;
                    if (dictionary2 != null)
                    {
                        using (Dictionary <string, object> .Enumerator enumerator = dictionary2.GetEnumerator())
                        {
                            while (enumerator.MoveNext())
                            {
                                KeyValuePair <string, object> current = enumerator.get_Current();
                                List <object> list = current.get_Value() as List <object>;
                                if (list != null)
                                {
                                    List <IPAddress> list2 = new List <IPAddress>();
                                    using (List <object> .Enumerator enumerator2 = list.GetEnumerator())
                                    {
                                        while (enumerator2.MoveNext())
                                        {
                                            object    current2  = enumerator2.get_Current();
                                            IPAddress iPAddress = null;
                                            if (IPAddress.TryParse(current2 as string, ref iPAddress))
                                            {
                                                list2.Add(iPAddress);
                                            }
                                        }
                                    }
                                    if (list2.get_Count() > 0)
                                    {
                                        IPHostEntry iPHostEntry = new IPHostEntry();
                                        iPHostEntry.set_AddressList(list2.ToArray());
                                        dictionary.set_Item(current.get_Key(), iPHostEntry);
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    Logger.DEBUG("Dns cache file isn't exist");
                }
            }
            catch (Exception ex)
            {
                Logger.ERROR(ex.get_StackTrace());
            }
            return(dictionary);
        }
Exemple #2
0
 public void AsyncGetHostEntry(string host, Action <IPHostEntry> resultAction)
 {
     Logger.DEBUG(host);
     try
     {
         IPAddress iPAddress = null;
         if (IPAddress.TryParse(host, ref iPAddress))
         {
             IPHostEntry iPHostEntry = new IPHostEntry();
             iPHostEntry.set_AddressList(new IPAddress[]
             {
                 iPAddress
             });
             resultAction.Invoke(iPHostEntry);
         }
         else if (this.dnsCache.ContainsKey(host))
         {
             IPHostEntry iPHostEntry2 = this.dnsCache.get_Item(host);
             resultAction.Invoke(iPHostEntry2);
         }
         else
         {
             Action <IAsyncResult> action = delegate(IAsyncResult ar)
             {
                 try
                 {
                     string text = (string)ar.get_AsyncState();
                     Logger.DEBUG(text + " end");
                     IPHostEntry entry = Dns.EndGetHostEntry(ar);
                     Logger.DEBUG(text + " end, entry.AddressList.Length=" + entry.get_AddressList().Length.ToString());
                     Action <int, Dictionary <string, object> > action2 = delegate(int status, Dictionary <string, object> content)
                     {
                         string               text2        = content.get_Item("host") as string;
                         IPHostEntry          iPHostEntry3 = content.get_Item("entry") as IPHostEntry;
                         Action <IPHostEntry> action3      = content.get_Item("resultAction") as Action <IPHostEntry>;
                         this.dnsCache.set_Item(text2, entry);
                         action3.Invoke(entry);
                     };
                     Message message = new Message();
                     message.status = 0;
                     message.content.set_Item("host", text);
                     message.content.set_Item("entry", entry);
                     message.content.set_Item("resultAction", resultAction);
                     message.action = action2;
                     this.EnqueueDrive(message);
                 }
                 catch (Exception ex2)
                 {
                     Logger.ERROR(ex2.get_Message() + ":" + ex2.get_StackTrace());
                 }
             };
             Logger.DEBUG(host + " begin");
             Dns.BeginGetHostEntry(host, new AsyncCallback(action.Invoke), host);
         }
     }
     catch (Exception ex)
     {
         resultAction.Invoke(null);
         Logger.ERROR(ex.get_Message() + ":" + ex.get_StackTrace());
     }
 }