1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-22 13:14:28 -04:00
v2fly/transport/internet/kcp/sending.go

396 lines
8.4 KiB
Go
Raw Normal View History

2016-06-26 17:51:17 -04:00
package kcp
2016-07-03 16:14:38 -04:00
import (
"sync"
"github.com/v2ray/v2ray-core/common/alloc"
)
2016-07-01 05:57:13 -04:00
type SendingWindow struct {
start uint32
cap uint32
len uint32
last uint32
data []*DataSegment
prev []uint32
next []uint32
2016-07-04 09:34:14 -04:00
totalInFlightSize uint32
writer SegmentWriter
onPacketLoss func(uint32)
2016-07-01 05:57:13 -04:00
}
2016-07-04 09:54:18 -04:00
func NewSendingWindow(size uint32, writer SegmentWriter, onPacketLoss func(uint32)) *SendingWindow {
2016-07-01 05:57:13 -04:00
window := &SendingWindow{
2016-07-03 16:14:38 -04:00
start: 0,
cap: size,
len: 0,
last: 0,
data: make([]*DataSegment, size),
prev: make([]uint32, size),
next: make([]uint32, size),
writer: writer,
onPacketLoss: onPacketLoss,
2016-07-01 05:57:13 -04:00
}
return window
}
func (this *SendingWindow) Len() int {
return int(this.len)
}
2016-07-04 09:54:18 -04:00
func (this *SendingWindow) Size() uint32 {
return this.cap
}
func (this *SendingWindow) IsFull() bool {
return this.len == this.cap
}
2016-07-01 05:57:13 -04:00
func (this *SendingWindow) Push(seg *DataSegment) {
pos := (this.start + this.len) % this.cap
this.data[pos] = seg
if this.len > 0 {
this.next[this.last] = pos
this.prev[pos] = this.last
}
this.last = pos
this.len++
}
func (this *SendingWindow) First() *DataSegment {
return this.data[this.start]
}
func (this *SendingWindow) Clear(una uint32) {
2016-07-01 06:12:32 -04:00
for this.Len() > 0 && this.data[this.start].Number < una {
this.Remove(0)
2016-07-01 05:57:13 -04:00
}
}
func (this *SendingWindow) Remove(idx uint32) {
2016-07-01 17:27:57 -04:00
if this.len == 0 {
return
}
2016-07-01 05:57:13 -04:00
pos := (this.start + idx) % this.cap
seg := this.data[pos]
2016-07-01 06:12:32 -04:00
if seg == nil {
return
}
2016-07-04 09:34:14 -04:00
this.totalInFlightSize--
2016-07-01 05:57:13 -04:00
seg.Release()
this.data[pos] = nil
2016-07-01 16:54:59 -04:00
if pos == this.start && pos == this.last {
this.len = 0
this.start = 0
this.last = 0
} else if pos == this.start {
delta := this.next[pos] - this.start
if this.next[pos] < this.start {
delta = this.next[pos] + this.cap - this.start
2016-07-01 05:57:13 -04:00
}
2016-07-01 16:54:59 -04:00
this.start = this.next[pos]
this.len -= delta
2016-07-01 05:57:13 -04:00
} else if pos == this.last {
this.last = this.prev[pos]
} else {
this.next[this.prev[pos]] = this.next[pos]
this.prev[this.next[pos]] = this.prev[pos]
}
}
func (this *SendingWindow) HandleFastAck(number uint32) {
2016-07-01 17:27:57 -04:00
if this.len == 0 {
return
}
2016-07-01 06:12:32 -04:00
2016-07-01 05:57:13 -04:00
for i := this.start; ; i = this.next[i] {
seg := this.data[i]
2016-07-05 15:47:35 -04:00
if number-seg.Number > 0x7FFFFFFF {
2016-07-01 05:57:13 -04:00
break
}
if number != seg.Number {
seg.ackSkipped++
}
if i == this.last {
break
}
}
}
2016-07-04 09:54:18 -04:00
func (this *SendingWindow) Flush(current uint32, resend uint32, rto uint32, maxInFlightSize uint32) {
2016-07-01 06:12:32 -04:00
if this.Len() == 0 {
2016-07-03 16:14:38 -04:00
return
2016-07-01 06:12:32 -04:00
}
2016-07-04 09:34:14 -04:00
var lost uint32
2016-07-04 07:37:42 -04:00
var inFlightSize uint32
2016-07-01 05:57:13 -04:00
for i := this.start; ; i = this.next[i] {
segment := this.data[i]
needsend := false
if segment.transmit == 0 {
needsend = true
segment.transmit++
2016-07-03 16:14:38 -04:00
segment.timeout = current + rto
2016-07-04 09:34:14 -04:00
this.totalInFlightSize++
2016-07-05 15:47:35 -04:00
} else if current-segment.timeout < 0x7FFFFFFF {
2016-07-01 05:57:13 -04:00
needsend = true
segment.transmit++
2016-07-03 16:14:38 -04:00
segment.timeout = current + rto
2016-07-04 09:34:14 -04:00
lost++
2016-07-03 16:14:38 -04:00
} else if segment.ackSkipped >= resend {
2016-07-01 05:57:13 -04:00
needsend = true
segment.transmit++
segment.ackSkipped = 0
2016-07-03 16:14:38 -04:00
segment.timeout = current + rto
2016-07-01 05:57:13 -04:00
}
if needsend {
2016-07-05 17:02:52 -04:00
segment.Timestamp = current
2016-07-03 16:14:38 -04:00
this.writer.Write(segment)
2016-07-04 07:37:42 -04:00
inFlightSize++
2016-07-04 09:54:18 -04:00
if inFlightSize >= maxInFlightSize {
2016-07-04 07:37:42 -04:00
break
}
2016-07-01 05:57:13 -04:00
}
if i == this.last {
break
}
}
2016-07-04 09:34:14 -04:00
if inFlightSize > 0 && this.totalInFlightSize != 0 {
rate := lost * 100 / this.totalInFlightSize
this.onPacketLoss(rate)
}
2016-07-01 05:57:13 -04:00
}
2016-06-26 17:51:17 -04:00
type SendingQueue struct {
start uint32
cap uint32
len uint32
2016-06-29 04:34:34 -04:00
list []*DataSegment
2016-06-26 17:51:17 -04:00
}
func NewSendingQueue(size uint32) *SendingQueue {
return &SendingQueue{
start: 0,
cap: size,
2016-06-29 04:34:34 -04:00
list: make([]*DataSegment, size),
2016-06-26 17:51:17 -04:00
len: 0,
}
}
func (this *SendingQueue) IsFull() bool {
return this.len == this.cap
}
func (this *SendingQueue) IsEmpty() bool {
return this.len == 0
}
2016-06-29 04:34:34 -04:00
func (this *SendingQueue) Pop() *DataSegment {
2016-06-26 17:51:17 -04:00
if this.IsEmpty() {
return nil
}
seg := this.list[this.start]
this.list[this.start] = nil
this.len--
this.start++
if this.start == this.cap {
this.start = 0
}
return seg
}
2016-06-29 04:34:34 -04:00
func (this *SendingQueue) Push(seg *DataSegment) {
2016-06-26 17:51:17 -04:00
if this.IsFull() {
return
}
this.list[(this.start+this.len)%this.cap] = seg
this.len++
}
func (this *SendingQueue) Clear() {
for i := uint32(0); i < this.len; i++ {
this.list[(i+this.start)%this.cap].Release()
this.list[(i+this.start)%this.cap] = nil
}
this.start = 0
this.len = 0
}
func (this *SendingQueue) Len() uint32 {
return this.len
}
2016-07-03 16:14:38 -04:00
type SendingWorker struct {
sync.Mutex
2016-07-05 17:02:52 -04:00
conn *Connection
2016-07-03 16:14:38 -04:00
window *SendingWindow
queue *SendingQueue
firstUnacknowledged uint32
nextNumber uint32
remoteNextNumber uint32
controlWindow uint32
fastResend uint32
updated bool
}
2016-07-05 17:02:52 -04:00
func NewSendingWorker(kcp *Connection) *SendingWorker {
2016-07-03 16:14:38 -04:00
worker := &SendingWorker{
2016-07-05 17:02:52 -04:00
conn: kcp,
2016-07-03 16:14:38 -04:00
queue: NewSendingQueue(effectiveConfig.GetSendingQueueSize()),
fastResend: 2,
remoteNextNumber: 32,
2016-07-04 09:54:18 -04:00
controlWindow: effectiveConfig.GetSendingInFlightSize(),
2016-07-03 16:14:38 -04:00
}
2016-07-04 09:54:18 -04:00
worker.window = NewSendingWindow(effectiveConfig.GetSendingWindowSize(), worker, worker.OnPacketLoss)
2016-07-03 16:14:38 -04:00
return worker
}
func (this *SendingWorker) ProcessReceivingNext(nextNumber uint32) {
this.Lock()
defer this.Unlock()
this.window.Clear(nextNumber)
this.FindFirstUnacknowledged()
}
// @Private
func (this *SendingWorker) FindFirstUnacknowledged() {
prevUna := this.firstUnacknowledged
if this.window.Len() > 0 {
this.firstUnacknowledged = this.window.First().Number
} else {
this.firstUnacknowledged = this.nextNumber
}
if this.firstUnacknowledged != prevUna {
this.updated = true
}
}
func (this *SendingWorker) ProcessAck(number uint32) {
2016-07-04 09:54:18 -04:00
if number-this.firstUnacknowledged > this.window.Size() {
2016-07-03 16:14:38 -04:00
return
}
this.Lock()
defer this.Unlock()
this.window.Remove(number - this.firstUnacknowledged)
this.FindFirstUnacknowledged()
}
2016-07-05 17:02:52 -04:00
func (this *SendingWorker) ProcessSegment(current uint32, seg *AckSegment) {
2016-07-03 16:14:38 -04:00
if this.remoteNextNumber < seg.ReceivingWindow {
this.remoteNextNumber = seg.ReceivingWindow
}
this.ProcessReceivingNext(seg.ReceivingNext)
var maxack uint32
for i := 0; i < int(seg.Count); i++ {
timestamp := seg.TimestampList[i]
number := seg.NumberList[i]
2016-07-05 17:02:52 -04:00
if current-timestamp < 10000 {
2016-07-05 17:53:46 -04:00
this.conn.roundTrip.Update(current - timestamp)
2016-07-03 16:14:38 -04:00
}
this.ProcessAck(number)
if maxack < number {
maxack = number
}
}
this.Lock()
this.window.HandleFastAck(maxack)
this.Unlock()
}
func (this *SendingWorker) Push(b []byte) int {
nBytes := 0
for len(b) > 0 && !this.queue.IsFull() {
var size int
2016-07-05 17:02:52 -04:00
if len(b) > int(this.conn.mss) {
size = int(this.conn.mss)
2016-07-03 16:14:38 -04:00
} else {
size = len(b)
}
2016-07-05 04:28:23 -04:00
seg := NewDataSegment()
seg.Data = alloc.NewSmallBuffer().Clear().Append(b[:size])
2016-07-03 16:14:38 -04:00
this.Lock()
this.queue.Push(seg)
this.Unlock()
b = b[size:]
nBytes += size
}
return nBytes
}
2016-07-04 08:17:42 -04:00
func (this *SendingWorker) Write(seg Segment) {
2016-07-03 16:14:38 -04:00
dataSeg := seg.(*DataSegment)
2016-07-05 17:02:52 -04:00
dataSeg.Conv = this.conn.conv
2016-07-03 16:14:38 -04:00
dataSeg.SendingNext = this.firstUnacknowledged
dataSeg.Opt = 0
2016-07-05 17:02:52 -04:00
if this.conn.state == StateReadyToClose {
2016-07-03 16:14:38 -04:00
dataSeg.Opt = SegmentOptionClose
}
2016-07-05 17:02:52 -04:00
this.conn.output.Write(dataSeg)
2016-07-03 16:14:38 -04:00
this.updated = false
}
func (this *SendingWorker) PingNecessary() bool {
return this.updated
}
2016-07-04 09:34:14 -04:00
func (this *SendingWorker) OnPacketLoss(lossRate uint32) {
2016-07-05 17:53:46 -04:00
if !effectiveConfig.Congestion || this.conn.roundTrip.Timeout() == 0 {
2016-07-03 16:14:38 -04:00
return
}
2016-07-04 09:34:14 -04:00
if lossRate >= 15 {
2016-07-03 16:14:38 -04:00
this.controlWindow = 3 * this.controlWindow / 4
2016-07-04 09:34:14 -04:00
} else if lossRate <= 5 {
2016-07-03 16:14:38 -04:00
this.controlWindow += this.controlWindow / 4
}
2016-07-04 09:54:18 -04:00
if this.controlWindow < 16 {
this.controlWindow = 16
2016-07-03 16:14:38 -04:00
}
2016-07-04 09:54:18 -04:00
if this.controlWindow > 2*effectiveConfig.GetSendingInFlightSize() {
this.controlWindow = 2 * effectiveConfig.GetSendingInFlightSize()
2016-07-03 16:14:38 -04:00
}
}
2016-07-05 17:02:52 -04:00
func (this *SendingWorker) Flush(current uint32) {
2016-07-03 16:14:38 -04:00
this.Lock()
defer this.Unlock()
2016-07-04 09:54:18 -04:00
cwnd := this.firstUnacknowledged + effectiveConfig.GetSendingInFlightSize()
2016-07-03 16:14:38 -04:00
if cwnd > this.remoteNextNumber {
cwnd = this.remoteNextNumber
}
if effectiveConfig.Congestion && cwnd > this.firstUnacknowledged+this.controlWindow {
cwnd = this.firstUnacknowledged + this.controlWindow
}
2016-07-04 09:54:18 -04:00
for !this.queue.IsEmpty() && !this.window.IsFull() {
2016-07-03 16:14:38 -04:00
seg := this.queue.Pop()
seg.Number = this.nextNumber
2016-07-05 17:02:52 -04:00
seg.timeout = current
2016-07-03 16:14:38 -04:00
seg.ackSkipped = 0
seg.transmit = 0
this.window.Push(seg)
this.nextNumber++
}
2016-07-05 17:53:46 -04:00
this.window.Flush(current, this.conn.fastresend, this.conn.roundTrip.Timeout(), cwnd)
2016-07-03 16:14:38 -04:00
}
func (this *SendingWorker) CloseWrite() {
this.Lock()
defer this.Unlock()
this.window.Clear(0xFFFFFFFF)
this.queue.Clear()
}