naia_shared/messages/channels/
channel_kinds.rs

1use std::{any::TypeId, collections::HashMap};
2
3use naia_serde::{BitReader, BitWrite, ConstBitLength, Serde, SerdeErr};
4
5use crate::messages::channels::channel::{Channel, ChannelSettings};
6
7type NetId = u16;
8
9/// ChannelKind - should be one unique value for each type of Channel
10#[derive(Eq, Hash, Copy, Clone, PartialEq, Debug)]
11pub struct ChannelKind {
12    type_id: TypeId,
13}
14
15// impl From<TypeId> for ChannelKind {
16//     fn from(type_id: TypeId) -> Self {
17//         Self {
18//             type_id
19//         }
20//     }
21// }
22
23impl ChannelKind {
24    pub fn of<C: Channel>() -> Self {
25        Self {
26            type_id: TypeId::of::<C>(),
27        }
28    }
29
30    pub fn ser(&self, channel_kinds: &ChannelKinds, writer: &mut dyn BitWrite) {
31        channel_kinds.kind_to_net_id(self).ser(writer);
32    }
33
34    pub fn de(channel_kinds: &ChannelKinds, reader: &mut BitReader) -> Result<Self, SerdeErr> {
35        let net_id: NetId = NetId::de(reader)?;
36        Ok(channel_kinds.net_id_to_kind(&net_id))
37    }
38}
39
40impl ConstBitLength for ChannelKind {
41    fn const_bit_length() -> u32 {
42        <NetId as ConstBitLength>::const_bit_length()
43    }
44}
45
46// ChannelKinds
47pub struct ChannelKinds {
48    current_net_id: NetId,
49    kind_map: HashMap<ChannelKind, (NetId, ChannelSettings)>,
50    net_id_map: HashMap<NetId, ChannelKind>,
51}
52
53impl ChannelKinds {
54    pub fn new() -> Self {
55        Self {
56            current_net_id: 0,
57            kind_map: HashMap::new(),
58            net_id_map: HashMap::new(),
59        }
60    }
61
62    pub fn add_channel<C: Channel>(&mut self, settings: ChannelSettings) {
63        let channel_kind = ChannelKind::of::<C>();
64        //info!("ChannelKinds adding channel: {:?}", channel_kind);
65        let net_id = self.current_net_id;
66        self.kind_map.insert(channel_kind, (net_id, settings));
67        self.net_id_map.insert(net_id, channel_kind);
68        self.current_net_id += 1;
69        //TODO: check for current_id overflow?
70    }
71
72    pub fn channels(&self) -> Vec<(ChannelKind, ChannelSettings)> {
73        // TODO: is there a better way to do this without copying + cloning?
74        // How to return a reference here (behind a Mutex ..)
75        let mut output = Vec::new();
76        for (kind, (_, settings)) in &self.kind_map {
77            output.push((*kind, settings.clone()));
78        }
79        output
80    }
81
82    pub fn channel(&self, kind: &ChannelKind) -> ChannelSettings {
83        let (_, settings) = self.kind_map.get(kind).expect("could not find ChannelKind for given Channel. Make sure Channel struct has `#[derive(Channel)]` on it!");
84        settings.clone()
85    }
86
87    fn net_id_to_kind(&self, net_id: &NetId) -> ChannelKind {
88        return *self.net_id_map.get(net_id).expect(
89            "Must properly initialize Channel with Protocol via `add_channel()` function!",
90        );
91    }
92
93    fn kind_to_net_id(&self, channel_kind: &ChannelKind) -> NetId {
94        return self
95            .kind_map
96            .get(channel_kind)
97            .expect(
98                "Must properly initialize Component with Protocol via `add_channel()` function!",
99            )
100            .0;
101    }
102}