Browse Source

remove all tuple (#718)

tags/3.3.1
破娃酱 Syrone Wong 8 years ago
parent
commit
7586739a52
2 changed files with 52 additions and 28 deletions
  1. +27
    -15
      shadowsocks-csharp/Util/Util.cs
  2. +25
    -13
      shadowsocks-csharp/View/LogForm.cs

+ 27
- 15
shadowsocks-csharp/Util/Util.cs View File

@@ -9,6 +9,20 @@ using Shadowsocks.Controller;
namespace Shadowsocks.Util
{
public struct BandwidthScaleInfo
{
public float value;
public string unit_name;
public long unit;
public BandwidthScaleInfo(float value, string unit_name, long unit)
{
this.value = value;
this.unit_name = unit_name;
this.unit = unit;
}
}
public class Utils
{
private static bool? _portableMode;
@@ -114,7 +128,7 @@ namespace Shadowsocks.Util
public static string FormatBandwidth(long n)
{
var result = GetBandwidthScale(n);
return $"{result.Item1:0.##}{result.Item2}";
return $"{result.value:0.##}{result.unit_name}";
}
public static string FormatBytes(long bytes)
@@ -127,32 +141,32 @@ namespace Shadowsocks.Util
const long E = P * 1024L;
if (bytes >= P * 990)
return (bytes / (double)E).ToString("F5") + "EB";
return (bytes / (double)E).ToString("F5") + "EiB";
if (bytes >= T * 990)
return (bytes / (double)P).ToString("F5") + "PB";
return (bytes / (double)P).ToString("F5") + "PiB";
if (bytes >= G * 990)
return (bytes / (double)T).ToString("F5") + "TB";
return (bytes / (double)T).ToString("F5") + "TiB";
if (bytes >= M * 990)
{
return (bytes / (double)G).ToString("F4") + "GB";
return (bytes / (double)G).ToString("F4") + "GiB";
}
if (bytes >= M * 100)
{
return (bytes / (double)M).ToString("F1") + "MB";
return (bytes / (double)M).ToString("F1") + "MiB";
}
if (bytes >= M * 10)
{
return (bytes / (double)M).ToString("F2") + "MB";
return (bytes / (double)M).ToString("F2") + "MiB";
}
if (bytes >= K * 990)
{
return (bytes / (double)M).ToString("F3") + "MB";
return (bytes / (double)M).ToString("F3") + "MiB";
}
if (bytes > K * 2)
{
return (bytes / (double)K).ToString("F1") + "KB";
return (bytes / (double)K).ToString("F1") + "KiB";
}
return bytes.ToString();
return bytes.ToString() + "B";
}
/// <summary>
@@ -160,11 +174,9 @@ namespace Shadowsocks.Util
/// </summary>
/// <param name="n">Raw bandwidth</param>
/// <returns>
/// Item1: float, bandwidth with suitable scale (eg. 56)
/// Item2: string, scale unit name (eg. KiB)
/// Item3: long, scale unit (eg. 1024)
/// The BandwidthScaleInfo struct
/// </returns>
public static Tuple<float, string, long> GetBandwidthScale(long n)
public static BandwidthScaleInfo GetBandwidthScale(long n)
{
long scale = 1;
float f = n;
@@ -193,7 +205,7 @@ namespace Shadowsocks.Util
scale <<= 10;
unit = "TiB";
}
return new Tuple<float, string, long>(f, unit, scale);
return new BandwidthScaleInfo(f, unit, scale);
}
public static RegistryKey OpenUserRegKey( string name, bool writable ) {


+ 25
- 13
shadowsocks-csharp/View/LogForm.cs View File

@@ -14,6 +14,18 @@ using System.Text;
namespace Shadowsocks.View
{
struct TrafficInfo
{
public long inbound;
public long outbound;
public TrafficInfo(long inbound, long outbound)
{
this.inbound = inbound;
this.outbound = outbound;
}
}
public partial class LogForm : Form
{
long lastOffset;
@@ -24,7 +36,7 @@ namespace Shadowsocks.View
#region chart
long lastMaxSpeed;
public ShadowsocksController.QueueLast<Tuple<long, long>> traffic = new ShadowsocksController.QueueLast<Tuple<long, long>>();
ShadowsocksController.QueueLast<TrafficInfo> traffic = new ShadowsocksController.QueueLast<TrafficInfo>();
#endregion
public LogForm(ShadowsocksController controller, string filename)
@@ -54,7 +66,7 @@ namespace Shadowsocks.View
List<float> outboundPoints = new List<float>();
TextAnnotation inboundAnnotation = new TextAnnotation();
TextAnnotation outboundAnnotation = new TextAnnotation();
Tuple<float, string, long> bandwidthScale;
BandwidthScaleInfo bandwidthScale;
const long minScale = 50;
long maxSpeed = 0;
long lastInbound, lastOutbound;
@@ -65,12 +77,12 @@ namespace Shadowsocks.View
return;
foreach (var trafficPerSecond in traffic)
{
inboundPoints.Add(trafficPerSecond.Item1);
outboundPoints.Add(trafficPerSecond.Item2);
maxSpeed = Math.Max(maxSpeed, Math.Max(trafficPerSecond.Item1, trafficPerSecond.Item2));
inboundPoints.Add(trafficPerSecond.inbound);
outboundPoints.Add(trafficPerSecond.outbound);
maxSpeed = Math.Max(maxSpeed, Math.Max(trafficPerSecond.inbound, trafficPerSecond.outbound));
}
lastInbound = traffic.Last().Item1;
lastOutbound = traffic.Last().Item2;
lastInbound = traffic.Last().inbound;
lastOutbound = traffic.Last().outbound;
}
if (maxSpeed > 0)
@@ -87,15 +99,15 @@ namespace Shadowsocks.View
bandwidthScale = Utils.GetBandwidthScale(maxSpeed);
//rescale the original data points, since it is List<float>, .ForEach does not work
inboundPoints = inboundPoints.Select(p => p / bandwidthScale.Item3).ToList();
outboundPoints = outboundPoints.Select(p => p / bandwidthScale.Item3).ToList();
inboundPoints = inboundPoints.Select(p => p / bandwidthScale.unit).ToList();
outboundPoints = outboundPoints.Select(p => p / bandwidthScale.unit).ToList();
if (trafficChart.IsHandleCreated)
{
trafficChart.Series["Inbound"].Points.DataBindY(inboundPoints);
trafficChart.Series["Outbound"].Points.DataBindY(outboundPoints);
trafficChart.ChartAreas[0].AxisY.LabelStyle.Format = "{0:0.##} " + bandwidthScale.Item2;
trafficChart.ChartAreas[0].AxisY.Maximum = bandwidthScale.Item1;
trafficChart.ChartAreas[0].AxisY.LabelStyle.Format = "{0:0.##} " + bandwidthScale.unit_name;
trafficChart.ChartAreas[0].AxisY.Maximum = bandwidthScale.value;
inboundAnnotation.AnchorDataPoint = trafficChart.Series["Inbound"].Points.Last();
inboundAnnotation.Text = Utils.FormatBandwidth(lastInbound);
outboundAnnotation.AnchorDataPoint = trafficChart.Series["Outbound"].Points.Last();
@@ -110,10 +122,10 @@ namespace Shadowsocks.View
{
lock (this)
{
traffic = new ShadowsocksController.QueueLast<Tuple<long, long>>();
traffic = new ShadowsocksController.QueueLast<TrafficInfo>();
foreach (var trafficPerSecond in controller.traffic)
{
traffic.Enqueue(new Tuple<long, long>(trafficPerSecond.inboundIncreasement, trafficPerSecond.outboundIncreasement));
traffic.Enqueue(new TrafficInfo(trafficPerSecond.inboundIncreasement, trafficPerSecond.outboundIncreasement));
}
}
}


Loading…
Cancel
Save