Skip to main content

rtc_dtls/
application_data.rs

1use bytes::BytesMut;
2use std::io::{Read, Write};
3
4use super::content::*;
5use shared::error::Result;
6
7// Application data messages are carried by the record layer and are
8// fragmented, compressed, and encrypted based on the current connection
9// state.  The messages are treated as transparent data to the record
10// layer.
11/// ## Specifications
12///
13/// * [RFC 5246 §10]
14///
15/// [RFC 5246 §10]: https://tools.ietf.org/html/rfc5246#section-10
16#[derive(Clone, PartialEq, Eq, Debug)]
17pub struct ApplicationData {
18    pub data: BytesMut,
19}
20
21impl ApplicationData {
22    pub fn content_type(&self) -> ContentType {
23        ContentType::ApplicationData
24    }
25
26    pub fn size(&self) -> usize {
27        self.data.len()
28    }
29
30    pub fn marshal<W: Write>(&self, writer: &mut W) -> Result<()> {
31        writer.write_all(&self.data)?;
32
33        Ok(writer.flush()?)
34    }
35
36    pub fn unmarshal<R: Read>(reader: &mut R) -> Result<Self> {
37        //TODO: use Bytes and implement trait Unmarshal
38        let mut data: Vec<u8> = vec![];
39        reader.read_to_end(&mut data)?;
40
41        Ok(ApplicationData {
42            data: BytesMut::from(&data[..]),
43        })
44    }
45}