use async_trait::async_trait;
use log::debug;
use flare_core::flare_net::net::{Command, Response};
#[async_trait]
pub trait MessageHandler: Send + Sync + 'static {
async fn on_message(&self, msg: Vec<u8>);
async fn on_custom_message(&self, msg: Vec<u8>);
async fn on_notice_message(&self, msg: Vec<u8>);
async fn on_response(&self, msg: &Response);
async fn on_ack_message(&self, msg: Vec<u8>);
async fn on_data(&self, data: Vec<u8>);
fn supported_commands(&self) -> Vec<Command>{
vec![ Command::ServerPushMsg , Command::ServerPushCustom ,
Command::ServerPushNotice , Command::ServerPushData,Command::ServerResponse,Command::ServerAck]
}
fn supports_command(&self, command: Command) -> bool {
self.supported_commands().contains(&command)
}
}
#[derive(Default)]
pub struct DefMessageHandler;
impl DefMessageHandler {
pub fn new() -> Self {
Self
}
}
#[async_trait]
impl MessageHandler for DefMessageHandler {
async fn on_message(&self, msg: Vec<u8>){
debug!("收到消息: {} bytes", msg.len());
}
async fn on_custom_message(&self, msg: Vec<u8>) {
debug!("收到自定义消息: {} bytes", msg.len());
}
async fn on_notice_message(&self, msg: Vec<u8>) {
debug!("收到通知消息: {} bytes", msg.len());
}
async fn on_response(&self, msg: &Response) {
debug!("收到响应: {:?}", msg);
}
async fn on_ack_message(&self, msg: Vec<u8>) {
debug!("收到ack消息: {:?}", msg);
}
async fn on_data(&self, data: Vec<u8>){
debug!("收到数据: {} bytes", data.len());
}
}