-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpacket_dispatch_queue.go
44 lines (37 loc) · 1.7 KB
/
packet_dispatch_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
package nex
// PacketDispatchQueue is an implementation of rdv::PacketDispatchQueue.
// PacketDispatchQueue is used to sequence incoming packets.
// In the original library each virtual connection stream only uses a single PacketDispatchQueue, but starting
// in PRUDPv1 NEX virtual connections may have multiple reliable substreams and thus multiple PacketDispatchQueues.
type PacketDispatchQueue struct {
queue map[uint16]PRUDPPacketInterface
nextExpectedSequenceId *Counter[uint16]
}
// Queue adds a packet to the queue to be dispatched
func (pdq *PacketDispatchQueue) Queue(packet PRUDPPacketInterface) {
pdq.queue[packet.SequenceID()] = packet
}
// GetNextToDispatch returns the next packet to be dispatched, nil if there are no packets
// and a boolean indicating whether anything was returned.
func (pdq *PacketDispatchQueue) GetNextToDispatch() (PRUDPPacketInterface, bool) {
if packet, ok := pdq.queue[pdq.nextExpectedSequenceId.Value]; ok {
return packet, true
}
return nil, false
}
// Dispatched removes a packet from the queue to be dispatched.
func (pdq *PacketDispatchQueue) Dispatched(packet PRUDPPacketInterface) {
pdq.nextExpectedSequenceId.Next()
delete(pdq.queue, packet.SequenceID())
}
// Purge clears the queue of all pending packets.
func (pdq *PacketDispatchQueue) Purge() {
clear(pdq.queue)
}
// NewPacketDispatchQueue initializes a new PacketDispatchQueue with a starting counter value.
func NewPacketDispatchQueue() *PacketDispatchQueue {
return &PacketDispatchQueue{
queue: make(map[uint16]PRUDPPacketInterface),
nextExpectedSequenceId: NewCounter[uint16](2), // * First DATA packet from a client will always be 2 as the CONNECT packet is assigned 1
}
}