Skip to content

Commit b3c2084

Browse files
committedOct 22, 2024
Code optimization, function asynchrony
1 parent 3bf2dc7 commit b3c2084

10 files changed

+28
-31
lines changed
 

‎v2rayN/ServiceLib/Handler/ProfileExHandler.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ public class ProfileExHandler
1414

1515
public ProfileExHandler()
1616
{
17+
Init();
18+
}
19+
20+
private async Task Init()
21+
{
22+
await InitData();
1723
Task.Run(async () =>
1824
{
19-
await Init();
2025
while (true)
2126
{
2227
await SaveQueueIndexIds();
@@ -25,7 +30,7 @@ public ProfileExHandler()
2530
});
2631
}
2732

28-
private async Task Init()
33+
private async Task InitData()
2934
{
3035
await SQLiteHelper.Instance.ExecuteAsync($"delete from ProfileExItem where indexId not in ( select indexId from ProfileItem )");
3136

‎v2rayN/ServiceLib/Handler/StatisticsHandler.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class StatisticsHandler
1414

1515
public List<ServerStatItem> ServerStat => _lstServerStat;
1616

17-
public void Init(Config config, Action<ServerSpeedItem> updateFunc)
17+
public async Task Init(Config config, Action<ServerSpeedItem> updateFunc)
1818
{
1919
_config = config;
2020
_updateFunc = updateFunc;
@@ -23,7 +23,7 @@ public void Init(Config config, Action<ServerSpeedItem> updateFunc)
2323
return;
2424
}
2525

26-
InitData();
26+
await InitData();
2727

2828
_statisticsV2Ray = new StatisticsV2rayService(config, UpdateServerStatHandler);
2929
_statisticsSingbox = new StatisticsSingboxService(config, UpdateServerStatHandler);

‎v2rayN/ServiceLib/Handler/TaskHandler.cs

-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ public class TaskHandler
55
private static readonly Lazy<TaskHandler> _instance = new(() => new());
66
public static TaskHandler Instance => _instance.Value;
77

8-
public TaskHandler()
9-
{
10-
}
11-
128
public void RegUpdateTask(Config config, Action<bool, string> updateFunc)
139
{
1410
Task.Run(() => UpdateTaskRunSubscription(config, updateFunc));

‎v2rayN/ServiceLib/Services/DownloadService.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public async Task DownloadFileAsync(string url, string fileName, bool blProxy, i
5959
UpdateCompleted?.Invoke(this, new RetResult(value > 100, $"...{value}%"));
6060
};
6161

62-
var webProxy = GetWebProxy(blProxy);
62+
var webProxy = await GetWebProxy(blProxy);
6363
await DownloaderHelper.Instance.DownloadFileAsync(webProxy,
6464
url,
6565
fileName,
@@ -84,7 +84,7 @@ await DownloaderHelper.Instance.DownloadFileAsync(webProxy,
8484
var webRequestHandler = new SocketsHttpHandler
8585
{
8686
AllowAutoRedirect = false,
87-
Proxy = GetWebProxy(blProxy)
87+
Proxy = await GetWebProxy(blProxy)
8888
};
8989
HttpClient client = new(webRequestHandler);
9090

@@ -151,7 +151,7 @@ await DownloaderHelper.Instance.DownloadFileAsync(webProxy,
151151
try
152152
{
153153
SetSecurityProtocol(AppHandler.Instance.Config.guiItem.enableSecurityProtocolTls13);
154-
var webProxy = GetWebProxy(blProxy);
154+
var webProxy = await GetWebProxy(blProxy);
155155
var client = new HttpClient(new SocketsHttpHandler()
156156
{
157157
Proxy = webProxy,
@@ -197,7 +197,7 @@ await DownloaderHelper.Instance.DownloadFileAsync(webProxy,
197197
{
198198
SetSecurityProtocol(AppHandler.Instance.Config.guiItem.enableSecurityProtocolTls13);
199199

200-
var webProxy = GetWebProxy(blProxy);
200+
var webProxy = await GetWebProxy(blProxy);
201201

202202
if (Utils.IsNullOrEmpty(userAgent))
203203
{
@@ -222,7 +222,7 @@ public async Task<int> RunAvailabilityCheck(IWebProxy? webProxy)
222222
{
223223
try
224224
{
225-
webProxy ??= GetWebProxy(true);
225+
webProxy ??= await GetWebProxy(true);
226226

227227
try
228228
{
@@ -274,28 +274,28 @@ public async Task<int> GetRealPingTime(string url, IWebProxy? webProxy, int down
274274
return responseTime;
275275
}
276276

277-
private WebProxy? GetWebProxy(bool blProxy)
277+
private async Task<WebProxy?> GetWebProxy(bool blProxy)
278278
{
279279
if (!blProxy)
280280
{
281281
return null;
282282
}
283283
var httpPort = AppHandler.Instance.GetLocalPort(EInboundProtocol.http);
284-
if (!SocketCheck(Global.Loopback, httpPort))
284+
if (await SocketCheck(Global.Loopback, httpPort) == false)
285285
{
286286
return null;
287287
}
288288

289289
return new WebProxy(Global.Loopback, httpPort);
290290
}
291291

292-
private bool SocketCheck(string ip, int port)
292+
private async Task<bool> SocketCheck(string ip, int port)
293293
{
294294
try
295295
{
296296
IPEndPoint point = new(IPAddress.Parse(ip), port);
297297
using Socket? sock = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
298-
sock.Connect(point);
298+
await sock.ConnectAsync(point);
299299
return true;
300300
}
301301
catch (Exception)

‎v2rayN/ServiceLib/Services/SpeedtestService.cs

+3-7
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ private async Task RunTcping()
9999
{
100100
continue;
101101
}
102-
tasks.Add(Task.Run(() =>
102+
tasks.Add(Task.Run(async () =>
103103
{
104104
try
105105
{
106-
int time = GetTcpingTime(it.Address, it.Port);
106+
int time = await GetTcpingTime(it.Address, it.Port);
107107
var output = FormatOut(time, Global.DelayUnit);
108108

109109
ProfileExHandler.Instance.SetTestDelay(it.IndexId, output);
@@ -336,7 +336,7 @@ private async Task<string> GetRealPingTime(DownloadService downloadHandle, IWebP
336336
return FormatOut(responseTime, Global.DelayUnit);
337337
}
338338

339-
private int GetTcpingTime(string url, int port)
339+
private async Task<int> GetTcpingTime(string url, int port)
340340
{
341341
int responseTime = -1;
342342

@@ -370,10 +370,6 @@ private int GetTcpingTime(string url, int port)
370370

371371
private string FormatOut(object time, string unit)
372372
{
373-
//if (time.ToString().Equals("-1"))
374-
//{
375-
// return "Timeout";
376-
//}
377373
return $"{time}";
378374
}
379375

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public StatisticsSingboxService(Config config, Action<ServerSpeedItem> updateFun
1717
_updateFunc = updateFunc;
1818
_exitFlag = false;
1919

20-
Task.Run(() => Run());
20+
Task.Run(Run);
2121
}
2222

2323
private async void Init()

‎v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ private async Task Init()
213213

214214
if (_config.guiItem.enableStatistics)
215215
{
216-
StatisticsHandler.Instance.Init(_config, UpdateStatisticsHandler);
216+
await StatisticsHandler.Instance.Init(_config, UpdateStatisticsHandler);
217217
}
218218

219219
await Reload();

‎v2rayN/v2rayN/Views/BackupAndRestoreView.xaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
Content="{x:Static resx:ResUI.menuClose}"
3131
DockPanel.Dock="Right"
3232
IsCancel="True"
33-
Style="{StaticResource MaterialDesignFlatButton}" />
33+
Style="{StaticResource DefButton}" />
3434

3535
<TextBlock
3636
x:Name="txtMsg"

‎v2rayN/v2rayN/Views/CheckUpdateView.xaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
Margin="{StaticResource Margin8}"
3939
Content="{x:Static resx:ResUI.menuCheckUpdate}"
4040
IsDefault="True"
41-
Style="{StaticResource MaterialDesignFlatButton}" />
41+
Style="{StaticResource DefButton}" />
4242

4343
<Button
4444
Width="100"
@@ -47,7 +47,7 @@
4747
Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}"
4848
Content="{x:Static resx:ResUI.menuClose}"
4949
IsCancel="True"
50-
Style="{StaticResource MaterialDesignFlatButton}" />
50+
Style="{StaticResource DefButton}" />
5151
</StackPanel>
5252

5353
<StackPanel>

‎v2rayN/v2rayN/Views/QrcodeView.xaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
Margin="{StaticResource Margin8}"
4242
HorizontalAlignment="Right"
4343
Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}"
44-
Content="{x:Static resx:ResUI.TbConfirm}"
44+
Content="{x:Static resx:ResUI.menuClose}"
4545
IsCancel="True"
4646
IsDefault="True"
47-
Style="{StaticResource MaterialDesignFlatButton}" />
47+
Style="{StaticResource DefButton}" />
4848
</Grid>
4949
</UserControl>

0 commit comments

Comments
 (0)
Please sign in to comment.