webrtc_sctp/chunk/
chunk_shutdown.rs

1use std::fmt;
2
3use bytes::{Buf, BufMut, Bytes, BytesMut};
4
5use super::chunk_header::*;
6use super::chunk_type::*;
7use super::*;
8
9///chunkShutdown represents an SCTP Chunk of type chunkShutdown
10///
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///|   Type = 7    | Chunk  Flags  |      Length = 8               |
15///+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16///|                      Cumulative TSN Ack                       |
17///+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
18#[derive(Default, Debug, Clone)]
19pub(crate) struct ChunkShutdown {
20    pub(crate) cumulative_tsn_ack: u32,
21}
22
23pub(crate) const CUMULATIVE_TSN_ACK_LENGTH: usize = 4;
24
25/// makes chunkShutdown printable
26impl fmt::Display for ChunkShutdown {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        write!(f, "{}", self.header())
29    }
30}
31
32impl Chunk for ChunkShutdown {
33    fn header(&self) -> ChunkHeader {
34        ChunkHeader {
35            typ: CT_SHUTDOWN,
36            flags: 0,
37            value_length: self.value_length() as u16,
38        }
39    }
40
41    fn unmarshal(raw: &Bytes) -> Result<Self> {
42        let header = ChunkHeader::unmarshal(raw)?;
43
44        if header.typ != CT_SHUTDOWN {
45            return Err(Error::ErrChunkTypeNotShutdown);
46        }
47
48        if raw.len() != CHUNK_HEADER_SIZE + CUMULATIVE_TSN_ACK_LENGTH {
49            return Err(Error::ErrInvalidChunkSize);
50        }
51
52        let reader = &mut raw.slice(CHUNK_HEADER_SIZE..CHUNK_HEADER_SIZE + header.value_length());
53
54        let cumulative_tsn_ack = reader.get_u32();
55
56        Ok(ChunkShutdown { cumulative_tsn_ack })
57    }
58
59    fn marshal_to(&self, writer: &mut BytesMut) -> Result<usize> {
60        self.header().marshal_to(writer)?;
61        writer.put_u32(self.cumulative_tsn_ack);
62        Ok(writer.len())
63    }
64
65    fn check(&self) -> Result<()> {
66        Ok(())
67    }
68
69    fn value_length(&self) -> usize {
70        CUMULATIVE_TSN_ACK_LENGTH
71    }
72
73    fn as_any(&self) -> &(dyn Any + Send + Sync) {
74        self
75    }
76}