forked from Sandertv/go-raknet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet_queue.go
52 lines (47 loc) · 1.31 KB
/
packet_queue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package raknet
// packetQueue is an ordered queue for reliable ordered packets.
type packetQueue struct {
lowest uint24
highest uint24
queue map[uint24][]byte
}
// newPacketQueue returns a new initialised ordered queue.
func newPacketQueue() *packetQueue {
return &packetQueue{queue: make(map[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 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() uint24 {
return queue.highest - queue.lowest
}