domo/domo_proto/src/packet/mod.rs
2023-12-06 23:30:09 +01:00

60 lines
No EOL
1.6 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, Clone, PartialEq, Eq)]
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;
}
/// Default packet is a ping from 0x0 to 0xFFFFFFFF
impl Default for Packet {
fn default() -> Self {
Self {
dest: Identifier::from(0xFFFFFFFF),
src: Identifier::from(0),
command: 0x00,
packet_id: Identifier::default(),
reply_to: Identifier::default(),
data: PacketData::NodeManagement(crate::commands::node_management::NodeManagementCommand::Ping)
}
}
}
/// 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()
}
}