forked from v2fly/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sending.go
451 lines (388 loc) · 9.52 KB
/
sending.go
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
package kcp
import (
"sync"
"github.com/v2ray/v2ray-core/common/alloc"
)
type SendingWindow struct {
start uint32
cap uint32
len uint32
last uint32
data []*DataSegment
prev []uint32
next []uint32
totalInFlightSize uint32
writer SegmentWriter
onPacketLoss func(uint32)
}
func NewSendingWindow(size uint32, writer SegmentWriter, onPacketLoss func(uint32)) *SendingWindow {
window := &SendingWindow{
start: 0,
cap: size,
len: 0,
last: 0,
data: make([]*DataSegment, size),
prev: make([]uint32, size),
next: make([]uint32, size),
writer: writer,
onPacketLoss: onPacketLoss,
}
return window
}
func (this *SendingWindow) Len() int {
return int(this.len)
}
func (this *SendingWindow) IsEmpty() bool {
return this.len == 0
}
func (this *SendingWindow) Size() uint32 {
return this.cap
}
func (this *SendingWindow) IsFull() bool {
return this.len == this.cap
}
func (this *SendingWindow) Push(seg *DataSegment) {
pos := (this.start + this.len) % this.cap
this.data[pos] = seg
if this.len > 0 {
this.next[this.last] = pos
this.prev[pos] = this.last
}
this.last = pos
this.len++
}
func (this *SendingWindow) First() *DataSegment {
return this.data[this.start]
}
func (this *SendingWindow) Clear(una uint32) {
for !this.IsEmpty() && this.data[this.start].Number < una {
this.Remove(0)
}
}
func (this *SendingWindow) Remove(idx uint32) {
if this.len == 0 {
return
}
pos := (this.start + idx) % this.cap
seg := this.data[pos]
if seg == nil {
return
}
this.totalInFlightSize--
seg.Release()
this.data[pos] = nil
if pos == this.start && pos == this.last {
this.len = 0
this.start = 0
this.last = 0
} else if pos == this.start {
delta := this.next[pos] - this.start
if this.next[pos] < this.start {
delta = this.next[pos] + this.cap - this.start
}
this.start = this.next[pos]
this.len -= delta
} else if pos == this.last {
this.last = this.prev[pos]
} else {
this.next[this.prev[pos]] = this.next[pos]
this.prev[this.next[pos]] = this.prev[pos]
}
}
func (this *SendingWindow) HandleFastAck(number uint32) {
if this.len == 0 {
return
}
for i := this.start; ; i = this.next[i] {
seg := this.data[i]
if number-seg.Number > 0x7FFFFFFF {
break
}
if number != seg.Number {
seg.ackSkipped++
}
if i == this.last {
break
}
}
}
func (this *SendingWindow) Flush(current uint32, resend uint32, rto uint32, maxInFlightSize uint32) {
if this.IsEmpty() {
return
}
var lost uint32
var inFlightSize uint32
for i := this.start; ; i = this.next[i] {
segment := this.data[i]
needsend := false
if segment.transmit == 0 {
needsend = true
segment.transmit++
segment.timeout = current + rto
this.totalInFlightSize++
} else if current-segment.timeout < 0x7FFFFFFF {
needsend = true
segment.transmit++
segment.timeout = current + rto
lost++
} else if segment.ackSkipped >= resend {
needsend = true
segment.transmit++
segment.ackSkipped = 0
segment.timeout = current + rto
}
if needsend {
segment.Timestamp = current
this.writer.Write(segment)
inFlightSize++
if inFlightSize >= maxInFlightSize {
break
}
}
if i == this.last {
break
}
}
if this.onPacketLoss != nil && inFlightSize > 0 && this.totalInFlightSize != 0 {
rate := lost * 100 / this.totalInFlightSize
this.onPacketLoss(rate)
}
}
type SendingQueue struct {
start uint32
cap uint32
len uint32
list []*alloc.Buffer
}
func NewSendingQueue(size uint32) *SendingQueue {
return &SendingQueue{
start: 0,
cap: size,
list: make([]*alloc.Buffer, size),
len: 0,
}
}
func (this *SendingQueue) IsFull() bool {
return this.len == this.cap
}
func (this *SendingQueue) IsEmpty() bool {
return this.len == 0
}
func (this *SendingQueue) Pop() *alloc.Buffer {
if this.IsEmpty() {
return nil
}
seg := this.list[this.start]
this.list[this.start] = nil
this.len--
this.start++
if this.start == this.cap {
this.start = 0
}
if this.IsEmpty() {
this.start = 0
}
return seg
}
func (this *SendingQueue) Last() *alloc.Buffer {
if this.IsEmpty() {
return nil
}
return this.list[(this.start+this.len-1+this.cap)%this.cap]
}
func (this *SendingQueue) Push(seg *alloc.Buffer) {
if this.IsFull() {
return
}
this.list[(this.start+this.len)%this.cap] = seg
this.len++
}
func (this *SendingQueue) Clear() {
for i := uint32(0); i < this.len; i++ {
this.list[(i+this.start)%this.cap].Release()
this.list[(i+this.start)%this.cap] = nil
}
this.start = 0
this.len = 0
}
func (this *SendingQueue) Len() uint32 {
return this.len
}
type SendingWorker struct {
sync.RWMutex
conn *Connection
window *SendingWindow
queue *SendingQueue
firstUnacknowledged uint32
nextNumber uint32
remoteNextNumber uint32
controlWindow uint32
fastResend uint32
updated bool
}
func NewSendingWorker(kcp *Connection) *SendingWorker {
worker := &SendingWorker{
conn: kcp,
queue: NewSendingQueue(effectiveConfig.GetSendingQueueSize()),
fastResend: 2,
remoteNextNumber: 32,
controlWindow: effectiveConfig.GetSendingInFlightSize(),
}
worker.window = NewSendingWindow(effectiveConfig.GetSendingWindowSize(), worker, worker.OnPacketLoss)
return worker
}
func (this *SendingWorker) ProcessReceivingNext(nextNumber uint32) {
this.Lock()
defer this.Unlock()
this.ProcessReceivingNextWithoutLock(nextNumber)
}
func (this *SendingWorker) ProcessReceivingNextWithoutLock(nextNumber uint32) {
this.window.Clear(nextNumber)
this.FindFirstUnacknowledged()
}
// @Private
func (this *SendingWorker) FindFirstUnacknowledged() {
prevUna := this.firstUnacknowledged
if !this.window.IsEmpty() {
this.firstUnacknowledged = this.window.First().Number
} else {
this.firstUnacknowledged = this.nextNumber
}
if this.firstUnacknowledged != prevUna {
this.updated = true
}
}
// @Private
func (this *SendingWorker) ProcessAck(number uint32) {
// number < this.firstUnacknowledged || number >= this.nextNumber
if number-this.firstUnacknowledged > 0x7FFFFFFF || number-this.nextNumber < 0x7FFFFFFF {
return
}
this.window.Remove(number - this.firstUnacknowledged)
this.FindFirstUnacknowledged()
}
func (this *SendingWorker) FillWindow(current uint32) {
for !this.queue.IsEmpty() && !this.window.IsFull() {
seg := NewDataSegment()
seg.Data = this.queue.Pop()
seg.Number = this.nextNumber
seg.timeout = current
seg.ackSkipped = 0
seg.transmit = 0
this.window.Push(seg)
this.nextNumber++
}
}
func (this *SendingWorker) ProcessSegment(current uint32, seg *AckSegment) {
defer seg.Release()
this.Lock()
defer this.Unlock()
if this.remoteNextNumber < seg.ReceivingWindow {
this.remoteNextNumber = seg.ReceivingWindow
}
this.ProcessReceivingNextWithoutLock(seg.ReceivingNext)
if current-seg.Timestamp < 10000 {
this.conn.roundTrip.Update(current-seg.Timestamp, current)
}
var maxack uint32
for i := 0; i < int(seg.Count); i++ {
number := seg.NumberList[i]
this.ProcessAck(number)
if maxack < number {
maxack = number
}
}
this.window.HandleFastAck(maxack)
this.FillWindow(current)
}
func (this *SendingWorker) Push(b []byte) int {
nBytes := 0
this.Lock()
defer this.Unlock()
if !this.queue.IsEmpty() {
lastSeg := this.queue.Last()
if lastSeg.Len() < int(this.conn.mss) {
delta := int(this.conn.mss) - lastSeg.Len()
if delta > len(b) {
delta = len(b)
}
lastSeg.Append(b[:delta])
b = b[delta:]
nBytes += delta
}
}
for len(b) > 0 && !this.queue.IsFull() {
var size int
if len(b) > int(this.conn.mss) {
size = int(this.conn.mss)
} else {
size = len(b)
}
this.queue.Push(AllocateBuffer().Clear().Append(b[:size]))
b = b[size:]
nBytes += size
}
return nBytes
}
// @Private
func (this *SendingWorker) Write(seg Segment) {
dataSeg := seg.(*DataSegment)
dataSeg.Conv = this.conn.conv
dataSeg.SendingNext = this.firstUnacknowledged
dataSeg.Option = 0
if this.conn.State() == StateReadyToClose {
dataSeg.Option = SegmentOptionClose
}
this.conn.output.Write(dataSeg)
this.updated = false
}
func (this *SendingWorker) PingNecessary() bool {
this.RLock()
defer this.RUnlock()
return this.updated
}
func (this *SendingWorker) MarkPingNecessary(b bool) {
this.Lock()
defer this.Unlock()
this.updated = b
}
func (this *SendingWorker) OnPacketLoss(lossRate uint32) {
if !effectiveConfig.Congestion || this.conn.roundTrip.Timeout() == 0 {
return
}
if lossRate >= 15 {
this.controlWindow = 3 * this.controlWindow / 4
} else if lossRate <= 5 {
this.controlWindow += this.controlWindow / 4
}
if this.controlWindow < 16 {
this.controlWindow = 16
}
if this.controlWindow > 2*effectiveConfig.GetSendingInFlightSize() {
this.controlWindow = 2 * effectiveConfig.GetSendingInFlightSize()
}
}
func (this *SendingWorker) Flush(current uint32) {
this.Lock()
defer this.Unlock()
cwnd := this.firstUnacknowledged + effectiveConfig.GetSendingInFlightSize()
if cwnd > this.remoteNextNumber {
cwnd = this.remoteNextNumber
}
if effectiveConfig.Congestion && cwnd > this.firstUnacknowledged+this.controlWindow {
cwnd = this.firstUnacknowledged + this.controlWindow
}
this.FillWindow(current)
this.window.Flush(current, this.conn.fastresend, this.conn.roundTrip.Timeout(), cwnd)
}
func (this *SendingWorker) CloseWrite() {
this.Lock()
defer this.Unlock()
this.window.Clear(0xFFFFFFFF)
this.queue.Clear()
}
func (this *SendingWorker) IsEmpty() bool {
this.RLock()
defer this.RUnlock()
return this.window.IsEmpty() && this.queue.IsEmpty()
}