route/vendor/github.com/lucas-clemente/quic-go/ackhandler/received_packet_handler.go

126 lines
3.4 KiB
Go
Raw Normal View History

2017-12-12 02:51:45 +00:00
package ackhandler
import (
"time"
2018-01-03 19:19:49 +00:00
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/wire"
2017-12-12 02:51:45 +00:00
)
type receivedPacketHandler struct {
largestObserved protocol.PacketNumber
2018-01-03 19:19:49 +00:00
ignoreBelow protocol.PacketNumber
2017-12-12 02:51:45 +00:00
largestObservedReceivedTime time.Time
packetHistory *receivedPacketHistory
ackSendDelay time.Duration
packetsReceivedSinceLastAck int
retransmittablePacketsReceivedSinceLastAck int
ackQueued bool
ackAlarm time.Time
2018-01-03 19:19:49 +00:00
lastAck *wire.AckFrame
version protocol.VersionNumber
2017-12-12 02:51:45 +00:00
}
// NewReceivedPacketHandler creates a new receivedPacketHandler
2018-01-03 19:19:49 +00:00
func NewReceivedPacketHandler(version protocol.VersionNumber) ReceivedPacketHandler {
2017-12-12 02:51:45 +00:00
return &receivedPacketHandler{
2018-01-03 19:19:49 +00:00
packetHistory: newReceivedPacketHistory(),
ackSendDelay: protocol.AckSendDelay,
version: version,
2017-12-12 02:51:45 +00:00
}
}
2018-01-20 18:07:01 +00:00
func (h *receivedPacketHandler) ReceivedPacket(packetNumber protocol.PacketNumber, rcvTime time.Time, shouldInstigateAck bool) error {
2018-01-03 19:19:49 +00:00
if packetNumber > h.largestObserved {
h.largestObserved = packetNumber
2018-01-20 18:07:01 +00:00
h.largestObservedReceivedTime = rcvTime
2017-12-12 02:51:45 +00:00
}
2018-01-03 19:19:49 +00:00
if packetNumber < h.ignoreBelow {
return nil
2017-12-12 02:51:45 +00:00
}
2018-01-03 19:19:49 +00:00
if err := h.packetHistory.ReceivedPacket(packetNumber); err != nil {
2017-12-12 02:51:45 +00:00
return err
}
2018-01-20 18:07:01 +00:00
h.maybeQueueAck(packetNumber, rcvTime, shouldInstigateAck)
2017-12-12 02:51:45 +00:00
return nil
}
2018-01-03 19:19:49 +00:00
// IgnoreBelow sets a lower limit for acking packets.
// Packets with packet numbers smaller than p will not be acked.
func (h *receivedPacketHandler) IgnoreBelow(p protocol.PacketNumber) {
h.ignoreBelow = p
h.packetHistory.DeleteBelow(p)
2017-12-12 02:51:45 +00:00
}
2018-01-20 18:07:01 +00:00
func (h *receivedPacketHandler) maybeQueueAck(packetNumber protocol.PacketNumber, rcvTime time.Time, shouldInstigateAck bool) {
2017-12-12 02:51:45 +00:00
h.packetsReceivedSinceLastAck++
if shouldInstigateAck {
h.retransmittablePacketsReceivedSinceLastAck++
}
// always ack the first packet
if h.lastAck == nil {
h.ackQueued = true
}
// if the packet number is smaller than the largest acked packet, it must have been reported missing with the last ACK
// note that it cannot be a duplicate because they're already filtered out by ReceivedPacket()
if h.lastAck != nil && packetNumber < h.lastAck.LargestAcked {
h.ackQueued = true
}
// check if a new missing range above the previously was created
2018-01-03 19:19:49 +00:00
if h.lastAck != nil && h.packetHistory.GetHighestAckRange().First > h.lastAck.LargestAcked {
2017-12-12 02:51:45 +00:00
h.ackQueued = true
}
if !h.ackQueued && shouldInstigateAck {
if h.retransmittablePacketsReceivedSinceLastAck >= protocol.RetransmittablePacketsBeforeAck {
h.ackQueued = true
} else {
if h.ackAlarm.IsZero() {
2018-01-20 18:07:01 +00:00
h.ackAlarm = rcvTime.Add(h.ackSendDelay)
2017-12-12 02:51:45 +00:00
}
}
}
if h.ackQueued {
// cancel the ack alarm
h.ackAlarm = time.Time{}
}
}
2018-01-03 19:19:49 +00:00
func (h *receivedPacketHandler) GetAckFrame() *wire.AckFrame {
2017-12-12 02:51:45 +00:00
if !h.ackQueued && (h.ackAlarm.IsZero() || h.ackAlarm.After(time.Now())) {
return nil
}
ackRanges := h.packetHistory.GetAckRanges()
2018-01-03 19:19:49 +00:00
ack := &wire.AckFrame{
2017-12-12 02:51:45 +00:00
LargestAcked: h.largestObserved,
2018-01-03 19:19:49 +00:00
LowestAcked: ackRanges[len(ackRanges)-1].First,
2017-12-12 02:51:45 +00:00
PacketReceivedTime: h.largestObservedReceivedTime,
}
if len(ackRanges) > 1 {
ack.AckRanges = ackRanges
}
h.lastAck = ack
h.ackAlarm = time.Time{}
h.ackQueued = false
h.packetsReceivedSinceLastAck = 0
h.retransmittablePacketsReceivedSinceLastAck = 0
return ack
}
2018-01-03 19:19:49 +00:00
func (h *receivedPacketHandler) GetAlarmTimeout() time.Time { return h.ackAlarm }