-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathAioIndy.pas
More file actions
409 lines (367 loc) · 11.8 KB
/
Copy pathAioIndy.pas
File metadata and controls
409 lines (367 loc) · 11.8 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// **************************************************************************************************
// Delphi Aio Library.
// Unit AioIndy
// https://github.com/Purik/AIO
// The contents of this file are subject to the Apache License 2.0 (the "License");
// you may not use this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
//
// The Original Code is AioIndy.pas.
//
// Contributor(s):
// Pavel Minenkov
// Purik
// https://github.com/Purik
//
// The Initial Developer of the Original Code is Pavel Minenkov [Purik].
// All Rights Reserved.
//
// **************************************************************************************************
unit AioIndy;
interface
uses IdIOHandler, IdServerIOHandler, IdComponent, IdIOHandlerSocket, IdGlobal,
Aio, SysUtils, IdCustomTransparentProxy, IdYarn, IdSocketHandle, IdThread,
IdSSLOpenSSL, IdSSLOpenSSLHeaders, IdCTypes;
type
{ TIdIOHandlerSocket reintroducing }
TAioIdIOHandlerSocket = class(TIdIOHandler)
const
CONN_TIMEOUT = 3000;
protected
FBinding: IAioProvider;
FBoundIP: string;
FBoundPort: TIdPort;
FDefaultPort: TIdPort;
FTransparentProxy: TIdCustomTransparentProxy;
FUseNagle: Boolean;
FConnected: Boolean;
FIPVersion: TIdIPVersion;
procedure ConnectClient; virtual;
function ReadDataFromSource(var VBuffer: TIdBytes): Integer; override;
function WriteDataToTarget(const ABuffer: TIdBytes; const AOffset, ALength: Integer): Integer; override;
function SourceIsAvailable: Boolean; override;
function CheckForError(ALastResult: Integer): Integer; override;
procedure RaiseError(AError: Integer); override;
function GetTransparentProxy: TIdCustomTransparentProxy; virtual;
procedure SetTransparentProxy(AProxy: TIdCustomTransparentProxy); virtual;
procedure InitComponent; override;
public
destructor Destroy; override;
function BindingAllocated: Boolean;
procedure Close; override;
function Connected: Boolean; override;
procedure Open; override;
function WriteFile(const AFile: String; AEnableTransferFile: Boolean = False): Int64; override;
//
property Binding: IAioProvider read FBinding;
//
procedure CheckForDisconnect(ARaiseExceptionIfDisconnected: Boolean = True;
AIgnoreBuffer: Boolean = False); override;
function Readable(AMSec: Integer = IdTimeoutDefault): Boolean; override;
published
property IPVersion: TIdIPVersion read FIPVersion write FIPVersion default ID_DEFAULT_IP_VERSION;
property BoundIP: string read FBoundIP write FBoundIP;
property BoundPort: TIdPort read FBoundPort write FBoundPort default IdBoundPortDefault;
property DefaultPort: TIdPort read FDefaultPort write FDefaultPort;
property TransparentProxy: TIdCustomTransparentProxy read GetTransparentProxy write SetTransparentProxy;
end;
TAioIdIOHandlerStack = class(TAioIdIOHandlerSocket)
protected
procedure ConnectClient; override;
function ReadDataFromSource(var VBuffer: TIdBytes): Integer; override;
function WriteDataToTarget(const ABuffer: TIdBytes; const AOffset, ALength: Integer): Integer; override;
public
procedure CheckForDisconnect(ARaiseExceptionIfDisconnected: Boolean = True;
AIgnoreBuffer: Boolean = False); override;
function Connected: Boolean; override;
function Readable(AMSec: Integer = IdTimeoutDefault): Boolean; override;
end;
implementation
uses IdExceptionCore, IdResourceStringsCore, Classes, IdStack, IdTCPConnection,
IdSocks, IdResourceStringsProtocols, IdStackConsts;
type
TAioIdSSLSocket = class(TIdSSLSocket)
end;
{ TAioIdIOHandlerSocket }
function TAioIdIOHandlerSocket.BindingAllocated: Boolean;
begin
Result := FBinding <> nil
end;
procedure TAioIdIOHandlerSocket.CheckForDisconnect(
ARaiseExceptionIfDisconnected, AIgnoreBuffer: Boolean);
begin
if ARaiseExceptionIfDisconnected and not FConnected then
RaiseConnClosedGracefully
end;
function TAioIdIOHandlerSocket.CheckForError(ALastResult: Integer): Integer;
begin
Result := GStack.CheckForSocketError(ALastResult, [Id_WSAESHUTDOWN, Id_WSAECONNABORTED, Id_WSAECONNRESET]);
end;
procedure TAioIdIOHandlerSocket.Close;
begin
if FBinding <> nil then begin
if Supports(FBinding, IAioTcpSocket) then
begin
(FBinding as IAioTcpSocket).Disconnect
end
end;
inherited Close;
end;
procedure TAioIdIOHandlerSocket.ConnectClient;
begin
IPVersion := Self.FIPVersion;
if Supports(FBinding, IAioTcpSocket) then
begin
FConnected := (FBinding as IAioTcpSocket).Connect(Host, Port, CONN_TIMEOUT)
end
else if Supports(FBinding, IAioUdpSocket) then
begin
(FBinding as IAioUdpSocket).Bind(Host, Port);
FConnected := True;
end;
end;
function TAioIdIOHandlerSocket.Connected: Boolean;
begin
Result := (BindingAllocated and FConnected and inherited Connected) or (not InputBufferIsEmpty);
end;
destructor TAioIdIOHandlerSocket.Destroy;
begin
if Assigned(FTransparentProxy) then begin
if FTransparentProxy.Owner = nil then begin
FreeAndNil(FTransparentProxy);
end;
end;
FBinding := nil;
inherited Destroy;
end;
function TAioIdIOHandlerSocket.GetTransparentProxy: TIdCustomTransparentProxy;
begin
// Necessary at design time for Borland SOAP support
if FTransparentProxy = nil then begin
FTransparentProxy := TIdSocksInfo.Create(nil); //default
end;
Result := FTransparentProxy;
end;
procedure TAioIdIOHandlerSocket.InitComponent;
begin
inherited InitComponent;
FIPVersion := ID_DEFAULT_IP_VERSION;
end;
procedure TAioIdIOHandlerSocket.Open;
begin
inherited Open;
if not Assigned(FBinding) then begin
FBinding := MakeAioTcpSocket
end else begin
FBinding := nil;
end;
FConnected := False;
//if the IOHandler is used to accept connections then port+host will be empty
if (Host <> '') and (Port > 0) then begin
ConnectClient
end;
end;
procedure TAioIdIOHandlerSocket.RaiseError(AError: Integer);
begin
GStack.RaiseSocketError(AError);
end;
function TAioIdIOHandlerSocket.Readable(AMSec: Integer): Boolean;
begin
Result := Connected
end;
function TAioIdIOHandlerSocket.ReadDataFromSource(
var VBuffer: TIdBytes): Integer;
begin
Result := 0;
if BindingAllocated and FBinding.ReadBytes(VBuffer) then
Result := Length(VBuffer);
end;
procedure TAioIdIOHandlerSocket.SetTransparentProxy(
AProxy: TIdCustomTransparentProxy);
var
LClass: TIdCustomTransparentProxyClass;
begin
// All this is to preserve the compatibility with old version
// In the case when we have SocksInfo as object created in runtime without owner form it is treated as temporary object
// In the case when the ASocks points to an object with owner it is treated as component on form.
if Assigned(AProxy) then begin
if not Assigned(AProxy.Owner) then begin
if Assigned(FTransparentProxy) then begin
if Assigned(FTransparentProxy.Owner) then begin
FTransparentProxy.RemoveFreeNotification(Self);
FTransparentProxy := nil;
end;
end;
LClass := TIdCustomTransparentProxyClass(AProxy.ClassType);
if Assigned(FTransparentProxy) and (FTransparentProxy.ClassType <> LClass) then begin
FreeAndNil(FTransparentProxy);
end;
if not Assigned(FTransparentProxy) then begin
FTransparentProxy := LClass.Create(nil);
end;
FTransparentProxy.Assign(AProxy);
end else begin
if Assigned(FTransparentProxy) then begin
if not Assigned(FTransparentProxy.Owner) then begin
FreeAndNil(FTransparentProxy);
end else begin
FTransparentProxy.RemoveFreeNotification(Self);
end;
end;
FTransparentProxy := AProxy;
FTransparentProxy.FreeNotification(Self);
end;
end
else if Assigned(FTransparentProxy) then begin
if not Assigned(FTransparentProxy.Owner) then begin
FreeAndNil(FTransparentProxy);
end else begin
FTransparentProxy.RemoveFreeNotification(Self);
FTransparentProxy := nil; //remove link
end;
end;
end;
function TAioIdIOHandlerSocket.SourceIsAvailable: Boolean;
begin
Result := BindingAllocated and FConnected
end;
function TAioIdIOHandlerSocket.WriteDataToTarget(const ABuffer: TIdBytes;
const AOffset, ALength: Integer): Integer;
begin
Result := 0;
if BindingAllocated then
begin
Result := FBinding.Write(@ABuffer[AOffset], ALength)
end;
end;
function TAioIdIOHandlerSocket.WriteFile(const AFile: String;
AEnableTransferFile: Boolean): Int64;
var
F: IAioFile;
FSize, RdSize: UInt64;
Buffer: Pointer;
begin
Result := 0;
if FileExists(AFile) and BindingAllocated then begin
F := MakeAioFile(AFile, fmOpenRead or fmShareDenyWrite);
FSize := f.Seek(0, soEnd);
Buffer := AllocMem(FSize);
try
RdSize := F.Read(Buffer, FSize);
// !!!
Assert(RdSize = FSize);
//
FBinding.Write(Buffer, FSize);
finally
FreeMem(Buffer);
end;
end
else
raise EIdFileNotFound.CreateFmt(RSFileNotFound, [AFile]);
end;
{ TAioIdIOHandlerStack }
procedure TAioIdIOHandlerStack.CheckForDisconnect(ARaiseExceptionIfDisconnected,
AIgnoreBuffer: Boolean);
var
LDisconnected: Boolean;
begin
// ClosedGracefully // Server disconnected
// IOHandler = nil // Client disconnected
if ClosedGracefully then begin
if BindingAllocated then begin
Close;
// Call event handlers to inform the user that we were disconnected
DoStatus(hsDisconnected);
//DoOnDisconnected;
end;
LDisconnected := True;
end else begin
LDisconnected := not BindingAllocated;
end;
// Do not raise unless all data has been read by the user
if LDisconnected then begin
if (InputBufferIsEmpty or AIgnoreBuffer) and ARaiseExceptionIfDisconnected then begin
RaiseConnClosedGracefully;
end;
end;
end;
procedure TAioIdIOHandlerStack.ConnectClient;
var
LHost: String;
LPort: Integer;
LIP: string;
LIPVersion : TIdIPVersion;
begin
inherited ConnectClient;
if Assigned(FTransparentProxy) then begin
if FTransparentProxy.Enabled then begin
LHost := FTransparentProxy.Host;
LPort := FTransparentProxy.Port;
LIPVersion := FTransparentProxy.IPVersion;
end else begin
LHost := Host;
LPort := Port;
LIPVersion := IPVersion;
end;
end else begin
LHost := Host;
LPort := Port;
LIPVersion := IPVersion;
end;
if LIPVersion = Id_IPv4 then
begin
if not GStack.IsIP(LHost) then begin
if Assigned(OnStatus) then begin
DoStatus(hsResolving, [LHost]);
end;
LIP := GStack.ResolveHost(LHost, LIPVersion);
end else begin
LIP := LHost;
end;
end
else
begin //IPv6
LIP := MakeCanonicalIPv6Address(LHost);
if LIP='' then begin //if MakeCanonicalIPv6Address failed, we have a hostname
if Assigned(OnStatus) then begin
DoStatus(hsResolving, [LHost]);
end;
LIP := GStack.ResolveHost(LHost, LIPVersion);
end else begin
LIP := LHost;
end;
end;
// TODO: Binding.SetPeer(LIP, LPort, LIPVersion);
// Connect
if Assigned(OnStatus) then begin
DoStatus(hsConnecting, [LIP]);
end;
if Assigned(FTransparentProxy) then begin
if FTransparentProxy.Enabled then begin
FTransparentProxy.Connect(Self, Host, Port, IPVersion);
end;
end;
end;
function TAioIdIOHandlerStack.Connected: Boolean;
begin
ReadFromSource(False, 0, False);
Result := inherited Connected;
end;
function TAioIdIOHandlerStack.Readable(AMSec: Integer): Boolean;
begin
Result := inherited Readable(AMSec)
end;
function TAioIdIOHandlerStack.ReadDataFromSource(
var VBuffer: TIdBytes): Integer;
begin
Assert(Binding<>nil);
Result := inherited ReadDataFromSource(VBuffer)
end;
function TAioIdIOHandlerStack.WriteDataToTarget(const ABuffer: TIdBytes;
const AOffset, ALength: Integer): Integer;
begin
Assert(Binding<>nil);
Result := inherited WriteDataToTarget(ABuffer, AOffset, ALength)
end;
end.