reduce 3rd packages

This commit is contained in:
Gang Zhuo
2016-03-04 03:04:10 -05:00
parent 3da00178de
commit 2aa0620ddd
6 changed files with 143 additions and 178 deletions
@@ -107,67 +107,30 @@ namespace Shadowsocks.Controller
var bytes = inbound - lastInbound;
_lastInboundCounter[id] = inbound;
var inboundSpeed = GetSpeedInKiBPerSecond(bytes, _monitorInterval.TotalSeconds);
_inboundSpeedRecords.GetOrAdd(id, new List<int> {inboundSpeed}).Add(inboundSpeed);
_inboundSpeedRecords.GetOrAdd(id, (k) =>
{
List<int> records = new List<int>();
records.Add(inboundSpeed);
return records;
});
var lastOutbound = _lastOutboundCounter[id];
var outbound = _outboundCounter[id];
bytes = outbound - lastOutbound;
_lastOutboundCounter[id] = outbound;
var outboundSpeed = GetSpeedInKiBPerSecond(bytes, _monitorInterval.TotalSeconds);
_outboundSpeedRecords.GetOrAdd(id, new List<int> {outboundSpeed}).Add(outboundSpeed);
_outboundSpeedRecords.GetOrAdd(id, (k) =>
{
List<int> records = new List<int>();
records.Add(outboundSpeed);
return records;
});
Logging.Debug(
$"{id}: current/max inbound {inboundSpeed}/{_inboundSpeedRecords[id].Max()} KiB/s, current/max outbound {outboundSpeed}/{_outboundSpeedRecords[id].Max()} KiB/s");
}
}
private async Task<ICMPResult> ICMPTest(Server server)
{
Logging.Debug("Ping " + server.FriendlyName());
if (server.server == "") return null;
var result = new ICMPResult(server);
try
{
var IP =
Dns.GetHostAddresses(server.server)
.First(
ip =>
ip.AddressFamily == AddressFamily.InterNetwork ||
ip.AddressFamily == AddressFamily.InterNetworkV6);
var ping = new Ping();
foreach (var _ in Enumerable.Range(0, Repeat))
{
try
{
var reply = await ping.SendTaskAsync(IP, TimeoutMilliseconds);
if (reply.Status.Equals(IPStatus.Success))
{
result.RoundtripTime.Add((int?) reply.RoundtripTime);
}
else
{
result.RoundtripTime.Add(null);
}
//Do ICMPTest in a random frequency
Thread.Sleep(TimeoutMilliseconds + new Random().Next()%TimeoutMilliseconds);
}
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 result;
}
private void Reset()
{
_inboundSpeedRecords.Clear();
@@ -183,7 +146,7 @@ namespace Shadowsocks.Controller
FilterRawStatistics();
}
private async void UpdateRecords()
private void UpdateRecords()
{
var records = new Dictionary<string, StatisticsRecord>();
@@ -202,14 +165,11 @@ namespace Shadowsocks.Controller
records[id] = record;
else
records.Add(id, record);
}
if (Config.Ping)
{
var icmpResults = await TaskEx.WhenAll(_controller.GetCurrentConfiguration().configs.Select(ICMPTest));
foreach (var result in icmpResults.Where(result => result != null))
if (Config.Ping)
{
records[result.Server.Identifier()].SetResponse(result.RoundtripTime);
MyPing ping = new MyPing(server, Repeat, record);
ping.Completed += ping_Completed;
ping.Start();
}
}
@@ -219,15 +179,22 @@ namespace Shadowsocks.Controller
}
}
private void ping_Completed(object sender, MyPing.CompletedEventArgs e)
{
Server server = ((MyPing)sender).server;
StatisticsRecord record = (StatisticsRecord)((MyPing)sender).userstate;
record.SetResponse(e.RoundtripTime);
}
private void AppendRecord(string serverIdentifier, StatisticsRecord record)
{
List<StatisticsRecord> records;
if (!RawStatistics.TryGetValue(serverIdentifier, out records))
{
records = new List<StatisticsRecord>();
RawStatistics[serverIdentifier] = records;
}
records.Add(record);
RawStatistics[serverIdentifier] = records;
}
private void Save()
@@ -298,21 +265,10 @@ namespace Shadowsocks.Controller
private static int GetSpeedInKiBPerSecond(long bytes, double seconds)
{
var result = (int) (bytes/seconds)/1024;
var result = (int)(bytes / seconds) / 1024;
return result;
}
private class ICMPResult
{
internal readonly List<int?> RoundtripTime = new List<int?>();
internal readonly Server Server;
internal ICMPResult(Server server)
{
Server = server;
}
}
public void Dispose()
{
_recorder.Dispose();
@@ -321,44 +277,131 @@ namespace Shadowsocks.Controller
public void UpdateLatency(Server server, int latency)
{
List<int> records;
_latencyRecords.TryGetValue(server.Identifier(), out records);
if (records == null)
_latencyRecords.GetOrAdd(server.Identifier(), (k) =>
{
records = new List<int>();
}
records.Add(latency);
_latencyRecords[server.Identifier()] = records;
List<int> records = new List<int>();
records.Add(latency);
return records;
});
}
public void UpdateInboundCounter(Server server, long n)
{
long count;
if (_inboundCounter.TryGetValue(server.Identifier(), out count))
_inboundCounter.AddOrUpdate(server.Identifier(), (k) =>
{
count += n;
}
else
{
count = n;
_lastInboundCounter[server.Identifier()] = 0;
}
_inboundCounter[server.Identifier()] = count;
_lastInboundCounter.GetOrAdd(server.Identifier(), 0);
return n;
}, (k, v) => (v + n));
}
public void UpdateOutboundCounter(Server server, long n)
{
long count;
if (_outboundCounter.TryGetValue(server.Identifier(), out count))
_outboundCounter.AddOrUpdate(server.Identifier(), (k) =>
{
count += n;
}
else
{
count = n;
_lastOutboundCounter[server.Identifier()] = 0;
}
_outboundCounter[server.Identifier()] = count;
_lastOutboundCounter.GetOrAdd(server.Identifier(), 0);
return n;
}, (k, v) => (v + n));
}
class MyPing
{
//arguments for ICMP tests
public const int TimeoutMilliseconds = 500;
public EventHandler<CompletedEventArgs> Completed;
public Server server;
public object userstate;
private int repeat;
private IPAddress ip;
private Ping ping;
private List<int?> RoundtripTime;
public MyPing(Server server, int repeat, object userstate)
{
this.server = server;
this.repeat = repeat;
this.userstate = userstate;
RoundtripTime = new List<int?>(repeat);
ping = new Ping();
ping.PingCompleted += Ping_PingCompleted;
}
public void Start()
{
Logging.Debug("Ping " + server.FriendlyName());
if (server.server == "")
return;
new Task(() => ICMPTest(0)).Start();
}
private void ICMPTest(int delay)
{
try
{
if (ip == null)
{
ip = Dns.GetHostAddresses(server.server)
.First(
ip =>
ip.AddressFamily == AddressFamily.InterNetwork ||
ip.AddressFamily == AddressFamily.InterNetworkV6);
}
repeat--;
if (delay > 0)
Thread.Sleep(delay);
ping.SendAsync(ip, TimeoutMilliseconds, null);
}
catch (Exception e)
{
Logging.Error($"An exception occured while eveluating {server.FriendlyName()}");
Logging.LogUsefulException(e);
}
}
private void Ping_PingCompleted(object sender, PingCompletedEventArgs e)
{
try
{
if (e.Reply.Status == IPStatus.Success)
{
RoundtripTime.Add((int?)e.Reply.RoundtripTime);
}
else
{
RoundtripTime.Add(null);
}
TestNext();
}
catch (Exception ex)
{
Logging.Error($"An exception occured while eveluating {server.FriendlyName()}");
Logging.LogUsefulException(ex);
}
}
private void TestNext()
{
if (repeat > 0)
{
//Do ICMPTest in a random frequency
int delay = TimeoutMilliseconds + new Random().Next() % TimeoutMilliseconds;
new Task(() => ICMPTest(delay)).Start();
}
else
{
Completed?.Invoke(this, new CompletedEventArgs
{
RoundtripTime = RoundtripTime
});
}
}
public class CompletedEventArgs : EventArgs
{
public List<int?> RoundtripTime;
}
}
}
}
@@ -5,7 +5,6 @@ using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Shadowsocks.Controller.Strategy;
@@ -311,7 +310,7 @@ namespace Shadowsocks.Controller
{
if (_config.availabilityStatistics)
{
new Task(() => availabilityStatistics.UpdateLatency(server, (int) latency.TotalMilliseconds)).Start();
availabilityStatistics.UpdateLatency(server, (int)latency.TotalMilliseconds);
}
}
@@ -320,7 +319,7 @@ namespace Shadowsocks.Controller
Interlocked.Add(ref inboundCounter, n);
if (_config.availabilityStatistics)
{
new Task(() => availabilityStatistics.UpdateInboundCounter(server, n)).Start();
availabilityStatistics.UpdateInboundCounter(server, n);
}
}
@@ -329,7 +328,7 @@ namespace Shadowsocks.Controller
Interlocked.Add(ref outboundCounter, n);
if (_config.availabilityStatistics)
{
new Task(() => availabilityStatistics.UpdateOutboundCounter(server, n)).Start();
availabilityStatistics.UpdateOutboundCounter(server, n);
}
}