-
Notifications
You must be signed in to change notification settings - Fork 3
/
SocketManager.pas
166 lines (144 loc) · 3.31 KB
/
SocketManager.pas
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
unit SocketManager;
interface
uses
System.SysUtils,
Winapi.Windows,
WinSock,
SyncObjs;
type
TSocketManagerItem = record
sock: TSocket;
isTcp: boolean;
isUdp: boolean;
hasSent: boolean;
fakeHttpProxyFlag: boolean;
createdAt: integer;
end;
TSocketManager = class
private
items: array of TSocketManagerItem;
criticalSection: TCriticalSection;
function FindIndexBySock(sock: TSocket): integer;
procedure DeleteByIndex(index: integer);
procedure CollectGarbage;
public
procedure Add(sock: TSocket; sockType, sockProtocol: integer);
function IsFirstSend(sock: TSocket; var item: TSocketManagerItem): boolean;
procedure SetFakeHttpProxyFlag(sock: TSocket);
function ResetFakeHttpProxyFlag(sock: TSocket): boolean;
constructor Create;
destructor Destroy; override;
end;
implementation
constructor TSocketManager.Create;
begin
criticalSection := TCriticalSection.Create;
end;
destructor TSocketManager.Destroy;
begin
criticalSection.Free;
end;
function TSocketManager.FindIndexBySock(sock: TSocket): integer;
var
i: integer;
begin
for i := 0 to High(items) do
begin
if items[i].sock = sock then
exit(i);
end;
result := -1;
end;
procedure TSocketManager.DeleteByIndex(index: integer);
var
lastIndex: integer;
begin
lastIndex := High(items);
if index < lastIndex then
items[index] := items[lastIndex];
SetLength(items, lastIndex);
end;
procedure TSocketManager.CollectGarbage;
var
i: integer;
tick: integer;
begin
tick := GetTickCount64 - 30000;
for i := High(items) downto 0 do
begin
if items[i].createdAt < tick then
begin
self.DeleteByIndex(i);
end;
end;
end;
procedure TSocketManager.Add(sock: TSocket; sockType, sockProtocol: integer);
var
item: TSocketManagerItem;
i: integer;
begin
item.sock := sock;
item.isTcp := (sockType = SOCK_STREAM) and ((sockProtocol = IPPROTO_TCP) or (sockProtocol = 0));
item.isUdp := (sockType = SOCK_DGRAM) and ((sockProtocol = IPPROTO_UDP) or (sockProtocol = 0));
item.hasSent := false;
item.fakeHttpProxyFlag := false;
item.createdAt := GetTickCount64;
criticalSection.Enter;
try
CollectGarbage;
i := FindIndexBySock(sock);
if i = -1 then
begin
i := Length(items);
SetLength(items, i + 1);
end;
items[i] := item;
finally
criticalSection.Leave;
end;
end;
function TSocketManager.IsFirstSend(sock: TSocket; var item: TSocketManagerItem): boolean;
var
i: integer;
begin
criticalSection.Enter;
try
i := FindIndexBySock(sock);
if (i = -1) or items[i].hasSent then
exit(false);
items[i].hasSent := true;
item := items[i];
result := true;
finally
criticalSection.Leave;
end;
end;
procedure TSocketManager.SetFakeHttpProxyFlag(sock: TSocket);
var
i: integer;
begin
criticalSection.Enter;
try
i := FindIndexBySock(sock);
if i >= 0 then
items[i].fakeHttpProxyFlag := true;
finally
criticalSection.Leave;
end;
end;
function TSocketManager.ResetFakeHttpProxyFlag(sock: TSocket): boolean;
var
i: integer;
begin
criticalSection.Enter;
try
i := FindIndexBySock(sock);
if (i = -1) or (not items[i].fakeHttpProxyFlag) then
exit(false);
items[i].fakeHttpProxyFlag := false;
result := true;
finally
criticalSection.Leave;
end;
end;
end.