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/sending.go

375 lines
7.6 KiB

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