diff --git a/packet_packer.go b/packet_packer.go index 783c067d..da2e4932 100644 --- a/packet_packer.go +++ b/packet_packer.go @@ -64,6 +64,25 @@ func (p *packetPacker) PackConnectionClose(ccf *frames.ConnectionCloseFrame, lea }, err } +func (p *packetPacker) PackAckPacket(leastUnacked protocol.PacketNumber, ackframe *frames.AckFrame) (*packedPacket, error) { + encLevel, sealer := p.cryptoSetup.GetSealer() + ph := p.getPublicHeader(leastUnacked, encLevel) + frames := []frames.Frame{ackframe} + if p.stopWaiting != nil { + p.stopWaiting.PacketNumber = ph.PacketNumber + p.stopWaiting.PacketNumberLen = ph.PacketNumberLen + frames = append(frames, p.stopWaiting) + p.stopWaiting = nil + } + raw, err := p.writeAndSealPacket(ph, frames, sealer) + return &packedPacket{ + number: ph.PacketNumber, + raw: raw, + frames: frames, + encryptionLevel: encLevel, + }, err +} + // RetransmitNonForwardSecurePacket retransmits a handshake packet, that was sent with less than forward-secure encryption func (p *packetPacker) RetransmitNonForwardSecurePacket(packet *ackhandler.Packet) (*packedPacket, error) { if packet.EncryptionLevel == protocol.EncryptionForwardSecure { diff --git a/packet_packer_test.go b/packet_packer_test.go index a556332f..971c9afd 100644 --- a/packet_packer_test.go +++ b/packet_packer_test.go @@ -2,6 +2,7 @@ package quic import ( "bytes" + "math" "github.com/lucas-clemente/quic-go/ackhandler" "github.com/lucas-clemente/quic-go/frames" @@ -703,4 +704,22 @@ var _ = Describe("Packet packer", func() { Expect(err).To(MatchError("PacketPacker BUG: Handshake retransmissions must contain a StopWaitingFrame")) }) }) + + Context("packing ACK packets", func() { + It("packs ACK packets", func() { + p, err := packer.PackAckPacket(0, &frames.AckFrame{}) + Expect(err).NotTo(HaveOccurred()) + Expect(p.frames).To(Equal([]frames.Frame{&frames.AckFrame{DelayTime: math.MaxInt64}})) + }) + + It("packs ACK packets with SWFs", func() { + packer.QueueControlFrameForNextPacket(&frames.StopWaitingFrame{}) + p, err := packer.PackAckPacket(0, &frames.AckFrame{}) + Expect(err).NotTo(HaveOccurred()) + Expect(p.frames).To(Equal([]frames.Frame{ + &frames.AckFrame{DelayTime: math.MaxInt64}, + &frames.StopWaitingFrame{PacketNumber: 1, PacketNumberLen: 2}, + })) + }) + }) }) diff --git a/session.go b/session.go index 15942648..eda61169 100644 --- a/session.go +++ b/session.go @@ -572,7 +572,20 @@ func (s *session) sendPacket() error { // Repeatedly try sending until we don't have any more data, or run out of the congestion window for { if !s.sentPacketHandler.SendingAllowed() { - return nil + // If we aren't allowed to send, at least try sending an ACK frame + ack := s.receivedPacketHandler.GetAckFrame() + if ack == nil { + return nil + } + swf := s.sentPacketHandler.GetStopWaitingFrame(false) + if swf != nil { + s.packer.QueueControlFrameForNextPacket(swf) + } + packet, err := s.packer.PackAckPacket(s.sentPacketHandler.GetLeastUnacked(), ack) + if err != nil { + return err + } + return s.sendPackedPacket(packet) } // check for retransmissions first