Skip to content

Commit 86bcf52

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 76f0d0c commit 86bcf52

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 {
665665
buf.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(), buf.buf);
707+
builder.finish_and_track(now, self, sent_frames.take(), &mut buf);
708708
}
709709
}
710710

@@ -828,7 +828,7 @@ impl Connection {
828828
non_retransmits: true,
829829
..SentFrames::default()
830830
}),
831-
buf.buf,
831+
&mut buf,
832832
);
833833
self.stats.udp_tx.on_sent(1, buf.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, buf.buf);
887+
builder.finish_and_track(now, self, sent_frames, &mut buf);
888888
self.path
889889
.congestion
890890
.on_sent(now, buf.len() as u64, last_packet_number);
@@ -922,7 +922,7 @@ impl Connection {
922922
non_retransmits: true,
923923
..Default::default()
924924
};
925-
builder.finish_and_track(now, self, Some(sent_frames), buf.buf);
925+
builder.finish_and_track(now, self, Some(sent_frames), &mut buf);
926926

927927
self.stats.path.sent_plpmtud_probes += 1;
928928

@@ -996,7 +996,7 @@ impl Connection {
996996
// sending a datagram of this size
997997
builder.pad_to(MIN_INITIAL_SIZE);
998998

999-
builder.finish(self, buf.buf);
999+
builder.finish(self, buf);
10001000
self.stats.udp_tx.on_sent(1, buf.len());
10011001

10021002
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)