add Len() and ContainsPacketNumber() to NackRange

This commit is contained in:
Marten Seemann 2016-04-27 16:04:50 +07:00
parent a4debcac71
commit 2c95141ee1
4 changed files with 65 additions and 10 deletions

View file

@ -107,7 +107,7 @@ func (h *sentPacketHandler) calculateExpectedEntropy(ackFrame *frames.AckFrame)
nackRange = ackFrame.NackRanges[nackRangeIndex] nackRange = ackFrame.NackRanges[nackRangeIndex]
} }
} }
if i >= nackRange.FirstPacketNumber && i <= nackRange.LastPacketNumber { if nackRange.ContainsPacketNumber(i) {
packet, ok := h.packetHistory[i] packet, ok := h.packetHistory[i]
if !ok { if !ok {
return 0, ErrMapAccess return 0, ErrMapAccess
@ -156,7 +156,7 @@ func (h *sentPacketHandler) ReceivedAck(ackFrame *frames.AckFrame) error {
nackRange = ackFrame.NackRanges[nackRangeIndex] nackRange = ackFrame.NackRanges[nackRangeIndex]
} }
} }
if i >= nackRange.FirstPacketNumber && i <= nackRange.LastPacketNumber { if nackRange.ContainsPacketNumber(i) {
h.nackPacket(i) h.nackPacket(i)
} else { } else {
h.ackPacket(i) h.ackPacket(i)

View file

@ -8,12 +8,6 @@ import (
"github.com/lucas-clemente/quic-go/utils" "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") var errInvalidNackRanges = errors.New("AckFrame: ACK frame contains invalid NACK ranges")
// An AckFrame in QUIC // 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 // 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 // this number is different from len(f.NackRanges) for the case of contiguous NACK ranges
for _, nackRange := range f.NackRanges { for _, nackRange := range f.NackRanges {
rangeLength := uint64(nackRange.LastPacketNumber - nackRange.FirstPacketNumber) rangeLength := nackRange.Len()
numRanges += rangeLength/0xFF + 1 numRanges += rangeLength/0xFF + 1
if rangeLength > 0 && rangeLength%0xFF == 0 { if rangeLength > 0 && rangeLength%0xFF == 0 {
numRanges-- numRanges--
@ -69,7 +63,7 @@ func (f *AckFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, pa
lastNackRange := f.NackRanges[i-1] lastNackRange := f.NackRanges[i-1]
missingPacketSequenceNumberDelta = uint64(lastNackRange.FirstPacketNumber) - uint64(nackRange.LastPacketNumber) - 1 missingPacketSequenceNumberDelta = uint64(lastNackRange.FirstPacketNumber) - uint64(nackRange.LastPacketNumber) - 1
} }
rangeLength := nackRange.LastPacketNumber - nackRange.FirstPacketNumber rangeLength := nackRange.Len()
utils.WriteUint48(b, missingPacketSequenceNumberDelta) utils.WriteUint48(b, missingPacketSequenceNumberDelta)
b.WriteByte(uint8(rangeLength % 0x100)) b.WriteByte(uint8(rangeLength % 0x100))

22
frames/nack_range.go Normal file
View 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
View 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())
})
})
})