55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
package net
|
|
|
|
import "cimeyclust.com/steel/pkg/utils"
|
|
|
|
// packetQueue is an ordered queue for reliable ordered packets.
|
|
type packetQueue struct {
|
|
lowest utils.Uint24
|
|
highest utils.Uint24
|
|
queue map[utils.Uint24][]byte
|
|
}
|
|
|
|
// newPacketQueue returns a new initialised ordered queue.
|
|
func newPacketQueue() *packetQueue {
|
|
return &packetQueue{queue: make(map[utils.Uint24][]byte)}
|
|
}
|
|
|
|
// put puts a value at the index passed. If the index was already occupied
|
|
// once, false is returned.
|
|
func (queue *packetQueue) put(index utils.Uint24, packet []byte) bool {
|
|
if index < queue.lowest {
|
|
return false
|
|
}
|
|
if _, ok := queue.queue[index]; ok {
|
|
return false
|
|
}
|
|
if index >= queue.highest {
|
|
queue.highest = index + 1
|
|
}
|
|
queue.queue[index] = packet
|
|
return true
|
|
}
|
|
|
|
// fetch attempts to take out as many values from the ordered queue as
|
|
// possible. Upon encountering an index that has no value yet, the function
|
|
// returns all values that it did find and takes them out.
|
|
func (queue *packetQueue) fetch() (packets [][]byte) {
|
|
index := queue.lowest
|
|
for index < queue.highest {
|
|
packet, ok := queue.queue[index]
|
|
if !ok {
|
|
break
|
|
}
|
|
delete(queue.queue, index)
|
|
packets = append(packets, packet)
|
|
index++
|
|
}
|
|
queue.lowest = index
|
|
return
|
|
}
|
|
|
|
// WindowSize returns the size of the window held by the packet queue.
|
|
func (queue *packetQueue) WindowSize() utils.Uint24 {
|
|
return queue.highest - queue.lowest
|
|
}
|