diff --git a/shadowsocks-csharp/Controller/Service/AvailabilityStatistics.cs b/shadowsocks-csharp/Controller/Service/AvailabilityStatistics.cs index 8f2e6eaa..f6e6831b 100644 --- a/shadowsocks-csharp/Controller/Service/AvailabilityStatistics.cs +++ b/shadowsocks-csharp/Controller/Service/AvailabilityStatistics.cs @@ -133,16 +133,18 @@ namespace Shadowsocks.Controller { Logging.Debug("Ping " + server.FriendlyName()); if (server.server == "") return null; - var IP = Dns.GetHostAddresses(server.server).First(ip => (ip.AddressFamily == AddressFamily.InterNetwork || ip.AddressFamily == AddressFamily.InterNetworkV6)); - var ping = new Ping(); var ret = new List(); - foreach (var timestamp in Enumerable.Range(0, Repeat).Select(_ => DateTime.Now.ToString(DateTimePattern))) - { - //ICMP echo. we can also set options and special bytes - try + try { + var IP = Dns.GetHostAddresses(server.server).First(ip => (ip.AddressFamily == AddressFamily.InterNetwork || ip.AddressFamily == AddressFamily.InterNetworkV6)); + var ping = new Ping(); + + foreach (var timestamp in Enumerable.Range(0, Repeat).Select(_ => DateTime.Now.ToString(DateTimePattern))) { - var reply = await ping.SendTaskAsync(IP, Timeout); - ret.Add(new List> + //ICMP echo. we can also set options and special bytes + try + { + var reply = await ping.SendTaskAsync(IP, Timeout); + ret.Add(new List> { new KeyValuePair("Timestamp", timestamp), new KeyValuePair("Server", server.FriendlyName()), @@ -150,14 +152,19 @@ namespace Shadowsocks.Controller new KeyValuePair("RoundtripTime", reply?.RoundtripTime.ToString()) //new KeyValuePair("data", reply.Buffer.ToString()); // The data of reply }); - Thread.Sleep(Timeout + new Random().Next() % Timeout); - //Do ICMPTest in a random frequency - } - catch (Exception e) - { - Logging.Error($"An exception occured while eveluating {server.FriendlyName()}"); - Logging.LogUsefulException(e); + Thread.Sleep(Timeout + new Random().Next() % Timeout); + //Do ICMPTest in a random frequency + } + catch (Exception e) + { + Logging.Error($"An exception occured while eveluating {server.FriendlyName()}"); + Logging.LogUsefulException(e); + } } + }catch(Exception e) + { + Logging.Error($"An exception occured while eveluating {server.FriendlyName()}"); + Logging.LogUsefulException(e); } return ret; } @@ -258,9 +265,20 @@ namespace Shadowsocks.Controller Logging.Debug($"loading statistics from {path}"); if (!File.Exists(path)) { - Console.WriteLine($"statistics file does not exist, try to reload {RetryInterval / 60 / 1000} minutes later"); - _timer.Change(RetryInterval, Interval); - return; + try { + using (FileStream fs = File.Create(path)) + { + //do nothing + } + }catch(Exception e) + { + Logging.LogUsefulException(e); + } + if (!File.Exists(path)) { + Console.WriteLine($"statistics file does not exist, try to reload {RetryInterval / 60 / 1000} minutes later"); + _timer.Change(RetryInterval, Interval); + return; + } } RawStatistics = (from l in File.ReadAllLines(path).Skip(1) let strings = l.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)