2023-04-29 22:17:12 +02:00
|
|
|
use std::io::Read;
|
|
|
|
|
|
|
|
|
|
use byteorder::{LittleEndian, ReadBytesExt};
|
|
|
|
|
use serde::Serialize;
|
2023-05-08 23:00:40 +02:00
|
|
|
use serde_with::serde_as;
|
2023-04-29 22:17:12 +02:00
|
|
|
|
|
|
|
|
use crate::models::data::payload::Payload;
|
|
|
|
|
use crate::result::Result;
|
|
|
|
|
|
2023-05-08 23:00:40 +02:00
|
|
|
#[serde_as]
|
2023-04-29 22:17:12 +02:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
|
pub struct Packet {
|
|
|
|
|
pub clock: f32,
|
|
|
|
|
pub payload: Payload,
|
2023-05-08 23:00:40 +02:00
|
|
|
|
|
|
|
|
#[cfg(feature = "raw-payload")]
|
|
|
|
|
#[serde_as(as = "serde_with::hex::Hex")]
|
|
|
|
|
pub raw_payload: Vec<u8>,
|
2023-04-29 22:17:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Packet {
|
|
|
|
|
pub fn from_reader(reader: &mut impl Read) -> Result<Option<Self>> {
|
|
|
|
|
let Ok(length) = reader.read_u32::<LittleEndian>() else { return Ok(None) };
|
|
|
|
|
let type_ = reader.read_u32::<LittleEndian>()?;
|
|
|
|
|
let clock = reader.read_f32::<LittleEndian>()?;
|
|
|
|
|
let payload = Self::read_payload(reader, length as usize)?;
|
2023-05-08 23:00:40 +02:00
|
|
|
let this = Self {
|
|
|
|
|
clock,
|
|
|
|
|
payload: Payload::new(type_, &payload)?,
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "raw-payload")]
|
|
|
|
|
raw_payload: payload,
|
|
|
|
|
};
|
2023-04-29 22:17:12 +02:00
|
|
|
Ok(Some(this))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn read_payload(reader: &mut impl Read, length: usize) -> Result<Vec<u8>> {
|
|
|
|
|
let mut buffer = Vec::new();
|
|
|
|
|
buffer.resize(length, 0);
|
|
|
|
|
reader.read_exact(&mut buffer)?;
|
|
|
|
|
Ok(buffer)
|
|
|
|
|
}
|
|
|
|
|
}
|