move the packet number generator to the ackhandler package

This commit is contained in:
Marten Seemann 2018-11-01 16:51:12 +07:00
parent 16848038be
commit ebbf839fa4
8 changed files with 184 additions and 83 deletions

View file

@ -59,6 +59,12 @@ func getMaxPacketSize(addr net.Addr) protocol.ByteCount {
return maxSize
}
type packetNumberManager interface {
PeekPacketNumber() protocol.PacketNumber
PopPacketNumber() protocol.PacketNumber
GetPacketNumberLen(protocol.PacketNumber) protocol.PacketNumberLen
}
type sealingManager interface {
GetSealer() (protocol.EncryptionLevel, handshake.Sealer)
GetSealerWithEncryptionLevel(protocol.EncryptionLevel) (handshake.Sealer, error)
@ -86,10 +92,9 @@ type packetPacker struct {
token []byte
packetNumberGenerator *packetNumberGenerator
getPacketNumberLen func(protocol.PacketNumber) protocol.PacketNumberLen
framer frameSource
acks ackFrameSource
pnManager packetNumberManager
framer frameSource
acks ackFrameSource
maxPacketSize protocol.ByteCount
hasSentPacket bool // has the packetPacker already sent a packet
@ -103,7 +108,7 @@ func newPacketPacker(
srcConnID protocol.ConnectionID,
initialStream cryptoStream,
handshakeStream cryptoStream,
getPacketNumberLen func(protocol.PacketNumber) protocol.PacketNumberLen,
packetNumberManager packetNumberManager,
remoteAddr net.Addr, // only used for determining the max packet size
token []byte,
cryptoSetup sealingManager,
@ -113,19 +118,18 @@ func newPacketPacker(
version protocol.VersionNumber,
) *packetPacker {
return &packetPacker{
cryptoSetup: cryptoSetup,
token: token,
destConnID: destConnID,
srcConnID: srcConnID,
initialStream: initialStream,
handshakeStream: handshakeStream,
perspective: perspective,
version: version,
framer: framer,
acks: acks,
getPacketNumberLen: getPacketNumberLen,
packetNumberGenerator: newPacketNumberGenerator(1, protocol.SkipPacketAveragePeriodLength),
maxPacketSize: getMaxPacketSize(remoteAddr),
cryptoSetup: cryptoSetup,
token: token,
destConnID: destConnID,
srcConnID: srcConnID,
initialStream: initialStream,
handshakeStream: handshakeStream,
perspective: perspective,
version: version,
framer: framer,
acks: acks,
pnManager: packetNumberManager,
maxPacketSize: getMaxPacketSize(remoteAddr),
}
}
@ -395,8 +399,8 @@ func (p *packetPacker) composeNextPacket(
}
func (p *packetPacker) getHeader(encLevel protocol.EncryptionLevel) *wire.Header {
pnum := p.packetNumberGenerator.Peek()
packetNumberLen := p.getPacketNumberLen(pnum)
pnum := p.pnManager.PeekPacketNumber()
packetNumberLen := p.pnManager.GetPacketNumberLen(pnum)
header := &wire.Header{
PacketNumber: pnum,
@ -481,7 +485,7 @@ func (p *packetPacker) writeAndSealPacket(
_ = sealer.Seal(raw[payloadStartIndex:payloadStartIndex], raw[payloadStartIndex:], header.PacketNumber, raw[:payloadStartIndex])
raw = raw[0 : buffer.Len()+sealer.Overhead()]
num := p.packetNumberGenerator.Pop()
num := p.pnManager.PopPacketNumber()
if num != header.PacketNumber {
return nil, errors.New("packetPacker BUG: Peeked and Popped packet numbers do not match")
}