|
1 | 1 |
|
2 | 2 |
|
3 | 3 | using fast_cf_ip_scanner.Data;
|
| 4 | +using Microsoft.Maui.Animations; |
| 5 | +using System.Collections.Concurrent; |
4 | 6 | using System.Diagnostics;
|
5 | 7 | using System.Net;
|
6 | 8 | using System.Net.NetworkInformation;
|
@@ -134,69 +136,75 @@ async Task HttpTest(string ipAddresse)
|
134 | 136 |
|
135 | 137 | public async Task<List<IPModel>> GetValidIPWithTCPTest(string[] ips, IpOptionModel ipOptions)
|
136 | 138 | {
|
137 |
| - |
138 |
| - var validIp = new List<IPModel>(); |
| 139 | + var validIp = new ConcurrentBag<IPModel>(); |
139 | 140 |
|
140 | 141 | async Task TestConnectionAsync(string ip)
|
141 | 142 | {
|
142 |
| - |
143 | 143 | var ports = new List<string>();
|
| 144 | + var stopwatch = new Stopwatch(); |
144 | 145 | var ipIsConnected = false;
|
| 146 | + var ping = 0; |
145 | 147 | for (int i = 0; i < ipOptions.CountOfRepeatTestForEachIp; i++)
|
146 | 148 | {
|
147 |
| - |
148 | 149 | foreach (var port in ipOptions.Ports)
|
149 | 150 | {
|
150 | 151 | try
|
151 | 152 | {
|
152 |
| - TcpClient tcpClient = new TcpClient(); |
153 |
| - tcpClient.ConnectAsync(ip, int.Parse(port)); |
154 |
| - await Task.Delay(ipOptions.MaxPingOfIP); |
155 |
| - if (tcpClient.Connected) |
| 153 | + using (var tcpClient = new TcpClient()) |
156 | 154 | {
|
157 |
| - ipIsConnected = true; |
158 |
| - ports.Add(port); |
| 155 | + stopwatch.Restart(); |
| 156 | + var connectTask = tcpClient.ConnectAsync(ip, int.Parse(port)); |
| 157 | + |
| 158 | + // Wait for either connection or timeout |
| 159 | + await Task.WhenAny(connectTask, Task.Delay(ipOptions.MaxPingOfIP)); |
| 160 | + |
| 161 | + stopwatch.Stop(); |
| 162 | + if (tcpClient.Connected) |
| 163 | + { |
| 164 | + |
| 165 | + ipIsConnected = true; |
| 166 | + ports.Add(port); |
| 167 | + |
| 168 | + // Calculate ping time |
| 169 | + ping += Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds); |
| 170 | + // Do something with pingTime if needed |
| 171 | + } |
159 | 172 | }
|
160 | 173 | }
|
161 |
| - catch |
162 |
| - { |
163 |
| - |
164 |
| - } |
| 174 | + catch { } |
165 | 175 | }
|
166 | 176 | }
|
167 | 177 |
|
168 | 178 | if (ipIsConnected)
|
169 | 179 | {
|
170 |
| - lock (validIp) |
| 180 | + validIp.Add(new IPModel |
171 | 181 | {
|
172 |
| - validIp.Add(new IPModel |
173 |
| - { |
174 |
| - IP = ip, |
175 |
| - Ping = 0, |
176 |
| - Ports = string.Join(",", ports) |
177 |
| - }); |
178 |
| - } |
| 182 | + IP = ip, |
| 183 | + // You can add the ping time here if needed |
| 184 | + Ping = ping / (ipOptions.CountOfRepeatTestForEachIp + ports.Count), // Placeholder for now |
| 185 | + Ports = string.Join(",", ports) |
| 186 | + }); |
179 | 187 | }
|
180 | 188 | }
|
181 |
| - while (true) |
| 189 | + |
| 190 | + while (validIp.Count < ipOptions.MinimumCountOfValidIp) |
182 | 191 | {
|
183 |
| - if (validIp.Count >= ipOptions.MinimumCountOfValidIp) |
184 |
| - { |
185 |
| - break; |
186 |
| - } |
187 | 192 | var newRandomIps = GetRandomIp(ips, ipOptions.CountOfIpForTest, ipOptions.CountOfIpRanges);
|
188 |
| - |
189 |
| - List<Task> tasks = new List<Task>(); |
| 193 | + var tasks = new List<Task>(); |
190 | 194 |
|
191 | 195 | foreach (var ip in newRandomIps)
|
192 | 196 | {
|
193 | 197 | tasks.Add(TestConnectionAsync(ip));
|
194 | 198 | }
|
195 |
| - await Task.Delay(ipOptions.MaxPingOfIP * (ipOptions.CountOfRepeatTestForEachIp + ipOptions.Ports.Count)); |
| 199 | + |
| 200 | + await Task.WhenAll(tasks); |
| 201 | + //await Task.Delay(ipOptions.MaxPingOfIP * (ipOptions.CountOfRepeatTestForEachIp + ipOptions.Ports.Count)); |
196 | 202 | }
|
197 |
| - return validIp; |
| 203 | + |
| 204 | + return validIp.ToList(); |
198 | 205 | }
|
199 | 206 |
|
| 207 | + |
200 | 208 | public async Task<List<IPModel>> GetValidIPWithUDPTest(string[] ips, int maxPing)
|
201 | 209 | {
|
202 | 210 |
|
|
0 commit comments