sparkles_core/protocol/
sender.rs1use alloc::boxed::Box;
2use alloc::sync::Arc;
3use alloc::vec::Vec;
4use core::fmt::Debug;
5use core::sync::atomic::{AtomicBool, Ordering};
6use enumset::{EnumSet, EnumSetType};
7use crate::protocol::packets::PacketType;
8
9pub trait Sender {
14 fn send_packet(&mut self, packet_type: PacketType, data: &[&[u8]]);
15 fn with_timestamp_freq_request(self, timestamp_freq_request: Arc<AtomicBool>) -> Self
16 where
17 Self: Sized;
18
19 fn poll(&mut self) {}
20}
21
22pub trait ConfiguredSender: Sender + Sized {
23 type Config: Debug;
24 fn new(cfg: &Self::Config) -> Option<Self>;
25 fn new_default() -> Option<Self>
26 where
27 Self::Config: Default
28 {
29 Self::new(&Self::Config::default())
30 }
31}
32
33#[derive(Default)]
35pub struct SenderChain {
36 senders: Vec<Box<dyn Sender>>,
37 timestamp_freq_request: Arc<AtomicBool>,
38}
39
40impl SenderChain {
41 pub fn take_tm_freq_requested(&self) -> bool {
42 self.timestamp_freq_request.swap(false, Ordering::Relaxed)
43 }
44}
45
46impl SenderChain {
47 pub fn with_sender<T: Sender + 'static>(&mut self, sender: T) {
48 self.senders.push(Box::new(sender.with_timestamp_freq_request(self.timestamp_freq_request.clone())));
49 }
50}
51
52impl Sender for SenderChain {
53 fn send_packet(&mut self, packet_type: PacketType, data: &[&[u8]]) {
54 for sender in self.senders.iter_mut() {
55 sender.send_packet(packet_type, data);
56 }
57 }
58 fn with_timestamp_freq_request(mut self, timestamp_freq_request: Arc<AtomicBool>) -> Self
59 where
60 Self: Sized,
61 {
62 self.timestamp_freq_request = timestamp_freq_request;
63 self
64 }
65 fn poll(&mut self) {
66 for sender in self.senders.iter_mut() {
67 sender.poll();
68 }
69 }
70}
71
72#[derive(Debug, EnumSetType)]
73#[enumset(repr="u8")]
74pub enum PacketFlags {
75 ShortPacket,
76 PacketEnd,
77 PacketStart
78}
79
80impl PacketFlags {
81 pub fn empty() -> EnumSet<PacketFlags> {
82 EnumSet::empty()
83 }
84}