/// <summary> /// 通过URL地址获取IP /// </summary> /// <param name="url"></param> /// <returns></returns> public static List<Object_IP> GetSourceList(string url) { List<Object_IP> list = new List<Object_IP>(); WebClient wc = new WebClient(); string strip = wc.DownloadString(url); strip = strip.Replace("[", "").Replace("]", "").Replace("{", "").Replace("}", "").Replace("(", "").Replace(")", "").Replace(" ", "").Replace("\r\n", ""); string[] tmpstr = strip.Split(new string[]{"|",","},StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < tmpstr.Length; i++) { var ip = new Object_IP(); ip.Ip = tmpstr[i].ToString(); list.Add(ip); } return list; }
/// <summary> /// 通过URL地址获取IP /// </summary> /// <param name="url"></param> /// <returns></returns> public static List <Object_IP> GetSourceList(string url) { List <Object_IP> list = new List <Object_IP>(); WebClient wc = new WebClient(); string strip = wc.DownloadString(url); strip = strip.Replace("[", "").Replace("]", "").Replace("{", "").Replace("}", "").Replace("(", "").Replace(")", "").Replace(" ", "").Replace("\r\n", ""); string[] tmpstr = strip.Split(new string[] { "|", "," }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < tmpstr.Length; i++) { var ip = new Object_IP(); ip.Ip = tmpstr[i].ToString(); list.Add(ip); } return(list); }
public void GetListItem(List<Object_Server> list,Action<Object_IP> callbackIP) { List<Object_IP> listIP = new List<Object_IP>(); list.ForEach(item => { Object_IP o = new Object_IP(); o.Ip = item.Ip; listIP.Add(o); }); Parallel.ForEach(listIP, (item) => { item.HttpSetSpeed(); callbackIP(item); }); }
public void GetListItem(List <Object_Server> list, Action <Object_IP> callbackIP) { List <Object_IP> listIP = new List <Object_IP>(); list.ForEach(item => { Object_IP o = new Object_IP(); o.Ip = item.Ip; listIP.Add(o); }); Parallel.ForEach(listIP, (item) => { item.HttpSetSpeed(); callbackIP(item); }); }
/// <summary> /// 按升序排列,空值默认为最大 /// </summary> /// <param name="o1"></param> /// <param name="o2"></param> /// <returns></returns> private int CompareSpeedAsc(Object_IP o1, Object_IP o2) { if (o1.Speed == null) { if (o2.Speed == null) { return(0); } return(1); } if (o2.Speed == null) { return(-1); } int retval = o1.Speed.Value.CompareTo(o2.Speed.Value); return(retval); }
/// <summary> /// 切换服务器 /// </summary> /// <param name="objp">切换前的服务器</param> /// <param name="callback">切换后的服务器</param> private void ServerSwitch(CancellationToken token, Object_IP objp,Action<Object_IP> callback) { DeterMineCall(() => { for (int i = switchSeed; i < dnsListTmp.Count; i++) { if (token.IsCancellationRequested) { return; } objp.Ip = dnsListTmp[i].Ip; objp.HttpSetSpeed(); switchSeed++; if (objp.Speed != null) { hostAction.AddDnsResolve(objp.Ip, Properties.Resources.domain); callback(objp); break; } } if (objp.Speed != null) { AddListViewItem("切换完成 [{0}] ,响应速度={1}秒", Color.Green, new object[] { objp.Ip, objp.Speed.Value.TotalMilliseconds.ToString("#0.00") }); } }); }
/// <summary> /// 监听方法 /// </summary> private void ListenMethod(object token) { CancellationToken ct = (CancellationToken)token; if (ct.IsCancellationRequested) { return; } lock (locker) { Object_IP localhost = new Object_IP(); string hostLine = hostAction.GetCacheServerIP(Properties.Resources.domain); if (ipAddress != null) { localhost.Ip = ipAddress.ToString(); ipAddress = null; } else { localhost.Ip = hostLine == "" ? Properties.Resources.domain : hostLine.Replace(" " + Properties.Resources.domain, ""); } localhost.HttpSetSpeed(); hostAction.AddDnsResolve(localhost.Ip, Properties.Resources.domain); if (localhost.Speed == null) { AddListViewItem("服务器 [{0}] 操作超时,正在切换... ", Color.FromArgb(192, 0, 192), localhost.Ip); ServerSwitch(ct, localhost, (obj) => localhost = obj); } else { if (localhost.Speed.Value.TotalSeconds > 1)//小于1秒,为正常速度 { AddListViewItem("服务器 [{0}] 速度过慢,响应速度={1}秒,正在切换... ", Color.FromArgb(192, 0, 192), localhost.Ip, localhost.Speed.Value.TotalSeconds.ToString("#0.00")); ServerSwitch(ct, localhost, (obj) => localhost = obj); } else { AddListViewItem("服务器 [{0}] 正常,响应速度={1}秒", Color.FromArgb(192, 0, 192), localhost.Ip, localhost.Speed.Value.TotalSeconds.ToString("#0.00")); } } } System.Threading.Thread.Sleep(10000); ListenMethod(token); }
/// <summary> /// 按降序排序,空值默认为最小 /// </summary> /// <param name="o1"></param> /// <param name="o2"></param> /// <returns></returns> private int CompareSpeedDesc(Object_IP o1, Object_IP o2) { if (o1.Speed == null) { if (o2.Speed == null) { return 0; } return 1; } if (o2.Speed == null) { return -1; } int retval = o2.Speed.Value.CompareTo(o1.Speed.Value); return retval; }