1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-05-05 20:19:15 -04:00

high performance sending queue

This commit is contained in:
v2ray
2016-06-26 23:51:17 +02:00
parent 67ac925ee7
commit 3925b62751
4 changed files with 149 additions and 47 deletions

View File

@@ -0,0 +1,60 @@
package kcp
type SendingQueue struct {
start uint32
cap uint32
len uint32
list []*Segment
}
func NewSendingQueue(size uint32) *SendingQueue {
return &SendingQueue{
start: 0,
cap: size,
list: make([]*Segment, size),
len: 0,
}
}
func (this *SendingQueue) IsFull() bool {
return this.len == this.cap
}
func (this *SendingQueue) IsEmpty() bool {
return this.len == 0
}
func (this *SendingQueue) Pop() *Segment {
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
}
func (this *SendingQueue) Push(seg *Segment) {
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
}