github.com/danielpfeifer02/quic-go-prio-packs@v0.41.0-28/internal/ackhandler/packet_number_generator.go (about) 1 package ackhandler 2 3 import ( 4 "fmt" 5 6 "github.com/danielpfeifer02/quic-go-prio-packs/internal/protocol" 7 "github.com/danielpfeifer02/quic-go-prio-packs/internal/utils" 8 ) 9 10 type packetNumberGenerator interface { 11 Peek() protocol.PacketNumber 12 // Pop pops the packet number. 13 // It reports if the packet number (before the one just popped) was skipped. 14 // It never skips more than one packet number in a row. 15 Pop() (skipped bool, _ protocol.PacketNumber) 16 } 17 18 type sequentialPacketNumberGenerator struct { 19 next protocol.PacketNumber 20 } 21 22 var _ packetNumberGenerator = &sequentialPacketNumberGenerator{} 23 24 func newSequentialPacketNumberGenerator(initial protocol.PacketNumber) packetNumberGenerator { 25 return &sequentialPacketNumberGenerator{next: initial} 26 } 27 28 func (p *sequentialPacketNumberGenerator) Peek() protocol.PacketNumber { 29 return p.next 30 } 31 32 func (p *sequentialPacketNumberGenerator) Pop() (bool, protocol.PacketNumber) { 33 next := p.next 34 p.next++ 35 return false, next 36 } 37 38 // The skippingPacketNumberGenerator generates the packet number for the next packet 39 // it randomly skips a packet number every averagePeriod packets (on average). 40 // It is guaranteed to never skip two consecutive packet numbers. 41 type skippingPacketNumberGenerator struct { 42 period protocol.PacketNumber 43 maxPeriod protocol.PacketNumber 44 45 next protocol.PacketNumber 46 nextToSkip protocol.PacketNumber 47 48 rng utils.Rand 49 } 50 51 var _ packetNumberGenerator = &skippingPacketNumberGenerator{} 52 53 func newSkippingPacketNumberGenerator(initial, initialPeriod, maxPeriod protocol.PacketNumber) packetNumberGenerator { 54 g := &skippingPacketNumberGenerator{ 55 next: initial, 56 period: initialPeriod, 57 maxPeriod: maxPeriod, 58 } 59 g.generateNewSkip() 60 return g 61 } 62 63 func (p *skippingPacketNumberGenerator) Peek() protocol.PacketNumber { 64 if p.next == p.nextToSkip { 65 return p.next + 1 66 } 67 return p.next 68 } 69 70 func (p *skippingPacketNumberGenerator) Pop() (bool, protocol.PacketNumber) { 71 next := p.next 72 if p.next == p.nextToSkip { 73 next++ 74 p.next += 2 75 p.generateNewSkip() 76 return true, next 77 } 78 p.next++ // generate a new packet number for the next packet 79 return false, next 80 } 81 82 func (p *skippingPacketNumberGenerator) generateNewSkip() { 83 // make sure that there are never two consecutive packet numbers that are skipped 84 p.nextToSkip = p.next + 3 + protocol.PacketNumber(p.rng.Int31n(int32(2*p.period))) 85 p.period = min(2*p.period, p.maxPeriod) 86 } 87 88 // PACKET_NUMBER_TAG 89 type settablePacketNumberGenerator struct { 90 next protocol.PacketNumber 91 changed bool 92 } 93 94 var _ packetNumberGenerator = &settablePacketNumberGenerator{} 95 96 func newSettablePacketNumberGenerator(initial protocol.PacketNumber) packetNumberGenerator { 97 return &settablePacketNumberGenerator{next: initial, changed: false} 98 } 99 100 func (p *settablePacketNumberGenerator) Peek() protocol.PacketNumber { 101 return p.next 102 } 103 104 func (p *settablePacketNumberGenerator) Pop() (bool, protocol.PacketNumber) { 105 next := p.next 106 p.next++ 107 oldChanged := p.changed 108 p.changed = false 109 return oldChanged, next // TODO: what does oldChanged == true cause? oldChanged is the value for skipped which might cause a protocol violation 110 } 111 112 func (p *settablePacketNumberGenerator) SetPacketNumber(next protocol.PacketNumber) { 113 if next < p.next { 114 panic("cannot lower the packet number") 115 } 116 if next == p.next { 117 fmt.Println("Packet number is the same as the current packet number. Some packet might have been shadowed.") 118 } 119 p.next = next 120 // p.changed = true 121 }