Skip to content

Commit

Permalink
Code optimization, function asynchrony
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Oct 22, 2024
1 parent 3bf2dc7 commit b3c2084
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 31 deletions.
9 changes: 7 additions & 2 deletions v2rayN/ServiceLib/Handler/ProfileExHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ public class ProfileExHandler

public ProfileExHandler()
{
Init();
}

private async Task Init()
{
await InitData();
Task.Run(async () =>
{
await Init();
while (true)
{
await SaveQueueIndexIds();
Expand All @@ -25,7 +30,7 @@ public ProfileExHandler()
});
}

private async Task Init()
private async Task InitData()
{
await SQLiteHelper.Instance.ExecuteAsync($"delete from ProfileExItem where indexId not in ( select indexId from ProfileItem )");

Expand Down
4 changes: 2 additions & 2 deletions v2rayN/ServiceLib/Handler/StatisticsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class StatisticsHandler

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;
Expand All @@ -23,7 +23,7 @@ public void Init(Config config, Action<ServerSpeedItem> updateFunc)
return;
}

InitData();
await InitData();

_statisticsV2Ray = new StatisticsV2rayService(config, UpdateServerStatHandler);
_statisticsSingbox = new StatisticsSingboxService(config, UpdateServerStatHandler);
Expand Down
4 changes: 0 additions & 4 deletions v2rayN/ServiceLib/Handler/TaskHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ public class TaskHandler
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));
Expand Down
18 changes: 9 additions & 9 deletions v2rayN/ServiceLib/Services/DownloadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public async Task DownloadFileAsync(string url, string fileName, bool blProxy, i
UpdateCompleted?.Invoke(this, new RetResult(value > 100, $"...{value}%"));
};

var webProxy = GetWebProxy(blProxy);
var webProxy = await GetWebProxy(blProxy);
await DownloaderHelper.Instance.DownloadFileAsync(webProxy,
url,
fileName,
Expand All @@ -84,7 +84,7 @@ await DownloaderHelper.Instance.DownloadFileAsync(webProxy,
var webRequestHandler = new SocketsHttpHandler
{
AllowAutoRedirect = false,
Proxy = GetWebProxy(blProxy)
Proxy = await GetWebProxy(blProxy)
};
HttpClient client = new(webRequestHandler);

Expand Down Expand Up @@ -151,7 +151,7 @@ await DownloaderHelper.Instance.DownloadFileAsync(webProxy,
try
{
SetSecurityProtocol(AppHandler.Instance.Config.guiItem.enableSecurityProtocolTls13);
var webProxy = GetWebProxy(blProxy);
var webProxy = await GetWebProxy(blProxy);
var client = new HttpClient(new SocketsHttpHandler()
{
Proxy = webProxy,
Expand Down Expand Up @@ -197,7 +197,7 @@ await DownloaderHelper.Instance.DownloadFileAsync(webProxy,
{
SetSecurityProtocol(AppHandler.Instance.Config.guiItem.enableSecurityProtocolTls13);

var webProxy = GetWebProxy(blProxy);
var webProxy = await GetWebProxy(blProxy);

if (Utils.IsNullOrEmpty(userAgent))
{
Expand All @@ -222,7 +222,7 @@ public async Task<int> RunAvailabilityCheck(IWebProxy? webProxy)
{
try
{
webProxy ??= GetWebProxy(true);
webProxy ??= await GetWebProxy(true);

try
{
Expand Down Expand Up @@ -274,28 +274,28 @@ public async Task<int> GetRealPingTime(string url, IWebProxy? webProxy, int down
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;
}

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)
Expand Down
10 changes: 3 additions & 7 deletions v2rayN/ServiceLib/Services/SpeedtestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ private async Task RunTcping()
{
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);
Expand Down Expand Up @@ -336,7 +336,7 @@ private async Task<string> GetRealPingTime(DownloadService downloadHandle, IWebP
return FormatOut(responseTime, Global.DelayUnit);
}

private int GetTcpingTime(string url, int port)
private async Task<int> GetTcpingTime(string url, int port)
{
int responseTime = -1;

Expand Down Expand Up @@ -370,10 +370,6 @@ private int GetTcpingTime(string url, int port)

private string FormatOut(object time, string unit)
{
//if (time.ToString().Equals("-1"))
//{
// return "Timeout";
//}
return $"{time}";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public StatisticsSingboxService(Config config, Action<ServerSpeedItem> updateFun
_updateFunc = updateFunc;
_exitFlag = false;

Task.Run(() => Run());
Task.Run(Run);
}

private async void Init()
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private async Task Init()

if (_config.guiItem.enableStatistics)
{
StatisticsHandler.Instance.Init(_config, UpdateStatisticsHandler);
await StatisticsHandler.Instance.Init(_config, UpdateStatisticsHandler);
}

await Reload();
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/Views/BackupAndRestoreView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions v2rayN/v2rayN/Views/CheckUpdateView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
Margin="{StaticResource Margin8}"
Content="{x:Static resx:ResUI.menuCheckUpdate}"
IsDefault="True"
Style="{StaticResource MaterialDesignFlatButton}" />
Style="{StaticResource DefButton}" />

<Button
Width="100"
Expand All @@ -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>
Expand Down
4 changes: 2 additions & 2 deletions v2rayN/v2rayN/Views/QrcodeView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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>

0 comments on commit b3c2084

Please sign in to comment.