From f29b7d92f5c5af5cb621e5290cde21d112771430 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Itsusinn=E9=80=B8=E6=96=B0?= Date: Thu, 8 Feb 2024 03:11:29 +0800 Subject: [PATCH] wip remove E --- src/obc/app_obc/app_ws.rs | 42 ++++++++++++++++++--------------------- src/obc/app_obc/mod.rs | 16 +++++++-------- 2 files changed, 27 insertions(+), 31 deletions(-) diff --git a/src/obc/app_obc/app_ws.rs b/src/obc/app_obc/app_ws.rs index 56bff96..9e81a66 100644 --- a/src/obc/app_obc/app_ws.rs +++ b/src/obc/app_obc/app_ws.rs @@ -16,6 +16,7 @@ use crate::{ use std::sync::Arc; +use color_eyre::eyre; use futures_util::{SinkExt, StreamExt}; use serde::{Deserialize, Serialize}; use tokio::net::TcpListener; @@ -25,23 +26,21 @@ use tokio_tungstenite::tungstenite::http::{header::USER_AGENT, Request}; use tokio_tungstenite::tungstenite::Message as WsMsg; use tokio_tungstenite::WebSocketStream; use tracing::{info, warn}; -use color_eyre::eyre; impl AppOBC where A: ProtocolItem, R: ProtocolItem, { - pub(crate) async fn ws( + pub(crate) async fn ws( &self, ob: &Arc>, config: Vec, tasks: &mut Vec>, ) -> WalleResult<()> where - E: ProtocolItem + GetSelf + Clone, - AH: ActionHandler + Send + Sync + 'static, - EH: EventHandler + Send + Sync + 'static, + AH: ActionHandler + Send + Sync + 'static, + EH: EventHandler + Send + Sync + 'static, { for wsc in config { info!(target: super::OBC, "Start try connect to {}", wsc.url); @@ -77,16 +76,15 @@ where } Ok(()) } - pub(crate) async fn wsr( + pub(crate) async fn wsr( &self, ob: &Arc>, config: Vec, tasks: &mut Vec>, ) -> WalleResult<()> where - E: ProtocolItem + GetSelf + Clone, - AH: ActionHandler + Send + Sync + 'static, - EH: EventHandler + Send + Sync + 'static, + AH: ActionHandler + Send + Sync + 'static, + EH: EventHandler + Send + Sync + 'static, { for wss in config { let addr = std::net::SocketAddr::new(wss.host, wss.port); @@ -123,18 +121,17 @@ where } } -async fn ws_loop( +async fn ws_loop( ob: Arc>, mut ws_stream: WebSocketStream, echo_map: EchoMap, bot_map: Arc>, // implt: String, ) where - E: ProtocolItem + GetSelf + Clone, A: ProtocolItem, R: ProtocolItem, - AH: ActionHandler + Send + Sync + 'static, - EH: EventHandler + Send + Sync + 'static, + AH: ActionHandler + Send + Sync + 'static, + EH: EventHandler + Send + Sync + 'static, { let (seq, mut action_rx) = bot_map.new_connect(); let mut signal_rx = ob.get_signal_rx().unwrap(); //todo @@ -171,7 +168,7 @@ async fn ws_loop( bot_map.connect_closs(&seq); } -async fn ws_recv( +async fn ws_recv( msg: WsMsg, ob: &Arc>, ws_stream: &mut WebSocketStream, @@ -181,20 +178,19 @@ async fn ws_recv( implt: &mut Option, ) -> bool where - E: ProtocolItem + Clone + GetSelf, A: ProtocolItem, R: ProtocolItem, - AH: ActionHandler + Send + Sync + 'static, - EH: EventHandler + Send + Sync + 'static, + AH: ActionHandler + Send + Sync + 'static, + EH: EventHandler + Send + Sync + 'static, { #[derive(Debug, Deserialize, Serialize)] #[serde(untagged)] - enum ReceiveItem { - Event(E), + enum ReceiveItem { + Event(Event), Resp(Echo), } - let handle_ok = |item: Result, eyre::Report>| async move { + let handle_ok = |item: Result, eyre::Report>| async move { match item { Ok(ReceiveItem::Event(event)) => { let ob = ob.clone(); @@ -210,7 +206,7 @@ where } }; - let event_process = |meta: Result| async move { + let meta_process = |meta: Result| async move { if let Ok(event) = meta.and_then(|e: ComEvent| { let e = e.to_v12(); >::try_from(e).map_err(|e| e.into()) @@ -229,11 +225,11 @@ where match msg { WsMsg::Text(text) => { - event_process(ProtocolItem::json_decode(&text)).await; + meta_process(ProtocolItem::json_decode(&text)).await; handle_ok(ProtocolItem::json_decode(&text)).await; } WsMsg::Binary(b) => { - event_process(ProtocolItem::rmp_decode(&b)).await; + meta_process(ProtocolItem::rmp_decode(&b)).await; handle_ok(ProtocolItem::rmp_decode(&b)).await; } WsMsg::Ping(b) => { diff --git a/src/obc/app_obc/mod.rs b/src/obc/app_obc/mod.rs index 2a24ea0..3e748b0 100644 --- a/src/obc/app_obc/mod.rs +++ b/src/obc/app_obc/mod.rs @@ -4,6 +4,7 @@ use std::sync::Arc; use super::OBC; use crate::ah::GetSelfs; +use crate::event::Event; use crate::prelude::{Bot, Version}; use crate::structs::Selft; use crate::util::{Echo, EchoInner, EchoS, GetSelf, ProtocolItem}; @@ -65,9 +66,8 @@ impl AppOBC { } } -impl ActionHandler for AppOBC +impl ActionHandler for AppOBC where - E: ProtocolItem + Clone + GetSelf, A: ProtocolItem + GetSelf, R: ProtocolItem, { @@ -78,8 +78,8 @@ where config: crate::config::AppConfig, ) -> WalleResult>> where - AH: ActionHandler + Send + Sync + 'static, - EH: EventHandler + Send + Sync + 'static, + AH: ActionHandler + Send + Sync + 'static, + EH: EventHandler + Send + Sync + 'static, { let mut tasks = vec![]; #[cfg(feature = "websocket")] @@ -96,8 +96,8 @@ where } async fn call(&self, action: A, _ob: &Arc>) -> WalleResult where - AH: ActionHandler + Send + Sync + 'static, - EH: EventHandler + Send + Sync + 'static, + AH: ActionHandler + Send + Sync + 'static, + EH: EventHandler + Send + Sync + 'static, { match self.bots.get_bot(&action.get_self()) { Some(action_txs) => { @@ -132,9 +132,9 @@ where } async fn before_call_event( &self, - event: E, + event: Event, _ob: &Arc>, - ) -> WalleResult { + ) -> WalleResult { if self._block_meta_event.load(Ordering::Relaxed) { use core::any::Any; let event: Box = Box::new(event.clone());