-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpacketcache.go
96 lines (78 loc) · 1.65 KB
/
packetcache.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package lrmp
type packetCache struct {
buffer []*Packet
mask int
}
/**
* constructs an LrmpPacketCache. The cache size should be multiple of
* two (2^n).
*/
func newPacketCache(size int) packetCache {
mask := 0
for mask = 1; mask < size; {
mask = mask << 1
}
buffer := make([]*Packet, mask)
mask--
pc := packetCache{buffer: buffer, mask: mask}
return pc
}
/**
* returns the maximum size of the cache.
*/
func (pc *packetCache) getMaxSize() int {
return pc.mask + 1
}
/**
* adds the given packet to the queue.
*/
func (pc *packetCache) addPacket(packet *Packet) {
i := int(packet.seqno) & pc.mask
pc.buffer[i] = packet
}
/**
* contains the packet.
*/
func (pc *packetCache) containPacket(seqno int64) bool {
i := int(seqno) & pc.mask
if pc.buffer[i] != nil {
return pc.buffer[i].seqno == seqno
}
return false
}
/**
* gets the packet corresponding to the given seqno.
*/
func (pc *packetCache) getPacket(seqno int64) *Packet {
i := int(seqno) & pc.mask
if pc.buffer[i] != nil && pc.buffer[i].seqno == seqno {
return pc.buffer[i]
}
return nil
}
/**
* remove the given packet from the queue.
*/
func (pc *packetCache) removePacket(obj *Packet) {
i := int(obj.seqno) & int(pc.mask)
if pc.buffer[i] != nil && pc.buffer[i].seqno == obj.seqno {
pc.buffer[i] = nil
}
}
/**
* remove the packet with the given seqno from the queue.
*/
func (pc *packetCache) removeBySeqNo(seqno int64) {
i := int(seqno) & pc.mask
if pc.buffer[i] != nil && pc.buffer[i].seqno == seqno {
pc.buffer[i] = nil
}
}
/**
* clear all cached packets
*/
func (pc *packetCache) clear() {
for i := 0; i < len(pc.buffer); i++ {
pc.buffer[i] = nil
}
}