mirror of
https://github.com/refraction-networking/uquic.git
synced 2025-04-05 21:27:35 +03:00
add Len() and ContainsPacketNumber() to NackRange
This commit is contained in:
parent
a4debcac71
commit
2c95141ee1
4 changed files with 65 additions and 10 deletions
|
@ -107,7 +107,7 @@ func (h *sentPacketHandler) calculateExpectedEntropy(ackFrame *frames.AckFrame)
|
|||
nackRange = ackFrame.NackRanges[nackRangeIndex]
|
||||
}
|
||||
}
|
||||
if i >= nackRange.FirstPacketNumber && i <= nackRange.LastPacketNumber {
|
||||
if nackRange.ContainsPacketNumber(i) {
|
||||
packet, ok := h.packetHistory[i]
|
||||
if !ok {
|
||||
return 0, ErrMapAccess
|
||||
|
@ -156,7 +156,7 @@ func (h *sentPacketHandler) ReceivedAck(ackFrame *frames.AckFrame) error {
|
|||
nackRange = ackFrame.NackRanges[nackRangeIndex]
|
||||
}
|
||||
}
|
||||
if i >= nackRange.FirstPacketNumber && i <= nackRange.LastPacketNumber {
|
||||
if nackRange.ContainsPacketNumber(i) {
|
||||
h.nackPacket(i)
|
||||
} else {
|
||||
h.ackPacket(i)
|
||||
|
|
|
@ -8,12 +8,6 @@ import (
|
|||
"github.com/lucas-clemente/quic-go/utils"
|
||||
)
|
||||
|
||||
// NackRange is a NACK range
|
||||
type NackRange struct {
|
||||
FirstPacketNumber protocol.PacketNumber
|
||||
LastPacketNumber protocol.PacketNumber
|
||||
}
|
||||
|
||||
var errInvalidNackRanges = errors.New("AckFrame: ACK frame contains invalid NACK ranges")
|
||||
|
||||
// An AckFrame in QUIC
|
||||
|
@ -45,7 +39,7 @@ func (f *AckFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, pa
|
|||
// calculate the number of NackRanges that are about to be written
|
||||
// this number is different from len(f.NackRanges) for the case of contiguous NACK ranges
|
||||
for _, nackRange := range f.NackRanges {
|
||||
rangeLength := uint64(nackRange.LastPacketNumber - nackRange.FirstPacketNumber)
|
||||
rangeLength := nackRange.Len()
|
||||
numRanges += rangeLength/0xFF + 1
|
||||
if rangeLength > 0 && rangeLength%0xFF == 0 {
|
||||
numRanges--
|
||||
|
@ -69,7 +63,7 @@ func (f *AckFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, pa
|
|||
lastNackRange := f.NackRanges[i-1]
|
||||
missingPacketSequenceNumberDelta = uint64(lastNackRange.FirstPacketNumber) - uint64(nackRange.LastPacketNumber) - 1
|
||||
}
|
||||
rangeLength := nackRange.LastPacketNumber - nackRange.FirstPacketNumber
|
||||
rangeLength := nackRange.Len()
|
||||
|
||||
utils.WriteUint48(b, missingPacketSequenceNumberDelta)
|
||||
b.WriteByte(uint8(rangeLength % 0x100))
|
||||
|
|
22
frames/nack_range.go
Normal file
22
frames/nack_range.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package frames
|
||||
|
||||
import "github.com/lucas-clemente/quic-go/protocol"
|
||||
|
||||
// NackRange is a NACK range
|
||||
type NackRange struct {
|
||||
FirstPacketNumber protocol.PacketNumber
|
||||
LastPacketNumber protocol.PacketNumber
|
||||
}
|
||||
|
||||
// Len gets the lengths of a NackRange
|
||||
func (n *NackRange) Len() uint64 {
|
||||
return uint64(n.LastPacketNumber) - uint64(n.FirstPacketNumber)
|
||||
}
|
||||
|
||||
// IsInRange checks if a packetNumber is contained in a NACK range
|
||||
func (n *NackRange) ContainsPacketNumber(packetNumber protocol.PacketNumber) bool {
|
||||
if packetNumber >= n.FirstPacketNumber && packetNumber <= n.LastPacketNumber {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
39
frames/nack_range_test.go
Normal file
39
frames/nack_range_test.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
package frames
|
||||
|
||||
import (
|
||||
"github.com/lucas-clemente/quic-go/protocol"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("NackRange", func() {
|
||||
Context("Length", func() {
|
||||
It("calculates the length for a NACK range with only one packet", func() {
|
||||
nackRange := NackRange{FirstPacketNumber: 2, LastPacketNumber: 2}
|
||||
Expect(nackRange.Len()).To(Equal(uint64(0)))
|
||||
})
|
||||
|
||||
It("calculates the length for a NACK range with more than one packet", func() {
|
||||
nackRange := NackRange{FirstPacketNumber: 10, LastPacketNumber: 20}
|
||||
Expect(nackRange.Len()).To(Equal(uint64(10)))
|
||||
})
|
||||
})
|
||||
|
||||
Context("ContainsPacketNumber", func() {
|
||||
It("determines if a packet is in a NACK range with only one packet", func() {
|
||||
nackRange := NackRange{FirstPacketNumber: 2, LastPacketNumber: 2}
|
||||
Expect(nackRange.ContainsPacketNumber(protocol.PacketNumber(1))).To(BeFalse())
|
||||
Expect(nackRange.ContainsPacketNumber(protocol.PacketNumber(3))).To(BeFalse())
|
||||
Expect(nackRange.ContainsPacketNumber(protocol.PacketNumber(2))).To(BeTrue())
|
||||
})
|
||||
|
||||
It("determines if a packet is in a NACK range with more than one packet", func() {
|
||||
nackRange := NackRange{FirstPacketNumber: 10, LastPacketNumber: 20}
|
||||
Expect(nackRange.ContainsPacketNumber(protocol.PacketNumber(1))).To(BeFalse())
|
||||
Expect(nackRange.ContainsPacketNumber(protocol.PacketNumber(10))).To(BeTrue())
|
||||
Expect(nackRange.ContainsPacketNumber(protocol.PacketNumber(15))).To(BeTrue())
|
||||
Expect(nackRange.ContainsPacketNumber(protocol.PacketNumber(20))).To(BeTrue())
|
||||
Expect(nackRange.ContainsPacketNumber(protocol.PacketNumber(21))).To(BeFalse())
|
||||
})
|
||||
})
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue