-
Notifications
You must be signed in to change notification settings - Fork 1
/
iramtpCli.pas
2443 lines (1956 loc) · 68.5 KB
/
iramtpCli.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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit iramtpCli;
{$B-} { Enable partial boolean evaluation }
{$T-} { Untyped pointers }
{$X+} { Enable extended syntax }
interface
uses
Winapi.Windows,
Winapi.Messages,
OverbyteIcsWinSock,
System.SysUtils,
System.Classes,
Vcl.Forms,
OverbyteIcsWndControl,
OverbyteIcsWSocket,
Strutils,
OverbyteIcsUtils,
OverByteIcsFtpSrvT;
const
{$IFDEF FTPCLI_BUFFER_OLD}
FTP_SND_BUF_SIZE = 1460;
FTP_RCV_BUF_SIZE = 4096;
{$ELSE}
{$IFDEF FTPCLI_BUFFER_SMALL}
FTP_SND_BUF_SIZE = 8192;
FTP_RCV_BUF_SIZE = 8192;
{$ELSE}
{$IFDEF FTPCLI_BUFFER_MEDIUM}
FTP_SND_BUF_SIZE = 16384;
FTP_RCV_BUF_SIZE = 16384;
{$ELSE}
FTP_SND_BUF_SIZE = 32768;
FTP_RCV_BUF_SIZE = 32768;
{$ENDIF}
{$ENDIF}
{$ENDIF}
type
TFtpOption = (ftpAcceptLF, ftpWaitUsingSleep);
TFtpOptions = set of TFtpOption;
TFtpState = (ftpNotConnected, ftpReady, ftpInternalReady, ftpDnsLookup, ftpConnected, ftpAbort,
ftpInternalAbort, ftpWaitingResponse );
TFtpRequest = (ftpNone, ftpOpenAsync, ftpConnectAsync, ftpReceiveAsync, ftpPortAsync, ftpGetAsync,
ftpQuitAsync, ftpPutAsync, ftpRqAbort, ftpTransmitAsync );
TFtpFct = (ftpFctNone, ftpFctOpen, ftpFctGet, ftpFctQuit, ftpFctPut, ftpFctPort);
TFtpFctSet = set of TFtpFct;
TFtpDisplay = procedure(Sender : TObject; var Msg : String) of object;
TFtpProgress64 = procedure(Sender : TObject; Count : Int64; var Abort : Boolean) of object;
TFtpCommand = procedure(Sender : TObject; var Cmd : String) of object;
TFtpRequestDone = procedure(Sender : TObject; RqType : TFtpRequest; ErrCode : Word) of object;
TFtpReadyToTransmit = procedure(Sender : TObject; var bCancel : Boolean) of object;
TFtpNextProc = procedure of object;
FtpException = class(Exception);
TCustomMtpCli = class(TIcsWndControl)
protected
FHostName : String;
FPort : String;
FSocketFamily : TSocketFamily;
FDataPortRangeStart : LongWord;
FDataPortRangeEnd : LongWord;
FLastDataPort : LongWord;
FExternalIPv4 : String;
FDSocketSndBufSize : Integer;
FDSocketRcvBufSize : Integer;
FLocalAddr : String;
FLocalAddr6 : String;
FDnsResult : String;
FType : Char;
FProxyServer : String;
FProxyPort : String;
FAppendFlag : Boolean;
FDisplayFileFlag : Boolean;
FControlSocket : TWSocket;
FDataSocket : TWSocket;
FStartTime : LongInt;
FStopTime : LongInt;
FStreamSize : LongInt;
FMemoryPtr : Pointer ;
FMemoryName : String;
FState : TFtpState;
FStatusCode : LongInt;
FRequestResult : Integer;
FFctSet : TFtpFctSet;
FFctPrv : TFtpFct;
FHighLevelResult : Integer;
FHighLevelFlag : Boolean;
FRestartFlag : Boolean;
FMsg_WM_FTP_REQUEST_DONE : UINT;
FMsg_WM_FTP_SENDDATA : UINT;
FMsg_WM_FTP_CLOSEDOWN : UINT;
FOptions : TFtpOptions;
FOnDisplay : TFtpDisplay;
FOnDisplayFile : TFtpDisplay;
FOnError : TFtpDisplay;
FOnCommand : TFtpCommand;
FOnResponse : TNotifyEvent;
FOnSessionConnected : TSessionConnected;
FOnSessionClosed : TSessionClosed;
FOnStateChange : TNotifyEvent;
FOnRequestDone : TFtpRequestDone;
FOnReadyToTransmit : TFtpReadyToTransmit;
FLocalStream : TMemoryStream;
FRequestType : TFtpRequest;
FRequestDoneFlag : Boolean;
FReceiveBuffer : array [0..FTP_RCV_BUF_SIZE - 1] of AnsiChar;
FReceiveLen : Integer;
FLastResponse : String;
FLastResponseSave : String;
FStatusCodeSave : LongInt;
FErrorMessage : String;
FError : Word;
FGetCommand : String;
FConnected : Boolean;
FSendBuffer : array [0..FTP_SND_BUF_SIZE - 1] of AnsiChar;
FOnProgress64 : TFtpProgress64;
FByteCount : TFtpBigInt;
FSizeResult : TFtpBigInt;
FNext : TFtpNextProc;
FWhenConnected : TFtpNextProc;
FDoneAsync : TFtpNextProc;
FOkResponses : array [0..15] of Integer;
FNextRequest : TFtpNextProc;
FServerSaidDone : Boolean;
FFileReceived : Boolean;
FFileSent : Boolean;
FEofFlag : Boolean;
FStorAnswerRcvd : Boolean;
FPutSessionOpened : Boolean;
FDataSocketSentFlag : Boolean;
FLastMultiResponse : String;
FCloseEndTick : LongWord;
FCloseEndSecs : LongWord;
FKeepAliveSecs : integer;
FClientIdStr : String;
FPosStart : TFtpBigInt;
FPosEnd : TFtpBigInt;
FDurationMsecs : Integer;
FSocksPassword : String;
FSocksPort : String;
FSocksServer : String;
FSocksUserCode : String;
procedure SetKeepAliveSecs (secs: integer);
procedure AbortComponent; override;
procedure SetMultiThreaded(const Value : Boolean); override;
procedure SetOnBgException(const Value: TIcsBgExceptionEvent); override;
procedure SetTerminated(const Value: Boolean); override;
procedure SetOnMessagePump(const Value: TNotifyEvent); override;
procedure SetErrorMessage;
procedure LocalStreamWrite(const Buffer; Count : Integer); virtual;
procedure LocalStreamWriteString(Str: PAnsiChar; Count: Integer); overload;
procedure LocalStreamWriteString(Str: PWideChar; Count: Integer; ACodePage: LongWord); overload;
procedure LocalStreamWriteString(Str: PWideChar; Count: Integer); overload;
procedure DataSocketGetDataAvailable(Sender: TObject; ErrCode : word); //retr
procedure DataSocketGetSessionConnected(Sender: TObject; ErrCode : word); //retr
procedure DataSocketGetSessionAvailable(Sender: TObject; ErrCode : word); //retr
procedure DataSocketGetSessionClosed(Sender: TObject; ErrCode : word); //retr
procedure DataSocketPutSessionConnected(Sender: TObject; ErrCode : word); // stor
procedure DataSocketPutDataAvailable(Sender: TObject; ErrCode : word); // stor
procedure DataSocketPutDataSent(Sender: TObject; ErrCode : word); // stor
procedure DataSocketPutSessionAvailable(Sender: TObject; ErrCode : word); // stor
procedure DataSocketPutSessionClosed(Sender: TObject; ErrCode : word); // stor
procedure SendCommand(Cmd : String); virtual;
procedure TriggerDisplay(Msg : String); virtual;
procedure TriggerReadyToTransmit(var bCancel : Boolean); virtual;
procedure TriggerDisplayFile(Msg : String); virtual;
procedure TriggerError(Msg: String); virtual;
procedure TriggerResponse; virtual;
procedure DisplayLastResponse;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
function Progress : Boolean; virtual;
procedure ControlSocketDnsLookupDone(Sender: TObject; ErrCode: Word);
procedure ControlSocketSessionConnected(Sender: TObject; ErrCode: Word); virtual;
procedure ControlSocketDataAvailable(Sender: TObject; ErrCode: Word);
procedure ControlSocketSessionClosed(Sender: TObject; ErrCode: Word);
procedure DataSocketPutAppendInit(const TargetPort, TargetIP : String); virtual;
procedure DataSocketGetInit(const TargetPort, TargetIP : String); virtual;
procedure TriggerRequestDone(ErrCode: Word);
procedure TriggerStateChange;
procedure StateChange(NewState : TFtpState);
procedure PortAsync; virtual;
procedure DoneQuitAsync;
procedure ExecAsync(RqType: TFtpRequest; Cmd: String; OkResponses : array of Word; DoneAsync : TFtpNextProc);
procedure NextExecAsync;
procedure DoGetAsync(RqType : TFtpRequest);
procedure Next1GetAsync;
procedure Next2GetAsync;
procedure Next3GetAsync;
procedure Next1PutAsync;
procedure Next2PutAsync;
procedure Next3PutAsync;
procedure DoPutAppendAsync;
procedure DoHighLevelAsync;
procedure HighLevelAsync(RqType : TFtpRequest; Fcts : TFtpFctSet);
procedure HandleError(const Msg : String);
function CheckReady : Boolean;
procedure TransfertStats; virtual;
procedure SetBinary(Value: Boolean);
function GetBinary: Boolean;
function GetConnected: Boolean;
procedure AllocateMsgHandlers; override;
procedure FreeMsgHandlers; override;
function MsgHandlersCount: Integer; override;
procedure WndProc(var MsgRec: TMessage); override;
procedure WMFtpRequestDone(var msg: TMessage); virtual;
procedure WMFtpSendData(var msg: TMessage); virtual;
procedure WMFtpCloseDown(var msg: TMessage); virtual;
procedure DestroyLocalStream;
procedure SetLocalStream (Stream:TmemoryStream);
procedure SetDataPortRangeStart (NewValue: LongWord);
procedure SetDataPortRangeEnd (NewValue: LongWord);
function OpenMemoryStream (Buffersize: integer): TmemoryStream;
{$IFDEF USE_INILE} inline; {$ENDIF}
procedure CreateLocalFileStream;
function CreateSocket: TWSocket; virtual;
property SocketFamily: TSocketFamily read FSocketFamily write FSocketFamily;
procedure HandleHttpTunnelError(Sender: TObject; ErrCode: Word;
TunnelServerAuthTypes: THttpTunnelServerAuthTypes; const Msg: String);
procedure HandleSocksError(Sender: TObject; ErrCode: Integer; Msg: String);
procedure SetDSocketSndBufSize(const Value: Integer);
procedure SetDSocketRcvBufSize(const Value: Integer);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure OpenAsync; virtual;
procedure ConnectAsync; virtual;
procedure QuitAsync; virtual;
procedure AbortAsync; virtual;
procedure GetAsync; virtual;
procedure ExecGetAsync; virtual;
procedure ReceiveAsync; virtual;
procedure PutAsync; virtual;
procedure ExecPutAsync; virtual;
procedure TransmitAsync; virtual;
property LastResponse : String read FLastResponse;
property LastMultiResponse : String read FLastMultiResponse;
property ErrorMessage : String read FErrorMessage;
property DnsResult : String read FDnsResult;
property ControlSocket : TWSocket read FControlSocket;
property DataSocket : TWSocket read FDataSocket;
property Connected : Boolean read GetConnected;
property StatusCode : LongInt read FStatusCode;
property State : TFtpState read FState;
property RequestType : TFtpRequest read FRequestType;
property KeepAliveSecs : Integer read FKeepAliveSecs write SetKeepAliveSecs;
property LocalStream : TMemoryStream read FLocalStream write SetLocalStream;
property OnProgress64 : TFtpProgress64 read FOnProgress64 write FOnProgress64;
property ByteCount : TFtpBigInt read FByteCount;
property SizeResult : TFtpBigInt read FSizeResult;
property ClientIdStr : String read FClientIdStr write FClientIdStr;
property PosStart : TFtpBigInt read FPosStart write FPosStart;
property PosEnd : TFtpBigInt read FPosEnd write FPosEnd;
property DurationMsecs : Integer read FDurationMsecs;
property StartTick : Integer read FStartTime;
property StreamSize : LongInt read FStreamSize write FStreamSize;
property MemoryPtr : Pointer read FMemoryPtr write FMemoryPtr;
property MemoryName : string read FMemoryName write FMemoryName;
property HostName : String read FHostName write FHostName;
property Port : String read FPort write FPort;
property DataPortRangeStart : LongWord read FDataPortRangeStart write SetDataPortRangeStart;
property DataPortRangeEnd : LongWord read FDataPortRangeEnd write SetDataPortRangeEnd;
property ExternalIPv4 : String read FExternalIPv4 write FExternalIPv4;
property LocalAddr : String read FLocalAddr write FLocalAddr;
property LocalAddr6 : String read FLocalAddr6 write FLocalAddr6;
property DisplayFileFlag : Boolean read FDisplayFileFlag write FDisplayFileFlag;
property SocksPassword : String read FSocksPassword write FSocksPassword;
property SocksPort : String read FSocksPort write FSocksPort;
property SocksServer : String read FSocksServer write FSocksServer;
property SocksUserCode : String read FSocksUserCode write FSocksUserCode;
property CloseEndSecs : LongWord read FCloseEndSecs write FCloseEndSecs;
property DataSocketSndBufSize : Integer read FDSocketSndBufSize write SetDSocketSndBufSize default 8192;
property DataSocketRcvBufSize : Integer read FDSocketRcvBufSize write SetDSocketRcvBufSize default 8192;
property OnDisplay : TFtpDisplay read FOnDisplay write FOnDisplay;
property OnDisplayFile : TFtpDisplay read FOnDisplayFile write FOnDisplayFile;
property OnError : TFTPDisplay read FOnError write FOnError;
property OnCommand : TFtpCommand read FOnCommand write FOnCommand;
property OnResponse : TNotifyEvent read FOnResponse write FOnResponse;
property OnSessionConnected : TSessionConnected read FOnSessionConnected write FOnSessionConnected;
property OnSessionClosed : TSessionClosed read FOnSessionClosed write FOnSessionClosed;
property OnRequestDone : TFtpRequestDone read FOnRequestDone write FOnRequestDone;
property OnStateChange : TNotifyEvent read FOnStateChange write FOnStateChange;
property OnReadyToTransmit : TFtpReadyToTransmit read FOnReadyToTransmit write FOnReadyToTransmit;
property OnBgException;
end;
TMtpClient = class(TCustomMtpCli)
protected
FTimeout : Integer;
FTimeStop : LongInt;
function Progress : Boolean; override;
function Synchronize(Proc : TFtpNextProc) : Boolean; virtual;
function WaitUntilReady : Boolean; virtual;
public
property MemoryPtr ;
constructor Create(AOwner: TComponent); override;
function Open : Boolean;
function Connect : Boolean;
function Get : Boolean;
function Put : Boolean;
function MtpPort : Boolean;
function Quit : Boolean;
function Abort : Boolean;
function Receive : Boolean;
function Transmit : Boolean;
published
property MemoryName ;
property StreamSize ;
property Timeout : Integer read FTimeout write FTimeout;
property MultiThreaded;
property HostName;
property Port;
property DataPortRangeStart;
property DataPortRangeEnd;
property ExternalIPv4;
property LocalAddr;
property LocalAddr6;
property DisplayFileFlag;
property ErrorMessage;
property SocksPassword;
property SocksPort;
property SocksServer;
property SocksUserCode;
property DataSocketSndBufSize;
property OnDisplay;
property OnDisplayFile;
property OnCommand;
property OnError;
property OnResponse;
property OnProgress64;
property OnSessionConnected;
property OnSessionClosed;
property OnRequestDone;
property OnStateChange;
property OnReadyToTransmit;
property OnBgException;
property SocketFamily;
end;
{$B-} { Enable partial boolean evaluation }
{$T-} { Untyped pointers }
{$X+} { Enable extended syntax }
{$H+} { Use long strings }
{$J+} { Allow typed constant to be modified }
function LookupFTPReq (const RqType: TFtpRequest): String;
function LookupFtpState (const FtpState: TFtpState): String;
procedure register;
implementation
procedure register;
begin
RegisterComponents('ira Mtp', [TMtpClient]);
end;
{$B-} { Do not evaluate boolean expressions more than necessary }
function LookupFTPReq (const RqType: TFtpRequest): String;
begin
case RqType of
ftpNone: result:='none';
ftpOpenAsync: result:='OpenAsync';
ftpConnectAsync: result:='ConnectAsync';
ftpReceiveAsync: result:='ReceiveAsync';
ftpPortAsync: result:='PortAsync';
ftpGetAsync: result:='GetAsync';
ftpQuitAsync: result:='QuitAsync';
ftpPutAsync: result:='PutAsync';
ftpRqAbort: result:='RqAbort';
ftpTransmitAsync: result:='TransmitAsync';
else
result:='unknown';
end;
end;
function LookupFtpState (const FtpState: TFtpState): String;
begin
case FtpState of
ftpNotConnected: result := 'Not Connected';
ftpReady: result := 'Ready';
ftpInternalReady: result := 'Internal Ready';
ftpDnsLookup: result := 'DNS Lookup';
ftpConnected: result := 'Connected';
ftpAbort: result := 'Abort';
ftpInternalAbort: result := 'Internal Abort';
ftpWaitingResponse: result := 'Waiting Response';
else
result:='unknown';
end;
end ;
function GetInteger(Data : PChar; var Number : LongInt) : PChar;
var
bSign : Boolean;
begin
Number := 0;
Result := StpBlk(Data);
if Result = nil then
Exit;
if (Result^ = '-') or (Result^ = '+') then begin
bSign := (Result^ = '-');
Inc(Result);
end
else
bSign := FALSE;
while (Result^ <> #0) and IsDigit(Result^) do begin
Number := Number * 10 + ord(Result^) - ord('0');
Inc(Result);
end;
if bSign then
Number := -Number;
end;
function GetInt64(Data : PChar; var Number : Int64) : PChar;
var
bSign : Boolean;
begin
Number := 0;
Result := StpBlk(Data);
if Result = nil then
Exit;
if (Result^ = '-') or (Result^ = '+') then begin
bSign := (Result^ = '-');
Inc(Result);
end
else
bSign := FALSE;
while (Result^ <> #0) and IsDigit(Result^) do begin
Number := Number * 10 + ord(Result^) - ord('0');
Inc(Result);
end;
if bSign then
Number := -Number;
end;
function GetQuotedString(Data : PChar; var Dst : String) : PChar;
begin
Dst := '';
Result := StpBlk(Data);
if (Result = nil) then
Exit;
if Result^ <> '"' then
Exit;
Inc(Result);
while Result^ <> #0 do begin
if Result^ <> '"' then
Dst := Dst + Result^
else begin
Inc(Result);
if Result^ <> '"' then
Break;
Dst := Dst + Result^;
end;
Inc(Result);
end;
end;
function GetNextString(Data : PChar; var Dst : String) : PChar;
begin
Dst := '';
Result := StpBlk(Data);
if Result = nil then
Exit;
while (Result^ <> #0) and (Result^ = #32) do
Inc(Result); { skip leading spaces }
while (Result^ <> #0) and (Result^ <> #32) do begin
Dst := Dst + Result^;
Inc(Result);
end;
end;
{* * * *}
{* * TCustomMtpCli * *}
{* * * *}
constructor TCustomMtpCli.Create(AOwner: TComponent);
{$IFDEF MSWINDOWS}
var
Len : Cardinal;
{$ENDIF}
begin
inherited Create(AOwner);
AllocateHWnd;
FOnDisplay := nil;
FOnDisplayFile := nil;
FPort := 'ftp';
FDataPortRangeStart := 0;
FDataPortRangeEnd := 0;
FCloseEndSecs := 5;
FProxyPort := 'ftp';
FState := ftpReady;
FProxyServer := '';
FSocksServer := '';
FLocalAddr := ICS_ANY_HOST_V4;
FLocalAddr6 := ICS_ANY_HOST_V6;
FKeepAliveSecs := 0;
FSocketFamily := DefaultSocketFamily;
FControlSocket := CreateSocket;
FControlSocket.ExceptAbortProc := AbortComponent;
FControlSocket.OnSessionConnected := ControlSocketSessionConnected;
FControlSocket.OnDataAvailable := ControlSocketDataAvailable;
FControlSocket.OnSessionClosed := ControlSocketSessionClosed;
FControlSocket.OnDnsLookupDone := ControlSocketDnsLookupDone;
FDataSocket := CreateSocket;
FDataSocket.ExceptAbortProc := AbortComponent;
FDSocketSndBufSize := 8192;
FDSocketRcvBufSize := 8192;
end;
destructor TCustomMtpCli.Destroy;
begin
DestroyLocalStream;
FDataSocket.Free;
FControlSocket.Free;
inherited Destroy;
end;
function TCustomMtpCli.MsgHandlersCount : Integer;
begin
Result := 3 + inherited MsgHandlersCount;
end;
procedure TCustomMtpCli.AllocateMsgHandlers;
begin
inherited AllocateMsgHandlers;
FMsg_WM_FTP_REQUEST_DONE := FWndHandler.AllocateMsgHandler(Self);
FMsg_WM_FTP_SENDDATA := FWndHandler.AllocateMsgHandler(Self);
FMsg_WM_FTP_CLOSEDOWN := FWndHandler.AllocateMsgHandler(Self);
end;
procedure TCustomMtpCli.FreeMsgHandlers;
begin
if Assigned(FWndHandler) then begin
FWndHandler.UnregisterMessage(FMsg_WM_FTP_REQUEST_DONE);
FWndHandler.UnregisterMessage(FMsg_WM_FTP_SENDDATA);
FWndHandler.UnregisterMessage(FMsg_WM_FTP_CLOSEDOWN);
end;
inherited FreeMsgHandlers;
end;
procedure TCustomMtpCli.WndProc(var MsgRec: TMessage);
begin
try
with MsgRec do begin
if Msg = FMsg_WM_FTP_REQUEST_DONE then
WMFtpRequestDone(MsgRec)
else if Msg = FMsg_WM_FTP_SENDDATA then
WMFtpSendData(MsgRec)
else if Msg = FMsg_WM_FTP_CLOSEDOWN then
WMFtpCloseDown(MsgRec)
else
inherited WndProc(MsgRec);
end;
except
on E: Exception do
HandleBackGroundException(E);
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TCustomMtpCli.AbortComponent;
begin
try
AbortAsync;
except
end;
inherited;
end;
procedure TCustomMtpCli.WMFtpRequestDone(var msg: TMessage);
begin
if Assigned(FOnRequestDone) then
FOnRequestDone(Self, FRequestType, Msg.LParam);
end;
procedure TCustomMtpCli.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if Operation = opRemove then begin
if AComponent = FControlSocket then
FControlSocket := nil
else if AComponent = FDataSocket then
FDataSocket := nil;
end;
end;
procedure TCustomMtpCli.SetDSocketSndBufSize(const Value: Integer);
begin
if Value < 1024 then
FDSocketSndBufSize := 1024
else
FDSocketSndBufSize := Value;
end;
procedure TCustomMtpCli.SetDSocketRcvBufSize(const Value: Integer);
begin
if Value < 1024 then
FDSocketRcvBufSize := 1024
else
FDSocketRcvBufSize := Value;
end;
procedure TCustomMtpCli.SetErrorMessage;
begin
if FErrorMessage = '' then
FErrorMessage := FLastResponse;
end;
function TCustomMtpCli.CreateSocket: TWSocket;
begin
Result := TWSocket.Create(Self);
end;
procedure TCustomMtpCli.DestroyLocalStream;
var
NewSize: Int64;
begin
if Assigned(FLocalStream) then begin
FLocalStream.Free;
FLocalStream := nil;
end;
end;
function TCustomMtpCli.OpenMemoryStream (Buffersize: integer): TmemoryStream;
begin
Result := TmemoryStream.Create ;
Result.SetSize(BufferSize);
end ;
procedure TCustomMtpCli.CreateLocalFileStream;
begin
try
FreeAndNil(FLocalStream);
FLocalStream := OpenMemoryStream(FStreamSize);//960*540*3);
except
on E:Exception do begin
FLastResponse := 'Unable to open local stream ';
FStatusCode := 550;
SetErrorMessage;
FRequestResult := FStatusCode;
TriggerRequestDone(FRequestResult);
exit;
end;
end;
end;
procedure TCustomMtpCli.LocalStreamWriteString(Str: PWideChar; Count: Integer;
ACodePage: LongWord);
begin
StreamWriteString(FLocalStream, Str, Count, ACodePage);
end;
procedure TCustomMtpCli.LocalStreamWriteString(Str: PWideChar; Count: Integer);
begin
StreamWriteString(FLocalStream, Str, Count, CP_ACP);
end;
procedure TCustomMtpCli.LocalStreamWriteString(Str: PAnsiChar; Count : Integer);
begin
FLocalStream.WriteBuffer(Str^, Count);
end;
procedure TCustomMtpCli.LocalStreamWrite(const Buffer; Count : Integer);
begin
FLocalStream.WriteBuffer(Buffer, Count);
end;
procedure TCustomMtpCli.SetKeepAliveSecs (secs: integer);
begin
if FKeepAliveSecs <> secs then begin
if secs = 0 then
FControlSocket.KeepAliveOnOff := wsKeepAliveOnSystem
else begin
FControlSocket.KeepAliveOnOff := wsKeepAliveOnCustom ;
FControlSocket.KeepAliveTime := LongWord (secs) * 1000;
if secs < 10 then
FControlSocket.KeepAliveInterval := 1000
else
FControlSocket.KeepAliveInterval := LongWord (secs div 5) * 1000;
end ;
end;
FKeepAliveSecs := secs;
end;
procedure TCustomMtpCli.SetLocalStream(Stream: TmemoryStream);
begin
FLocalStream := Stream;
end;
procedure TCustomMtpCli.SetDataPortRangeStart(NewValue: LongWord);
begin
if NewValue > 65535 then
HandleError('DataPortRangeStart must be in the range 0..65535')
else
FDataPortRangeStart := NewValue;
end;
procedure TCustomMtpCli.SetDataPortRangeEnd(NewValue: LongWord);
begin
if NewValue > 65535 then
HandleError('DataPortRangeEnd must be in the range 0..65535')
else
FDataPortRangeEnd := NewValue
end;
procedure TCustomMtpCli.TriggerDisplay(Msg : String);
begin
if Assigned(FOnDisplay) then
FOnDisplay(Self, Msg);
end;
procedure TCustomMtpCli.TriggerDisplayFile(Msg : String);
begin
if Assigned(FOnDisplayFile) then
FOnDisplayFile(Self, Msg);
end;
procedure TCustomMtpCli.TriggerError(Msg : String);
begin
if Assigned(FOnError) then
FOnError(Self, Msg);
end;
procedure TCustomMtpCli.DisplayLastResponse;
begin
if Pos('Will attempt to restart', FLastResponse) > 0 then
TriggerDisplay('< DEBUG !');
TriggerDisplay('< ' + FLastResponse);
end;
procedure TCustomMtpCli.SetMultiThreaded(const Value : Boolean);
begin
if Assigned(FDataSocket) then
FDataSocket.MultiThreaded := Value;
if Assigned(FControlSocket) then
FControlSocket.MultiThreaded := Value;
inherited SetMultiThreaded(Value);
end;
procedure TCustomMtpCli.SetTerminated(const Value: Boolean);
begin
if Assigned(FDataSocket) then
FDataSocket.Terminated := Value;
if Assigned(FControlSocket) then
FControlSocket.Terminated := Value;
inherited SetTerminated(Value);
end;
procedure TCustomMtpCli.SetOnBgException(const Value: TIcsBgExceptionEvent);
begin
if Assigned(FDataSocket) then
FDataSocket.OnBgException := Value;
if Assigned(FControlSocket) then
FControlSocket.OnBgException := Value;
inherited SetOnBgException(Value);
end;
procedure TCustomMtpCli.SetOnMessagePump(const Value: TNotifyEvent);
begin
if Assigned(FDataSocket) then
FDataSocket.OnMessagePump := Value;
if Assigned(FControlSocket) then
FControlSocket.OnMessagePump := Value;
inherited SetOnMessagePump(Value);
end;
procedure TCustomMtpCli.StateChange(NewState : TFtpState);
begin
if FState <> NewState then begin
FState := NewState;
TriggerStateChange;
end;
end;
function TCustomMtpCli.GetBinary : Boolean;
begin
Result := (FType = 'I');
end;
procedure TCustomMtpCli.SetBinary(Value : Boolean);
begin
if Value then
FType := 'I'
else
FType := 'A';
end;
function TCustomMtpCli.Progress : Boolean;
var
Abort : Boolean;
begin
Abort := FALSE;
if Assigned(FOnProgress64) then
FOnProgress64(Self, FByteCount , Abort);
if Abort then begin
// TriggerDisplay('! Abort requested');
// FDataSocket.Close;
AbortAsync ;
end;
Result := not Abort;
end;
procedure TCustomMtpCli.SendCommand(Cmd : String);
var
RawCmd: AnsiString;
begin
if Assigned(FOnCommand) then
FOnCommand(Self, Cmd);
TriggerDisplay('> ' + Cmd);
RawCmd := Cmd;
if FControlSocket.State = wsConnected then
FControlSocket.SendStr(RawCmd + #13#10)
else begin
if cmd = 'QUIT' then
FStatusCode := 200
else
FStatusCode := 550;
FNextRequest := nil;
FDoneAsync := nil;
FConnected := FALSE;
FRequestResult := FStatusCode;
FLastResponse := IntToStr(FStatusCode) + ' not connected';
if FStatusCode = 550 then begin
SetErrorMessage;
TriggerRequestDone(550);
end
else
TriggerRequestDone(0);
end;
end;
procedure TCustomMtpCli.HandleError(const Msg : String);
begin
FFctSet := [];
FFctPrv := ftpFctNone;
FLastResponse := '';
FErrorMessage := '';
FNextRequest := nil;
if Assigned(FOnError) then
TriggerError(Msg)
else
raise FtpException.Create(Msg);
end;
function TCustomMtpCli.CheckReady : Boolean;
begin
Result := (FState in [ftpReady, ftpInternalReady, ftpConnected]);
if not Result then
HandleError('FTP component not ready, state ' + LookupFtpState (FState));
end;
procedure TCustomMtpCli.OpenAsync;
begin
if not CheckReady then begin
TriggerDisplay('Not ready for Open');
Exit;
end;
if FConnected then begin
HandleError('FTP component already connected');
Exit;
end;
if not FHighLevelFlag then
FRequestType := ftpOpenAsync;
FRequestDoneFlag := FALSE;
FReceiveLen := 0;
FRequestResult := 0;
FDnsResult := '';
FControlSocket.SocketFamily := FSocketFamily;
FLastResponse := '';
FErrorMessage := '';
FStatusCode := 0;
FControlSocket.SocksAuthentication := socksNoAuthentication;
FControlSocket.HttpTunnelServer := '';
FDataSocket.HttpTunnelServer := '';