lynn_tcp 1.2.5

Lightweight asynchronous TCP framework
Documentation
use crate::{app::AsyncFunc, handler::IntoSystem};
use dashmap::DashMap;
use std::sync::Arc;
use tracing::warn;

pub(super) struct LynnRouter {
    pub(crate) map: DashMap<u16, Arc<AsyncFunc>>,
}

impl LynnRouter {
    pub(super) fn new() -> Self {
        LynnRouter {
            map: DashMap::new(),
        }
    }

    pub(super) fn add_router<Param>(&self, method_id: u16, handler: impl IntoSystem<Param>) {
        if self.map.contains_key(&method_id) {
            warn!(
                "Router - Duplicate method_id {} detected, existing handler will be overwritten",
                method_id
            );
        }
        self.map.insert(method_id, Arc::new(Box::new(handler.to_system())));
    }

    pub(crate) fn get_handler_by_method_id(&self, method_id: &u16) -> Option<Arc<AsyncFunc>> {
        self.map.get(method_id).map(|ref_| ref_.clone())
    }
}