Skip to content

Commit

Permalink
up speed
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Aug 27, 2019
1 parent dcd050e commit 54bebd9
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 112 deletions.
136 changes: 52 additions & 84 deletions v2rayN/v2rayN/Forms/MainForm.cs
@@ -1,12 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Windows.Forms;
using v2rayN.Handler;
using v2rayN.HttpProxyHandler;
using v2rayN.Mode;
using System.Collections.Generic;
using System.IO;

namespace v2rayN.Forms
{
Expand All @@ -17,7 +17,6 @@ public partial class MainForm : BaseForm
private V2rayUpdateHandle v2rayUpdateHandle;
private V2rayUpdateHandle v2rayUpdateHandle2;
private List<int> lvSelecteds = new List<int>();

private StatisticsHandler statistics = null;

#region Window 事件
Expand All @@ -44,70 +43,60 @@ private void MainForm_Load(object sender, EventArgs e)
v2rayHandler.ProcessEvent += v2rayHandler_ProcessEvent;
if (config.enableStatistics)
{
statistics = new StatisticsHandler(config, UpdateHandler);
statistics = new StatisticsHandler(config, UpdateStatisticsHandler);
}
}

private void UpdateHandler(ulong totalUp, ulong totalDown, ulong up, ulong down, List<Mode.ServerStatistics> statistics)
private void UpdateStatisticsHandler(ulong totalUp, ulong totalDown, ulong up, ulong down, List<Mode.ServerStatistics> statistics)
{
double up_amount = 0.0, down_amount;
string up_unit = "", down_unit;

up /= (ulong)(config.statisticsFreshRate / 1000f);
down /= (ulong)(config.statisticsFreshRate / 1000f);
try
{
up /= (ulong)(config.statisticsFreshRate / 1000f);
down /= (ulong)(config.statisticsFreshRate / 1000f);
toolSslServerSpeed.Text = string.Format(
"{0}/s↑ | {1}/s↓",
Utils.HumanFy(up),
Utils.HumanFy(down)
);

Utils.ToHumanReadable(up, out up_amount, out up_unit);
Utils.ToHumanReadable(down, out down_amount, out down_unit);
up_unit += "/s";
down_unit += "/s";
List<string[]> datas = new List<string[]>();
for (int i = 0; i < config.vmess.Count; i++)
{
string totalUp_ = string.Empty,
totalDown_ = string.Empty,
todayUp_ = string.Empty,
todayDown_ = string.Empty;
var index = statistics.FindIndex(item_ => (config.vmess[i].address == item_.address && config.vmess[i].port == item_.port && config.vmess[i].path == item_.path));
if (index != -1)
{
totalUp_ = Utils.HumanFy(statistics[index].totalUp);
totalDown_ = Utils.HumanFy(statistics[index].totalDown);
todayUp_ = Utils.HumanFy(statistics[index].todayUp);
todayDown_ = Utils.HumanFy(statistics[index].todayDown);
}

toolSslServerSpeed.Text = string.Format(
"{0:f2} {1}↑ | {2:f2} {3}↓",
up_amount,
up_unit,
down_amount,
down_unit
);
datas.Add(new string[] { totalUp_, totalDown_, todayUp_, todayDown_ });
}

List<string[]> datas = new List<string[]>();
for (int i = 0; i < config.vmess.Count; i++)
{
string totalUp_ = string.Empty,
totalDown_ = string.Empty,
todayUp_ = string.Empty,
todayDown_ = string.Empty;
var index = statistics.FindIndex(item_ => (config.vmess[i].address == item_.address && config.vmess[i].port == item_.port && config.vmess[i].path == item_.path));
if (index != -1)
lvServers.Invoke((MethodInvoker)delegate
{
Func<ulong, string> human_fy = (amount) =>
lvServers.SuspendLayout();
for (int i = 0; i < datas.Count; i++)
{
double result;
string unit;
Utils.ToHumanReadable(amount, out result, out unit);
return $"{string.Format("{0:f2}", result)}{unit}";
};
totalUp_ = human_fy(statistics[index].totalUp);
totalDown_ = human_fy(statistics[index].totalDown);
todayUp_ = human_fy(statistics[index].todayUp);
todayDown_ = human_fy(statistics[index].todayDown);
}
var indexStart = 9;
lvServers.Items[i].SubItems[indexStart++].Text = datas[i][0];
lvServers.Items[i].SubItems[indexStart++].Text = datas[i][1];
lvServers.Items[i].SubItems[indexStart++].Text = datas[i][2];
lvServers.Items[i].SubItems[indexStart++].Text = datas[i][3];
}
lvServers.ResumeLayout();
});

datas.Add(new string[] { totalUp_, totalDown_, todayUp_, todayDown_ });
}

