Skip to content

Commit

Permalink
moq-transport: Add lengths to control messages
Browse files Browse the repository at this point in the history
draft-06 change

See: [PR 520](moq-wg/moq-transport#520), [§6](https://www.ietf.org/archive/id/draft-ietf-moq-transport-06.html#section-6), [§1.3](https://www.ietf.org/archive/id/draft-ietf-moq-transport-06.html#section-1.3-5.2.1)

We're not doing anything particularly useful with this. Partly, this is
because there seems to be agreement that messages shouldn't be skipped,
and it's not clear what else we'd use these lengths for.
  • Loading branch information
englishm committed Nov 2, 2024
1 parent dbf504f commit d3ac08b
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion moq-transport/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ 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)?;

if r.remaining() < len as usize {
return Err(DecodeError::InvalidLength(len as usize, r.remaining()));
}

match t {
$($val => {
Expand All @@ -102,7 +107,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 d3ac08b

Please sign in to comment.