You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/transport/internet/kcp/receiving.go

255 lines
4.9 KiB

package kcp
9 years ago
import (
"sync"
"v2ray.com/core/common/buf"
9 years ago
)
type ReceivingWindow struct {
start uint32
size uint32
list []*DataSegment
}
func NewReceivingWindow(size uint32) *ReceivingWindow {
return &ReceivingWindow{
start: 0,
size: size,
list: make([]*DataSegment, size),
}
}
8 years ago
func (v *ReceivingWindow) Size() uint32 {
return v.size
}
8 years ago
func (v *ReceivingWindow) Position(idx uint32) uint32 {
return (idx + v.start) % v.size
}
8 years ago
func (v *ReceivingWindow) Set(idx uint32, value *DataSegment) bool {
pos := v.Position(idx)
if v.list[pos] != nil {
return false
}
8 years ago
v.list[pos] = value
return true
}
8 years ago
func (v *ReceivingWindow) Remove(idx uint32) *DataSegment {
pos := v.Position(idx)
e := v.list[pos]
v.list[pos] = nil
return e
}
8 years ago
func (v *ReceivingWindow) RemoveFirst() *DataSegment {
return v.Remove(0)
}
8 years ago
func (v *ReceivingWindow) Advance() {
v.start++
if v.start == v.size {
v.start = 0
}
}
9 years ago
9 years ago
type AckList struct {
9 years ago
writer SegmentWriter
9 years ago
timestamps []uint32
numbers []uint32
nextFlush []uint32
flushCandidates []uint32
dirty bool
}
func NewAckList(writer SegmentWriter) *AckList {
9 years ago
return &AckList{
writer: writer,
timestamps: make([]uint32, 0, 128),
numbers: make([]uint32, 0, 128),
nextFlush: make([]uint32, 0, 128),
flushCandidates: make([]uint32, 0, 128),
}
9 years ago
}
8 years ago
func (v *AckList) Add(number uint32, timestamp uint32) {
v.timestamps = append(v.timestamps, timestamp)
v.numbers = append(v.numbers, number)
v.nextFlush = append(v.nextFlush, 0)
v.dirty = true
9 years ago
}
8 years ago
func (v *AckList) Clear(una uint32) {
9 years ago
count := 0
8 years ago
for i := 0; i < len(v.numbers); i++ {
if v.numbers[i] < una {
continue
9 years ago
}
if i != count {
8 years ago
v.numbers[count] = v.numbers[i]
v.timestamps[count] = v.timestamps[i]
v.nextFlush[count] = v.nextFlush[i]
}
count++
9 years ago
}
8 years ago
if count < len(v.numbers) {
v.numbers = v.numbers[:count]
v.timestamps = v.timestamps[:count]
v.nextFlush = v.nextFlush[:count]
v.dirty = true
}
9 years ago
}
8 years ago
func (v *AckList) Flush(current uint32, rto uint32) {
v.flushCandidates = v.flushCandidates[:0]
seg := NewAckSegment()
for i := 0; i < len(v.numbers); i++ {
8 years ago
if v.nextFlush[i] > current {
if len(v.flushCandidates) < cap(v.flushCandidates) {
v.flushCandidates = append(v.flushCandidates, v.numbers[i])
}
continue
}
8 years ago
seg.PutNumber(v.numbers[i])
seg.PutTimestamp(v.timestamps[i])
timeout := rto / 2
if timeout < 20 {
timeout = 20
}
8 years ago
v.nextFlush[i] = current + timeout
if seg.IsFull() {
v.writer.Write(seg)
seg.Release()
seg = NewAckSegment()
v.dirty = false
}
9 years ago
}
if v.dirty || !seg.IsEmpty() {
for _, number := range v.flushCandidates {
if seg.IsFull() {
break
}
seg.PutNumber(number)
}
8 years ago
v.writer.Write(seg)
seg.Release()
v.dirty = false
}
9 years ago
}
type ReceivingWorker struct {
sync.RWMutex
conn *Connection
leftOver *buf.Buffer
window *ReceivingWindow
acklist *AckList
nextNumber uint32
windowSize uint32
9 years ago
}
func NewReceivingWorker(kcp *Connection) *ReceivingWorker {
9 years ago
worker := &ReceivingWorker{
conn: kcp,
window: NewReceivingWindow(kcp.Config.GetReceivingBufferSize()),
windowSize: kcp.Config.GetReceivingInFlightSize(),
9 years ago
}
worker.acklist = NewAckList(worker)
9 years ago
return worker
}
8 years ago
func (v *ReceivingWorker) Release() {
v.leftOver.Release()
}
8 years ago
func (v *ReceivingWorker) ProcessSendingNext(number uint32) {
v.Lock()
defer v.Unlock()
8 years ago
v.acklist.Clear(number)
9 years ago
}
8 years ago
func (v *ReceivingWorker) ProcessSegment(seg *DataSegment) {
v.Lock()
defer v.Unlock()
9 years ago
number := seg.Number
8 years ago
idx := number - v.nextNumber
if idx >= v.windowSize {
9 years ago
return
}
8 years ago
v.acklist.Clear(seg.SendingNext)
v.acklist.Add(number, seg.Timestamp)
9 years ago
8 years ago
if !v.window.Set(idx, seg) {
9 years ago
seg.Release()
}
}
8 years ago
func (v *ReceivingWorker) Read(b []byte) int {
v.Lock()
defer v.Unlock()
total := 0
8 years ago
if v.leftOver != nil {
nBytes := copy(b, v.leftOver.Bytes())
8 years ago
if nBytes < v.leftOver.Len() {
v.leftOver.SliceFrom(nBytes)
return nBytes
}
8 years ago
v.leftOver.Release()
v.leftOver = nil
total += nBytes
}
9 years ago
for total < len(b) {
8 years ago
seg := v.window.RemoveFirst()
9 years ago
if seg == nil {
break
}
8 years ago
v.window.Advance()
v.nextNumber++
9 years ago
nBytes := copy(b[total:], seg.Data.Bytes())
total += nBytes
if nBytes < seg.Data.Len() {
seg.Data.SliceFrom(nBytes)
8 years ago
v.leftOver = seg.Data
seg.Data = nil
seg.Release()
break
}
seg.Release()
}
return total
9 years ago
}
8 years ago
func (v *ReceivingWorker) Flush(current uint32) {
v.Lock()
defer v.Unlock()
8 years ago
v.acklist.Flush(current, v.conn.roundTrip.Timeout())
9 years ago
}
func (v *ReceivingWorker) Write(seg Segment) error {
ackSeg := seg.(*AckSegment)
8 years ago
ackSeg.Conv = v.conn.conv
ackSeg.ReceivingNext = v.nextNumber
ackSeg.ReceivingWindow = v.nextNumber + v.windowSize
if v.conn.state == StateReadyToClose {
8 years ago
ackSeg.Option = SegmentOptionClose
9 years ago
}
return v.conn.output.Write(ackSeg)
9 years ago
}
8 years ago
func (v *ReceivingWorker) CloseRead() {
9 years ago
}
8 years ago
func (v *ReceivingWorker) UpdateNecessary() bool {
return len(v.acklist.numbers) > 0
}