implement a StreamFrame Queue to handle high priority StreamFrames for retransmission

fixes #21
This commit is contained in:
Marten Seemann
2016-04-27 21:05:58 +07:00
parent c641e24e8c
commit 6a7f331269
4 changed files with 190 additions and 12 deletions

66
stream_frame_queue.go Normal file
View File

@@ -0,0 +1,66 @@
package quic
import (
"sync"
"github.com/lucas-clemente/quic-go/frames"
)
// StreamFrameQueue is a Queue that handles StreamFrames
type StreamFrameQueue struct {
prioFrames []*frames.StreamFrame
frames []*frames.StreamFrame
mutex sync.Mutex
}
// Push adds a new StreamFrame to the queue
func (q *StreamFrameQueue) Push(frame *frames.StreamFrame, prio bool) {
q.mutex.Lock()
defer q.mutex.Unlock()
if prio {
q.prioFrames = append(q.prioFrames, frame)
} else {
q.frames = append(q.frames, frame)
}
}
// Len returns the total number of queued StreamFrames
func (q *StreamFrameQueue) Len() int {
q.mutex.Lock()
defer q.mutex.Unlock()
return len(q.prioFrames) + len(q.frames)
}
// Pop returns the next element and deletes it from the queue
func (q *StreamFrameQueue) Pop() *frames.StreamFrame {
q.mutex.Lock()
defer q.mutex.Unlock()
if len(q.prioFrames) > 0 {
frame := q.prioFrames[0]
q.prioFrames = q.prioFrames[1:]
return frame
}
if len(q.frames) > 0 {
frame := q.frames[0]
q.frames = q.frames[1:]
return frame
}
return nil
}
// Front returns the next element without modifying the queue
func (q *StreamFrameQueue) Front() *frames.StreamFrame {
q.mutex.Lock()
defer q.mutex.Unlock()
if len(q.prioFrames) > 0 {
return q.prioFrames[0]
}
if len(q.frames) > 0 {
return q.frames[0]
}
return nil
}