binate/frame/codec/
resume_ok.rs

1use super::*;
2use bytes::{Buf, BufMut, BytesMut};
3
4/// The resume_ok frame.
5///
6/// # Frame Contents
7///
8/// The general format for a Resume OK frame is given below.
9///
10/// ```text
11///  0                   1                   2                   3
12///  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
13/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
14/// |                         Stream ID = 0                         |
15/// +-----------+-+-+---------------+-------------------------------+
16/// |Frame Type |0|0|    Flags      |
17/// +-------------------------------+-------------------------------+
18/// |0|                                                             |
19/// +               Last Received Client Position                   +
20/// |                                                               |
21/// +---------------------------------------------------------------+
22/// ```
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub struct ResumeOkFrame {
25    last_received_client_position: u64,
26}
27
28impl ResumeOkFrame {
29    /// RESUME OK frames MUST always use Stream ID 0 as they pertain to the connection.
30    pub const STREAM_ID: u32 = 0;
31
32    /// Create a new `ResumeOk` frame.
33    ///
34    /// - `last_received_client_position` and `first_available_client_position` MUST be <=
35    /// [`MAX_U63`].
36    pub fn new(mut last_received_client_position: u64) -> Self {
37        debug_assert_max_u63!(last_received_client_position);
38        last_received_client_position &= MAX_U63;
39        ResumeOkFrame { last_received_client_position }
40    }
41
42    /// Returns the last implied position the server received from the client.
43    pub fn last_received_server_position(&self) -> u64 {
44        self.last_received_client_position
45    }
46}
47
48impl Encode for ResumeOkFrame {
49    fn encode(&self, buf: &mut BytesMut) {
50        buf.put_u32(0);
51        buf.put_u16(FrameType::RESUME_OK.bits());
52        buf.put_u64(self.last_received_client_position);
53    }
54
55    fn len(&self) -> usize {
56        // len(stream_id): 4
57        // len(flags): 2
58        // len(last_received_client_position): 8
59        14
60    }
61}
62
63impl Decode for ResumeOkFrame {
64    type Value = Self;
65
66    fn decode<B: Buf>(
67        buf: &mut B,
68        _stream_id: u32,
69        _flags: Flags,
70    ) -> Result<Self::Value> {
71        let last_received_client_position = eat_u63(buf)?;
72        Ok(ResumeOkFrame { last_received_client_position })
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn test_codec() {
82        let lease = ResumeOkFrame::new(1);
83
84        let mut buf = BytesMut::new();
85        lease.encode(&mut buf);
86        let mut buf = buf.freeze();
87
88        // len(stream_id): 4
89        // len(flags): 2
90        // len(last_received_client_position): 8
91        let buf_len = buf.len();
92        assert_eq!(buf_len, 4 + 2 + 8);
93
94        // Eat the stream_id and flags before decoding bytes.
95        let stream_id = eat_stream_id(&mut buf).unwrap();
96        let (frame_type, flags) = eat_flags(&mut buf).unwrap();
97        assert_eq!(frame_type, FrameType::RESUME_OK);
98        assert_eq!(flags, Flags::empty());
99
100        let decoded =
101            ResumeOkFrame::decode(&mut buf, stream_id, flags).unwrap();
102
103        assert_eq!(decoded, lease);
104        assert_eq!(lease.len(), buf_len);
105        assert_eq!(decoded.len(), buf_len);
106    }
107}