Skip to content

Commit 8cd825f

Browse files
committed
.
1 parent b08e2bd commit 8cd825f

File tree

5 files changed

+77
-46
lines changed

5 files changed

+77
-46
lines changed

fast cf ip scanner/Model/IPModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ public class IPModel
1919
public int Ping { get; set; }
2020

2121
public string Ports { get; set; }
22+
public int CountOfTimeout { get; set; }
2223
}
2324
}

fast cf ip scanner/Services/IPService.cs

Lines changed: 60 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public async Task<List<IPModel>> GetIpValid(string[] ips, IpOptionModel ipOption
3838
//case "UDP test":
3939
// validIps = await GetValidIPWithUDPTest(ips, ipOptions);
4040
// break;
41-
case "Terminal Ping":
41+
case "Terminal Ping test":
4242
validIps = await GetValidIpWithPingTest(ips, ipOptions);
4343
break;
4444

@@ -64,9 +64,9 @@ async Task HttpTest(string ipAddresse)
6464
Timeout = TimeSpan.FromSeconds(ipOptions.MaxPingOfIP),
6565
};
6666

67-
int totalPing = 0;
6867
var ports = new List<string>();
69-
var ipIsConnected = false;
68+
69+
var timeoutCount = 0;
7070
for (int i = 0; i < ipOptions.CountOfRepeatTestForEachIp; i++)
7171
{
7272
foreach (var port in ipOptions.Ports)
@@ -80,36 +80,36 @@ async Task HttpTest(string ipAddresse)
8080

8181
if (result != null)
8282
{
83-
ipIsConnected = true;
84-
ports.Add(port);
83+
if(!ports.Any(p => p == port))
84+
{
85+
ports.Add(port);
86+
}
8587
}
8688
}
8789
catch
8890
{
89-
ipIsConnected = false;
90-
stopwatch.Stop();
91+
timeoutCount++;
9192
}
92-
93-
94-
var currentPing = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
95-
totalPing += currentPing;
9693
}
9794
}
98-
int ping = totalPing / (ipOptions.CountOfRepeatTestForEachIp + ipOptions.Ports.Count);
99-
if (ipIsConnected)
95+
if (ports.Any())
10096
{
97+
var totalPing = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
98+
int ping = totalPing / (ipOptions.CountOfRepeatTestForEachIp * ipOptions.Ports.Count);
10199
lock (validIp)
102100
{
103101
validIp.Add(new IPModel
104102
{
105103
IP = ipAddresse.ToString(),
106104
Ping = ping,
107-
Ports = string.Join(",", ports)
105+
Ports = string.Join(",", ports),
106+
CountOfTimeout = timeoutCount
108107
});
109108
}
110109
}
111-
112110
}
111+
112+
113113
#region repeat test
114114
while (true)
115115
{
@@ -145,8 +145,8 @@ async Task TestConnectionAsync(string ip)
145145
{
146146
var ports = new List<string>();
147147
var stopwatch = new Stopwatch();
148-
var ipIsConnected = false;
149-
var ping = 0;
148+
var totolTimeOut = 0;
149+
var totalPing = 0;
150150
for (int i = 0; i < ipOptions.CountOfRepeatTestForEachIp; i++)
151151
{
152152
foreach (var port in ipOptions.Ports)
@@ -161,32 +161,40 @@ async Task TestConnectionAsync(string ip)
161161
// Wait for either connection or timeout
162162
await Task.WhenAny(connectTask, Task.Delay(ipOptions.MaxPingOfIP));
163163

164-
stopwatch.Stop();
165164
if (tcpClient.Connected)
166165
{
167-
168-
ipIsConnected = true;
169-
ports.Add(port);
170-
171-
// Calculate ping time
172-
ping += Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
173-
// Do something with pingTime if needed
166+
stopwatch.Stop();
167+
totalPing += Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
168+
if (!ports.Any(p => p == port))
169+
{
170+
ports.Add(port);
171+
}
172+
}
173+
else
174+
{
175+
totolTimeOut++;
174176
}
175177
}
176178
}
177-
catch { }
179+
catch {
180+
totolTimeOut++;
181+
}
178182
}
179183
}
180184

181-
if (ipIsConnected)
185+
if (ports.Any())
182186
{
183-
validIp.Add(new IPModel
187+
int ping = totalPing / (ipOptions.CountOfRepeatTestForEachIp * ipOptions.Ports.Count);
188+
lock (validIp)
184189
{
185-
IP = ip,
186-
// You can add the ping time here if needed
187-
Ping = ping / (ipOptions.CountOfRepeatTestForEachIp + ports.Count), // Placeholder for now
188-
Ports = string.Join(",", ports)
189-
});
190+
validIp.Add(new IPModel
191+
{
192+
IP = ip,
193+
Ping = ping,
194+
Ports = string.Join(",", ports),
195+
CountOfTimeout = totolTimeOut
196+
});
197+
}
190198
}
191199
}
192200

@@ -200,8 +208,14 @@ async Task TestConnectionAsync(string ip)
200208
tasks.Add(TestConnectionAsync(ip));
201209
}
202210

