Skip to content

Commit

Permalink
Add additive flag.
Browse files Browse the repository at this point in the history
If present a received initial window update is intepreted as additive,
i.e. the amount of credit is added to the default of 256 KiB.
  • Loading branch information
twittner committed Aug 27, 2020
1 parent 56a97af commit eeebf9b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,7 @@ impl<T: AsyncRead + AsyncWrite + Unpin> Connection<T> {
}

let is_finish = frame.header().flags().contains(header::FIN); // half-close
let is_additive = frame.header().flags().contains(header::ADD); // additive window update

if frame.header().flags().contains(header::SYN) { // new stream
if !self.is_valid_remote_id(stream_id, Tag::WindowUpdate) {
Expand All @@ -761,7 +762,7 @@ impl<T: AsyncRead + AsyncWrite + Unpin> Connection<T> {
return Action::Terminate(Frame::protocol_error())
}
let stream = {
let credit = frame.header().credit();
let credit = frame.header().credit() + if is_additive { DEFAULT_CREDIT } else { 0 };
let config = self.config.clone();
let sender = self.stream_sender.clone();
let mut stream = Stream::new(stream_id, self.id, config, DEFAULT_CREDIT, credit, sender);
Expand Down
19 changes: 7 additions & 12 deletions src/frame/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,6 @@ impl Flags {
}
}

const MAX_FLAG_VAL: u16 = 8;

/// Indicates the start of a new stream.
pub const SYN: Flags = Flags(1);

Expand All @@ -346,6 +344,10 @@ pub const FIN: Flags = Flags(4);
/// Indicates an immediate stream reset.
pub const RST: Flags = Flags(8);

/// Temporary flag indicating that the initial window update is additive.
/// (See https://github.com/paritytech/yamux/issues/92)
pub const ADD: Flags = Flags(0x8000);

/// The serialised header size in bytes.
pub const HEADER_SIZE: usize = 12;

Expand Down Expand Up @@ -381,10 +383,6 @@ pub fn decode(buf: &[u8; HEADER_SIZE]) -> Result<Header<()>, HeaderDecodeError>
_marker: std::marker::PhantomData
};

if hdr.flags.0 > MAX_FLAG_VAL {
return Err(HeaderDecodeError::Flags(hdr.flags.0))
}

Ok(hdr)
}

Expand All @@ -395,17 +393,14 @@ pub enum HeaderDecodeError {
/// Unknown version.
Version(u8),
/// An unknown frame type.
Type(u8),
/// Unknown flags.
Flags(u16)
Type(u8)
}

impl std::fmt::Display for HeaderDecodeError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
HeaderDecodeError::Version(v) => write!(f, "unknown version: {}", v),
HeaderDecodeError::Type(t) => write!(f, "unknown frame type: {}", t),
HeaderDecodeError::Flags(x) => write!(f, "unknown flags type: {}", x)
HeaderDecodeError::Type(t) => write!(f, "unknown frame type: {}", t)
}
}
}
Expand All @@ -428,7 +423,7 @@ mod tests {
Header {
version: Version(0),
tag,
flags: Flags(std::cmp::min(g.gen(), MAX_FLAG_VAL)),
flags: Flags(g.gen()),
stream_id: StreamId(g.gen()),
length: Len(g.gen()),
_marker: std::marker::PhantomData
Expand Down

0 comments on commit eeebf9b

Please sign in to comment.