Skip to content

Commit 15cd02d

Browse files
committed
Switch PacketBuilder to use TransmitBuf
This allows making TransmitBuf::buf private at last. Now all the logic it handles is fully encapsulated.
1 parent f9433cb commit 15cd02d

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(buf.segment_size() as u16);
660660
}
661661

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

664664
if buf.num_datagrams() == 1 && space_id == SpaceId::Data {
665665
// Now that we know the size of the first datagram, check whether
@@ -702,7 +702,7 @@ impl Connection {
702702
// datagram.
703703
// Finish current packet without adding extra padding
704704
if let Some(builder) = builder_storage.take() {
705-
builder.finish_and_track(now, self, sent_frames.take(), buf.buf);
705+
builder.finish_and_track(now, self, sent_frames.take(), &mut buf);
706706
}
707707
}
708708

@@ -826,7 +826,7 @@ impl Connection {
826826
non_retransmits: true,
827827
..SentFrames::default()
828828
}),
829-
buf.buf,
829+
&mut buf,
830830
);
831831
self.stats.udp_tx.on_sent(1, buf.len());
832832
return Some(Transmit {
@@ -882,7 +882,7 @@ impl Connection {
882882
builder.pad_to(MIN_INITIAL_SIZE);
883883
}
884884
let last_packet_number = builder.exact_number;
885-
builder.finish_and_track(now, self, sent_frames, buf.buf);
885+
builder.finish_and_track(now, self, sent_frames, &mut buf);
886886
self.path
887887
.congestion
888888
.on_sent(now, buf.len() as u64, last_packet_number);
@@ -920,7 +920,7 @@ impl Connection {
920920
non_retransmits: true,
921921
..Default::default()
922922
};
923-
builder.finish_and_track(now, self, Some(sent_frames), buf.buf);
923+
builder.finish_and_track(now, self, Some(sent_frames), &mut buf);
924924

925925
self.stats.path.sent_plpmtud_probes += 1;
926926

@@ -994,7 +994,7 @@ impl Connection {
994994
// sending a datagram of this size
995995
builder.pad_to(MIN_INITIAL_SIZE);
996996

997-
builder.finish(self, buf.buf);
997+
builder.finish(self, buf);
998998
self.stats.udp_tx.on_sent(1, buf.len());
999999

10001000
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 TransmitBuf<'_>,
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 TransmitBuf<'_>,
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_buf.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 TransmitBuf<'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
@@ -187,6 +187,11 @@ impl<'a> TransmitBuf<'a> {
187187
pub(super) fn len(&self) -> usize {
188188
self.buf.len()
189189
}
190+
191+
/// Returns the already written bytes in the buffer
192+
pub(super) fn as_mut_slice(&mut self) -> &mut [u8] {
193+
self.buf.as_mut_slice()
194+
}
190195
}
191196

192197
unsafe impl BufMut for TransmitBuf<'_> {

0 commit comments

Comments
 (0)