203-
await Task.WhenAll(tasks);
204-
//await Task.Delay(ipOptions.MaxPingOfIP * (ipOptions.CountOfRepeatTestForEachIp + ipOptions.Ports.Count));
211+
for (int i = 0; i < ipOptions.MaxPingOfIP / 100; i++)
212+
{
213+
await Task.Delay(100);
214+
if (validIp.Count >= ipOptions.MinimumCountOfValidIp)
215+
{
216+
return validIp.ToList();
217+
}
218+
}
205219
}
206220

207221
return validIp.ToList();
@@ -280,7 +294,8 @@ async Task TestConnectionAsync(string ipAddress)
280294
{
281295
try
282296
{
283-
PingReply reply = pingSender.Send(ipAddress, ipOptions.MaxPingOfIP);
297+
var reply = await pingSender.SendPingAsync(ipAddress, ipOptions.MaxPingOfIP);
298+
284299

285300
if (reply.Status == IPStatus.Success)
286301
{
@@ -307,7 +322,7 @@ async Task TestConnectionAsync(string ipAddress)
307322
{
308323
IP = ipAddress,
309324
Ping = avgPing,
310-
325+
CountOfTimeout = totalTimeOut
311326
});
312327
}
313328
}
@@ -322,10 +337,15 @@ async Task TestConnectionAsync(string ipAddress)
322337
{
323338
tasks.Add(TestConnectionAsync(ip));
324339
}
325-
326-
await Task.WhenAll(tasks);
340+
for (int i = 0; i < ipOptions.MaxPingOfIP / 100; i++)
341+
{
342+
await Task.Delay(100);
343+
if (validIp.Count >= ipOptions.MinimumCountOfValidIp)
344+
{
345+
return validIp.ToList();
346+
}
347+
}
327348
}
328-
329349
return validIp.ToList();
330350
}
331351

fast cf ip scanner/ViewModels/ScanPageViewModel.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using fast_cf_ip_scanner.Views;
1+

2+
using fast_cf_ip_scanner.Views;
3+
using System;
24
using System.ComponentModel;
35

46
namespace fast_cf_ip_scanner.ViewModels
@@ -32,8 +34,12 @@ public ScanPageViewModel(IPService iPServices,WorkerService workerService)
3234
[RelayCommand]
3335
async void GetValidIPs()
3436
{
35-
var protocol = await App.Current.MainPage.DisplayActionSheet("which protocol", "Cancel", null,new string[]{"Http test","TCP test", "Terminal Ping" });
36-
if(protocol == "Cancel" || protocol ==null)
37+
var protocols = new string[] { "Http test", "TCP test", "Terminal Ping test" };
38+
39+
var selectedProtocol =
40+
await App.Current.MainPage.DisplayActionSheet("which protocol", "Cancel", null, protocols);
41+
42+
if(selectedProtocol == "Cancel" || selectedProtocol == null)
3743
{
3844
return;
3945
}
@@ -53,7 +59,7 @@ async void GetValidIPs()
5359
{
5460
while (validIp.Count == 0)
5561
{
56-
validIp.AddRange(await _iPServices.GetIpValid(ips, _ipOption, protocol));
62+
validIp.AddRange(await _iPServices.GetIpValid(ips, _ipOption, selectedProtocol));
5763
}
5864

5965
validIp.Sort((x, y) => x.Ping.CompareTo(y.Ping));

fast cf ip scanner/Views/ScanPage.xaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<RowDefinition Height="Auto" />
6969
<RowDefinition Height="Auto" />
7070
<RowDefinition Height="Auto" />
71+
<RowDefinition Height="Auto" />
7172
</Grid.RowDefinitions>
7273
<Label Grid.Row="0" Grid.Column="0" Text="ip :" TextColor="White" />
7374
<Label Grid.Row="0" Grid.Column="1" Text="{Binding IP}" TextColor="White" />
@@ -77,6 +78,9 @@
7778

7879
<Label Grid.Row="2" Grid.Column="0" Text="ports :" TextColor="White" />
7980
<Label Grid.Row="2" Grid.Column="1" Text="{Binding Ports}" TextColor="White" />
81+
82+
<Label Grid.Row="3" Grid.Column="0" Text="TimeOut :" TextColor="White" />
83+
<Label Grid.Row="3" Grid.Column="1" Text="{Binding CountOfTimeout}" TextColor="White" />
8084
</Grid>
8185
</DataTemplate>
8286
</CollectionView.ItemTemplate>

fast cf ip scanner/fast cf ip scanner.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
<ApplicationId>com.DevoTalk.fast_cf_ip_scanner</ApplicationId>
1616
<ApplicationIdGuid>4fb46e13-cf0a-419b-99f7-22b80a864ae4</ApplicationIdGuid>
1717
<!-- Versions -->
18-
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
19-
<ApplicationVersion>1</ApplicationVersion>
18+
<ApplicationDisplayVersion>1.3</ApplicationDisplayVersion>
19+
<ApplicationVersion>1.3</ApplicationVersion>
2020
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">14.2</SupportedOSPlatformVersion>
2121
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">14.0</SupportedOSPlatformVersion>
2222
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>

0 commit comments

Comments
 (0)