Skip to content

Commit fb11268

Browse files
committed
Switch PacketBuilder to use TransmitBuilder
This allows making TransmitBuilder::buf private at last. Now all the logic it handles is fully encapsulated.
1 parent 17262a0 commit fb11268

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

quinn-proto/src/connection/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ impl Connection {
659659
builder.pad_to(transmit.segment_size() as u16);
660660
}
661661

662-
builder.finish_and_track(now, self, sent_frames.take(), transmit.buf);
662+
builder.finish_and_track(now, self, sent_frames.take(), &mut transmit);
663663

664664
if transmit.num_datagrams() == 1 {
665665
transmit.clip_datagram_size();
@@ -704,7 +704,7 @@ impl Connection {
704704
// datagram.
705705
// Finish current packet without adding extra padding
706706
if let Some(builder) = builder_storage.take() {
707-
builder.finish_and_track(now, self, sent_frames.take(), transmit.buf);
707+
builder.finish_and_track(now, self, sent_frames.take(), &mut transmit);
708708
}
709709
}
710710

@@ -828,7 +828,7 @@ impl Connection {
828828
non_retransmits: true,
829829
..SentFrames::default()
830830
}),
831-
transmit.buf,
831+
&mut transmit,
832832
);
833833
self.stats.udp_tx.on_sent(1, transmit.len());
834834
return Some(Transmit {
@@ -884,7 +884,7 @@ impl Connection {
884884
builder.pad_to(MIN_INITIAL_SIZE);
885885
}
886886
let last_packet_number = builder.exact_number;
887-
builder.finish_and_track(now, self, sent_frames, transmit.buf);
887+
builder.finish_and_track(now, self, sent_frames, &mut transmit);
888888
self.path
889889
.congestion
890890
.on_sent(now, transmit.len() as u64, last_packet_number);
@@ -928,7 +928,7 @@ impl Connection {
928928
non_retransmits: true,
929929
..Default::default()
930930
};
931-
builder.finish_and_track(now, self, Some(sent_frames), transmit.buf);
931+
builder.finish_and_track(now, self, Some(sent_frames), &mut transmit);
932932

933933
self.stats.path.sent_plpmtud_probes += 1;
934934

@@ -1006,7 +1006,7 @@ impl Connection {
10061006
// sending a datagram of this size
10071007
builder.pad_to(MIN_INITIAL_SIZE);
10081008

1009-
builder.finish(self, buf.buf);
1009+
builder.finish(self, buf);
10101010
self.stats.udp_tx.on_sent(1, buf.len());
10111011

10121012
Some(Transmit {

quinn-proto/src/connection/packet_builder.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use bytes::Bytes;
1+
use bytes::{BufMut, Bytes};
22
use rand::Rng;
33
use tracing::{trace, trace_span};
44

@@ -120,9 +120,9 @@ impl PacketBuilder {
120120
version,
121121
}),
122122
};
123-
let partial_encode = header.encode(buffer.buf);
123+
let partial_encode = header.encode(buffer);
124124
if conn.peer_params.grease_quic_bit && conn.rng.random() {
125-
buffer.buf[partial_encode.start] ^= FIXED_BIT;
125+
buffer.as_mut_slice()[partial_encode.start] ^= FIXED_BIT;
126126
}
127127

128128
let (sample_size, tag_len) = if let Some(ref crypto) = space.crypto {
@@ -183,7 +183,7 @@ impl PacketBuilder {
183183
now: Instant,
184184
conn: &mut Connection,
185185
sent: Option<SentFrames>,
186-
buffer: &mut Vec<u8>,
186+
buffer: &mut TransmitBuilder<'_>,
187187
) {
188188
let ack_eliciting = self.ack_eliciting;
189189
let exact_number = self.exact_number;
@@ -226,11 +226,15 @@ impl PacketBuilder {
226226
}
227227

228228
/// Encrypt packet, returning the length of the packet and whether padding was added
229-
pub(super) fn finish(self, conn: &mut Connection, buffer: &mut Vec<u8>) -> (usize, bool) {
229+
pub(super) fn finish(
230+
self,
231+
conn: &mut Connection,
232+
buffer: &mut TransmitBuilder<'_>,
233+
) -> (usize, bool) {
230234
let pad = buffer.len() < self.min_size;
231235
if pad {
232236
trace!("PADDING * {}", self.min_size - buffer.len());
233-
buffer.resize(self.min_size, 0);
237+
buffer.put_bytes(0, self.min_size - buffer.len());
234238
}
235239

236240
let space = &conn.spaces[self.space];
@@ -249,9 +253,9 @@ impl PacketBuilder {
249253
"Mismatching crypto tag len"
250254
);
251255

252-
buffer.resize(buffer.len() + packet_crypto.tag_len(), 0);
256+
buffer.put_bytes(0, packet_crypto.tag_len());
253257
let encode_start = self.partial_encode.start;
254-
let packet_buf = &mut buffer[encode_start..];
258+
let packet_buf = &mut buffer.as_mut_slice()[encode_start..];
255259
self.partial_encode.finish(
256260
packet_buf,
257261
header_crypto,

quinn-proto/src/connection/transmit_builder.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use super::BufLen;
2727
#[derive(Debug)]
2828
pub(super) struct TransmitBuilder<'a> {
2929
/// The buffer itself, packets are written to this buffer
30-
pub(super) buf: &'a mut Vec<u8>,
30+
buf: &'a mut Vec<u8>,
3131
/// Offset into the buffer at which the current datagram starts
3232
///
3333
/// Note that when coalescing packets this might be before the start of the current
@@ -142,6 +142,11 @@ impl<'a> TransmitBuilder<'a> {
142142
self.buf_capacity = self.buf.len();
143143
}
144144

145+
/// Returns the already written bytes in the buffer
146+
pub(super) fn as_mut_slice(&mut self) -> &mut [u8] {
147+
self.buf.as_mut_slice()
148+
}
149+
145150
/// Returns the GSO segment size
146151
///
147152
/// This is also the maximum size datagrams are allowed to be. The first and last

0 commit comments

Comments
 (0)