-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathpacket_tracking.go
114 lines (92 loc) · 2.36 KB
/
packet_tracking.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package probing
import (
"sync"
"time"
"github.com/google/uuid"
)
type PacketTracker struct {
currentUUID uuid.UUID
packets map[uuid.UUID]PacketSequence
sequence int
nextSequence int
timeout time.Duration
timeoutCh chan *inFlightPacket
mutex sync.RWMutex
}
type PacketSequence struct {
packets map[int]inFlightPacket
}
func (ps PacketSequence) NewInflightPacket(sequence int) {
ps.packets[sequence] = inFlightPacket{}
}
func (ps PacketSequence) GetPacket(sequence int) (inFlightPacket, bool) {
packet, ok := ps.packets[sequence]
return packet, ok
}
func (ps PacketSequence) RemovePacket(sequence int) {
delete(ps.packets, sequence)
}
type inFlightPacket struct {
timeoutTimer *time.Timer
}
func newPacketTracker(t time.Duration) *PacketTracker {
firstUUID := uuid.New()
var firstSequence = map[uuid.UUID]map[int]struct{}{}
firstSequence[firstUUID] = make(map[int]struct{})
return &PacketTracker{
packets: map[uuid.UUID]PacketSequence{},
sequence: 0,
timeout: t,
}
}
func (t *PacketTracker) AddPacket() int {
t.mutex.Lock()
defer t.mutex.Unlock()
if t.nextSequence > 65535 {
newUUID := uuid.New()
t.packets[newUUID] = PacketSequence{}
t.currentUUID = newUUID
t.nextSequence = 0
}
t.sequence = t.nextSequence
t.packets[t.currentUUID].NewInflightPacket(t.sequence)
// if t.timeout > 0 {
// t.packets[t.currentUUID][t.sequence].timeoutTimer = time.Timer(t.timeout)
// }
t.nextSequence++
return t.sequence
}
// DeletePacket removes a packet from the tracker.
func (t *PacketTracker) DeletePacket(u uuid.UUID, seq int) {
t.mutex.Lock()
defer t.mutex.Unlock()
if t.hasPacket(u, seq) {
// if _, ok := t.packets[u].GetPacket(seq) ; ok != nil {
// t.packets[u][seq].timeoutTimer.Stop()
// }
t.packets[u].RemovePacket(seq)
}
}
func (t *PacketTracker) hasPacket(u uuid.UUID, seq int) bool {
inflight, ok := t.packets[u]
if ok == false {
return ok
}
_, ok = inflight.GetPacket(seq)
return ok
}
// HasPacket checks the tracker to see if it's currently tracking a packet.
func (t *PacketTracker) HasPacket(u uuid.UUID, seq int) bool {
t.mutex.RLock()
defer t.mutex.Unlock()
return t.hasPacket(u, seq)
}
func (t *PacketTracker) HasUUID(u uuid.UUID) bool {
_, hasUUID := t.packets[u]
return hasUUID
}
func (t *PacketTracker) CurrentUUID() uuid.UUID {
// t.mutex.RLock()
// defer t.mutex.Unlock()
return t.currentUUID
}