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 {
        // W:("unmp_net_protocol {} is not exist.", id);
    };
}