Browse Source

Code optimization, function asynchrony

pull/5891/head
2dust 1 month ago
parent
commit
b3c2084b76
  1. 9
      v2rayN/ServiceLib/Handler/ProfileExHandler.cs
  2. 4
      v2rayN/ServiceLib/Handler/StatisticsHandler.cs
  3. 4
      v2rayN/ServiceLib/Handler/TaskHandler.cs
  4. 18
      v2rayN/ServiceLib/Services/DownloadService.cs
  5. 10
      v2rayN/ServiceLib/Services/SpeedtestService.cs
  6. 2
      v2rayN/ServiceLib/Services/Statistics/StatisticsSingboxService.cs
  7. 2
      v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs
  8. 2
      v2rayN/v2rayN/Views/BackupAndRestoreView.xaml
  9. 4
      v2rayN/v2rayN/Views/CheckUpdateView.xaml
  10. 4
      v2rayN/v2rayN/Views/QrcodeView.xaml

9
v2rayN/ServiceLib/Handler/ProfileExHandler.cs

@ -14,9 +14,14 @@ namespace ServiceLib.Handler
public ProfileExHandler()
{
Init();
}
private async Task Init()
{
await InitData();
Task.Run(async () =>
{
await Init();
while (true)
{
await SaveQueueIndexIds();
@ -25,7 +30,7 @@ namespace ServiceLib.Handler
});
}
private async Task Init()
private async Task InitData()
{
await SQLiteHelper.Instance.ExecuteAsync($"delete from ProfileExItem where indexId not in ( select indexId from ProfileItem )");

4
v2rayN/ServiceLib/Handler/StatisticsHandler.cs

@ -14,7 +14,7 @@
public List<ServerStatItem> ServerStat => _lstServerStat;
public void Init(Config config, Action<ServerSpeedItem> updateFunc)
public async Task Init(Config config, Action<ServerSpeedItem> updateFunc)
{
_config = config;
_updateFunc = updateFunc;
@ -23,7 +23,7 @@
return;
}
InitData();
await InitData();
_statisticsV2Ray = new StatisticsV2rayService(config, UpdateServerStatHandler);
_statisticsSingbox = new StatisticsSingboxService(config, UpdateServerStatHandler);

4
v2rayN/ServiceLib/Handler/TaskHandler.cs

@ -5,10 +5,6 @@
private static readonly Lazy<TaskHandler> _instance = new(() => new());
public static TaskHandler Instance => _instance.Value;
public TaskHandler()
{
}
public void RegUpdateTask(Config config, Action<bool, string> updateFunc)
{
Task.Run(() => UpdateTaskRunSubscription(config, updateFunc));

18
v2rayN/ServiceLib/Services/DownloadService.cs

@ -59,7 +59,7 @@ namespace ServiceLib.Services
UpdateCompleted?.Invoke(this, new RetResult(value > 100, $"...{value}%"));
};
var webProxy = GetWebProxy(blProxy);
var webProxy = await GetWebProxy(blProxy);
await DownloaderHelper.Instance.DownloadFileAsync(webProxy,
url,
fileName,
@ -84,7 +84,7 @@ namespace ServiceLib.Services
var webRequestHandler = new SocketsHttpHandler
{
AllowAutoRedirect = false,
Proxy = GetWebProxy(blProxy)
Proxy = await GetWebProxy(blProxy)
};
HttpClient client = new(webRequestHandler);
@ -151,7 +151,7 @@ namespace ServiceLib.Services
try
{
SetSecurityProtocol(AppHandler.Instance.Config.guiItem.enableSecurityProtocolTls13);
var webProxy = GetWebProxy(blProxy);
var webProxy = await GetWebProxy(blProxy);
var client = new HttpClient(new SocketsHttpHandler()
{
Proxy = webProxy,
@ -197,7 +197,7 @@ namespace ServiceLib.Services
{
SetSecurityProtocol(AppHandler.Instance.Config.guiItem.enableSecurityProtocolTls13);
var webProxy = GetWebProxy(blProxy);
var webProxy = await GetWebProxy(blProxy);
if (Utils.IsNullOrEmpty(userAgent))
{
@ -222,7 +222,7 @@ namespace ServiceLib.Services
{
try
{
webProxy ??= GetWebProxy(true);
webProxy ??= await GetWebProxy(true);
try
{
@ -274,14 +274,14 @@ namespace ServiceLib.Services
return responseTime;
}
private WebProxy? GetWebProxy(bool blProxy)
private async Task<WebProxy?> GetWebProxy(bool blProxy)
{
if (!blProxy)
{
return null;
}
var httpPort = AppHandler.Instance.GetLocalPort(EInboundProtocol.http);
if (!SocketCheck(Global.Loopback, httpPort))
if (await SocketCheck(Global.Loopback, httpPort) == false)
{
return null;
}
@ -289,13 +289,13 @@ namespace ServiceLib.Services
return new WebProxy(Global.Loopback, httpPort);
}
private bool SocketCheck(string ip, int port)
private async Task<bool> SocketCheck(string ip, int port)
{
try
{
IPEndPoint point = new(IPAddress.Parse(ip), port);
using Socket? sock = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(point);
await sock.ConnectAsync(point);
return true;
}
catch (Exception)

10
v2rayN/ServiceLib/Services/SpeedtestService.cs

@ -99,11 +99,11 @@ namespace ServiceLib.Services
{
continue;
}
tasks.Add(Task.Run(() =>
tasks.Add(Task.Run(async () =>
{
try
{
int time = GetTcpingTime(it.Address, it.Port);
int time = await GetTcpingTime(it.Address, it.Port);
var output = FormatOut(time, Global.DelayUnit);
ProfileExHandler.Instance.SetTestDelay(it.IndexId, output);
@ -336,7 +336,7 @@ namespace ServiceLib.Services
return FormatOut(responseTime, Global.DelayUnit);
}
private int GetTcpingTime(string url, int port)
private async Task<int> GetTcpingTime(string url, int port)
{
int responseTime = -1;
@ -370,10 +370,6 @@ namespace ServiceLib.Services
private string FormatOut(object time, string unit)
{
//if (time.ToString().Equals("-1"))
//{
// return "Timeout";
//}
return $"{time}";
}

2
v2rayN/ServiceLib/Services/Statistics/StatisticsSingboxService.cs

@ -17,7 +17,7 @@ namespace ServiceLib.Services.Statistics
_updateFunc = updateFunc;
_exitFlag = false;
Task.Run(() => Run());
Task.Run(Run);
}
private async void Init()

2
v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs

@ -213,7 +213,7 @@ namespace ServiceLib.ViewModels
if (_config.guiItem.enableStatistics)
{
StatisticsHandler.Instance.Init(_config, UpdateStatisticsHandler);
await StatisticsHandler.Instance.Init(_config, UpdateStatisticsHandler);
}
await Reload();

2
v2rayN/v2rayN/Views/BackupAndRestoreView.xaml

@ -30,7 +30,7 @@
Content="{x:Static resx:ResUI.menuClose}"
DockPanel.Dock="Right"
IsCancel="True"
Style="{StaticResource MaterialDesignFlatButton}" />
Style="{StaticResource DefButton}" />
<TextBlock
x:Name="txtMsg"

4
v2rayN/v2rayN/Views/CheckUpdateView.xaml

@ -38,7 +38,7 @@
Margin="{StaticResource Margin8}"
Content="{x:Static resx:ResUI.menuCheckUpdate}"
IsDefault="True"
Style="{StaticResource MaterialDesignFlatButton}" />
Style="{StaticResource DefButton}" />
<Button
Width="100"
@ -47,7 +47,7 @@
Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}"
Content="{x:Static resx:ResUI.menuClose}"
IsCancel="True"
Style="{StaticResource MaterialDesignFlatButton}" />
Style="{StaticResource DefButton}" />
</StackPanel>
<StackPanel>

4
v2rayN/v2rayN/Views/QrcodeView.xaml

@ -41,9 +41,9 @@
Margin="{StaticResource Margin8}"
HorizontalAlignment="Right"
Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}"
Content="{x:Static resx:ResUI.TbConfirm}"
Content="{x:Static resx:ResUI.menuClose}"
IsCancel="True"
IsDefault="True"
Style="{StaticResource MaterialDesignFlatButton}" />
Style="{StaticResource DefButton}" />
</Grid>
</UserControl>
Loading…
Cancel
Save