mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-05 00:47:51 -05:00
redo kcp.SendingWindow
This commit is contained in:
parent
bbeae5be48
commit
a8b0660df0
@ -1,6 +1,7 @@
|
|||||||
package kcp
|
package kcp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"container/list"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"v2ray.com/core/common"
|
"v2ray.com/core/common"
|
||||||
@ -8,31 +9,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type SendingWindow struct {
|
type SendingWindow struct {
|
||||||
start uint32
|
cache *list.List
|
||||||
cap uint32
|
|
||||||
len uint32
|
|
||||||
last uint32
|
|
||||||
|
|
||||||
data []DataSegment
|
|
||||||
inuse []bool
|
|
||||||
prev []uint32
|
|
||||||
next []uint32
|
|
||||||
|
|
||||||
totalInFlightSize uint32
|
totalInFlightSize uint32
|
||||||
writer SegmentWriter
|
writer SegmentWriter
|
||||||
onPacketLoss func(uint32)
|
onPacketLoss func(uint32)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSendingWindow(size uint32, writer SegmentWriter, onPacketLoss func(uint32)) *SendingWindow {
|
func NewSendingWindow(writer SegmentWriter, onPacketLoss func(uint32)) *SendingWindow {
|
||||||
window := &SendingWindow{
|
window := &SendingWindow{
|
||||||
start: 0,
|
cache: list.New(),
|
||||||
cap: size,
|
|
||||||
len: 0,
|
|
||||||
last: 0,
|
|
||||||
data: make([]DataSegment, size),
|
|
||||||
prev: make([]uint32, size),
|
|
||||||
next: make([]uint32, size),
|
|
||||||
inuse: make([]bool, size),
|
|
||||||
writer: writer,
|
writer: writer,
|
||||||
onPacketLoss: onPacketLoss,
|
onPacketLoss: onPacketLoss,
|
||||||
}
|
}
|
||||||
@ -43,82 +28,42 @@ func (sw *SendingWindow) Release() {
|
|||||||
if sw == nil {
|
if sw == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sw.len = 0
|
for sw.cache.Len() > 0 {
|
||||||
for _, seg := range sw.data {
|
seg := sw.cache.Front().Value.(*DataSegment)
|
||||||
seg.Release()
|
seg.Release()
|
||||||
|
sw.cache.Remove(sw.cache.Front())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sw *SendingWindow) Len() int {
|
func (sw *SendingWindow) Len() uint32 {
|
||||||
return int(sw.len)
|
return uint32(sw.cache.Len())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sw *SendingWindow) IsEmpty() bool {
|
func (sw *SendingWindow) IsEmpty() bool {
|
||||||
return sw.len == 0
|
return sw.cache.Len() == 0
|
||||||
}
|
|
||||||
|
|
||||||
func (sw *SendingWindow) Size() uint32 {
|
|
||||||
return sw.cap
|
|
||||||
}
|
|
||||||
|
|
||||||
func (sw *SendingWindow) IsFull() bool {
|
|
||||||
return sw.len == sw.cap
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sw *SendingWindow) Push(number uint32) *buf.Buffer {
|
func (sw *SendingWindow) Push(number uint32) *buf.Buffer {
|
||||||
pos := (sw.start + sw.len) % sw.cap
|
seg := NewDataSegment()
|
||||||
sw.data[pos].Number = number
|
seg.Number = number
|
||||||
sw.data[pos].timeout = 0
|
|
||||||
sw.data[pos].transmit = 0
|
sw.cache.PushBack(seg)
|
||||||
sw.inuse[pos] = true
|
return seg.Data()
|
||||||
if sw.len > 0 {
|
|
||||||
sw.next[sw.last] = pos
|
|
||||||
sw.prev[pos] = sw.last
|
|
||||||
}
|
|
||||||
sw.last = pos
|
|
||||||
sw.len++
|
|
||||||
return sw.data[pos].Data()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sw *SendingWindow) FirstNumber() uint32 {
|
func (sw *SendingWindow) FirstNumber() uint32 {
|
||||||
return sw.data[sw.start].Number
|
return sw.cache.Front().Value.(*DataSegment).Number
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sw *SendingWindow) Clear(una uint32) {
|
func (sw *SendingWindow) Clear(una uint32) {
|
||||||
for !sw.IsEmpty() && sw.data[sw.start].Number < una {
|
for !sw.IsEmpty() {
|
||||||
sw.Remove(0)
|
seg := sw.cache.Front().Value.(*DataSegment)
|
||||||
|
if seg.Number >= una {
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
seg.Release()
|
||||||
|
sw.cache.Remove(sw.cache.Front())
|
||||||
func (sw *SendingWindow) Remove(idx uint32) bool {
|
|
||||||
if sw.IsEmpty() {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pos := (sw.start + idx) % sw.cap
|
|
||||||
if !sw.inuse[pos] {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
sw.inuse[pos] = false
|
|
||||||
sw.totalInFlightSize--
|
|
||||||
if pos == sw.start && pos == sw.last {
|
|
||||||
sw.len = 0
|
|
||||||
sw.start = 0
|
|
||||||
sw.last = 0
|
|
||||||
} else if pos == sw.start {
|
|
||||||
delta := sw.next[pos] - sw.start
|
|
||||||
if sw.next[pos] < sw.start {
|
|
||||||
delta = sw.next[pos] + sw.cap - sw.start
|
|
||||||
}
|
|
||||||
sw.start = sw.next[pos]
|
|
||||||
sw.len -= delta
|
|
||||||
} else if pos == sw.last {
|
|
||||||
sw.last = sw.prev[pos]
|
|
||||||
} else {
|
|
||||||
sw.next[sw.prev[pos]] = sw.next[pos]
|
|
||||||
sw.prev[sw.next[pos]] = sw.prev[pos]
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sw *SendingWindow) HandleFastAck(number uint32, rto uint32) {
|
func (sw *SendingWindow) HandleFastAck(number uint32, rto uint32) {
|
||||||
@ -143,8 +88,9 @@ func (sw *SendingWindow) Visit(visitor func(seg *DataSegment) bool) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := sw.start; ; i = sw.next[i] {
|
for e := sw.cache.Front(); e != nil; e = e.Next() {
|
||||||
if !visitor(&sw.data[i]) || i == sw.last {
|
seg := e.Value.(*DataSegment)
|
||||||
|
if !visitor(seg) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -186,6 +132,23 @@ func (sw *SendingWindow) Flush(current uint32, rto uint32, maxInFlightSize uint3
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (sw *SendingWindow) Remove(number uint32) bool {
|
||||||
|
if sw.IsEmpty() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for e := sw.cache.Front(); e != nil; e = e.Next() {
|
||||||
|
seg := e.Value.(*DataSegment)
|
||||||
|
if seg.Number > number {
|
||||||
|
return false
|
||||||
|
} else if seg.Number == number {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
type SendingWorker struct {
|
type SendingWorker struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
conn *Connection
|
conn *Connection
|
||||||
@ -195,6 +158,7 @@ type SendingWorker struct {
|
|||||||
remoteNextNumber uint32
|
remoteNextNumber uint32
|
||||||
controlWindow uint32
|
controlWindow uint32
|
||||||
fastResend uint32
|
fastResend uint32
|
||||||
|
windowSize uint32
|
||||||
firstUnacknowledgedUpdated bool
|
firstUnacknowledgedUpdated bool
|
||||||
closed bool
|
closed bool
|
||||||
}
|
}
|
||||||
@ -205,8 +169,9 @@ func NewSendingWorker(kcp *Connection) *SendingWorker {
|
|||||||
fastResend: 2,
|
fastResend: 2,
|
||||||
remoteNextNumber: 32,
|
remoteNextNumber: 32,
|
||||||
controlWindow: kcp.Config.GetSendingInFlightSize(),
|
controlWindow: kcp.Config.GetSendingInFlightSize(),
|
||||||
|
windowSize: kcp.Config.GetSendingBufferSize(),
|
||||||
}
|
}
|
||||||
worker.window = NewSendingWindow(kcp.Config.GetSendingBufferSize(), worker, worker.OnPacketLoss)
|
worker.window = NewSendingWindow(worker, worker.OnPacketLoss)
|
||||||
return worker
|
return worker
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -247,7 +212,7 @@ func (w *SendingWorker) processAck(number uint32) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
removed := w.window.Remove(number - w.firstUnacknowledged)
|
removed := w.window.Remove(number)
|
||||||
if removed {
|
if removed {
|
||||||
w.FindFirstUnacknowledged()
|
w.FindFirstUnacknowledged()
|
||||||
}
|
}
|
||||||
@ -299,7 +264,7 @@ func (w *SendingWorker) Push(f buf.Supplier) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if w.window.IsFull() {
|
if w.window.Len() > w.windowSize {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,42 +0,0 @@
|
|||||||
package kcp_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
. "v2ray.com/core/transport/internet/kcp"
|
|
||||||
. "v2ray.com/ext/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestSendingWindow(t *testing.T) {
|
|
||||||
assert := With(t)
|
|
||||||
|
|
||||||
window := NewSendingWindow(5, nil, nil)
|
|
||||||
window.Push(0)
|
|
||||||
window.Push(1)
|
|
||||||
window.Push(2)
|
|
||||||
assert(window.Len(), Equals, 3)
|
|
||||||
|
|
||||||
window.Remove(1)
|
|
||||||
assert(window.Len(), Equals, 3)
|
|
||||||
assert(window.FirstNumber(), Equals, uint32(0))
|
|
||||||
|
|
||||||
window.Remove(0)
|
|
||||||
assert(window.Len(), Equals, 1)
|
|
||||||
assert(window.FirstNumber(), Equals, uint32(2))
|
|
||||||
|
|
||||||
window.Remove(0)
|
|
||||||
assert(window.Len(), Equals, 0)
|
|
||||||
|
|
||||||
window.Push(4)
|
|
||||||
assert(window.Len(), Equals, 1)
|
|
||||||
assert(window.FirstNumber(), Equals, uint32(4))
|
|
||||||
|
|
||||||
window.Push(5)
|
|
||||||
assert(window.Len(), Equals, 2)
|
|
||||||
|
|
||||||
window.Remove(1)
|
|
||||||
assert(window.Len(), Equals, 2)
|
|
||||||
|
|
||||||
window.Remove(0)
|
|
||||||
assert(window.Len(), Equals, 0)
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user