webrtc_dtls/
application_data.rs

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