Skip to content

Commit

Permalink
moq-transport: Add lengths to control messages
Browse files Browse the repository at this point in the history
  • Loading branch information
englishm committed Nov 2, 2024
1 parent dbf504f commit 5011ff1
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion moq-transport/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ macro_rules! message_types {
impl Decode for Message {
fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
let t = u64::decode(r)?;
let _len = u64::decode(r)?;

// TODO: Check the length of the message.

match t {
$($val => {
Expand All @@ -102,7 +105,18 @@ macro_rules! message_types {
match self {
$(Self::$name(ref m) => {
self.id().encode(w)?;
m.encode(w)

// Find out the length of the message
// by encoding it into a buffer and then encoding the length.
// This is a bit wasteful, but it's the only way to know the length.
let mut buf = Vec::new();
m.encode(&mut buf).unwrap();
(buf.len() as u64).encode(w)?;

// At least don't encode the message twice.
// Instead, write the buffer directly to the writer.
w.put_slice(&buf);
Ok(())
},)*
}
}
Expand Down

0 comments on commit 5011ff1

Please sign in to comment.