domo/domo_proto/src/packet/mod.rs
2023-10-15 18:06:33 +02:00

46 lines
No EOL
1.2 KiB
Rust

use crate::identifier::Identifier;
use std::fmt::Debug;
use std::io;
use packet_data::PacketData;
use crate::packet::raw_packet::RawPacket;
/// High level abstraction for packet data.
pub mod packet_data;
/// Low level abstraction for packets.
pub mod raw_packet;
/// Size of the packet header.
pub const PACKET_HEADER_SIZE: usize = 20;
/// Packet size with CRC32.
pub const FULL_PACKET_SIZE: usize = 65559;
/// Packet size without CRC32.
pub const BASE_PACKET_SIZE: usize = 65555;
/// The abstraction for all DomoProto packets.
#[derive(Debug)]
pub struct Packet {
pub dest: Identifier,
pub src: Identifier,
pub packet_id: Identifier,
pub reply_to: Identifier,
pub command: u8,
pub data: PacketData,
}
pub trait ToPacket {
fn to_packet(self, src: Identifier, dest: Identifier, packet_id: Identifier, reply_to: Identifier) -> Packet;
}
/// Secretly calls RawPacket::try_from().into()
impl TryFrom<Vec<u8>> for Packet {
type Error = io::Error;
fn try_from(data: Vec<u8>) -> io::Result<Self> {
Ok(RawPacket::try_from(data)?.into())
}
}
impl Into<Vec<u8>> for Packet {
fn into(self) -> Vec<u8> {
RawPacket::from(self).into()
}
}