forked from v2fly/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sending.go
376 lines (319 loc) · 7.62 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
package kcp
import (
"sync"
)
type SendingWindow struct {
start uint32
cap uint32
len uint32
last uint32
data []DataSegment
inuse []bool
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),
inuse: make([]bool, size),
writer: writer,
onPacketLoss: onPacketLoss,
}
return window
}
func (v *SendingWindow) Release() {
if v == nil {
return
}
v.len = 0
for _, seg := range v.data {
seg.Release()
}
}
func (v *SendingWindow) Len() int {
return int(v.len)
}
func (v *SendingWindow) IsEmpty() bool {
return v.len == 0
}
func (v *SendingWindow) Size() uint32 {
return v.cap
}
func (v *SendingWindow) IsFull() bool {
return v.len == v.cap
}
func (v *SendingWindow) Push(number uint32, data []byte) {
pos := (v.start + v.len) % v.cap
v.data[pos].SetData(data)
v.data[pos].Number = number
v.data[pos].timeout = 0
v.data[pos].transmit = 0
v.inuse[pos] = true
if v.len > 0 {
v.next[v.last] = pos
v.prev[pos] = v.last
}
v.last = pos
v.len++
}
func (v *SendingWindow) FirstNumber() uint32 {
return v.data[v.start].Number
}
func (v *SendingWindow) Clear(una uint32) {
for !v.IsEmpty() && v.data[v.start].Number < una {
v.Remove(0)
}
}
func (v *SendingWindow) Remove(idx uint32) bool {
if v.IsEmpty() {
return false
}
pos := (v.start + idx) % v.cap
if !v.inuse[pos] {
return false
}
v.inuse[pos] = false
v.totalInFlightSize--
if pos == v.start && pos == v.last {
v.len = 0
v.start = 0
v.last = 0
} else if pos == v.start {
delta := v.next[pos] - v.start
if v.next[pos] < v.start {
delta = v.next[pos] + v.cap - v.start
}
v.start = v.next[pos]
v.len -= delta
} else if pos == v.last {
v.last = v.prev[pos]
} else {
v.next[v.prev[pos]] = v.next[pos]
v.prev[v.next[pos]] = v.prev[pos]
}
return true
}
func (v *SendingWindow) HandleFastAck(number uint32, rto uint32) {
if v.IsEmpty() {
return
}
v.Visit(func(seg *DataSegment) bool {
if number == seg.Number || number-seg.Number > 0x7FFFFFFF {
return false
}
if seg.transmit > 0 && seg.timeout > rto/3 {
seg.timeout -= rto / 3
}
return true
})
}
func (v *SendingWindow) Visit(visitor func(seg *DataSegment) bool) {
if v.IsEmpty() {
return
}
for i := v.start; ; i = v.next[i] {
if !visitor(&v.data[i]) || i == v.last {
break
}
}
}
func (v *SendingWindow) Flush(current uint32, rto uint32, maxInFlightSize uint32) {
if v.IsEmpty() {
return
}
var lost uint32
var inFlightSize uint32
v.Visit(func(segment *DataSegment) bool {
if current-segment.timeout >= 0x7FFFFFFF {
return true
}
if segment.transmit == 0 {
// First time
v.totalInFlightSize++
} else {
lost++
}
segment.timeout = current + rto
segment.Timestamp = current
segment.transmit++
v.writer.Write(segment)
inFlightSize++
if inFlightSize >= maxInFlightSize {
return false
}
return true
})
if v.onPacketLoss != nil && inFlightSize > 0 && v.totalInFlightSize != 0 {
rate := lost * 100 / v.totalInFlightSize
v.onPacketLoss(rate)
}
}
type SendingWorker struct {
sync.RWMutex
conn *Connection
window *SendingWindow
firstUnacknowledged uint32
firstUnacknowledgedUpdated bool
nextNumber uint32
remoteNextNumber uint32
controlWindow uint32
fastResend uint32
}
func NewSendingWorker(kcp *Connection) *SendingWorker {
worker := &SendingWorker{
conn: kcp,
fastResend: 2,
remoteNextNumber: 32,
controlWindow: kcp.Config.GetSendingInFlightSize(),
}
worker.window = NewSendingWindow(kcp.Config.GetSendingBufferSize(), worker, worker.OnPacketLoss)
return worker
}
func (v *SendingWorker) Release() {
v.window.Release()
}
func (v *SendingWorker) ProcessReceivingNext(nextNumber uint32) {
v.Lock()
defer v.Unlock()
v.ProcessReceivingNextWithoutLock(nextNumber)
}
func (v *SendingWorker) ProcessReceivingNextWithoutLock(nextNumber uint32) {
v.window.Clear(nextNumber)
v.FindFirstUnacknowledged()
}
// Private: Visible for testing.
func (v *SendingWorker) FindFirstUnacknowledged() {
first := v.firstUnacknowledged
if !v.window.IsEmpty() {
v.firstUnacknowledged = v.window.FirstNumber()
} else {
v.firstUnacknowledged = v.nextNumber
}
if first != v.firstUnacknowledged {
v.firstUnacknowledgedUpdated = true
}
}
// Private: Visible for testing.
func (v *SendingWorker) ProcessAck(number uint32) bool {
// number < v.firstUnacknowledged || number >= v.nextNumber
if number-v.firstUnacknowledged > 0x7FFFFFFF || number-v.nextNumber < 0x7FFFFFFF {
return false
}
removed := v.window.Remove(number - v.firstUnacknowledged)
if removed {
v.FindFirstUnacknowledged()
}
return removed
}
func (v *SendingWorker) ProcessSegment(current uint32, seg *AckSegment, rto uint32) {
defer seg.Release()
v.Lock()
defer v.Unlock()
if v.remoteNextNumber < seg.ReceivingWindow {
v.remoteNextNumber = seg.ReceivingWindow
}
v.ProcessReceivingNextWithoutLock(seg.ReceivingNext)
if seg.Count == 0 {
return
}
var maxack uint32
var maxackRemoved bool
for i := 0; i < int(seg.Count); i++ {
number := seg.NumberList[i]
removed := v.ProcessAck(number)
if maxack < number {
maxack = number
maxackRemoved = removed
}
}
if maxackRemoved {
v.window.HandleFastAck(maxack, rto)
if current-seg.Timestamp < 10000 {
v.conn.roundTrip.Update(current-seg.Timestamp, current)
}
}
}
func (v *SendingWorker) Push(b []byte) int {
nBytes := 0
v.Lock()
defer v.Unlock()
for len(b) > 0 && !v.window.IsFull() {
var size int
if len(b) > int(v.conn.mss) {
size = int(v.conn.mss)
} else {
size = len(b)
}
v.window.Push(v.nextNumber, b[:size])
v.nextNumber++
b = b[size:]
nBytes += size
}
return nBytes
}
// Private: Visible for testing.
func (v *SendingWorker) Write(seg Segment) {
dataSeg := seg.(*DataSegment)
dataSeg.Conv = v.conn.conv
dataSeg.SendingNext = v.firstUnacknowledged
dataSeg.Option = 0
if v.conn.State() == StateReadyToClose {
dataSeg.Option = SegmentOptionClose
}
v.conn.output.Write(dataSeg)
}
func (v *SendingWorker) OnPacketLoss(lossRate uint32) {
if !v.conn.Config.Congestion || v.conn.roundTrip.Timeout() == 0 {
return
}
if lossRate >= 15 {
v.controlWindow = 3 * v.controlWindow / 4
} else if lossRate <= 5 {
v.controlWindow += v.controlWindow / 4
}
if v.controlWindow < 16 {
v.controlWindow = 16
}
if v.controlWindow > 2*v.conn.Config.GetSendingInFlightSize() {
v.controlWindow = 2 * v.conn.Config.GetSendingInFlightSize()
}
}
func (v *SendingWorker) Flush(current uint32) {
v.Lock()
defer v.Unlock()
cwnd := v.firstUnacknowledged + v.conn.Config.GetSendingInFlightSize()
if cwnd > v.remoteNextNumber {
cwnd = v.remoteNextNumber
}
if v.conn.Config.Congestion && cwnd > v.firstUnacknowledged+v.controlWindow {
cwnd = v.firstUnacknowledged + v.controlWindow
}
if !v.window.IsEmpty() {
v.window.Flush(current, v.conn.roundTrip.Timeout(), cwnd)
} else if v.firstUnacknowledgedUpdated {
v.conn.Ping(current, CommandPing)
}
v.firstUnacknowledgedUpdated = false
}
func (v *SendingWorker) CloseWrite() {
v.Lock()
defer v.Unlock()
v.window.Clear(0xFFFFFFFF)
}
func (v *SendingWorker) IsEmpty() bool {
v.RLock()
defer v.RUnlock()
return v.window.IsEmpty()
}
func (v *SendingWorker) UpdateNecessary() bool {
return !v.IsEmpty()
}