-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathuHTTPClient.pas
922 lines (891 loc) · 39.7 KB
/
uHTTPClient.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
unit uHTTPClient;
{******************************************************************************}
{* Package HTTP Client Unit *}
{* Revolutionary Confederation of Anarcho Syndicalists *}
{* Written by: black.rabbit 2011-2012 *}
{******************************************************************************}
interface
{$I 'std.inc'}
uses
Windows, SysUtils, Variants, Classes,
Controls, ComCtrls, Gauges,
sListView,
{ utils }
DateUtils, Utils, Strings, VarRecs, Versions, EClasses,
DllThreads,
{ synapse }
WinSock, BlckSock, SynaUtil, HTTPSend,
PingSend,
{ http }
HTTPClient,
{ kernel }
Kernel, ProtoClasses, CryptoClasses, MetaClasses, ParaClasses,
HypoClasses, HyperClasses,
{ engine }
Engine,
{ SQLite }
SQLite3, SQLite3DLL, SQLiteTable3;
{ èêîíêè }
const
pckDefault = 0;
pckEmpty = 1;
pckFull = 2;
pckTest = 3;
pckError = 4;
pckSend = 5;
pckReceive = 6;
type
PLogRecord = ^TLogRecord;
TLogRecord = packed record
Caption : ShortString;
ImageIndex : Integer;
Sender : ShortString;
Receiver : ShortString;
Message : ShortString;
end;
type
{ îøèáêà http-êëèåíòà }
{$M+}
EHTTPClientError = class (EThreadError) end;
{$M-}
{ ïîòîê ïàêåòíîãî êëèåíòà }
{$M+}
CHTTPPackageClient = class of THTTPPackageClient;
PHTTPPackageClient = ^THTTPPackageClient;
THTTPPackageClient = class (TDllThread)
public
class procedure _raise (anArgs: array of const;
const anEGUID: String = ''); override;
class procedure _raise (anArgs: array of const;
anEGUID: array of const); override;
private
f_Threads: TDllThreads; { äî÷åðíèå ïîòîêè }
f_ProxyHost: String; { õîñò proxy-ñåðâåðà }
f_ProxyPort: WORD; { ïîðò proxy-ñåðâåðà }
f_ProxyLogin: String; { ëîãèí íà proxy-ñåðâåð }
f_ProxyPassword: String; { ïàðîëü íà proxy-ñåðâåð }
f_ProxyProtocol: String; { ïðîòîêîë proxy-ñåðâåðà }
f_TimeOut: LongWord; { âðåìÿ îæèäàíèÿ ms }
f_DBFileName: String; { ôàéë áàçû äàííûõ }
f_DB: TSQLiteDatabase; { îáúåêò áàçû äàííûõ }
f_ListView: TsListView; { ëîã }
f_Gauge: TGauge; { ïîêàçàòåëü ïðîãðåññà çàãðóçêè }
f_StatusPanel: TStatusPanel; { ñòðîêà ñîñòîÿíèÿ çàãðóçêè }
f_Users: TUsers; { ñïèñîê êîíòàêòîâ â ðàññûëêå }
f_UserIndex: LongInt; { ïîçèöèÿ â îáõîäå ñïèñêà }
public
// DBFileName, ListView, Gauge, StatusPanel, TimeOut, ProxyHost, ProxyPort, ProxyLogin, ProxyPassword, ProxyProtocol
constructor Create (anArgs: array of const); override;
destructor Destroy; override;
public
procedure Main; override;
procedure Return; override;
protected
procedure WriteStatus (const aMessage: String); overload;
procedure WriteStatus (const aMessage: String;
aParams: array of const); overload;
public
property Threads: TDllThreads read f_Threads write f_Threads;
property ProxyHost: String read f_ProxyHost write f_ProxyHost;
property ProxyPort: WORD read f_ProxyPort write f_ProxyPort;
property ProxyLogin: String read f_ProxyLogin write f_ProxyLogin;
property ProxyPassword: String read f_ProxyPassword write f_ProxyPassword;
property ProxyProtocol: String read f_ProxyProtocol write f_ProxyProtocol;
property TimeOut: LongWord read f_TimeOut write f_TimeOut;
property DBFileName: String read f_DBFileName;
property DB: TSQLiteDatabase read f_DB write f_DB;
property ListView: TsListView read f_ListView write f_ListView;
property Gauge: TGauge read f_Gauge write f_Gauge;
property StatusPanel: TStatusPanel read f_StatusPanel write f_StatusPanel;
property Users: TUsers read f_Users write f_Users;
property UserIndex: LongInt read f_UserIndex write f_UserIndex;
end;
{$M-}
{ äî÷åðíèé ïîòîê ïàêåòíîãî êëèåíòà }
{$M+}
CHTTPPackageClientThread = class of THTTPPackageClientThread;
PHTTPPackageClientThread = ^THTTPPackageClientThread;
THTTPPackageClientThread = class (TDllThread)
public
class procedure _raise (anArgs: array of const;
const anEGUID: String = ''); override;
class procedure _raise (anArgs: array of const;
anEGUID: array of const); override;
private
f_HTTP: THTTPClient; { êëèåíò }
f_ProxyHost: String; { õîñò proxy-ñåðâåðà }
f_ProxyPort: WORD; { ïîðò proxy-ñåðâåðà }
f_ProxyLogin: String; { ëîãèí íà proxy-ñåðâåð }
f_ProxyPassword: String; { ïàðîëü íà proxy-ñåðâåð }
f_ProxyProtocol: String; { ïðîòîêîë proxy-ñåðâåðà }
f_TimeOut: LongWord; { âðåìÿ îæèäàíèÿ ms }
f_DBFileName: String; { ôàéë áàçû äàííûõ }
f_DB: TSQLiteDatabase; { îáúåêò áàçû äàííûõ }
f_ReceiverID: TID; { èäåíòèôèêàòîð ïîëó÷àòåëÿ }
f_Receiver: TUser; { ïîëó÷àòåëü }
f_ListView: TsListView; { ëîã }
f_Gauge: TGauge; { ïîêàçàòåëü ïðîãðåññà çàãðóçêè }
f_StatusPanel: TStatusPanel; { ñòðîêà ñîñòîÿíèÿ çàãðóçêè }
f_Packages: TPackages; { ñïèñîê ïàêåòîâ äëÿ ðàññûëêè }
f_PackageIndex: LongInt; { ïîçèöèÿ â îáõîäå ñïèñêà }
f_IdleTime: LongInt; { âðåìÿ îæèäàíèÿ ìåæäó çàïðîñàìè }
private
f_Log: TItems;
f_Status: String;
f_MaxProgress: WORD;
f_Progress: WORD;
public
// DBFileName, ReceiverID, ListView, Gauge, StatusPanel, TimeOut, ProxyHost, ProxyPort, ProxyLogin, ProxyPassword, ProxyProtocol
constructor Create (anArgs: array of const); override;
destructor Destroy; override;
public
procedure Main; override;
procedure Return; override;
protected
procedure WriteStatus (const aMessage: String); overload;
procedure WriteStatus (const aMessage: String;
aParams: array of const); overload;
procedure WriteLog (const aSender: String;
const aReceiver: String;
const aMessage: String;
const anImageIndex: Integer = pckDefault;
const aPackageType: String = '';
const aMetaClassID: String = ''); overload;
procedure WriteLog (const aSender: String;
const aReceiver: String;
const aMessage: String;
aParams: array of const;
const anImageIndex: Integer = pckDefault;
const aPackageType: String = '';
const aMetaClassID: String = ''); overload;
public
property HTTP: THTTPClient read f_HTTP;
property ProxyHost: String read f_ProxyHost write f_ProxyHost;
property ProxyPort: WORD read f_ProxyPort write f_ProxyPort;
property ProxyLogin: String read f_ProxyLogin write f_ProxyLogin;
property ProxyPassword: String read f_ProxyPassword write f_ProxyPassword;
property ProxyProtocol: String read f_ProxyProtocol write f_ProxyProtocol;
property TimeOut: LongWord read f_TimeOut write f_TimeOut;
property DBFileName: String read f_DBFileName;
property DB: TSQLiteDatabase read f_DB write f_DB;
property ReceiverID: TID read f_ReceiverID write f_ReceiverID;
property Receiver: TUser read f_Receiver;
property ListView: TsListView read f_ListView write f_ListView;
property Gauge: TGauge read f_Gauge write f_Gauge;
property StatusPanel: TStatusPanel read f_StatusPanel write f_StatusPanel;
property Packages: TPackages read f_Packages write f_Packages;
property PackageIndex: LongInt read f_PackageIndex write f_PackageIndex;
property IdleTime: LongInt read f_IdleTime write f_IdleTime;
property Log: TItems read f_Log write f_Log;
property Status: String read f_Status write f_Status;
property MaxProgress: WORD read f_MaxProgress write f_MaxProgress;
property Progress: WORD read f_Progress write f_Progress;
end;
{$M-}
{ THTTPPackageClient Errors }
resourcestring
ERR_THTTPPACKAGECLIENT_INCORRECT_DATABASE = 'Íå èíèöèàëèçèðîâàí îáúåêò ÁÄ!';
ERR_THTTPPACKAGECLIENT_INVALID_PROXY_HOST = 'Íåêîððåêòíûé proxy-õîñò!';
ERR_THTTPPACKAGECLIENT_INVALID_PROXY_PORT = 'Íåêîððåêòíûé proxy-ïîðò!';
ERR_THTTPPACKAGECLIENT_INVALID_PROXY_PROTOCOL = 'Íåêîððåêòíûé proxy-ïðîòîêîë!';
ERR_THTTPPACKAGECLIENT_INVALID_TIMEOUT = 'Íåêîððåêòíûé òàéìàóò äëÿ ñîåäèíåíèé!';
ERR_THTTPPACKAGECLIENT_CREATE = 'Îøèáêà ñîçäàíèÿ ïîòîêà ïàêåòíîãî êëèåíòà!';
ERR_THTTPPACKAGECLIENT_DESTROY = 'Îøèáêà óíè÷òîæåíèÿ ïîòîêà ïàêåòíîãî êëèåíòà!';
ERR_THTTPPACKAGECLIENT_MAIN = 'Îøèáêà âûïîëíåíèÿ ãëàâíîé ôóíêöèè ïîòîêà ïàêåòíîãî êëèåíòà!';
ERR_THTTPPACKAGECLIENT_RETURN = 'Îøèáêà ôóíêöèè âîçâðàòà ïàêåòíîãî êëèåíòà!';
ERR_THTTPPACKAGECLIENT_WRITE_STATUS = 'Îøèáêà îòîáðàæåíèÿ ñòàòóñà!';
{ THTTPPackageClientThread Errors }
resourcestring
ERR_THTTPPACKAGECLIENTTHREAD_INCORRECT_DATABASE = 'Íå èíèöèàëèçèðîâàí îáúåêò ÁÄ!';
ERR_THTTPPACKAGECLIENTTHREAD_INVALID_RECEIVER_ID = 'Íåêîððåêòíûé èäåíòèôèêàòîð ïîëó÷àòåëÿ!';
ERR_THTTPPACKAGECLIENTTHREAD_INVALID_RECEIVER = 'Íåêîððåêòíûé ïîëó÷àòåëü!';
ERR_THTTPPACKAGECLIENTTHREAD_INVALID_PROXY_HOST = 'Íåêîððåêòíûé proxy-õîñò!';
ERR_THTTPPACKAGECLIENTTHREAD_INVALID_PROXY_PORT = 'Íåêîððåêòíûé proxy-ïîðò!';
ERR_THTTPPACKAGECLIENTTHREAD_INVALID_PROXY_PROTOCOL = 'Íåêîððåêòíûé proxy-ïðîòîêîë!';
ERR_THTTPPACKAGECLIENTTHREAD_INVALID_TIMEOUT = 'Íåêîððåêòíûé òàéìàóò äëÿ ñîåäèíåíèé!';
ERR_THTTPPACKAGECLIENTTHREAD_INVALID_DATA = 'Íåêîððåêòíûå äàííûå!';
ERR_THTTPPACKAGECLIENTTHREAD_CREATE = 'Îøèáêà ñîçäàíèÿ äî÷åðíåãî ïîòîêà ïàêåòíîãî êëèåíòà!';
ERR_THTTPPACKAGECLIENTTHREAD_DESTROY = 'Îøèáêà óíè÷òîæåíèÿ äî÷åðíåãî ïîòîêà ïàêåòíîãî êëèåíòà!';
ERR_THTTPPACKAGECLIENTTHREAD_MAIN = 'Îøèáêà âûïîëíåíèÿ ãëàâíîé ôóíêöèè äî÷åðíåãî ïîòîêà ïàêåòíîãî êëèåíòà!';
ERR_THTTPPACKAGECLIENTTHREAD_RETURN = 'Îøèáêà âûïîëíåíèÿ ôóíêöèè âîçâðàòà äî÷åðíåãî ïîòîêà ïàêåòíîãî êëèåíòà!';
ERR_THTTPPACKAGECLIENTTHREAD_WRITE_STATUS = 'Îøèáêà îòîáðàæåíèÿ ñòàòóñà!';
ERR_THTTPPACKAGECLIENTTHREAD_WRITE_LOG = 'Îøèáêà çàïèñè â ëîã!';
{ THTTPPackage Hints }
resourcestring
MSG_THTTPPACKAGE_SEND_TO = 'Îòïðàâêà ïàêåòîâ äëÿ ''%s''...';
MSG_THTTPPACKAGE_SEND_ERROR = 'Îøèáêà îòïðàâêè ïàêåòà. HTTP Error: %d';
MSG_THTTPPACKAGE_SENDED_PACKAGE = 'Ïàêåò ''%s'' îòïðàâëåí ïîëó÷àòåëþ.';
MSG_THTTPPACKAGE_RECEIVED_PACKAGE = 'Ïàêåò ''%s'' äîñòàâëåí ïîëó÷àòåëþ.';
MSG_THTTPPACKAGE_EXECUTED_PACKAGE = 'Ïàêåò ''%s'' îáðàáîòàí ïîëó÷àòåëåì.';
MSG_THTTPPACKAGE_REJECTED_PACKAGE = 'Ïàêåò ''%s'' îòâåðãíóò ïîëó÷àòåëåì.';
implementation
{ THTTPPackageClient }
class procedure THTTPPackageClient._raise (anArgs: array of const;
const anEGUID: String = '');
begin
raise EHTTPClientError.Create ( _([self],anArgs), anEGUID );
end;
class procedure THTTPPackageClient._raise (anArgs: array of const;
anEGUID: array of const);
begin
raise EHTTPClientError.Create ( _([self],anArgs), anEGUID );
end;
constructor THTTPPackageClient.Create (anArgs: array of const);
var
I : Integer;
args : array_of_const;
OBJ : TObject;
begin
try
{ ïåðåäàåì ïàðàìåòðû ñîçäàíèÿ áàçîâîãî ïîòîêà,
íà÷èíàÿ ñ îäèííàäöàòîãî àðãóìåíòà }
if ( High (anArgs) >= 10 ) then
begin
SetLength ( Args, High (anArgs)-10 +1 );
for I := 10 to High (anArgs) do
args [I-10] := anArgs [I];
end
else
args := _array_of_const ([]);
inherited Create (args);
{ ÿâíî óêàçûâàåì íà íåîáõîäèìîñòü óíè÷òîæåíèÿ ïîòîêà ïî îêîí÷àíèþ ðàáîòû }
FreeOnTerminate := TRUE;
{ èìÿ ïîòîêà }
Name := ClassName;
{ ïðèîðèòåò }
Priority := tpIdle;
{ ïåðâûé ïàðàìåòð - ôàéë ÁÄ }
f_DBFileName := '';
f_DB := NIL;
if notEmpty (0,anArgs) then
begin
f_DBFileName := toString (anArgs [0]);
f_DB := TSQLiteDatabase.Create (f_DBFileName);
end;
if ( not Assigned (f_DB) ) then
raise Exception.Create (ERR_THTTPPACKAGECLIENT_INCORRECT_DATABASE);
{ âòîðîé ïàðàìåòð - ListView
ëîã ïî÷òû }
f_ListView := NIL;
if notEmpty (1,anArgs) then
begin
OBJ := toObject (anArgs [1]);
if ( Assigned (OBJ) and OBJ.InheritsFrom (TsListView) ) then
f_ListView := TsListView (OBJ);
end;
{ òðåòèé ïàðàìåòð - Gauge
ïîêàçàòåëü ïðîãðåññà çàãðóçêè }
f_Gauge := NIL;
if notEmpty (2,anArgs) then
begin
OBJ := toObject (anArgs [2]);
if ( Assigned (OBJ) and OBJ.InheritsFrom (TControl) ) then
f_Gauge := TGauge (OBJ);
end;
{ ÷åòâåðòûé ïàðàìåòð - StatusPanel
ïîêàçàòåëü ïðîãðåññà çàãðóçêè }
f_StatusPanel := NIL;
if notEmpty (3,anArgs) then
begin
OBJ := toObject (anArgs [3]);
if ( Assigned (OBJ) and OBJ.InheritsFrom (TStatusPanel) ) then
f_StatusPanel := TStatusPanel (OBJ);
end;
{ ïÿòûé ïàðàìåòð - âðåìÿ îæèäàíèÿ ms }
f_TimeOut := 120000;
if notEmpty (4,anArgs) then
begin
f_TimeOut := toInteger (anArgs [4]);
end;
if ( TimeOut = 0 ) then
raise Exception.Create (ERR_THTTPPACKAGECLIENT_INVALID_TIMEOUT);
{ øåñòîé ïàðàìåòð - õîñò proxy-ñåðâåðà }
f_ProxyHost := '';
if notEmpty (5,anArgs) then
begin
f_ProxyHost := toString (anArgs [5]);
end;
if ( ProxyHost <> '' ) and not isIPAddress (ProxyHost) then
raise Exception.Create (ERR_THTTPPACKAGECLIENT_INVALID_PROXY_HOST);
{ ñåäüìîé ïàðàìåòð - ïîðò proxy-ñåðâåðà }
f_ProxyPort := 0;
if notEmpty (6,anArgs) then
begin
f_ProxyPort := toInteger (anArgs [6]);
end;
if ( ProxyPort <= 0 ) and ( ProxyHost <> '' ) then
raise Exception.Create (ERR_THTTPPACKAGECLIENT_INVALID_PROXY_PORT);
{ âîñüìîé ïàðàìåòð - ëîãèí proxy-ñåðâåðà }
f_ProxyLogin := '';
if notEmpty (7,anArgs) then
begin
f_ProxyLogin := toString (anArgs [7]);
end;
{ äåâÿòûé ïàðàìåòð - ïàðîëü proxy-ñåðâåðà }
f_ProxyPassword := '';
if notEmpty (8,anArgs) then
begin
f_ProxyPassword := toString (anArgs [8]);
end;
{ äåñÿòûé ïàðàìåòð - ïðîòîêîë proxy-ñåðâåðà }
f_ProxyProtocol := '';
if notEmpty (9,anArgs) then
begin
f_ProxyProtocol := toString (anArgs [9]);
end;
{ ñîçäàåì êîíòåéíåð äî÷åðíèõ ïîòîêîâ }
f_Threads := TDllThreads.Create ([]);
{ ñïèñîê ïîëüçîâàòåëåé }
f_Users := NIL;
f_UserIndex := -1;
except on E: Exception do
_raise ([ 'Create', ERR_THTTPPACKAGECLIENT_CREATE, E, Exception (FatalException) ],
['{CCF6F48B-3667-4DD9-ADD1-5676797FD0ED}']);
end;
end;
destructor THTTPPackageClient.Destroy;
begin
try
try
_FillChar ( f_ProxyHost, Length (f_ProxyHost), $00 );
f_ProxyPort := 0;
_FillChar ( f_ProxyLogin, Length (f_ProxyLogin), $00 );
_FillChar ( f_ProxyPassword, Length (f_ProxyPassword), $00 );
if Assigned (Threads) then
Threads.Terminate;
FreeAndNil (f_Threads);
FreeAndNil (f_Users);
FreeAndNil (f_DB);
finally
inherited Destroy;
end;
except on E: Exception do
_raise ([ 'Destroy', ERR_THTTPPACKAGECLIENT_DESTROY, E, Exception (FatalException) ],
['{769B1840-E59A-4FF4-A30E-A31EAFE47018}']);
end;
end;
procedure THTTPPackageClient.Main;
begin
try
if Terminated then Exit;
inherited Main;
if not Assigned (Users) then
try
f_Users := TUsers.Load (DB,[ _([]),
_([]),
_([USER_ID]) ],
[],
[],
[objSimple]) as TUsers;
except
FreeAndNil (f_Users);
end;
Sleep (TimeOut{*10});
except on E: Exception do
_raise ([ 'Main', ERR_THTTPPACKAGECLIENT_MAIN, E, Exception (FatalException) ],
['{0F6AC9B4-8CED-44E8-B8AE-5388F7154BBC}']);
end;
end;
procedure THTTPPackageClient.Return;
var
Thr : THTTPPackageClientThread;
begin
try
if Terminated then Exit;
if ( not Assigned (Users) or (Users.Count <= 0) ) then
Terminate
else if ( UserIndex >= Users.Count - 1 ) then
begin
UserIndex := -1;
end
{ åñëè îäèí èç äî÷åðíèõ ïîòîêîâ çàâåðøèëñÿ - çàïóñêàåì åãî ñíîâà }
else if ( UserIndex <= Users.Count - 1 ) and
not Assigned ( Threads.ItemOf [ Users.ItemAt [UserIndex+1].KeyHash ] ) then
try
inherited Return;
UserIndex := UserIndex + 1;
if {( Users.ItemAt [UserIndex].IP <> '127.0.0.1' ) and}
( PingHost (Users.ItemAt [UserIndex].IP) >= 0 ) then
try
Thr := THTTPPackageClientThread.Create ([ DBFileName,
Users.ItemAt [UserIndex].ID,
ListView,
Gauge,
StatusPanel,
TimeOut,
ProxyHost, ProxyPort,
ProxyLogin, ProxyPassword,
ProxyProtocol,
FALSE, TRUE,
TP_IDLE,
NIL, NIL,
Users.ItemAt [UserIndex].KeyHash ]);
if ( Threads.Add (Thr) < 0 ) then
FreeAndNil (Thr);
except
FreeAndNil (Thr);
end;
except on E: Exception do
WriteStatus (E.Message);
end;
except on E: Exception do
_raise ([ 'Return', ERR_THTTPPACKAGECLIENT_RETURN, E, Exception (FatalException) ],
['{C1DE6724-28A6-4B18-8921-1906B006E425}']);
end;
end;
procedure THTTPPackageClient.WriteStatus (const aMessage: String);
begin
try
if Assigned (StatusPanel) then
begin
StatusPanel.Text := aMessage;
ProcessMessages;
end;
except on E: Exception do
_raise (['WriteStatus',ERR_THTTPPACKAGECLIENT_WRITE_STATUS,E],
['{42AD2D85-5DAE-4561-B9F6-F68290CC2D65}']);
end;
end;
procedure THTTPPackageClient.WriteStatus (const aMessage: String;
aParams: array of const);
begin
try
WriteStatus ( Format (aMessage,aParams) );
except on E: Exception do
_raise (['WriteStatus',ERR_THTTPPACKAGECLIENT_WRITE_STATUS,E],
['{1C6E36C5-D670-4274-AA8C-52E2A00BF96A}']);
end;
end;
{ THTTPPackageClientThread }
class procedure THTTPPackageClientThread._raise (anArgs: array of const;
const anEGUID: String = '');
begin
raise EHTTPClientError.Create ( _([self],anArgs), anEGUID );
end;
class procedure THTTPPackageClientThread._raise (anArgs: array of const;
anEGUID: array of const);
begin
raise EHTTPClientError.Create ( _([self],anArgs), anEGUID );
end;
constructor THTTPPackageClientThread.Create (anArgs: array of const);
var
I : Integer;
args : array_of_const;
OBJ : TObject;
begin
try
{ ïåðåäàåì ïàðàìåòðû ñîçäàíèÿ áàçîâîãî ïîòîêà,
íà÷èíàÿ ñ äâåíàäöàòîãî àðãóìåíòà }
if ( High (anArgs) >= 11 ) then
begin
SetLength ( Args, High (anArgs)-11 +1 );
for I := 11 to High (anArgs) do
args [I-11] := anArgs [I];
end
else
args := _array_of_const ([]);
inherited Create (args);
{ ÿâíî óêàçûâàåì íà íåîáõîäèìîñòü óíè÷òîæåíèÿ ïîòîêà ïî îêîí÷àíèþ ðàáîòû }
FreeOnTerminate := TRUE;
{ ïåðâûé ïàðàìåòð - ôàéë ÁÄ }
f_DBFileName := '';
f_DB := NIL;
if notEmpty (0,anArgs) then
begin
f_DBFileName := toString (anArgs [0]);
f_DB := TSQLiteDatabase.Create (f_DBFileName);
end;
if ( not Assigned (f_DB) ) then
raise Exception.Create (ERR_THTTPPACKAGECLIENTTHREAD_INCORRECT_DATABASE);
{ âòîðîé ïàðàìåòð - ReceiverID
èäåíòèôèêàòîð ïîëó÷àòåëÿ }
f_ReceiverID := 0;
if notEmpty (1,anArgs) then
begin
f_ReceiverID := toInt64 (anArgs [1]);
end;
if not ( ReceiverID > 0 ) then
raise Exception.Create (ERR_THTTPPACKAGECLIENTTHREAD_INVALID_RECEIVER_ID);
f_Receiver := TUser.Load (DB,ReceiverID,[objSimple]) as TUser;
if not Assigned (Receiver) then
raise Exception.Create (ERR_THTTPPACKAGECLIENTTHREAD_INVALID_RECEIVER);
{ òðåòèé ïàðàìåòð - ListView
ëîã ïî÷òû }
f_ListView := NIL;
if notEmpty (2,anArgs) then
begin
OBJ := toObject (anArgs [2]);
if ( Assigned (OBJ) and OBJ.InheritsFrom (TsListView) ) then
f_ListView := TsListView (OBJ);
end;
{ ÷åòâåðòûé ïàðàìåòð - Gauge
ïîêàçàòåëü ïðîãðåññà çàãðóçêè }
f_Gauge := NIL;
if notEmpty (3,anArgs) then
begin
OBJ := toObject (anArgs [3]);
if ( Assigned (OBJ) and OBJ.InheritsFrom (TControl) ) then
f_Gauge := TGauge (OBJ);
end;
{ ïÿòûé ïàðàìåòð - StatusPanel
ïîêàçàòåëü ïðîãðåññà çàãðóçêè }
f_StatusPanel := NIL;
if notEmpty (4,anArgs) then
begin
OBJ := toObject (anArgs [4]);
if ( Assigned (OBJ) and OBJ.InheritsFrom (TStatusPanel) ) then
f_StatusPanel := TStatusPanel (OBJ);
end;
{ øåñòîé ïàðàìåòð - âðåìÿ îæèäàíèÿ ms }
f_TimeOut := 120000;
if notEmpty (5,anArgs) then
begin
f_TimeOut := toInteger (anArgs [5]);
end;
if ( TimeOut = 0 ) then
raise Exception.Create (ERR_THTTPPACKAGECLIENTTHREAD_INVALID_TIMEOUT);
{ ñåäüìîé ïàðàìåòð - õîñò proxy-ñåðâåðà }
f_ProxyHost := '';
if notEmpty (6,anArgs) then
begin
f_ProxyHost := toString (anArgs [6]);
end;
if ( ProxyHost <> '' ) and not isIPAddress (ProxyHost) then
raise Exception.Create (ERR_THTTPPACKAGECLIENTTHREAD_INVALID_PROXY_HOST);
{ âîñüìîé ïàðàìåòð - ïîðò proxy-ñåðâåðà }
f_ProxyPort := 0;
if notEmpty (7,anArgs) then
begin
f_ProxyPort := toInteger (anArgs [7]);
end;
if ( ProxyPort <= 0 ) and ( ProxyHost <> '' ) then
raise Exception.Create (ERR_THTTPPACKAGECLIENTTHREAD_INVALID_PROXY_PORT);
{ äåâÿòûé ïàðàìåòð - ëîãèí proxy-ñåðâåðà }
f_ProxyLogin := '';
if notEmpty (8,anArgs) then
begin
f_ProxyLogin := toString (anArgs [8]);
end;
{ äåñÿòûé ïàðàìåòð - ïàðîëü proxy-ñåðâåðà }
f_ProxyPassword := '';
if notEmpty (9,anArgs) then
begin
f_ProxyPassword := toString (anArgs [9]);
end;
{ îäèííàäöàòûé ïàðàìåòð - ïðîòîêîë proxy-ñåðâåðà }
f_ProxyProtocol := '';
if notEmpty (10,anArgs) then
begin
f_ProxyProtocol := toString (anArgs [10]);
end;
{ ñïèñîê ïàêåòîâ }
f_Packages := NIL;
f_PackageIndex := -1;
{ êëèåíò }
f_HTTP := THTTPClient.Create ([ Receiver.IP, Receiver.Port,
ProxyHost, ProxyPort,
ProxyLogin, ProxyPassword,
ProxyProtocol,
TimeOut,
'POST', '/',
'Application/hex-stream' ]);
{ âðåìÿ îæèäàíèÿ }
f_IdleTime := -1;
{ ëîã }
f_Log := TItems.Create ([]);
except on E: Exception do
_raise ([ 'Create', ERR_THTTPPACKAGECLIENTTHREAD_CREATE, E, Exception (FatalException) ],
['{FF55D018-CCE7-4B20-8BF1-710450D118AE}']);
end;
end;
destructor THTTPPackageClientThread.Destroy;
begin
try
_FillChar ( f_ProxyHost, Length (f_ProxyHost), $00 );
f_ProxyPort := 0;
_FillChar ( f_ProxyLogin, Length (f_ProxyLogin), $00 );
_FillChar ( f_ProxyPassword, Length (f_ProxyPassword), $00 );
FreeAndNil (f_Log);
FreeAndNil (f_HTTP);
if ( ReceiverID <> USER_ID ) and ( ReceiverID <> 0 ) then
FreeAndNil (f_Receiver);
FreeAndNil (f_Packages);
FreeAndNil (f_DB);
inherited Destroy;
except on E: Exception do
_raise ([ 'Destroy', ERR_THTTPPACKAGECLIENTTHREAD_DESTROY, E, Exception (FatalException) ],
['{4545AECC-0B18-4123-B1F0-683F7FDB0335}']);
end;
end;
procedure THTTPPackageClientThread.Main;
var
Stream : TStream;
KeyHash : Hex;
IDStatus : TID;
begin
try
if Terminated then Exit;
inherited Main;
if not Assigned (Packages) then
try
f_Packages := TPackages.Load (DB,[ _([]),
_([{USER_KEY_HASH}]),
_([Receiver.KeyHash]),
_([USER_ID]),
_([]),
_([PACKAGE_CREATED_STATUS_ID]),
_([ TUser, {TPic,} TMessage, TCategorie, TMetaObject ]) ],
[ _pck_time_stamp_create ]) as TPackages;
MaxProgress := Packages.Count;
Progress := 0;
except
FreeAndNil (f_Packages);
end;
// äåëàåì çäåñü íàìåðåííî çàäåðæêó, ÷òîáû íå ïåðåãðóæàòü ñåòü
// ïðîñòî âîçâðàùàåì óïðàâëåíèå â îñíîâíîé ïîòîê
Sleep (1);
if not Assigned (Packages) or ( Packages.Count <= 0 ) then
Terminate
else if ( PackageIndex >= Packages.Count-1 ) then
Terminate
else
try
{ îæèäàåì }
IdleTime := IdleTime +1;
if ( IdleTime >= TimeOut div 10 ) then
IdleTime := -1
else
Exit;
{ ïîñëå îæèäàíèÿ - îáðàáàòûâàåì ñëåäóþùèé ïàêåò }
PackageIndex := PackageIndex + 1;
{ ïðîãðåññ }
Progress := Progress + 1;
{ çàïèñûâàåì ïàêåò }
if Assigned (Packages.ItemAt [PackageIndex]) then
begin
HTTP.Clear;
Stream := TMemoryStream.Create;
try
Packages.ItemAt [PackageIndex].SaveToStream (Stream);
HTTP.InputData.Position := 0;
WriteStreamL (HTTP.InputData,Stream);
finally
FreeAndNil (Stream);
end;
{ îòïðàâëÿåì ïàêåò }
if HTTP.Send then
begin
{ ÷èòàåì îòâåò ñåðâåðà }
with HTTP.OutputData, HTTP do
begin
Position := 0;
while ( Position < Size ) do
try
{ ÷èòàåì ñòàòóñ ïàêåòà, êîòîðûé ïðèñâîèë åìó ïîëó÷àòåëü }
KeyHash := ReadStrL (OutputData);
IDStatus := StrToInt ( HexToStr ( ReadStrL (OutputData) ) );
if ( Packages.ItemAt [PackageIndex].KeyHash = KeyHash ) then
begin
case IDStatus of
{ ìåíÿåì ñòàòóñ ïàêåòà, åñëè îí ïîëó÷åí èëè îáðàáîòàí, íà "îòïðàâëåííûé" }
PACKAGE_RECEIVED_STATUS_ID,
PACKAGE_EXECUTED_STATUS_ID : begin
Packages.ItemAt [PackageIndex].IDStatus := PACKAGE_SENDED_STATUS_ID;
Packages.ItemAt [PackageIndex].Save;
end;
{ èíà÷å - ìåíÿåì ñòàòóñ ïàêåòà íà "îòâåðãíóòûé" }
else begin
// ïðîñòî óäàëÿåì îòïðàâëåííûé ïàêåò
Packages.ItemAt [PackageIndex].Delete;
//Packages.ItemAt [PackageIndex].IDStatus := PACKAGE_REJECTED_STATUS_ID;
//Packages.ItemAt [PackageIndex].Save;
end;
end;
{ ïèøåì â ëîã ñòàòóñ ïàêåòà }
case IDStatus of
PACKAGE_RECEIVED_STATUS_ID :
WriteLog ( Format ('%s:%d',
[ HTTP.Sock.GetLocalSinIP,
HTTP.Sock.GetLocalSinPort ]),
Format ('%s:%d',
[ HTTP.Sock.GetRemoteSinIP,
HTTP.Sock.GetRemoteSinPort ]),
Format (MSG_THTTPPACKAGE_RECEIVED_PACKAGE,
[ KeyHash ]),
pckSend,
Packages.ItemAt [PackageIndex].PckType.IDExternal,
Packages.ItemAt [PackageIndex].MetaClass.GetClassID );
PACKAGE_EXECUTED_STATUS_ID :
WriteLog ( Format ('%s:%d',
[ HTTP.Sock.GetLocalSinIP,
HTTP.Sock.GetLocalSinPort ]),
Format ('%s:%d',
[ HTTP.Sock.GetRemoteSinIP,
HTTP.Sock.GetRemoteSinPort ]),
Format (MSG_THTTPPACKAGE_EXECUTED_PACKAGE,
[ KeyHash ]),
pckFull,
Packages.ItemAt [PackageIndex].PckType.IDExternal,
Packages.ItemAt [PackageIndex].MetaClass.GetClassID );
PACKAGE_REJECTED_STATUS_ID :
WriteLog ( Format ('%s:%d',
[ HTTP.Sock.GetLocalSinIP,
HTTP.Sock.GetLocalSinPort ]),
Format ('%s:%d',
[ HTTP.Sock.GetRemoteSinIP,
HTTP.Sock.GetRemoteSinPort ]),
Format (MSG_THTTPPACKAGE_REJECTED_PACKAGE,
[ KeyHash ]),
pckError,
Packages.ItemAt [PackageIndex].PckType.IDExternal,
Packages.ItemAt [PackageIndex].MetaClass.GetClassID );
else
WriteLog ( Format ('%s:%d',
[ HTTP.Sock.GetLocalSinIP,
HTTP.Sock.GetLocalSinPort ]),
Format ('%s:%d',
[ HTTP.Sock.GetRemoteSinIP,
HTTP.Sock.GetRemoteSinPort ]),
Format (MSG_THTTPPACKAGE_SENDED_PACKAGE,
[ KeyHash ]),
pckSend,
Packages.ItemAt [PackageIndex].PckType.IDExternal,
Packages.ItemAt [PackageIndex].MetaClass.GetClassID );
end;
end
else
raise Exception.CreateFmt (ERR_TPACKAGE_NOT_FOUND_HASH,[KeyHash]);
except on E: Exception do
{ ïèøåì â ëîã ïðè÷èíó îøèáêè }
WriteLog ( Format ('%s:%d',
[ HTTP.Sock.GetLocalSinIP,
HTTP.Sock.GetLocalSinPort ]),
Format ('%s:%d',
[ HTTP.Sock.GetRemoteSinIP,
HTTP.Sock.GetRemoteSinPort ]),
Format ('%s : %s',
[ ERR_THTTPPACKAGECLIENTTHREAD_INVALID_DATA,
E.Message ]),
pckError );
end;
end;
end
else
WriteLog ( Format ('%s:%d',
[ HTTP.HTTP.Sock.GetLocalSinIP,
HTTP.HTTP.Sock.GetLocalSinPort ]),
Format ('%s:%d',
[ HTTP.HTTP.Sock.GetRemoteSinIP,
HTTP.HTTP.Sock.GetRemoteSinPort ]),
Format (MSG_THTTPPACKAGE_SEND_ERROR,
[ HTTP.HTTP.ResultCode ]),
pckError );
end;
except on Error: Exception do
WriteStatus (Error.Message);
end;
except on E: Exception do
_raise ([ 'Main', ERR_THTTPPACKAGECLIENTTHREAD_MAIN, E, Exception (FatalException) ],
['{180FCCAC-75A3-47D4-BA25-CEA16ECB0B96}']);
end;
end;
procedure THTTPPackageClientThread.Return;
var
Itm : TListItem;
ImageIndex : Integer;
begin
try
inherited Return;
if Assigned (ListView) and Assigned (Log) then
begin
while Log.Count > 0 do
begin
Itm := ListView.Items.Add;
Itm.Caption := PLogRecord (Log.Item [0])^.Caption;
Itm.ImageIndex := PLogRecord (Log.Item [0])^.ImageIndex;
Itm.SubItems.Add ( PLogRecord (Log.Item [0])^.Sender );
Itm.SubItems.Add ( PLogRecord (Log.Item [0])^.Receiver );
Itm.SubItems.Add ( PLogRecord (Log.Item [0])^.Message );
Dispose ( PLogRecord (Log.Item [0]) );
Log.Delete (0);
end;
end;
if Assigned (StatusPanel) and notEmpty (Status) then
begin
StatusPanel.Text := Status;
end;
if Assigned (Gauge) then
begin
Gauge.MinValue := 0;
Gauge.MaxValue := MaxProgress;
Gauge.Progress := Progress;
end;
ProcessMessages;
except on E: Exception do
_raise ([ 'Return', ERR_THTTPPACKAGECLIENTTHREAD_RETURN, E, Exception (FatalException) ],
['{C48FD3B5-8272-4396-9DC5-0E5CA897F2C3}']);
end;
end;
procedure THTTPPackageClientThread.WriteStatus (const aMessage: String);
begin
try
Status := aMessage;
except on E: Exception do
_raise (['WriteStatus',ERR_THTTPPACKAGECLIENTTHREAD_WRITE_STATUS,E],
['{68F05474-C488-443C-B50B-EB60732C56D3}']);
end;
end;
procedure THTTPPackageClientThread.WriteStatus (const aMessage: String;
aParams: array of const);
begin
try
WriteStatus ( Format (aMessage,aParams) );
except on E: Exception do
_raise (['WriteStatus',ERR_THTTPPACKAGECLIENTTHREAD_WRITE_STATUS,E],
['{B766E5C6-1371-4D40-83C4-B684541C6878}']);
end;
end;
procedure THTTPPackageClientThread.WriteLog (const aSender: String;
const aReceiver: String;
const aMessage: String;
const anImageIndex: Integer = pckDefault;
const aPackageType: String = '';
const aMetaClassID: String = '');
var
Rec : PLogRecord;
begin
try
if Assigned (Log) then
begin
Rec := AllocMem ( SizeOf (TLogRecord) + 1 );
Rec^.Caption := _DateTimeToStr (now);
Rec^.ImageIndex := anImageIndex;
Rec^.Sender := aSender;
Rec^.Receiver := aReceiver;
if notEmpty (aMetaClassID) then
Rec^.Message := Format ('%s : %s : %s',[aMetaClassID,aPackageType,aMessage])
else
Rec^.Message := aMessage;
Log.Add (Rec);
end;
except on E: Exception do
_raise (['WriteLog',ERR_THTTPPACKAGECLIENTTHREAD_WRITE_LOG,E],
['{6205E9B7-E16B-4034-9580-F5AAAAD48A62}']);
end;
end;
procedure THTTPPackageClientThread.WriteLog (const aSender: String;
const aReceiver: String;
const aMessage: String;
aParams: array of const;
const anImageIndex: Integer = pckDefault;
const aPackageType: String = '';
const aMetaClassID: String = '');
begin
try
WriteLog ( aSender, aReceiver, Format (aMessage,aParams), anImageIndex, aPackageType, aMetaClassID );
except on E: Exception do
_raise (['WriteLog',ERR_THTTPPACKAGECLIENTTHREAD_WRITE_LOG,E],
['{BBC5AE04-4D9B-403F-AD4A-E37D99C5BC57}']);
end;
end;
end.