trigger-rs/crates/gate-server/src/message_handler.rs
traffic95 8f606a8256 Add property calculations (#5)
Reviewed-on: ObolSquad/trigger-rs#5
Co-authored-by: traffic95 <traffic95@xeondev.com>
Co-committed-by: traffic95 <traffic95@xeondev.com>
2025-03-18 20:44:22 +00:00

33 lines
994 B
Rust

use tokio::sync::mpsc;
use tracing::warn;
use crate::{AppState, net::NetPacket};
#[derive(Clone)]
pub struct MessageHandler(mpsc::UnboundedSender<(u64, NetPacket)>);
impl MessageHandler {
pub fn new(state: &'static AppState) -> Self {
let (tx, rx) = mpsc::unbounded_channel();
tokio::spawn(Self::handler_loop(state, rx));
Self(tx)
}
pub fn enqueue(&self, session_id: u64, packet: NetPacket) {
let _ = self.0.send((session_id, packet));
}
async fn handler_loop(
state: &'static AppState,
mut rx: mpsc::UnboundedReceiver<(u64, NetPacket)>,
) {
while let Some((session_id, packet)) = rx.recv().await {
if let Some(connection) = state.connection_mgr.get(session_id) {
crate::handlers::client::handle_message(connection.as_ref(), state, packet).await;
} else {
warn!("connection with session_id {session_id} doesn't exist");
}
}
}
}