-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiving.go
312 lines (268 loc) · 6.07 KB
/
receiving.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
package kcp
import (
"io"
"sync"
"time"
"github.com/v2ray/v2ray-core/common/alloc"
)
type ReceivingWindow struct {
start uint32
size uint32
list []*DataSegment
}
func NewReceivingWindow(size uint32) *ReceivingWindow {
return &ReceivingWindow{
start: 0,
size: size,
list: make([]*DataSegment, size),
}
}
func (this *ReceivingWindow) Size() uint32 {
return this.size
}
func (this *ReceivingWindow) Position(idx uint32) uint32 {
return (idx + this.start) % this.size
}
func (this *ReceivingWindow) Set(idx uint32, value *DataSegment) bool {
pos := this.Position(idx)
if this.list[pos] != nil {
return false
}
this.list[pos] = value
return true
}
func (this *ReceivingWindow) Remove(idx uint32) *DataSegment {
pos := this.Position(idx)
e := this.list[pos]
this.list[pos] = nil
return e
}
func (this *ReceivingWindow) RemoveFirst() *DataSegment {
return this.Remove(0)
}
func (this *ReceivingWindow) Advance() {
this.start++
if this.start == this.size {
this.start = 0
}
}
type ReceivingQueue struct {
sync.Mutex
closed bool
cache *alloc.Buffer
queue chan *alloc.Buffer
timeout time.Time
}
func NewReceivingQueue(size uint32) *ReceivingQueue {
return &ReceivingQueue{
queue: make(chan *alloc.Buffer, size),
}
}
func (this *ReceivingQueue) Read(buf []byte) (int, error) {
if this.closed {
return 0, io.EOF
}
if this.cache.Len() > 0 {
nBytes, err := this.cache.Read(buf)
if this.cache.IsEmpty() {
this.cache.Release()
this.cache = nil
}
return nBytes, err
}
var totalBytes int
L:
for totalBytes < len(buf) {
timeToSleep := time.Millisecond
select {
case payload, open := <-this.queue:
if !open {
return totalBytes, io.EOF
}
nBytes, err := payload.Read(buf)
totalBytes += nBytes
if err != nil {
return totalBytes, err
}
if !payload.IsEmpty() {
this.cache = payload
}
buf = buf[nBytes:]
case <-time.After(timeToSleep):
if totalBytes > 0 {
break L
}
if !this.timeout.IsZero() && this.timeout.Before(time.Now()) {
return totalBytes, errTimeout
}
timeToSleep += 500 * time.Millisecond
}
}
return totalBytes, nil
}
func (this *ReceivingQueue) Put(payload *alloc.Buffer) bool {
this.Lock()
defer this.Unlock()
if this.closed {
payload.Release()
return false
}
select {
case this.queue <- payload:
return true
default:
return false
}
}
func (this *ReceivingQueue) SetReadDeadline(t time.Time) error {
this.timeout = t
return nil
}
func (this *ReceivingQueue) Close() {
this.Lock()
defer this.Unlock()
if this.closed {
return
}
this.closed = true
close(this.queue)
}
type AckList struct {
sync.Mutex
writer SegmentWriter
timestamps []uint32
numbers []uint32
nextFlush []uint32
}
func NewAckList(writer SegmentWriter) *AckList {
return &AckList{
writer: writer,
timestamps: make([]uint32, 0, 32),
numbers: make([]uint32, 0, 32),
nextFlush: make([]uint32, 0, 32),
}
}
func (this *AckList) Add(number uint32, timestamp uint32) {
this.Lock()
defer this.Unlock()
this.timestamps = append(this.timestamps, timestamp)
this.numbers = append(this.numbers, number)
this.nextFlush = append(this.nextFlush, 0)
}
func (this *AckList) Clear(una uint32) {
this.Lock()
defer this.Unlock()
count := 0
for i := 0; i < len(this.numbers); i++ {
if this.numbers[i] >= una {
if i != count {
this.numbers[count] = this.numbers[i]
this.timestamps[count] = this.timestamps[i]
this.nextFlush[count] = this.nextFlush[i]
}
count++
}
}
if count < len(this.numbers) {
this.numbers = this.numbers[:count]
this.timestamps = this.timestamps[:count]
this.nextFlush = this.nextFlush[:count]
}
}
func (this *AckList) Flush(current uint32, rto uint32) {
seg := new(AckSegment)
this.Lock()
for i := 0; i < len(this.numbers); i++ {
if this.nextFlush[i] <= current {
seg.Count++
seg.NumberList = append(seg.NumberList, this.numbers[i])
seg.TimestampList = append(seg.TimestampList, this.timestamps[i])
this.nextFlush[i] = current + rto/2
if seg.Count == 128 {
break
}
}
}
this.Unlock()
if seg.Count > 0 {
this.writer.Write(seg)
}
}
type ReceivingWorker struct {
kcp *KCP
queue *ReceivingQueue
window *ReceivingWindow
windowMutex sync.Mutex
acklist *AckList
updated bool
nextNumber uint32
windowSize uint32
}
func NewReceivingWorker(kcp *KCP) *ReceivingWorker {
windowSize := effectiveConfig.GetReceivingWindowSize()
worker := &ReceivingWorker{
kcp: kcp,
queue: NewReceivingQueue(effectiveConfig.GetReceivingQueueSize()),
window: NewReceivingWindow(windowSize),
windowSize: windowSize,
}
worker.acklist = NewAckList(worker)
return worker
}
func (this *ReceivingWorker) ProcessSendingNext(number uint32) {
this.acklist.Clear(number)
}
func (this *ReceivingWorker) ProcessSegment(seg *DataSegment) {
number := seg.Number
idx := number - this.nextNumber
if idx >= this.windowSize {
return
}
this.ProcessSendingNext(seg.SendingNext)
this.acklist.Add(number, seg.Timestamp)
this.windowMutex.Lock()
defer this.windowMutex.Unlock()
if !this.window.Set(idx, seg) {
seg.Release()
}
for {
seg := this.window.RemoveFirst()
if seg == nil {
break
}
if !this.queue.Put(seg.Data) {
this.window.Set(0, seg)
break
}
seg.Data = nil
this.window.Advance()
this.nextNumber++
this.updated = true
}
}
func (this *ReceivingWorker) Read(b []byte) (int, error) {
return this.queue.Read(b)
}
func (this *ReceivingWorker) SetReadDeadline(t time.Time) {
this.queue.SetReadDeadline(t)
}
func (this *ReceivingWorker) Flush() {
this.acklist.Flush(this.kcp.current, this.kcp.rx_rto)
}
func (this *ReceivingWorker) Write(seg Segment) {
ackSeg := seg.(*AckSegment)
ackSeg.Conv = this.kcp.conv
ackSeg.ReceivingNext = this.nextNumber
ackSeg.ReceivingWindow = this.nextNumber + this.windowSize
if this.kcp.state == StateReadyToClose {
ackSeg.Opt = SegmentOptionClose
}
this.kcp.output.Write(ackSeg)
this.updated = false
}
func (this *ReceivingWorker) CloseRead() {
this.queue.Close()
}
func (this *ReceivingWorker) PingNecessary() bool {
return this.updated
}