Skip to main content

ntex_h2/frame/
window_update.rs

1use crate::frame::{self, FrameError, Head, Kind, StreamId};
2
3use ntex_bytes::BufMut;
4
5const SIZE_INCREMENT_MASK: u32 = 1 << 31;
6
7#[derive(Debug, Copy, Clone, Eq, PartialEq)]
8pub struct WindowUpdate {
9    stream_id: StreamId,
10    size_increment: u32,
11}
12
13impl WindowUpdate {
14    pub fn new(stream_id: StreamId, size_increment: u32) -> WindowUpdate {
15        WindowUpdate {
16            stream_id,
17            size_increment,
18        }
19    }
20
21    pub fn stream_id(&self) -> StreamId {
22        self.stream_id
23    }
24
25    #[allow(clippy::cast_possible_wrap)]
26    pub fn size_increment(&self) -> i32 {
27        self.size_increment as i32
28    }
29
30    /// Builds a `WindowUpdate` frame from a raw frame.
31    pub fn load(head: Head, payload: &[u8]) -> Result<WindowUpdate, FrameError> {
32        debug_assert_eq!(head.kind(), crate::frame::Kind::WindowUpdate);
33        if payload.len() != 4 {
34            return Err(FrameError::BadFrameSize);
35        }
36
37        // Clear the most significant bit, as that is reserved and MUST be ignored
38        // when received.
39        let size_increment = unpack_octets_4!(payload, 0, u32) & !SIZE_INCREMENT_MASK;
40
41        Ok(WindowUpdate {
42            stream_id: head.stream_id(),
43            size_increment,
44        })
45    }
46
47    pub fn encode<B: BufMut>(&self, dst: &mut B) {
48        log::trace!(
49            "encoding WINDOW_UPDATE; id={:?}, inc={}",
50            self.stream_id,
51            self.size_increment
52        );
53        let head = Head::new(Kind::WindowUpdate, 0, self.stream_id);
54        head.encode(4, dst);
55        dst.put_u32(self.size_increment);
56    }
57}
58
59impl From<WindowUpdate> for frame::Frame {
60    fn from(src: WindowUpdate) -> Self {
61        frame::Frame::WindowUpdate(src)
62    }
63}