-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
sending.go
365 lines (302 loc) · 7.25 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
package kcp
import (
"container/list"
"sync"
"v2ray.com/core/common/buf"
)
type SendingWindow struct {
cache *list.List
totalInFlightSize uint32
writer SegmentWriter
onPacketLoss func(uint32)
}
func NewSendingWindow(writer SegmentWriter, onPacketLoss func(uint32)) *SendingWindow {
window := &SendingWindow{
cache: list.New(),
writer: writer,
onPacketLoss: onPacketLoss,
}
return window
}
func (sw *SendingWindow) Release() {
if sw == nil {
return
}
for sw.cache.Len() > 0 {
seg := sw.cache.Front().Value.(*DataSegment)
seg.Release()
sw.cache.Remove(sw.cache.Front())
}
}
func (sw *SendingWindow) Len() uint32 {
return uint32(sw.cache.Len())
}
func (sw *SendingWindow) IsEmpty() bool {
return sw.cache.Len() == 0
}
func (sw *SendingWindow) Push(number uint32, b *buf.Buffer) {
seg := NewDataSegment()
seg.Number = number
seg.payload = b
sw.cache.PushBack(seg)
}
func (sw *SendingWindow) FirstNumber() uint32 {
return sw.cache.Front().Value.(*DataSegment).Number
}
func (sw *SendingWindow) Clear(una uint32) {
for !sw.IsEmpty() {
seg := sw.cache.Front().Value.(*DataSegment)
if seg.Number >= una {
break
}
seg.Release()
sw.cache.Remove(sw.cache.Front())
}
}
func (sw *SendingWindow) HandleFastAck(number uint32, rto uint32) {
if sw.IsEmpty() {
return
}
sw.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 (sw *SendingWindow) Visit(visitor func(seg *DataSegment) bool) {
if sw.IsEmpty() {
return
}
for e := sw.cache.Front(); e != nil; e = e.Next() {
seg := e.Value.(*DataSegment)
if !visitor(seg) {
break
}
}
}
func (sw *SendingWindow) Flush(current uint32, rto uint32, maxInFlightSize uint32) {
if sw.IsEmpty() {
return
}
var lost uint32
var inFlightSize uint32
sw.Visit(func(segment *DataSegment) bool {
if current-segment.timeout >= 0x7FFFFFFF {
return true
}
if segment.transmit == 0 {
// First time
sw.totalInFlightSize++
} else {
lost++
}
segment.timeout = current + rto
segment.Timestamp = current
segment.transmit++
sw.writer.Write(segment)
inFlightSize++
if inFlightSize >= maxInFlightSize {
return false
}
return true
})
if sw.onPacketLoss != nil && inFlightSize > 0 && sw.totalInFlightSize != 0 {
rate := lost * 100 / sw.totalInFlightSize
sw.onPacketLoss(rate)
}
}
func (sw *SendingWindow) Remove(number uint32) bool {
if sw.IsEmpty() {
return false
}
for e := sw.cache.Front(); e != nil; e = e.Next() {
seg := e.Value.(*DataSegment)
if seg.Number > number {
return false
} else if seg.Number == number {
if sw.totalInFlightSize > 0 {
sw.totalInFlightSize--
}
seg.Release()
sw.cache.Remove(e)
return true
}
}
return false
}
type SendingWorker struct {
sync.RWMutex
conn *Connection
window *SendingWindow
firstUnacknowledged uint32
nextNumber uint32
remoteNextNumber uint32
controlWindow uint32
fastResend uint32
windowSize uint32
firstUnacknowledgedUpdated bool
closed bool
}
func NewSendingWorker(kcp *Connection) *SendingWorker {
worker := &SendingWorker{
conn: kcp,
fastResend: 2,
remoteNextNumber: 32,
controlWindow: kcp.Config.GetSendingInFlightSize(),
windowSize: kcp.Config.GetSendingBufferSize(),
}
worker.window = NewSendingWindow(worker, worker.OnPacketLoss)
return worker
}
func (w *SendingWorker) Release() {
w.Lock()
w.window.Release()
w.closed = true
w.Unlock()
}
func (w *SendingWorker) ProcessReceivingNext(nextNumber uint32) {
w.Lock()
defer w.Unlock()
w.ProcessReceivingNextWithoutLock(nextNumber)
}
func (w *SendingWorker) ProcessReceivingNextWithoutLock(nextNumber uint32) {
w.window.Clear(nextNumber)
w.FindFirstUnacknowledged()
}
func (w *SendingWorker) FindFirstUnacknowledged() {
first := w.firstUnacknowledged
if !w.window.IsEmpty() {
w.firstUnacknowledged = w.window.FirstNumber()
} else {
w.firstUnacknowledged = w.nextNumber
}
if first != w.firstUnacknowledged {
w.firstUnacknowledgedUpdated = true
}
}
func (w *SendingWorker) processAck(number uint32) bool {
// number < v.firstUnacknowledged || number >= v.nextNumber
if number-w.firstUnacknowledged > 0x7FFFFFFF || number-w.nextNumber < 0x7FFFFFFF {
return false
}
removed := w.window.Remove(number)
if removed {
w.FindFirstUnacknowledged()
}
return removed
}
func (w *SendingWorker) ProcessSegment(current uint32, seg *AckSegment, rto uint32) {
defer seg.Release()
w.Lock()
defer w.Unlock()
if w.closed {
return
}
if w.remoteNextNumber < seg.ReceivingWindow {
w.remoteNextNumber = seg.ReceivingWindow
}
w.ProcessReceivingNextWithoutLock(seg.ReceivingNext)
if seg.IsEmpty() {
return
}
var maxack uint32
var maxackRemoved bool
for _, number := range seg.NumberList {
removed := w.processAck(number)
if maxack < number {
maxack = number
maxackRemoved = removed
}
}
if maxackRemoved {
w.window.HandleFastAck(maxack, rto)
if current-seg.Timestamp < 10000 {
w.conn.roundTrip.Update(current-seg.Timestamp, current)
}
}
}
func (w *SendingWorker) Push(b *buf.Buffer) bool {
w.Lock()
defer w.Unlock()
if w.closed {
return false
}
if w.window.Len() > w.windowSize {
return false
}
w.window.Push(w.nextNumber, b)
w.nextNumber++
return true
}
func (w *SendingWorker) Write(seg Segment) error {
dataSeg := seg.(*DataSegment)
dataSeg.Conv = w.conn.meta.Conversation
dataSeg.SendingNext = w.firstUnacknowledged
dataSeg.Option = 0
if w.conn.State() == StateReadyToClose {
dataSeg.Option = SegmentOptionClose
}
return w.conn.output.Write(dataSeg)
}
func (w *SendingWorker) OnPacketLoss(lossRate uint32) {
if !w.conn.Config.Congestion || w.conn.roundTrip.Timeout() == 0 {
return
}
if lossRate >= 15 {
w.controlWindow = 3 * w.controlWindow / 4
} else if lossRate <= 5 {
w.controlWindow += w.controlWindow / 4
}
if w.controlWindow < 16 {
w.controlWindow = 16
}
if w.controlWindow > 2*w.conn.Config.GetSendingInFlightSize() {
w.controlWindow = 2 * w.conn.Config.GetSendingInFlightSize()
}
}
func (w *SendingWorker) Flush(current uint32) {
w.Lock()
if w.closed {
w.Unlock()
return
}
cwnd := w.firstUnacknowledged + w.conn.Config.GetSendingInFlightSize()
if cwnd > w.remoteNextNumber {
cwnd = w.remoteNextNumber
}
if w.conn.Config.Congestion && cwnd > w.firstUnacknowledged+w.controlWindow {
cwnd = w.firstUnacknowledged + w.controlWindow
}
if !w.window.IsEmpty() {
w.window.Flush(current, w.conn.roundTrip.Timeout(), cwnd)
w.firstUnacknowledgedUpdated = false
}
updated := w.firstUnacknowledgedUpdated
w.firstUnacknowledgedUpdated = false
w.Unlock()
if updated {
w.conn.Ping(current, CommandPing)
}
}
func (w *SendingWorker) CloseWrite() {
w.Lock()
defer w.Unlock()
w.window.Clear(0xFFFFFFFF)
}
func (w *SendingWorker) IsEmpty() bool {
w.RLock()
defer w.RUnlock()
return w.window.IsEmpty()
}
func (w *SendingWorker) UpdateNecessary() bool {
return !w.IsEmpty()
}
func (w *SendingWorker) FirstUnacknowledged() uint32 {
w.RLock()
defer w.RUnlock()
return w.firstUnacknowledged
}