#[allow(unused_imports)]
use super::peer::{Peer, PeerId};
use bitflags::bitflags;
use serde::{Serialize, Deserialize};
use bincode;
pub mod sdp;
mod mbp;
bitflags! {
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
#[serde(transparent)]
pub struct ProtocolType : u8 {
const SDP = 0b10000000;
const SSDP = 0b01000000;
const MBP = 0b00100000;
}
}
bitflags! {
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
#[serde(transparent)]
pub struct PacketType : u8 {
const ECHO = 0b10000000;
const INIT = 0b01000000;
const HI = 0b00100000;
const HI_INIT = Self::INIT.bits() | Self::HI.bits();
const SYN = 0b00010000;
const ACK = 0b00001000;
const CHAT = 0b00000100;
const CONV = 0b00000010;
const CHAN = 0b00000001;
const ACK_SYN = Self::ACK.bits() | Self::SYN.bits();
const ACK_HI = Self::ACK.bits() | Self::HI.bits();
const ACK_INIT = Self::ACK.bits() | Self::INIT.bits();
const ACK_HI_INIT = Self::ACK.bits() | Self::INIT.bits() | Self::HI.bits();
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Header {
pub protocol_type: ProtocolType,
pub packet_type: PacketType,
pub length: u16,
pub src_id: PeerId,
pub rec_id: PeerId
}
impl Header {
pub fn new(
protocol_type: ProtocolType,
packet_type: PacketType,
length: u16,
src_id: PeerId,
rec_id: PeerId
) -> Header {
Header {
protocol_type, packet_type, length,
src_id, rec_id
}
}
pub fn serialize(&self) -> Vec<u8> {
bincode::serialize(&self).unwrap()
}
#[track_caller]
pub fn deserialize(bytes: Vec<u8>) -> Header {
match bincode::deserialize(&bytes) {
Ok(header) => header,
Err(_) => panic!("Wrong size of `Header`"),
}
}
}