use bytes::{BufMut, Bytes, BytesMut};
use super::handshake::HandshakeMessage;
use crate::dtls::record::ContentType;
#[derive(Debug, Clone)]
pub enum ContentMessage {
ChangeCipherSpec(ChangeCipherSpecMessage),
Alert(crate::dtls::alert::Alert),
Handshake(HandshakeMessage),
ApplicationData(Bytes),
}
impl ContentMessage {
pub fn content_type(&self) -> ContentType {
match self {
Self::ChangeCipherSpec(_) => ContentType::ChangeCipherSpec,
Self::Alert(_) => ContentType::Alert,
Self::Handshake(_) => ContentType::Handshake,
Self::ApplicationData(_) => ContentType::ApplicationData,
}
}
pub fn data(&self) -> Bytes {
match self {
Self::ChangeCipherSpec(msg) => msg.data.clone(),
Self::Alert(alert) => {
let result = alert.serialize();
match result {
Ok(bytes) => bytes,
Err(_) => Bytes::new(),
}
}
Self::Handshake(_) => {
Bytes::new()
}
Self::ApplicationData(data) => data.clone(),
}
}
}
#[derive(Debug, Clone)]
pub struct ChangeCipherSpecMessage {
pub data: Bytes,
}
impl ChangeCipherSpecMessage {
pub fn new() -> Self {
let mut buf = BytesMut::with_capacity(1);
buf.put_u8(1);
Self { data: buf.freeze() }
}
}
impl Default for ChangeCipherSpecMessage {
fn default() -> Self {
Self::new()
}
}