-
Notifications
You must be signed in to change notification settings - Fork 2
/
COMLNK.PAS
2018 lines (1694 loc) · 47.6 KB
/
COMLNK.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
{/////////////////////////////////////////////////////////////////////////
//
// Dos Navigator Version 1.51 Copyright (C) 1991-99 RIT Research Labs
//
// This programs is free for commercial and non-commercial use as long as
// the following conditions are aheared to.
//
// Copyright remains RIT Research Labs, and as such any Copyright notices
// in the code are not to be removed. If this package is used in a
// product, RIT Research Labs should be given attribution as the RIT Research
// Labs of the parts of the library used. This can be in the form of a textual
// message at program startup or in documentation (online or textual)
// provided with the package.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. All advertising materials mentioning features or use of this software
// must display the following acknowledgement:
// "Based on Dos Navigator by RIT Research Labs."
//
// THIS SOFTWARE IS PROVIDED BY RIT RESEARCH LABS "AS IS" AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The licence and distribution terms for any publically available
// version or derivative of this code cannot be changed. i.e. this code
// cannot simply be copied and put under another distribution licence
// (including the GNU Public Licence).
//
//////////////////////////////////////////////////////////////////////////}
unit ComLnk;
{$DEFINE CL_PACK}
{$I LINK.INC} interface
{$A+}
type
TCL_BlockType = Byte;
const
b_Nothing = $FF;
bt_DataZero = 0;
bt_Data = 1;
{$IFDEF CL_PACK}
bt_DataLZW = 2;
{$ENDIF}
bt_Spec = 3;
bt_SpcBits = 2;
bt_TypMask = (1 shl bt_SpcBits)-1;
bt_NotSpc = not bt_TypMask;
{*** bt_DataZero ***}
b_GetDrvs = 0 shl bt_SpcBits + bt_DataZero;
{*** bt_DataUnp ***}
b_Drives = 1 shl bt_SpcBits + bt_Data;
b_GetDir = 2 shl bt_SpcBits + bt_Data;
b_GetDirNfo = 3 shl bt_SpcBits + bt_Data;
b_Dir = 4 shl bt_SpcBits + bt_Data;
b_DirNfo = 5 shl bt_SpcBits + bt_Data;
b_GetFile = 6 shl bt_SpcBits + bt_Data;
b_dFileBlk = 7 shl bt_SpcBits + bt_Data;
b_dFileNfo = 8 shl bt_SpcBits + bt_Data;
b_cFileBlk = 9 shl bt_SpcBits + bt_Data;
b_cFileNfo = 10 shl bt_SpcBits + bt_Data;
b_DirValid = 11 shl bt_SpcBits + bt_Data;
b_EraseFile = 12 shl bt_SpcBits + bt_Data;
b_EraseDir = 13 shl bt_SpcBits + bt_Data;
b_MkDir = 14 shl bt_SpcBits + bt_Data;
b_IOstatus = 15 shl bt_SpcBits + bt_Data;
{*** bt_Spec0 ***}
b_RemoteOK = 1 shl bt_SpcBits + bt_Spec;
b_SubBlkOK = 2 shl bt_SpcBits + bt_Spec;
b_Logon = 3 shl bt_SpcBits + bt_Spec;
b_LogonOK = 4 shl bt_SpcBits + bt_Spec;
b_ResendFull = 5 shl bt_SpcBits + bt_Spec;
b_SRej0 = 6 shl bt_SpcBits + bt_Spec;
b_NotifyLast = 7 shl bt_SpcBits + bt_Spec;
function CL_InitComLink(AMaxBuf: LongInt; AMaxBlockSize, AStartBlockSize: Word): Boolean;
procedure CL_DoneComLink;
function CL_QueueFree(BufSz: LongInt; Delta: Integer): Boolean;
procedure CL_Clear;
function CL_OK: Boolean;
function CL_InputBlock(var AP: Pointer; var ASize: Word): TCL_BlockType;
procedure CL_PostBlock(P: Pointer; A_Size: Word; ATyp: TCL_BlockType);
function CL_GetBufPtr: Pointer;
procedure CL_FillNfo(var ANumErrs, ABlkSz: Word; var ATotalTrs: LongInt);
{$IFDEF CL_LOG}
procedure CL_WLog(const s: string);
procedure CL_Statux;
var CL_LogF : File;
{$ENDIF}
function DropCD: Boolean;
implementation uses Objects,Dos,
xTime, Advance,
ComLnkIO
;
const
CL_MaxRemoteOK = 4;
function SStr(L: LongInt): string; var s: string; begin Str(L, s); SStr := s end;
{$IFDEF CL_LOG}
procedure WLog(s: string);
begin
s := 'btc'+ItoS(BiosTics^)+' '+s+#13#10; InOutRes := 0;
{$IFDEF HARD_LOG}
FileMode := 1;Reset(Cl_LogF, 1); if IOResult>0 then Exit;
Seek(Cl_LogF, FileSize(Cl_LogF)); if IOResult>0 then Exit;
{$ENDIF}
BlockWrite(CL_LogF, s[1], Length(s));
{$IFDEF HARD_LOG}
Close(CL_LogF);
{$ENDIF}
InOutRes := 0;
end;
procedure CL_WLog(const s: string); begin WLog(s) end;
{$ENDIF}
procedure Halt_;
begin
{$IFDEF CL_LOG}
WLog('!!!! H A L T !!!!');
Close(CL_LogF);
{$ENDIF}
RunError(221);
end;
const
cDle = 16;
cXon = 17;
cXoff = 19;
cBlkA = ord('l');
cBlkB = ord('i');
cBlkC = ord('n');
cBlkD = ord('k');
CBlkPrefix = cBlkA shl (8*0) +
cBlkB shl (8*1) +
cBlkC shl (8*2) +
cBlkD shl (8*3) ;
MinBlockSize = 256;
SuccessStep = 8;
type
TArr8 = array[0..7] of Byte;
TCollectState = (
stUnknown,
stSeekPfx,
stCollCRC,
stCollHdr,
stCollBlk
);
TransBlockPrefix = packed record
Prefix : LongInt;
CRC32 : TArr8;
end;
PTransBlockHeader = ^TTransBlockHeader;
TTransBlockHeader = packed record { Transmition Block Header }
ChkSum : Byte; {Must be the first}
Typ : TCL_BlockType;
case Byte of
0 : ( Id : LongInt;
TotSize : Word;
SubSize : Word;
SubOfs : Word);
1 : ( LastSnt : LongInt; {!!! The same as Id }
FirstSnt : LongInt; {!!! The same as TotSize & SubSize }
Rsrvd1 : Word); {!!! The same as SubOfs }
2 : ( RqBegin : LongInt; {!!! The same as Id }
RqEnd : LongInt; {!!! The same as TotSize & SubSize }
Rsrvd2 : Word); {!!! The same as SubOfs }
end;
PTransBlock = ^TTransBlock;
TTransBlock = packed record { Outbound Block Header }
Hdr : TTransBlockHeader;
Buf : array[0..0] of Byte;
end;
const
TransBlkHdrSz = SizeOf(TTransBlockHeader);
type
PSortedCL_Coll = ^TSortedCL_Coll;
TSortedCL_Coll = object(TSortedCollection)
constructor Init;
procedure FreeItem(Item: Pointer); virtual;
function Compare(Key1, Key2: Pointer): Integer; virtual;
function Volume: LongInt;
end;
TComState = (csLogon, csBegin, csOK);
TZeroInitData = record
TotTrs : LongInt;
NumErrs : Word;
Last4 : LongInt;
CId : LongInt;
LastSntRj : TTransBlockHeader;
LastSntBl : TTransBlockHeader;
LastRcvBl : TTransBlockHeader;
NeedClrId : LongInt;
CollPos : Word;
CollCRC : TArr8;
RemoteOK : Byte;
APIok : Boolean;
SentAllTm : TCL_Timer;
end;
PLinkData = ^TLinkData;
TLinkData = record
Port : Pointer;
OutSpec : PSortedCL_Coll;
OutQueue : PSortedCL_Coll;
OutSent : PSortedCL_Coll;
InDirty : PSortedCL_Coll;
InClean : PSortedCL_Coll;
InSpec : PSortedCL_Coll;
z : TZeroInitData;
CollState : TCollectState;
CollBlkAm : Word;
TmpBufAm : Word;
TmpBuf : PByteArray;
ComState : TComState;
DelBlkSz : Word;
DelBlkPtr : Pointer;
CharDLE : Boolean;
NeedDLE : Boolean;
MaxBuf : LongInt;
InCRC : LongInt;
InSize : Word;
CollBlk : PTransBlock;
SubBlkSz : Word;
MxOBlkSz : Word;
RcvBufSz : Word;
SuccessNm : Byte;
EscTbl : array[byte] of Boolean;
end;
const
TransPrfxSz = SizeOf(TransBlockPrefix);
SafeOutKeep = $100;
unk = $FFFF;
var
Lnk : PLinkData;
function DropCD: Boolean;
begin
DropCD := CL_DCD(Lnk^.Port) = False;
end;
function CL_GetBufPtr: Pointer;
begin
CL_GetBufPtr := Lnk^.TmpBuf;
end;
function CL_OK; begin Lnk^.z.APIok := Lnk^.ComState = csOK; CL_OK := Lnk^.z.APIok end;
procedure ClearRemoteOK;
begin with Lnk^.z do if RemoteOK<>0 then begin
RemoteOK := 0;
CL_ClearTimer(SentAllTm);
end end;
function BufdOut: LongInt;
begin
with Lnk^ do
BufdOut := OutSpec^.Volume +
OutQueue^.Volume +
OutSent^.Volume +
InDirty^.Volume +
InClean^.Volume +
InSpec^.Volume;
end;
function DeltaSent: LongInt;
var
MinId: LongInt;
procedure CalcMinId(P: PTransBlock); far;
begin with P^.Hdr do begin
if (Typ and bt_TypMask) <> bt_Spec then if MinId > Id then MinId := Id;
end end;
begin
MinId := Lnk^.z.CId;
Lnk^.OutQueue^.ForEach(@CalcMinId);
Lnk^.OutSent^.ForEach(@CalcMinId);
DeltaSent := Lnk^.z.CId - MinId;
end;
{$IFDEF CL_LOG}
function ACSStr(ATyp: TComState): string;
begin
case ATyp of
csLogon : ACSStr := 'csLogon';
csBegin : ACSStr := 'csBegin';
csOK : ACSStr := 'csOK';
else ACSStr := '!!!== unknown TComState';
end
end;
function ATypStr(ATyp: TCL_BlockType): string;
{$IFDEF CL_PACK}
var
LZW: Boolean;
{$ENDIF}
begin
{$IFDEF CL_PACK}
LZW := (ATyp and bt_TypMask) = bt_DataLZW;
if LZW then ATyp := (ATyp and bt_NotSpc) or bt_Data;
{$ENDIF}
case ATyp of
b_IOstatus : ATypStr := 'b_IOstatus';
b_Nothing : ATypStr := 'b_Nothing';
b_GetDir : ATypStr := 'b_GetDir';
b_Drives : ATypStr := 'b_Drives';
b_Dir : ATypStr := 'b_Dir';
b_GetDirNfo : ATypStr := 'b_GetDirNfo';
b_cFileBlk : ATypStr := 'b_cFileBlk';
b_cFileNfo : ATypStr := 'b_cFileNfo';
b_dFileBlk : ATypStr := 'b_dFileBlk';
b_dFileNfo : ATypStr := 'b_dFileNfo';
b_GetFile : ATypStr := 'b_GetFile';
b_DirNfo : ATypStr := 'b_DirNfo';
b_GetDrvs : ATypStr := 'b_GetDrvs';
b_RemoteOK : ATypStr := 'b_RemoteOK';
b_SubBlkOK : ATypStr := 'b_SubBlkOK';
b_ResendFull: ATypStr := 'b_ResendFull';
b_SRej0 : ATypStr := 'b_SRej0';
b_NotifyLast: ATypStr := 'b_NotifyLast';
b_Logon : ATypStr := 'b_Logon';
b_LogonOK : ATypStr := 'b_LogonOK';
else ATypStr := '!!!== unknown TCL_BlockType (' + SStr(ATyp)+')';
end
end;
procedure WrHdrNfo(const Pfx: string; const H: TTransBlockHeader);
begin
with H do WLog(Pfx+' Typ = '+ATypStr(Typ)+', Id = '+SStr(Id)+
', TS = '+ SStr(TotSize)+
', SO = '+ SStr(SubOfs)+
', SS = '+ SStr(SubSize)
);
end;
procedure ShowColl(const s: string; P: PCollection);
procedure ShowIt(P: PTransBlock); far; begin WrHdrNfo(s, P^.Hdr) end;
begin
P^.ForEach(@ShowIt);
end;
procedure CL_Statux;
begin
with Lnk^ do
WLog(' MA='+SStr(MemAvail)+', BufdO='+SStr(BufdOut)+
', SQc='+SStr(OutSpec^.Count)+
', OQc='+SStr(OutQueue^.Count)+
', OSc='+SStr(OutSent^.Count)+
', DIc='+SStr(InDirty^.Count)+
', CIc='+SStr(InClean^.Count)+
', DSn='+SStr(DeltaSent)+
', zCS='+ACSStr(ComState)
);
ShowColl(' *OutSent ', Lnk^.OutSent);
ShowColl(' *InDirty ', Lnk^.InDirty);
end;
{$ENDIF}
{$IFDEF CL_PACK}
var
LZWbuf : Pointer;
le76, le77 : byte;
InputOffs, InputSeg, OutPutOffs, OutPutSeg, Temp_offs, Temp_seg,
le6A, le6C, le6E, le70, le72, le74, le78,
le7A_0, le7A_2, le7A_4, le7A_6,
le82a, le82b, OutPutOffs2 : word;
CodeTable, PrefixTable, SymbolTable, CodeBuffer: LongInt;
MaxCode, RunCode, RunningBits,
CurCode, OldCode, CurBuffShift,
TempSeg, Temp, DataSize: Word;
procedure LZWCompr; external; {$L lzwc2}
procedure LZWDecompr; external; {$L lzwd2}
function LZW_Decompress(var Source,Dest): word; assembler;
asm
mov ax, word ptr LZWbuf[0]
mov bx, word ptr LZWbuf[2]
mov cx, word ptr Source[0]
mov dx, word ptr Source[2]
mov si, word ptr Dest[0]
mov di, word ptr Dest[2]
call LZWDecompr
end;
function LZW_Compress(var Source,Dest; Size:word): word; assembler;
asm
mov ax, Size
mov DataSize, ax
mov ax, word ptr LZWbuf[0]
mov bx, word ptr LZWbuf[2]
mov cx, word ptr Source[0]
mov dx, word ptr Source[2]
mov si, word ptr Dest[0]
mov di, word ptr Dest[2]
call LZWCompr
end;
const
LZWCprBufSize = 22*1024;
LZWDcprBufSize = 18*1024;
{$ENDIF}
procedure InsertOutQ(P: PTransBlock);
begin
CL_ClearTimer(Lnk^.z.SentAllTm);
Lnk^.OutQueue^.Insert(P);
{$IFDEF CL_LOG}
WrHdrNfo('@ InsOut',P^.Hdr);
{$ENDIF}
end;
procedure WaitNewBlk;
begin
with Lnk^ do begin CollState := stSeekPfx; NeedDLE := False end;
end;
procedure ClearAll;
begin with Lnk^ do begin
FillChar(z, SizeOf(z), 0);
WaitNewBlk;
z.LastSntBl.Id := -1;
z.LastRcvBl.Id := -1;
InDirty ^. FreeAll;
InClean ^. FreeAll;
InSpec ^. FreeAll;
OutQueue ^. FreeAll;
OutSent ^. FreeAll;
end end;
procedure CL_Clear;
begin
ClearAll;
Lnk^.ComState := csLogon;
end;
function DataBufSz(const Hdr: TTransBlockHeader): LongInt;
begin
case (Hdr.Typ and bt_TypMask) of
bt_DataZero,
bt_Spec : DataBufSz := 0;
bt_Data
{$IFDEF CL_PACK}
,bt_DataLZW
{$ENDIF}
: DataBufSz := Hdr.SubSize;
else Halt_;
end;
end;
procedure Free_Mem(var AP; Size: Word);
var
P: Pointer absolute AP;
begin
FreeMem(P, Size); P := nil;
end;
procedure Get_Mem(var AP; Size: Word);
var
P: Pointer absolute AP;
begin
GetMem(P, Size);
end;
procedure FreeTrans(P: Pointer);
var
A : PTransBlock absolute P;
S : Word;
begin
S := DataBufSz(A^.Hdr);
Free_Mem(A, S + TransBlkHdrSz);
end;
function NewTrans(S: Word; AHdr: PTransBlockHeader): Pointer;
var
P: PTransBlock;
begin
Get_Mem(P, S+TransBlkHdrSz);
with P^ do
begin
if AHdr<>nil then Hdr := AHdr^ else FillChar(Hdr, TransBlkHdrSz, 0);
Hdr.SubSize := S;
end;
NewTrans := P;
end;
function SpecExist(AType: TCL_BlockType): Boolean;
function IsThis(P: PTransBlock): Boolean; far;
begin
IsThis := P^.Hdr.Typ = AType;
end;
begin
SpecExist := Lnk^.OutSpec^.FirstThat(@IsThis) <> nil;
end;
procedure InsertSpecQ(P: PTransBlock);
var cc: Word;
begin with Lnk^.OutSpec^ do begin
cc := Count; Insert(P);
if cc = Count then FreeTrans(P);
end end;
procedure GlobalNotify(AId: LongInt; ATyp: TCL_BlockType);
var
P: PTransBlock;
begin
P := NewTrans(0, nil);
with P^.Hdr do
begin
Id := AId;
Typ := ATyp;
end;
InsertSpecQ(P);
end;
function AllocBlk(const Blk: TTransBlock): PTransBlock;
var
S: Word;
N: PTransBlock;
begin
S := DataBufSz(Blk.Hdr);
N := NewTrans(S, @Blk.Hdr); N^.Hdr.SubSize := Blk.Hdr.SubSize;
if S>0 then Move(Blk.Buf, N^.Buf, S);
AllocBlk := N;
end;
procedure LocInsSpec(P: PTransBlock);
begin
if Lnk^.ComState <> csLogon then Lnk^.InSpec^.Insert(AllocBlk(P^));
end;
procedure InsertInSpec(P: PTransBlock);
var
H: TTransBlockHeader;
begin
H := P^.Hdr;
case H.Typ of
b_Logon : begin
if Lnk^.ComState = csOK then
begin
ClearAll;
Lnk^.ComState := csBegin
end;
GlobalNotify(0, b_LogonOK)
end;
b_LogonOK : Lnk^.ComState := csBegin;
b_NotifyLast : begin
if Lnk^.ComState = csBegin then Lnk^.ComState := csOK;
LocInsSpec(P);
end;
else begin LocInsSpec(P) end;
end;
end;
var
CL_LastSgn: LongInt;
function Sgn(L: LongInt): LongInt;
begin
if L = 0 then CL_LastSgn := 0 else
if L > 0 then CL_LastSgn := +1 else
CL_LastSgn := -1;
Sgn := CL_LastSgn;
end;
constructor TSortedCL_Coll.Init;
begin
if not inherited Init(64,64) then Fail;
Duplicates := True;
end;
function TSortedCL_Coll.Volume: LongInt;
var V: LongInt; procedure AddVol(Blk: PTransBlock); far; begin Inc(V, DataBufSz(Blk^.Hdr)) end;
begin V := 0; ForEach(@AddVol); Volume := V; end;
function CmpTransHdr(const Hdr1, Hdr2: TTransBlockHeader): Integer;
var
AId: LongInt;
ASo: LongInt;
begin
with Hdr1 do begin AId := Id; ASo := SubOfs end;
with Hdr2 do
begin
if Sgn(AId-Id)<>0 then CmpTransHdr := CL_LastSgn else
if Sgn(ASo-SubOfs)<>0 then CmpTransHdr := CL_LastSgn else
begin CmpTransHdr := 0;
end;
end;
end;
function TSortedCL_Coll.Compare;
var
A : PTransBlock absolute Key1;
B : PTransBlock absolute Key2;
begin
Compare := CmpTransHdr(A^.Hdr, B^.Hdr);
end;
procedure TSortedCL_Coll.FreeItem;
begin FreeTrans(Item) end;
function ByteSum(const ABuf; Cnt: LongInt): Byte;
var
R: Byte;
I: Word;
Buf: TByteArray absolute ABuf;
begin
R := 0;
for I := 0 to Cnt-1 do Inc(R, Buf[I]);
ByteSum := R;
end;
function FlushOB: Boolean;
begin
with Lnk^ do begin
if DelBlkSz > 0 then
begin
if CL_OutBuffFree(Port) > DelBlkSz then
begin
CL_PutBlock(Port, DelBlkPtr^, DelBlkSz);
Free_Mem(DelBlkPtr, DelBlkSz);
DelBlkSz := 0; DelBlkPtr := nil;
FlushOB := True;
end else FlushOB := False;
end else FlushOB := True;
end;
end;
procedure PutBlock(const Buf; Size: Word);
var
a,b: LongInt;
ar: TByteArray absolute Buf;
begin
if not FlushOB then begin {$IFDEF CL_LOG}WLog('FlushOB Suxxxx');{$ENDIF} Halt_ end;
with Lnk^ do
begin
a := CL_OutBuffFree(Lnk^.Port);
if a>=Size then CL_PutBlock(Port, Buf, Size) else
begin
{$IFDEF CL_LOG}WLog('PutBlock-lazy!!!!');{$ENDIF}
b := Size - a;
CL_PutBlock(Port, Buf, a);
Get_Mem(DelBlkPtr, b);
Move(ar[a], DelBlkPtr^, b);
DelBlkSz := b;
end;
end;
end;
function OutB_Free: Word;
var
f,Result: LongInt;
begin
FlushOB;
if Lnk^.DelBlkSz > 0 then Result := 0 else
begin
f := CL_OutBuffFree(Lnk^.Port);
if f<=SafeOutKeep then Result := 0 else Result := f-SafeOutKeep;
end;
{$IFDEF CL_LOG}
WLog('OutB_Free = '+SStr(Result));
{$ENDIF}
OutB_Free := Result;
end;
function OutB_Used: Word;
begin
FlushOB; OutB_Used := CL_OutBuffUsed(Lnk^.Port) + Lnk^.DelBlkSz;;
end;
procedure PackLI(L: LongInt; var Arr: TArr8);
var
i: Byte;
begin
for i := 0 to 7 do
begin
Arr[7-i] := (L and $F)+$40;
L := L shr 4;
end;
end;
function UnpackLI(Arr: TArr8; var LL: LongInt): Boolean;
var
i: Byte;
L: LongInt;
b: Byte;
begin
UnpackLI := False;
L := 0;
for i := 0 to 7 do
begin
L := L shl 4;
b := Arr[i];
if (b < $40) or (b>$4F) then Exit;
Inc(L, b-$40);
end;
LL := L;
UnpackLI := True;
end;
function Calc_CRC(const Buffer; Len : Word; Crc32TableOfs : Word): LongInt; assembler;
asm
MOV CX,Len {CX = Len}
JCXZ @@ucDone {nothing to do if Len = 0}
LES SI,Buffer {DS:SI => Buffer}
XOR AX,AX
CWD
@@ucNext:
MOV BL,AL {BL = AL}
MOV AL,[ES:SI]
INC SI
XOR BL,AL {XOR BL with AL}
XOR BH,BH {use result as index into DWORD table}
SHL BX,1
SHL BX,1
MOV AL,AH {shift DX:AX left 8 bits}
MOV AH,DL
MOV DL,DH
XOR DH,DH
ADD BX,Crc32TableOfs
XOR AX,[BX] {XOR with CRC table entry}
XOR DX,[BX+2]
LOOP @@ucNext {repeat}
@@ucDone:
end;
function CalcCRC(const Buffer; Len : Word): LongInt;
begin
CalcCRC := Calc_CRC(Buffer, Len, CL_GetCRC32ofs);
end;
function CL_QueueFree;
begin
CL_QueueFree := (DeltaSent < Delta) and (Lnk^.ComState = csOK) and (BufdOut < BufSz);
end;
procedure SendSubBlk(var Blk: TTransBlock);
var
Pfx : TransBlockPrefix;
TS : Word;
I,O : Word;
B : Byte;
begin
TS := DataBufSz(Blk.Hdr)+TransBlkHdrSz;
{$IFDEF CL_LOG}
WrHdrNfo('+ Sent ',Blk.Hdr);
{$ENDIF}
with Blk do Hdr.ChkSum := ByteSum(Hdr.Typ, TransBlkHdrSz-SizeOf(Hdr.ChkSum));
PackLI(CalcCRC(Blk,TS),Pfx.CRC32);
Pfx.Prefix := CBlkPrefix;
Move(Pfx, Lnk^.TmpBuf^[0], TransPrfxSz);
O := TransPrfxSz;
for I := 0 to TS-1 do
begin
B := PByteArray(@Blk)^[I];
if Lnk^.EscTbl[B]
then begin Lnk^.TmpBuf^[O]:=cDle; Lnk^.TmpBuf^[O+1]:= B xor $40; Inc(O,2) end
else begin Lnk^.TmpBuf^[O]:=B; Inc(O) end;
end;
PutBlock(Lnk^.TmpBuf^, O);
end;
procedure SendDataBlk(var Blk: TTransBlock);
begin
if CmpTransHdr(Blk.Hdr, Lnk^.z.LastSntBl)>0 then Lnk^.z.LastSntBl := Blk.Hdr;
ClearRemoteOK;
SendSubBlk(Blk);
end;
procedure SendSpecBlk(var Blk: TTransBlock); begin SendSubBlk(Blk) end;
procedure StartSentAllTm; begin CL_NewTimer(Lnk^.z.SentAllTm, 5) end;
procedure NotifyState;
begin
with Lnk^ do
begin
if ComState = csLogon then GlobalNotify(0, b_Logon) else
begin
z.LastSntBl.Typ := b_NotifyLast;
SendSpecBlk(PTransBlock(Addr(z.LastSntBl))^);
end;
StartSentAllTm;
end;
end;
procedure FlushQueue;
var
CBS : Word;
OB : PTransBlock;
OBH : TTransBlockHeader;
NBO : PTransBlock;
NBL : PTransBlock;
NS : Word;
LS : Word;
SS : Word;
begin
repeat
if OutB_Free < 100 then Exit;
with Lnk^ do
begin
with OutSpec^ do
while Count>0 do
begin
SendSpecBlk(PTransBlock(At(0))^); { Send special block without buffering }
AtFree(0);
if OutB_Free < 100 then Exit;
end;
if OutQueue^.Count = 0 then
begin
if z.RemoteOK < CL_MaxRemoteOK then
begin
if z.RemoteOK <> 0 then Inc(z.RemoteOK);
end else Break;
if CL_TimerInstalled(z.SentAllTm) then
begin
if (CL_TimerExpired(z.SentAllTm)) and
(not SpecExist(b_Logon)) and
(not SpecExist(b_NotifyLast)) then NotifyState;
end else NotifyState;
Break;
end;
if OutSpec^.Count > 0 then Continue;
CBS := SubBlkSz;
end;
if OutB_Free < CBS then Break;
OB := Lnk^.OutQueue^.At(0);
OBH := OB^.Hdr;
if (OBH.Typ and bt_TypMask) = bt_Spec then
begin
{$IFDEF CL_LOG}
WrHdrNfo('!!! SpecBlock in DataOut Collection !!!', OBH);
{$ENDIF}
Halt_;
end else
begin
SS := DataBufSz(OBH);
if SS<=CBS then
begin
SendDataBlk(OB^); { Send last or single block with buffering }
Lnk^.OutSent^.Insert(OB);
Lnk^.OutQueue^.Delete(OB);
end else
begin
if SS>CBS then NS := CBS else NS := SS;
NBO := NewTrans(NS, @OBH);
Move(OB^.Buf, NBO^.Buf, NS);
LS := SS-NS;
NBL := NewTrans(LS, @OBH); with NBL^ do
begin
Inc(Hdr.SubOfs, NS);
Move(OB^.Buf[NS], Buf, LS);
end;
with Lnk^ do
begin
OutQueue^.Free(OB);
InsertOutQ(NBL);
SendDataBlk(NBO^); { Send last or single block with buffering }
Lnk^.OutSent^.Insert(NBO);
end;
end;
end;
until False;
end;
procedure Insss(P: Pointer; ASize: Word; ATyp: TCL_BlockType);
var
OB : PTransBlock;
begin
OB := NewTrans(ASize, nil);
if ASize > 0 then Move(P^, OB^.Buf, ASize);