cm_telemetry/
lib.rs

1mod net;
2
3pub mod dirt;
4pub mod f1;
5
6/// TelemetryPacket is an alias for a vector of bytes
7pub type TelemetryPacket = [u8];
8
9/// TelemetryEvent specifies a way to serialize itself from a Packet
10pub trait TelemetryEvent {
11    fn from_packet(packet: &TelemetryPacket) -> Result<Self, Box<dyn std::error::Error>>
12    where
13        Self: Sized;
14}
15
16/// TelemetryServer implements a generic server that can bind and recv packets
17/// exposes the next_event method that returns an unpacked "TelemetryEvent"
18pub struct TelemetryServer<T: TelemetryEvent> {
19    srv: net::Server,
20    phantom: std::marker::PhantomData<T>, // needed to avoid "type unused" compile error
21}
22
23impl<T: TelemetryEvent> TelemetryServer<T> {
24    /// new initializes a Server with the given address
25    pub fn new(address: &'static str) -> Result<TelemetryServer<T>, std::io::Error> {
26        let srv = net::Server::new(address)?;
27        Ok(TelemetryServer {
28            srv,
29            phantom: std::marker::PhantomData,
30        })
31    }
32
33    /// next will call recv on the inner UDP server (this blocks)
34    /// and will call from_packet from the given T
35    pub fn next(&self) -> Result<T, Box<dyn std::error::Error>> {
36        let packet = self.srv.recv()?;
37        T::from_packet(&packet)
38    }
39}