This repository has been archived by the owner on Sep 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
uKcp.pas
1383 lines (1232 loc) · 36.3 KB
/
uKcp.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
//=====================================================================
//
// KCP - A Better ARQ Protocol Implementation
// skywind3000 (at) gmail.com, 2010-2011
// Delphi Code By Killeven (at) [email protected]
// Features:
// + Average RTT reduce 30% - 40% vs traditional ARQ like tcp.
// + Maximum RTT reduce three times vs tcp.
// + Lightweight, distributed as a single source file.
//=====================================================================
{.$DEFINE QPRINT}
unit uKcp;
interface
uses
uKcpDef, System.SysUtils;
{$INCLUDE 'kcpdef.inc'}
//---------------------------------------------------------------------
// interface
//---------------------------------------------------------------------
// create a new kcp control object, 'conv' must equal in two endpoint
// from the same connection. 'user' will be passed to the output callback
// output callback can be setup like this: 'kcp->output = my_udp_output'
function ikcp_create(conv: UInt32; user: Pointer): PKcpCb;
// release kcp control object
procedure ikcp_release(kcp: PkcpCb);
// set output callback, which will be invoked by kcp
procedure ikcp_setoutput(kcp: PkcpCb; output: TOutPut);
// user/upper level recv: returns size, returns below zero for EAGAIN
function ikcp_recv(kcp: PkcpCb; buffer: PUInt8; len: Int32): Int32;
// user/upper level send, returns below zero for error
function ikcp_send(kcp: PkcpCb; const buf: PUInt8; len: Int32): Int32;
// update state (call it repeatedly, every 10ms-100ms), or you can ask
// ikcp_check when to call it again (without ikcp_input/_send calling).
// 'current' - current timestamp in millisec.
procedure ikcp_update(kcp: PkcpCb; current: UInt32);
// Determine when should you invoke ikcp_update:
// returns when you should invoke ikcp_update in millisec, if there
// is no ikcp_input/_send calling. you can call ikcp_update in that
// time, instead of call update repeatly.
// Important to reduce unnacessary ikcp_update invoking. use it to
// schedule ikcp_update (eg. implementing an epoll-like mechanism,
// or optimize ikcp_update when handling massive kcp connections)
function ikcp_check(const kcp: PkcpCb; current: UInt32): UInt32;
// when you received a low level packet (eg. UDP packet), call it
function ikcp_input(kcp: PKcpCb; const buf: PUInt8; size: UInt32): Int32;
// flush pending data
procedure ikcp_flush(kcp: PkcpCb);
// check the size of next message in the recv queue
function ikcp_peeksize(const kcp: PkcpCb): Int32;
// change MTU size, default is 1400
function ikcp_setmtu(kcp: PKcpCb; mtu: Int32): Int32;
// set maximum window size: sndwnd=32, rcvwnd=32 by default
function ikcp_wndsize(kcp: PkcpCb; sndwnd: Int32; rcvwnd: Int32): Int32;
// get how many packet is waiting to be sent
function ikcp_waitsnd(const kcp: PKcpCb): Int32;
// fastest: ikcp_nodelay(kcp, 1, 20, 2, 1)
// nodelay: 0:disable(default), 1:enable
// interval: internal update timer interval in millisec, default is 100ms
// resend: 0:disable fast resend(default), 1:enable fast resend
// nc: 0:normal congestion control(default), 1:disable congestion control
function ikcp_nodelay(kcp: PkcpCb; nodelay: Int32; interval: Int32; resend: Int32; nc: Int32): Int32;
// read conv
function ikcp_getconv(const ptr: Pointer): UInt32;
// setup allocator
procedure ikcp_allocator(malloc: TMalloc; free: TFree);
procedure ikcp_log(kcp: PKcpCb; mask: Int32; const fmt: PTSTR; const param: array of const);
implementation
//=====================================================================
// KCP BASIC
//=====================================================================
const
IKCP_RTO_NDL = 30; // no delay min rto
IKCP_RTO_MIN = 100; // normal min rto
IKCP_RTO_DEF = 200;
IKCP_RTO_MAX = 60000;
IKCP_CMD_PUSH = 81; // cmd: push data
IKCP_CMD_ACK = 82; // cmd: ack
IKCP_CMD_WASK = 83; // cmd: window probe (ask)
IKCP_CMD_WINS = 84; // cmd: window size (tell)
IKCP_ASK_SEND = 1; // need to send IKCP_CMD_WASK
IKCP_ASK_TELL = 2; // need to send IKCP_CMD_WINS
IKCP_WND_SND = 32;
IKCP_WND_RCV = 32;
IKCP_MTU_DEF = 1400;
IKCP_ACK_FAST = 3;
IKCP_INTERVAL_ = 100;
IKCP_OVERHEAD = 24;
IKCP_DEADLINK = 20;
IKCP_THRESH_INIT = 2;
IKCP_THRESH_MIN = 2;
IKCP_PROBE_INIT = 7000; // 7 secs to probe window size
IKCP_PROBE_LIMIT = 120000; // up to 120 secs to probe window
// queue operation
procedure iqueue_init(p: PKcpSeg);
begin
p^.next := p;
p^.prev := p;
end;
function iqueue_is_empty(p: PKcpSeg): Boolean;
begin
Result := (p = p^.next);
end;
procedure iqueue_add(node: PKcpSeg; head: PKcpSeg);
begin
node^.prev := head;
node^.next := head^.next;
head^.next^.prev := node;
head^.next := node;
end;
procedure iqueue_add_tail(node: PKcpSeg; head: PKcpSeg);
begin
node^.prev := head^.prev;
node^.next := head;
head^.prev^.next := node;
head^.prev := node;
end;
procedure iqueue_del(p: PKcpSeg);
begin
p^.next^.prev := p^.prev;
p^.prev^.next := p^.next;
p^.next := nil;
p^.prev := nil;
end;
procedure iqueue_del_init(p: PKcpSeg);
begin
iqueue_del(p);
iqueue_init(p);
end;
function iqueue_entry(seg: PKcpSeg): PKcpSeg;
begin
Result := seg;
end;
//---------------------------------------------------------------------
// encode / decode
//---------------------------------------------------------------------
// encode 8 bits unsigned int
function ikcp_encode8u(p: PUInt8; c: UInt8): PUInt8;
begin
p^ := c;
Result := PUInt8(UInt32(p) + 1);
end;
// decode 8 bits unsigned int
function ikcp_decode8u(p: PUInt8; c: PUInt8): PUInt8;
begin
c^ := p^;
Result := PUInt8(UInt32(p) + 1);
end;
// encode 16 bits unsigned int (lsb)
function ikcp_encode16u(p: PUInt8; w: UInt16): PUInt8;
begin
PUInt16(p)^ := w;
Result := PUInt8(UInt32(p) + 2);
end;
// decode 16 bits unsigned int (lsb)
function ikcp_decode16u(p: PUInt8; w: PUInt16): PUInt8;
begin
w^ := PUInt16(p)^;
Result := PUInt8(UInt32(p) + 2);
end;
// encode 32 bits unsigned int (lsb)
function ikcp_encode32u(p: PUInt8; dw: UInt32): PUInt8;
begin
PUInt32(p)^ := dw;
Result := PUInt8(UInt32(p) + 4);
end;
// decode 32 bits unsigned int (lsb)
function ikcp_decode32u(const p: PUInt8; dw: PUInt32): PUInt8;
begin
dw^ := PUInt32(p)^;
Result := PUInt8(UInt32(p) + 4);
end;
function _imin_(a, b: UInt32): UInt32;
begin
if (a <= b) then
Result := a
else
Result := b;
end;
function _imax_(a, b: UInt32): UInt32;
begin
if (a >= b) then
Result := a
else
Result := b;
end;
function _ibound_(lower, middle, upper: UInt32): UInt32;
begin
Result := _imin_(_imax_(lower, middle), upper);
end;
function _itimediff(later, earlier: UInt32): Int32;
begin
Result := later - earlier;
end;
//---------------------------------------------------------------------
// manage segment
//---------------------------------------------------------------------
var
ikcp_malloc_hook: TMalloc = nil;
ikcp_free_hook: TFree = nil;
// internal malloc
function ikcp_malloc(size: Int32): Pointer;
begin
if (@ikcp_malloc_hook <> nil) then
Result := ikcp_malloc_hook(size)
else
Result := GetMemory(size);
end;
// internal free
procedure ikcp_free(ptr: Pointer);
begin
if (@ikcp_free_hook <> nil) then
ikcp_free_hook(ptr)
else
FreeMemory(ptr);
end;
// redefine allocator
procedure ikcp_allocator(malloc: TMalloc; free: TFree);
begin
@ikcp_malloc_hook := @malloc;
@ikcp_free_hook := @free;
end;
// allocate a new kcp segment
function ikcp_segment_new(kcp: PKcpCb; size: Int32): PKcpSeg;
begin
Result := PKcpSeg(ikcp_malloc(SizeOf(TKcpSeg) + size));
end;
procedure ikcp_segment_delete(kcp: PKcpCb; seg: PKcpSeg);
begin
ikcp_free(Pointer(seg));
end;
procedure ikcp_log(kcp: PKcpCb; mask: Int32; const fmt: PTSTR; const param: array of const);
var
buffer: {$IFDEF Unicode}AnsiString{$ELSE}string{$ENDIF};
begin
if (((kcp^.logmask and mask) = 0) or (Pointer(@kcp^.writelog) = nil)) then Exit;
buffer := Format(fmt, param) + #13#10;
kcp^.writelog(PTSTR(buffer), kcp, kcp^.user);
end;
function ikcp_canlog(const kcp: PKcpCb; mask: Int32): Boolean;
begin
Result := True;
if (((mask and kcp^.logmask) = 0) or (Pointer(@kcp^.writelog) = nil)) then
Result := False;
end;
function ikcp_output(kcp: PKcpCb; const data: PUInt8; size: Int32): Boolean;
begin
assert(kcp <> nil, 'ikcp_output');
assert(Pointer(@kcp^.output) <> nil, 'ikcp_output');
Result := False;
if (ikcp_canlog(kcp, CONST_IKCP_LOG_OUTPUT)) then
begin
ikcp_log(kcp, CONST_IKCP_LOG_OUTPUT, '[RO] %d bytes', [size]);
end;
if (size = 0) then Exit;
Result := kcp^.output(data, size, kcp, kcp^.user);
end;
// output queue
procedure ikcp_qprint(name: PTSTR; head: PKcpSeg);
var
p: PKcpSeg;
begin
{$IFDEF QPRINT}
printf('<%s>: [', [name]);
p := head^.next;
while (p^.next <> head) do
begin
printf('%d %d', [p^.sn, Int32(p^.ts mod 10000)]);
if (p^.next <> head) then printf(',', []);
p := p^.next;
end;
printf(']' + #13#10, []);
{$ENDIF}
end;
//---------------------------------------------------------------------
// create a new kcpcb
//---------------------------------------------------------------------
function ikcp_create(conv: UInt32; user: Pointer): PKcpCb;
begin
Result := PKcpCb(ikcp_malloc(SizeOf(TKcpCb)));
Result^.conv := conv;
Result^.user := user;
Result^.snd_una := 0;
Result^.snd_nxt := 0;
Result^.rcv_nxt := 0;
Result^.ts_recent := 0;
Result^.ts_lastack := 0;
Result^.ts_probe := 0;
Result^.probe_wait := 0;
Result^.snd_wnd := IKCP_WND_SND;
Result^.rcv_wnd := IKCP_WND_RCV;
Result^.rmt_wnd := IKCP_WND_RCV;
Result^.cwnd := 0;
Result^.incr := 0;
Result^.probe := 0;
Result^.mtu := IKCP_MTU_DEF;
Result^.mss := Result^.mtu - IKCP_OVERHEAD;
Result^.stream := 0;
Result^.buffer := PUInt8(ikcp_malloc((Result^.mtu + IKCP_OVERHEAD) * 3));
if (Result^.buffer = nil) then
begin
ikcp_free(Result);
Result := nil;
Exit;
end;
iqueue_init(@Result^.snd_queue);
iqueue_init(@Result^.rcv_queue);
iqueue_init(@Result^.snd_buf);
iqueue_init(@Result^.rcv_buf);
Result^.nrcv_buf := 0;
Result^.nsnd_buf := 0;
Result^.nrcv_que := 0;
Result^.nsnd_que := 0;
Result^.state := 0;
Result^.acklist := nil;
Result^.ackblock := 0;
Result^.ackcount := 0;
Result^.rx_srtt := 0;
Result^.rx_rttval := 0;
Result^.rx_rto := IKCP_RTO_DEF;
Result^.rx_minrto := IKCP_RTO_MIN;
Result^.current := 0;
Result^.interval := IKCP_INTERVAL_;
Result^.ts_flush := IKCP_INTERVAL_;
Result^.nodelay := 0;
Result^.updated := 0;
Result^.logmask := 0;
Result^.ssthresh := IKCP_THRESH_INIT;
Result^.fastresend := 0;
Result^.nocwnd := 0;
Result^.xmit := 0;
Result^.dead_link := IKCP_DEADLINK;
@Result^.output := nil;
@Result^.writelog := nil;
end;
//---------------------------------------------------------------------
// release a new kcpcb
//---------------------------------------------------------------------
procedure ikcp_release(kcp: PkcpCb);
var
seg: PKcpSeg;
begin
assert(kcp <> nil, 'ikcp_release');
if (kcp <> nil) then
begin
while (not iqueue_is_empty(@kcp^.snd_buf)) do
begin
seg := iqueue_entry(kcp^.snd_buf.next);
iqueue_del(seg);
ikcp_segment_delete(kcp, seg);
end;
while (not iqueue_is_empty(@kcp^.rcv_buf)) do
begin
seg := iqueue_entry(kcp^.rcv_buf.next);
iqueue_del(seg);
ikcp_segment_delete(kcp, seg);
end;
while (not iqueue_is_empty(@kcp^.snd_queue)) do
begin
seg := iqueue_entry(kcp^.snd_queue.next);
iqueue_del(seg);
ikcp_segment_delete(kcp, seg);
end;
while (not iqueue_is_empty(@kcp^.rcv_queue)) do
begin
seg := iqueue_entry(kcp^.rcv_queue.next);
iqueue_del(seg);
ikcp_segment_delete(kcp, seg);
end;
if (kcp^.buffer <> nil) then
ikcp_free(Pointer(kcp^.buffer));
kcp^.nrcv_buf := 0;
kcp^.nsnd_buf := 0;
kcp^.nrcv_que := 0;
kcp^.nsnd_que := 0;
kcp^.ackcount := 0;
kcp^.buffer := nil;
kcp^.acklist := nil;
ikcp_free(kcp);
end;
end;
//---------------------------------------------------------------------
// set output callback, which will be invoked by kcp
//---------------------------------------------------------------------
procedure ikcp_setoutput(kcp: PkcpCb; output: TOutPut);
begin
@kcp^.output := @output;
end;
//---------------------------------------------------------------------
// user/upper level recv: returns size, returns below zero for EAGAIN
//---------------------------------------------------------------------
function ikcp_recv(kcp: PkcpCb; buffer: PUInt8; len: Int32): Int32;
var
seg, p: PKcpSeg;
ispeek, recover: Boolean;
peeksize, fragment: Int32;
begin
assert(kcp <> nil, 'ikcp_recv 1');
ispeek := len < 0;
if (iqueue_is_empty(@kcp^.rcv_queue)) then Exit(-1);
if (len < 0) then len := -len;
peeksize := ikcp_peeksize(kcp);
if (peeksize < 0) then Exit(-2);
if (peeksize > len) then Exit(-3);
recover := False;
if (kcp^.nrcv_que >= kcp^.rcv_wnd) then recover := True;
// merge fragment
len := 0;
p := kcp^.rcv_queue.next;
while (p <> @kcp^.rcv_queue) do
begin
seg := iqueue_entry(p);
p := p^.next;
if (buffer <> nil) then
begin
memcpy(buffer, @seg^.data, seg^.len);
Inc(buffer, seg^.len);
end;
Inc(len, seg^.len);
fragment := seg^.frg;
if (ikcp_canlog(kcp, CONST_IKCP_LOG_RECV)) then
begin
ikcp_log(kcp, CONST_IKCP_LOG_RECV, 'recv sn = %d', [seg^.sn]);
end;
if (not ispeek) then
begin
iqueue_del(seg);
ikcp_segment_delete(kcp, seg);
Dec(kcp^.nrcv_que);
end;
if (fragment = 0) then Break;
end;
assert(len = peeksize, 'ikcp_recv 2');
// move available data from rcv_buf -> rcv_queue
while (not iqueue_is_empty(@kcp^.rcv_buf)) do
begin
seg := iqueue_entry(kcp^.rcv_buf.next);
if ((seg^.sn = kcp^.rcv_nxt) and (kcp^.nrcv_que < kcp^.rcv_wnd)) then
begin
iqueue_del(seg);
Dec(kcp^.nrcv_buf);
iqueue_add_tail(seg, @kcp^.rcv_queue);
Inc(kcp^.nrcv_que);
Inc(kcp^.rcv_nxt);
end
else Break;
end;
// fast recover
if ((kcp^.nrcv_que < kcp^.rcv_wnd) and recover) then
begin
kcp^.probe := kcp^.probe or IKCP_ASK_TELL;
end;
Result := len;
end;
//---------------------------------------------------------------------
// peek data size
//---------------------------------------------------------------------
function ikcp_peeksize(const kcp: PkcpCb): Int32;
var
seg: PKcpSeg;
begin
if (iqueue_is_empty(@kcp^.rcv_queue)) then Exit(-1);
seg := iqueue_entry(kcp^.rcv_queue.next);
if (seg^.frg = 0) then Exit(seg^.len);
if (kcp^.nrcv_que < seg^.frg + 1) then Exit(-1);
Result := 0;
seg := kcp^.rcv_queue.next;
while (seg <> @kcp^.rcv_queue) do
begin
Result := Result + seg^.len;
if (seg^.frg = 0) then Break;
seg := seg^.next;
end;
end;
//---------------------------------------------------------------------
// user/upper level send, returns below zero for error
//---------------------------------------------------------------------
function ikcp_send(kcp: PkcpCb; const buf: PUInt8; len: Int32): Int32;
var
seg, old: PKcpSeg;
capacity, extend, count, i, size: Int32;
buffer: PUInt8;
begin
assert(kcp^.mss > 0, 'ikcp_send 0');
buffer := buf;
if (len < 0) then Exit(-1);
// append to previous segment in streaming mode (if possible)
if (kcp^.stream <> 0) then
begin
if (not iqueue_is_empty(@kcp^.snd_queue)) then
begin
old := iqueue_entry(kcp^.snd_queue.prev);
if (old^.len < kcp^.mss) then
begin
capacity := kcp^.mss - old^.len;
if (len < capacity) then
extend := len
else
extend := capacity;
seg := ikcp_segment_new(kcp, old^.len + extend);
assert(seg <> nil, 'ikcp_send 1');
if (seg = nil) then Exit(-2);
iqueue_add_tail(seg, @kcp^.snd_queue);
memcpy(@seg^.data, @old^.data, old^.len);
if (buffer <> nil) then
begin
memcpy(Pointer(UInt32(@seg^.data) + old^.len), buffer, extend);
Inc(buffer, extend);
end;
seg^.len := old^.len + extend;
seg^.frg := 0;
Dec(len, extend);
iqueue_del_init(old);
ikcp_segment_delete(kcp, old);
end;
end;
if (len <= 0) then Exit(0);
end;
if (len <= kcp^.mss) then
count := 1
else
count := (len + kcp^.mss - 1) div kcp^.mss;
if (count > 255) then Exit(-2);
if (count = 0) then count := 1;
// fragment
for i := 0 to count - 1 do
begin
if (len > kcp^.mss) then
size := kcp^.mss
else
size := len;
seg := ikcp_segment_new(kcp, size);
assert(seg <> nil, 'ikcp_send 2');
if (seg = nil) then Exit(-2);
if ((buffer <> nil) and (len > 0)) then
begin
memcpy(@seg^.data, buffer, size);
end;
seg^.len := size;
if (kcp^.stream = 0) then
seg^.frg := count - i - 1
else
seg^.frg := 0;
iqueue_init(seg);
iqueue_add_tail(seg, @kcp^.snd_queue);
Inc(kcp^.nsnd_que);
if (buffer <> nil) then Inc(buffer, size);
Dec(len, size);
end;
Result := 0;
end;
//---------------------------------------------------------------------
// parse ack
//---------------------------------------------------------------------
procedure ikcp_update_ack(kcp: PkcpCb; rtt: Int32);
var
rto: Int32;
delta: Int32;
begin
rto := 0;
if (kcp^.rx_srtt = 0) then
begin
kcp^.rx_srtt := rtt;
kcp^.rx_rttval := rtt div 2;
end
else begin
delta := rtt - kcp^.rx_srtt;
if (delta < 0) then delta := -delta;
kcp^.rx_rttval := (3 * kcp^.rx_rttval + delta) div 4;
kcp^.rx_srtt := (7 * kcp^.rx_srtt + rtt) div 8;
if (kcp^.rx_srtt < 1) then kcp^.rx_srtt := 1;
end;
rto := kcp^.rx_srtt + _imax_(1, 4 * kcp^.rx_rttval);
kcp^.rx_rto := _ibound_(kcp^.rx_minrto, rto, IKCP_RTO_MAX);
end;
procedure ikcp_shrink_buf(kcp: PKcpCb);
var
seg: PKcpSeg;
begin
seg := iqueue_entry(kcp^.snd_buf.next);
if (seg <> @kcp^.snd_buf) then
kcp^.snd_una := seg^.sn
else
kcp^.snd_una := kcp^.snd_nxt;
end;
procedure ikcp_parse_ack(kcp: PkcpCb; sn: UInt32);
var
seg, next: PKcpSeg;
begin
if ((_itimediff(sn, kcp^.snd_una) < 0) or (_itimediff(sn, kcp^.snd_nxt) >= 0)) then Exit;
seg := iqueue_entry(kcp^.snd_buf.next);
while (seg <> @kcp^.snd_buf) do
begin
next := seg^.next;
if (sn = seg^.sn) then
begin
iqueue_del(seg);
ikcp_segment_delete(kcp, seg);
Dec(kcp^.nsnd_buf);
Break
end;
if (_itimediff(sn, seg^.sn) < 0) then Break;
seg := next;
end;
end;
procedure ikcp_parse_una(kcP: PkcpCb; una: UInt32);
var
seg, next: PKcpSeg;
begin
seg := iqueue_entry(kcp^.snd_buf.next);
while (seg <> @kcp^.snd_buf) do
begin
next := seg^.next;
if (_itimediff(una, seg^.sn) > 0) then
begin
iqueue_del(seg);
ikcp_segment_delete(kcp, seg);
Dec(kcp^.nsnd_buf);
end
else Break;
seg := next;
end;
end;
procedure ikcp_parse_fastack(kcp: PkcpCb; sn: UInt32);
var
seg, next: PKcpSeg;
begin
if ((_itimediff(sn, kcp^.snd_una) < 0) or (_itimediff(sn, kcp^.snd_nxt) >= 0)) then Exit;
seg := iqueue_entry(kcp^.snd_buf.next);
while (seg <> @kcp^.snd_buf) do
begin
next := seg^.next;
if (_itimediff(sn, seg^.sn) < 0) then
Break
else if (sn <> seg^.sn) then
Inc(seg^.fastack);
seg := next;
end;
end;
{$PointerMath ON}
procedure ikcp_ack_push(kcp: PkcpCb; sn: UInt32; ts: UInt32);
var
newsize, newblock, x: UInt32;
ptr, acklist: PUInt32;
begin
newsize := kcp^.ackcount + 1;
if (newsize > kcp^.ackblock) then
begin
newblock := 8;
while (newblock < newsize) do newblock := newblock shl 1;
acklist := PUInt32(ikcp_malloc(newblock * SizeOf(UInt32) * 2));
if (acklist = nil) then
begin
assert(acklist <> nil, 'ikcp_ack_push 0');
Abort();
end;
if (kcp^.acklist <> nil) then
begin
for x := 0 to kcp^.ackcount - 1 do
begin
acklist[x * 2 + 0] := kcp^.acklist[x * 2 + 0];
acklist[x * 2 + 1] := kcp^.acklist[x * 2 + 1];
end;
ikcp_free(kcp^.acklist);
end;
kcp^.acklist := acklist;
kcp^.ackblock := newblock;
end;
ptr := @kcp^.acklist[kcp^.ackcount * 2];
ptr[0] := sn;
ptr[1] := ts;
Inc(kcp^.ackcount);
end;
procedure ikcp_ack_get(const kcp: PkcpCb; p: UInt32; sn: PUInt32; ts: PUInt32);
begin
if (sn <> nil) then sn^ := kcp^.acklist[p * 2 + 0];
if (ts <> nil) then ts^ := kcp^.acklist[p * 2 + 1];
end;
{$PointerMath OFF}
//---------------------------------------------------------------------
// parse data(fixed)
//---------------------------------------------------------------------
procedure ikcp_parse_data(kcp: PkcpCb; newseg: PKcpSeg);
var
prev, seg: PKcpSeg;
sn: UInt32;
re: Boolean;
begin
sn := newseg^.sn;
re := False;
if ((_itimediff(sn, kcp^.rcv_nxt + kcp^.rcv_wnd) >= 0) or
(_itimediff(sn, kcp^.rcv_nxt) < 0)) then
begin
ikcp_segment_delete(kcp, newseg);
Exit;
end;
seg := iqueue_entry(kcp^.rcv_buf.prev);
while (seg <> @kcp^.rcv_buf) do
begin
prev := seg^.prev;
if (seg^.sn = sn) then
begin
re := True;
Break;
end;
if (_itimediff(sn, seg^.sn) > 0) then Break;
seg := prev;
end;
if (not re) then
begin
iqueue_init(newseg);
iqueue_add(newseg, seg);
Inc(kcp^.nrcv_buf);
end
else begin
ikcp_segment_delete(kcp, newseg);
end;
{$IFDEF QPRINT}
ikcp_qprint('rcvbuf', @kcp^.rcv_buf);
printf('rcv_nxt = %d' + #13#10, [kcp^.rcv_nxt]);
{$ENDIF}
// move available data from rcv_buf -> rcv_queue
while (not iqueue_is_empty(@kcp^.rcv_buf)) do
begin
seg := iqueue_entry(kcp^.rcv_buf.next);
if ((seg^.sn = kcp^.rcv_nxt) and (kcp^.nrcv_que < kcp^.rcv_wnd)) then
begin
iqueue_del(seg);
Dec(kcp^.nrcv_buf);
iqueue_add_tail(seg, @kcp^.rcv_queue);
Inc(kcp^.nrcv_que);
Inc(kcp^.rcv_nxt);
end
else Break;
end;
{$IFDEF QPRINT}
ikcp_qprint('queue', @kcp^.rcv_queue);
printf('rcv_nxt = %d' + #13#10, [kcp^.rcv_nxt]);
printf('snd(buf=%d, queue=%d)' + #13#10, [kcp^.nsnd_buf, kcp^.nsnd_que]);
printf('rcv(buf=%d, queue=%d)' + #13#10, [kcp^.nrcv_buf, kcp^.nrcv_que]);
{$ENDIF}
end;
//---------------------------------------------------------------------
// input data
//---------------------------------------------------------------------
function ikcp_input(kcp: PKcpCb; const buf: PUInt8; size: UInt32): Int32;
var
flag: Boolean;
una, maxack, mss: UInt32;
ts, sn, len, una_, conv: UInt32;
wnd: UInt16;
cmd, frg: UInt8;
seg: PKcpSeg;
data: PUInt8;
begin
data := buf;
una := kcp^.snd_una;
maxack := 0;
flag := False;
if (ikcp_canlog(kcp, CONST_IKCP_LOG_INPUT)) then
ikcp_log(kcp, CONST_IKCP_LOG_INPUT, '[RI] %d bytes', [size]);
if ((data = nil) or (size < 24)) then Exit(-1);
while (True) do
begin
if (size < IKCP_OVERHEAD) then Break;
data := ikcp_decode32u(data, @conv);
if (conv <> kcp^.conv) then Exit(-1);
data := ikcp_decode8u(data, @cmd);
data := ikcp_decode8u(data, @frg);
data := ikcp_decode16u(data, @wnd);
data := ikcp_decode32u(data, @ts);
data := ikcp_decode32u(data, @sn);
data := ikcp_decode32u(data, @una_);
data := ikcp_decode32u(data, @len);
Dec(size, IKCP_OVERHEAD);
if (size < len) then Exit(-2);
if ((cmd <> IKCP_CMD_PUSH) and (cmd <> IKCP_CMD_ACK) and (cmd <> IKCP_CMD_WASK) and
(cmd <> IKCP_CMD_WINS)) then Exit(-3);
kcp^.rmt_wnd := wnd;
ikcp_parse_una(kcp, una_);
ikcp_shrink_buf(kcp);
if (cmd = IKCP_CMD_ACK) then
begin
if (_itimediff(kcp^.current, ts) >= 0) then
ikcp_update_ack(kcp, _itimediff(kcp^.current, ts));
ikcp_parse_ack(kcp, sn);
ikcp_shrink_buf(kcp);
if (not flag) then
begin
flag := True;
maxack := sn;
end
else begin
if (_itimediff(sn, maxack) > 0) then maxack := sn;
end;
if (ikcp_canlog(kcp, CONST_IKCP_LOG_IN_ACK)) then
begin
ikcp_log(kcp, CONST_IKCP_LOG_IN_DATA, 'input ack: sn=%d rtt=%d rto=%d', [sn,
_itimediff(kcp^.current, ts), kcp^.rx_rto]);
end;
end
else if (cmd = IKCP_CMD_PUSH) then
begin
if (ikcp_canlog(kcp, CONST_IKCP_LOG_IN_ACK)) then
begin
ikcp_log(kcp, CONST_IKCP_LOG_IN_DATA, 'input psh: sn=%d ts=%d', [sn, ts]);
end;
if (_itimediff(sn, kcp^.rcv_nxt + kcp^.rcv_wnd) < 0) then
begin
ikcp_ack_push(kcp, sn, ts);
if (_itimediff(sn, kcp^.rcv_nxt) >= 0) then
begin
seg := ikcp_segment_new(kcp, len);
seg^.conv := conv;
seg^.cmd := cmd;
seg^.frg := frg;
seg^.wnd := wnd;
seg^.ts := ts;
seg^.sn := sn;
seg^.una := una_;
seg^.len := len;
if (len > 0) then
memcpy(@seg^.data, data, len);
ikcp_parse_data(kcp, seg);
end;
end;
end
else if (cmd = IKCP_CMD_WASK) then
begin
// ready to send back IKCP_CMD_WINS in ikcp_flush
// tell remote my window size
kcp^.probe := kcp^.probe or IKCP_ASK_TELL;
if (ikcp_canlog(kcp, CONST_IKCP_LOG_IN_PROBE)) then
begin
ikcp_log(kcp, CONST_IKCP_LOG_IN_PROBE, 'input probe', []);
end;
end
else if (cmd = IKCP_CMD_WINS) then
begin
// do nothing
if (ikcp_canlog(kcp, CONST_IKCP_LOG_IN_WINS)) then
begin
ikcp_log(kcp, CONST_IKCP_LOG_IN_WINS, 'input wins: %d', [wnd]);
end;
end
else Exit(-3);
Inc(data, len);
Dec(size, len);
end;
if (flag) then ikcp_parse_fastack(kcp, maxack);
if (_itimediff(kcp^.snd_una, una) > 0) then
begin
if (kcp^.cwnd < kcp^.rmt_wnd) then
begin
mss := kcp^.mss;
if (kcp^.cwnd < kcp^.ssthresh) then
begin
Inc(kcp^.cwnd);
Inc(kcp^.incr, mss);
end
else begin
if (kcp^.incr < mss) then kcp^.incr := mss;
kcp^.incr := kcp^.incr + ((mss * mss) div kcp^.incr) + (mss div 16);
if ((kcp^.cwnd + 1) * mss <= kcp^.incr) then Inc(kcp^.cwnd);
end;
if (kcp^.cwnd > kcp^.rmt_wnd) then
begin
kcp^.cwnd := kcp^.rmt_wnd;
kcp^.incr := kcp^.rmt_wnd * mss;
end;
end;
end;
Result := 0;
end;
//---------------------------------------------------------------------
// ikcp_encode_seg
//---------------------------------------------------------------------
function ikcp_encode_seg(ptr: PUInt8; const seg: PKcpSeg): PUInt8;
begin
ptr := ikcp_encode32u(ptr, seg^.conv);
ptr := ikcp_encode8u(ptr, UInt8(seg^.cmd));
ptr := ikcp_encode8u(ptr, UInt8(seg^.frg));
ptr := ikcp_encode16u(ptr, UInt16(seg^.wnd));
ptr := ikcp_encode32u(ptr, seg^.ts);
ptr := ikcp_encode32u(ptr, seg^.sn);
ptr := ikcp_encode32u(ptr, seg^.una);
ptr := ikcp_encode32u(ptr, seg^.len);
Result := ptr;
end;
function ikcp_wnd_unused(const kcp: PkcpCb): Int32;
begin
Result := 0;