v2ray-core/transport/internet/kcp/receiving.go

99 lines
1.9 KiB
Go
Raw Normal View History

2016-06-25 19:35:18 +00:00
package kcp
type ReceivingWindow struct {
start uint32
size uint32
2016-06-29 08:34:34 +00:00
list []*DataSegment
2016-06-25 19:35:18 +00:00
}
func NewReceivingWindow(size uint32) *ReceivingWindow {
return &ReceivingWindow{
start: 0,
size: size,
2016-06-29 08:34:34 +00:00
list: make([]*DataSegment, size),
2016-06-25 19:35:18 +00:00
}
}
func (this *ReceivingWindow) Size() uint32 {
return this.size
}
func (this *ReceivingWindow) Position(idx uint32) uint32 {
return (idx + this.start) % this.size
}
2016-06-29 08:34:34 +00:00
func (this *ReceivingWindow) Set(idx uint32, value *DataSegment) bool {
2016-06-25 19:35:18 +00:00
pos := this.Position(idx)
if this.list[pos] != nil {
return false
}
this.list[pos] = value
return true
}
2016-06-29 08:34:34 +00:00
func (this *ReceivingWindow) Remove(idx uint32) *DataSegment {
2016-06-25 19:35:18 +00:00
pos := this.Position(idx)
e := this.list[pos]
this.list[pos] = nil
return e
}
2016-06-29 08:34:34 +00:00
func (this *ReceivingWindow) RemoveFirst() *DataSegment {
2016-06-25 19:35:18 +00:00
return this.Remove(0)
}
func (this *ReceivingWindow) Advance() {
this.start++
if this.start == this.size {
this.start = 0
}
}
2016-06-27 20:34:46 +00:00
type ACKList struct {
timestamps []uint32
numbers []uint32
}
func (this *ACKList) Add(number uint32, timestamp uint32) {
this.timestamps = append(this.timestamps, timestamp)
this.numbers = append(this.numbers, number)
}
2016-06-29 21:41:04 +00:00
func (this *ACKList) Clear(una uint32) bool {
2016-06-27 20:34:46 +00:00
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]
}
count++
}
}
2016-06-29 21:41:04 +00:00
if count < len(this.numbers) {
this.numbers = this.numbers[:count]
this.timestamps = this.timestamps[:count]
return true
}
return false
2016-06-27 20:34:46 +00:00
}
func (this *ACKList) AsSegment() *ACKSegment {
count := len(this.numbers)
2016-06-29 08:34:34 +00:00
if count == 0 {
return nil
2016-06-27 20:34:46 +00:00
}
2016-06-29 08:34:34 +00:00
if count > 128 {
count = 128
}
seg := &ACKSegment{
2016-06-27 20:34:46 +00:00
Count: byte(count),
NumberList: this.numbers[:count],
TimestampList: this.timestamps[:count],
}
2016-06-29 08:34:34 +00:00
//this.numbers = nil
//this.timestamps = nil
return seg
2016-06-27 20:34:46 +00:00
}