diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index 5b6cd588..a2a34eee 100644 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -33,8 +33,10 @@ namespace Shadowsocks.Controller public AvailabilityStatistics availabilityStatistics = AvailabilityStatistics.Instance; public StatisticsStrategyConfiguration StatisticsConfiguration { get; private set; } - public long inboundCounter = 0; - public long outboundCounter = 0; + private long _inboundCounter = 0; + private long _outboundCounter = 0; + public long InboundCounter => Interlocked.Read(ref _inboundCounter); + public long OutboundCounter => Interlocked.Read(ref _outboundCounter); public QueueLast traffic; private bool stopped = false; @@ -364,7 +366,7 @@ namespace Shadowsocks.Controller public void UpdateInboundCounter(Server server, long n) { - Interlocked.Add(ref inboundCounter, n); + Interlocked.Add(ref _inboundCounter, n); if (_config.availabilityStatistics) { availabilityStatistics.UpdateInboundCounter(server, n); @@ -373,7 +375,7 @@ namespace Shadowsocks.Controller public void UpdateOutboundCounter(Server server, long n) { - Interlocked.Add(ref outboundCounter, n); + Interlocked.Add(ref _outboundCounter, n); if (_config.availabilityStatistics) { availabilityStatistics.UpdateOutboundCounter(server, n); @@ -579,19 +581,17 @@ namespace Shadowsocks.Controller { TrafficPerSecond previous = traffic.Last; TrafficPerSecond current = new TrafficPerSecond(); - current.inboundCounter = inboundCounter; - current.outboundCounter = outboundCounter; - current.inboundIncreasement = inboundCounter - previous.inboundCounter; - current.outboundIncreasement = outboundCounter - previous.outboundCounter; + + var inbound = current.inboundCounter = InboundCounter; + var outbound = current.outboundCounter = OutboundCounter; + current.inboundIncreasement = inbound - previous.inboundCounter; + current.outboundIncreasement = outbound - previous.outboundCounter; traffic.Enqueue(current); if (traffic.Count > queueMaxSize) traffic.Dequeue(); - if (TrafficChanged != null) - { - TrafficChanged(this, new EventArgs()); - } + TrafficChanged?.Invoke(this, new EventArgs()); Thread.Sleep(1000); } diff --git a/shadowsocks-csharp/View/LogForm.cs b/shadowsocks-csharp/View/LogForm.cs index 481c341a..7aab4e8e 100644 --- a/shadowsocks-csharp/View/LogForm.cs +++ b/shadowsocks-csharp/View/LogForm.cs @@ -171,7 +171,7 @@ namespace Shadowsocks.View } this.Text = I18N.GetString("Log Viewer") + - $" [in: {Utils.FormatBandwidth(controller.inboundCounter)}, out: {Utils.FormatBandwidth(controller.outboundCounter)}]"; + $" [in: {Utils.FormatBandwidth(controller.InboundCounter)}, out: {Utils.FormatBandwidth(controller.OutboundCounter)}]"; } private void LogForm_Load(object sender, EventArgs e)