lvServers.Invoke((MethodInvoker)delegate
catch (Exception ex)
{
lvServers.SuspendLayout();
for (int i = 0; i < datas.Count; i++)
{
var indexStart = 9;
lvServers.Items[i].SubItems[indexStart++].Text = datas[i][0];
lvServers.Items[i].SubItems[indexStart++].Text = datas[i][1];
lvServers.Items[i].SubItems[indexStart++].Text = datas[i][2];
lvServers.Items[i].SubItems[indexStart++].Text = datas[i][3];
}
lvServers.ResumeLayout();
});
Utils.SaveLog(ex.Message, ex);
}
}

private void MainForm_VisibleChanged(object sender, EventArgs e)
Expand Down Expand Up @@ -179,8 +168,7 @@ protected override void WndProc(ref Message m)
Utils.SaveLog("Windows shutdown UnsetProxy");
//CloseV2ray();
ConfigHandler.ToJsonFile(config);

statistics?.saveToFile();
statistics?.saveToFile();
ProxySetting.UnsetProxy();
m.Result = (IntPtr)1;
break;
Expand Down Expand Up @@ -262,17 +250,10 @@ private void RefreshServersView()
var index = statistics.Statistic.FindIndex(item_ => item_.address == item.address);
if (index != -1)
{
Func<ulong, string> human_fy = (amount) =>
{
double result;
string unit;
Utils.ToHumanReadable(amount, out result, out unit);
return $"{string.Format("{0:f2}", result)}{unit}";
};
totalUp = human_fy(statistics.Statistic[index].totalUp);
totalDown = human_fy(statistics.Statistic[index].totalDown);
todayUp = human_fy(statistics.Statistic[index].todayUp);
todayDown = human_fy(statistics.Statistic[index].todayDown);
totalUp = Utils.HumanFy(statistics.Statistic[index].totalUp);
totalDown = Utils.HumanFy(statistics.Statistic[index].totalDown);
todayUp = Utils.HumanFy(statistics.Statistic[index].todayUp);
todayDown = Utils.HumanFy(statistics.Statistic[index].todayDown);
}

