Move packet finalization into a seperate function
`finish_and_track_packet` will now take information from the builder
when finalizing the packet.
@@ -483,7 +483,7 @@ where
)?;
coalesce = coalesce && !builder.short_header;
- let sent = if close {
+ let sent_frames = if close {
trace!("sending CONNECTION_CLOSE");
match self.state {
State::Closed(state::Closed { ref reason }) => {
@@ -514,35 +514,18 @@ where
Some(self.populate_packet(space_id, &mut buf, buf_capacity - builder.tag_len))
};
- let exact_number = builder.exact_number;
- let (size, padded) = self.finish_packet(builder, &mut buf);
-
- if let Some(mut sent) = sent {
- sent.padding = padded;
+ if let Some(sent) = &sent_frames {
// If we sent any acks, don't immediately resend them. Setting this even if ack_only is
// false needlessly prevents us from ACKing the next packet if it's ACK-only, but saves
// the need for subtler logic to avoid double-transmitting acks all the time.
+ // This reset needs to happen before we check whether more data
+ // is available in this space - because otherwise it would return
+ // `true` purely due to the ACKs.
self.spaces[space_id].permit_ack_only &= sent.acks.is_empty();
-
- self.on_packet_sent(
- now,
- space_id,
- exact_number,
- SentPacket {
- acks: sent.acks,
- time_sent: now,
- size: if sent.padding || ack_eliciting {
- size as u16
- } else {
- 0
- },
- ack_eliciting,
- retransmits: sent.retransmits,
- stream_frames: sent.stream_frames,
- },
- );
}
+ self.finish_and_track_packet(now, builder, sent_frames, &mut buf);
+
if !coalesce || buf_capacity - buf.len() < MIN_PACKET_SPACE {
break;
}
@@ -748,6 +731,41 @@ where
(buffer.len() - encode_start, pad)
}
+ fn finish_and_track_packet(
+ &mut self,
+ now: Instant,
+ builder: PacketBuilder,
+ sent: Option<SentFrames>,
+ buffer: &mut Vec<u8>,
+ ) {
+ let ack_eliciting = builder.ack_eliciting;
+ let exact_number = builder.exact_number;
+ let space_id = builder.space;
+ let (size, padded) = self.finish_packet(builder, buffer);
+
+ if let Some(mut sent) = sent {
+ sent.padding = padded;
+
+ self.on_packet_sent(
+ now,
+ space_id,
+ exact_number,
+ SentPacket {
+ acks: sent.acks,
+ time_sent: now,
+ size: if sent.padding || ack_eliciting {
+ size as u16
+ } else {
+ 0
+ },
+ ack_eliciting,
+ retransmits: sent.retransmits,
+ stream_frames: sent.stream_frames,
+ },
+ );
+ }
+ }
+
/// Process `ConnectionEvent`s generated by the associated `Endpoint`
///
/// Will execute protocol logic upon receipt of a connection event, in turn preparing signals
