From aa710a3420ef4d834ee1df4099b25f3c83b9c31d Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 22 May 2021 01:27:17 +0200 Subject: rework command response mechanism --- mumd/src/state.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'mumd/src/state.rs') diff --git a/mumd/src/state.rs b/mumd/src/state.rs index 8072b8e..a224afc 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -18,7 +18,7 @@ use mumlib::command::{Command, CommandResponse, MessageTarget}; use mumlib::config::Config; use mumlib::Error; use crate::state::user::UserDiff; -use std::{net::{SocketAddr, ToSocketAddrs}, sync::{Arc, RwLock}}; +use std::{iter, net::{SocketAddr, ToSocketAddrs}, sync::{Arc, RwLock}}; use tokio::sync::{mpsc, watch}; macro_rules! at { @@ -29,21 +29,23 @@ macro_rules! at { macro_rules! now { ($data:expr) => { - ExecutionContext::Now(Box::new(move || $data)) + ExecutionContext::Now(Box::new(move || Box::new(iter::once($data)))) }; } +type Responses = Box>>>; + //TODO give me a better name pub enum ExecutionContext { TcpEventCallback( TcpEvent, - Box mumlib::error::Result>>, + Box Responses>, ), TcpEventSubscriber( TcpEvent, Box>>) -> bool>, ), - Now(Box mumlib::error::Result>>), + Now(Box Responses>), Ping( Box mumlib::error::Result>, Box) -> mumlib::error::Result> + Send>, @@ -545,7 +547,7 @@ pub fn handle_command( at!(TcpEvent::Connected, |res| { //runs the closure when the client is connected if let TcpEventData::Connected(res) = res { - res.map(|msg| { + Box::new(iter::once(res.map(|msg| { Some(CommandResponse::ServerConnect { welcome_message: if msg.has_welcome_text() { Some(msg.get_welcome_text().to_string()) @@ -553,7 +555,7 @@ pub fn handle_command( None }, }) - }) + }))) } else { unreachable!("callback should be provided with a TcpEventData::Connected"); } @@ -584,12 +586,12 @@ pub fn handle_command( } }), Box::new(move |pong| { - Ok(pong.map(|pong| CommandResponse::ServerStatus { + Ok(pong.map(|pong| (CommandResponse::ServerStatus { version: pong.version, users: pong.users, max_users: pong.max_users, bandwidth: pong.bandwidth, - })) + }))) }), ), Command::Status => { @@ -643,11 +645,14 @@ pub fn handle_command( ) } else { let messages = std::mem::take(&mut state.message_buffer); - let messages = messages.into_iter() + let messages: Vec<_> = messages.into_iter() .map(|(msg, user)| (msg, state.get_user_name(user).unwrap())) + .map(|e| Ok(Some(CommandResponse::PastMessage { message: e }))) .collect(); - now!(Ok(Some(CommandResponse::PastMessages { messages }))) + ExecutionContext::Now(Box::new(move || { + Box::new(messages.into_iter()) + })) } } Command::SendMessage { message, targets } => { -- cgit v1.2.1