Code optimization, function asynchrony

This commit is contained in:
2dust 2024-10-22 17:50:42 +08:00
parent 3bf2dc711d
commit b3c2084b76
10 changed files with 28 additions and 31 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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