1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use super::Protocol;
use crate::net::Id;
use alloc::collections::btree_map::BTreeMap;
use alloc::sync::Arc;
use spin::RwLock;
lazy_static! {
static ref PROTOCOLS: RwLock<BTreeMap<u8, Arc<dyn Protocol + Send + Sync>>> = RwLock::new(BTreeMap::new());
}
pub fn add_protocol(protocol: Arc<dyn Protocol + Send + Sync>) {
let mut protocols = PROTOCOLS.write();
protocols.insert(protocol.id(), protocol);
}
pub fn distribute(id: u8, remote: &Id, data: &[u8]) {
let protocols = PROTOCOLS.read();
if let Some(protocol) = protocols.get(&id) {
protocol.recv(remote, data);
} else {
};
}