-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroupWorker.cs
68 lines (59 loc) · 2.1 KB
/
GroupWorker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TAC_Grabber
{
public class GroupWorker
{
public int SkipIMEICount { get; set; }
public string GroupName { get; private set; }
private readonly BaseHTTPClient[] clients;
private readonly IMEI.IMEIGenerator IMEIGenerator;
private Dictionary<int, Task<string>> _cache = new Dictionary<int, Task<string>>();
public GroupWorker(BaseHTTPClient[] clients,IMEI.IMEIGenerator generator)
{
this.clients = clients;
this.IMEIGenerator = generator;
GroupName = clients.First().GetType().Name;
_cache = clients.ToDictionary(
x => x.GetHashCode()
, x => x.GetQueryAsync(
IMEIGenerator.GetValidIMEI(SkipIMEICount++)
.First()
));
}
public async Task<string[]> GetTask()
{
if(_cache.Count==0)
{
throw new Exception();
}
while(true)
{
await Task.Delay(1);
var completedTasks = _cache
.Where(x => x.Value.IsCompleted)
.ToArray();
var lst = new List<string>(completedTasks.Length);
foreach(var completedTask in completedTasks)
{
var result = await completedTask.Value;
lst.Add(result);
try
{
_cache[completedTask.Key] = clients.First(x => x.GetHashCode() == completedTask.Key).GetQueryAsync(IMEIGenerator.GetValidIMEI(SkipIMEICount++)
.First());
}
catch
{
_cache.Remove(completedTask.Key);
}
}
if (lst.Count > 0)
return lst.ToArray();
}
}
}
}