lvItem = new ListViewItem(new string[]
Expand Down Expand Up @@ -509,23 +490,10 @@ private void lvServers_KeyDown(object sender, KeyEventArgs e)
menuSelectAll_Click(null, null);
break;
case Keys.T:
// Speed test selected servers
if (!config.sysAgentEnabled || config.listenerType != 1)
{
UI.Show(UIRes.I18N("NeedHttpGlobalProxy"));
return;
}

UI.Show(UIRes.I18N("SpeedServerTips"));

GetLvSelectedIndex();
ServerSpeedTest();
menuSpeedServer_Click(null, null);
break;
case Keys.P:
// Ping selected servers
GetLvSelectedIndex();
ClearTestResult();
bgwPing.RunWorkerAsync();
menuPingServer_Click(null, null);
break;
}
}
Expand Down Expand Up @@ -1090,7 +1058,7 @@ private void bgwPing_ProgressChanged(object sender, System.ComponentModel.Progre
private void SetTestResult(int k, string txt)
{
config.vmess[k].testResult = txt;
lvServers.Items[k].SubItems[lvServers.Items[k].SubItems.Count - 1].Text = txt;
lvServers.Items[k].SubItems[8].Text = txt;
}
private void ClearTestResult()
{
Expand Down
19 changes: 8 additions & 11 deletions v2rayN/v2rayN/Forms/OptionSettingForm.cs
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
using v2rayN.Handler;

Expand Down Expand Up @@ -336,15 +333,15 @@ private int SaveGUI()

config.statisticsFreshRate = (int)cbFreshrate.SelectedValue;

if(lastEnableStatistics != config.enableStatistics)
{
/// https://stackoverflow.com/questions/779405/how-do-i-restart-my-c-sharp-winform-application
// Shut down the current app instance.
Application.Exit();
//if(lastEnableStatistics != config.enableStatistics)
//{
// /// https://stackoverflow.com/questions/779405/how-do-i-restart-my-c-sharp-winform-application
// // Shut down the current app instance.
// Application.Exit();

// Restart the app passing "/restart [processId]" as cmd line args
Process.Start(Application.ExecutablePath, "/restart " + Process.GetCurrentProcess().Id);
}
// // Restart the app passing "/restart [processId]" as cmd line args
// Process.Start(Application.ExecutablePath, "/restart " + Process.GetCurrentProcess().Id);
//}
return 0;
}

Expand Down
6 changes: 3 additions & 3 deletions v2rayN/v2rayN/Global.cs
Expand Up @@ -149,9 +149,9 @@ class Global
public const uint InboundAPIPort = 10805;
public enum StatisticsFreshRate
{
quick = 500,
medium = 1000,
slow = 2000
quick = 1000,
medium = 2000,
slow = 3000
}
public const string StatisticLogDirectory = "Statistics";
public const string StatisticLogOverall = "overall.txt";
Expand Down
9 changes: 3 additions & 6 deletions v2rayN/v2rayN/Handler/StatisticsHandler.cs
@@ -1,13 +1,10 @@
using System;
using System.Threading;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Diagnostics;

using System.Threading;
using v2rayN.Mode;

namespace v2rayN.Handler
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/Properties/AssemblyInfo.cs
Expand Up @@ -33,4 +33,4 @@
// 方法是按如下所示使用“*”:
//[assembly: AssemblyVersion("1.0.*")]
//[assembly: AssemblyVersion("1.0.0")]
[assembly: AssemblyFileVersion("2.36")]
[assembly: AssemblyFileVersion("2.37")]
20 changes: 14 additions & 6 deletions v2rayN/v2rayN/Tool/Utils.cs
Expand Up @@ -273,19 +273,19 @@ public static void ToHumanReadable(ulong amount, out double result, out string u
{
var factor = 1024u;
var KBs = amount / factor;
if(KBs > 0)
if (KBs > 0)
{
// multi KB
var MBs = KBs / factor;
if(MBs > 0)
if (MBs > 0)
{
// multi MB
var GBs = MBs / factor;
if(GBs > 0)
if (GBs > 0)
{
// multi GB
var TBs = GBs / factor;
if(TBs > 0)
if (TBs > 0)
{
// 你是魔鬼吗? 用这么多流量
result = TBs + GBs % factor / (factor + 0.0);
Expand All @@ -303,20 +303,28 @@ public static void ToHumanReadable(ulong amount, out double result, out string u
result = KBs + amount % factor / (factor + 0.0);
unit = "KB";
return;
}
}
else
{
result = amount;
unit = "B";
}
}

public static string HumanFy(ulong amount)
{
double result;
string unit;
ToHumanReadable(amount, out result, out unit);
return $"{string.Format("{0:f2}", result)}{unit}";
}

public static void DedupServerList(List<Mode.VmessItem> source, out List<Mode.VmessItem> result)
{
var list = new List<Mode.VmessItem>();
foreach (var item in source)
{
if(!list.Exists(i => item.address == i.address && item.port == i.port && item.path == i.path))
if (!list.Exists(i => item.address == i.address && item.port == i.port && item.path == i.path))
{
list.Add(item);
}
Expand Down
3 changes: 2 additions & 1 deletion v2rayN/v2rayN/v2rayN.csproj.user
Expand Up @@ -16,6 +16,7 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<StartAction>Project</StartAction>
<StartArguments>/restart 1001</StartArguments>
<StartArguments>
</StartArguments>
</PropertyGroup>
</Project>

0 comments on commit 54bebd9

Please sign in to comment.