flare_im_core/server/
server_handler.rs

1use flare_core::context::AppContext;
2use flare_core::error::{FlareErr, Result};
3use crate::server::handlers::CommandHandler;
4use async_trait::async_trait;
5use log::debug;
6use flare_core::flare_net::net::{Command, ResCode, Response};
7
8/// 服务端处理器
9#[async_trait]
10pub trait ServerHandler: Send + Sync {
11    /// 处理发送消息
12    async fn handle_send_message(&self, ctx:  &AppContext) -> Result<Response>;
13
14    /// 处理拉取消息
15    async fn handle_pull_message(&self, ctx:  &AppContext) -> Result<Response>;
16
17    /// 处理数据请求
18    async fn handle_request(&self, ctx:  &AppContext) -> Result<Response>;
19
20    /// 处理消息ack
21    async fn handle_ack(&self, ctx:  &AppContext) -> Result<Response>;
22}
23
24/// 服务端命令处理器
25pub struct ServerCommandHandler<T>(pub T);
26
27impl<T> ServerCommandHandler<T> {
28    pub fn new(handler: T) -> Self {
29        Self(handler)
30    }
31}
32
33// 实现 ServerHandler
34#[async_trait]
35impl<T: ServerHandler + Send + Sync> ServerHandler for ServerCommandHandler<T> {
36    async fn handle_send_message(&self, ctx:  &AppContext) -> Result<Response> {
37        self.0.handle_send_message(ctx).await
38    }
39
40    async fn handle_pull_message(&self, ctx:  &AppContext) -> Result<Response> {
41        self.0.handle_pull_message(ctx).await
42    }
43
44    async fn handle_request(&self, ctx:  &AppContext) -> Result<Response> {
45        self.0.handle_request(ctx).await
46    }
47
48    async fn handle_ack(&self, ctx:  &AppContext) -> Result<Response> {
49        self.0.handle_ack(ctx).await
50    }
51}
52
53#[async_trait]
54impl<T: ServerHandler + Send + Sync> CommandHandler for ServerCommandHandler<T> {
55    async fn handle_command(&self, ctx:  &AppContext) -> Result<Response> {
56        let command = ctx.command().ok_or_else(|| 
57            FlareErr::invalid_command("Missing command"))?;
58
59        if !self.supports_command(command) {
60            return Ok(Response {
61                code: ResCode::InvalidCommand as i32,
62                message: format!("Unsupported command: {:?}", command),
63                data: Vec::new(),
64            });
65        }
66
67        match command {
68            Command::ClientSendMessage => self.handle_send_message(ctx).await,
69            Command::ClientPullMessage => self.handle_pull_message(ctx).await,
70            Command::ClientRequest => self.handle_request(ctx).await,
71            Command::ClientAck => self.handle_ack(ctx).await,
72            _ => Ok(Response {
73                code: ResCode::InvalidCommand as i32,
74                message: format!("Unexpected command: {:?}", command),
75                data: Vec::new(),
76            })
77        }
78    }
79
80    fn supported_commands(&self) -> Vec<Command> {
81        vec![
82            Command::ClientSendMessage,
83            Command::ClientPullMessage,
84            Command::ClientRequest,
85            Command::ClientAck,
86        ]
87    }
88}
89
90/// 默认的服务端处理器实现
91#[derive(Default)]
92pub struct DefServerHandler;
93
94impl DefServerHandler {
95    pub fn new() -> Self {
96        Self {}
97    }
98}
99
100#[async_trait]
101impl ServerHandler for DefServerHandler {
102    async fn handle_send_message(&self, ctx:  &AppContext) -> Result<Response> {
103        debug!("处理发送消息请求 - addr: {}", ctx.remote_addr());
104
105        let message = ctx.string_data()?;
106        if message.is_empty() {
107            return Ok(Response {
108                code: ResCode::InvalidParams as i32,
109                message: "Message cannot be empty".into(),
110                data: Vec::new(),
111            });
112        }
113
114        // 这里可以添加实际的消息发送逻辑
115        Ok(Response {
116            code: ResCode::Success as i32,
117            message: "消息发送成功".into(),
118            data: Vec::new(),
119        })
120    }
121
122    async fn handle_pull_message(&self, ctx:  &AppContext) -> Result<Response> {
123        debug!("处理拉取消息请求 - addr: {}", ctx.remote_addr());
124
125        // 这里可以添加实际的消息拉取逻辑
126        Ok(Response {
127            code: ResCode::Success as i32,
128            message: "消息拉取成功".into(),
129            data: Vec::new(), // 这里应该返回实际的消息数据
130        })
131    }
132
133    async fn handle_request(&self, ctx:  &AppContext) -> Result<Response> {
134        debug!("处理数据请求 - addr: {}", ctx.remote_addr());
135
136        // 这里可以添加实际的数据请求处理逻辑
137        Ok(Response {
138            code: ResCode::Success as i32,
139            message: "请求处理成功".into(),
140            data: Vec::new(), // 这里应该返回请求的数据
141        })
142    }
143
144    async fn handle_ack(&self, ctx:  &AppContext) -> Result<Response> {
145        debug!("处理消息确认 - addr: {}", ctx.remote_addr());
146
147        let msg_id = ctx.msg_id()?;
148        if msg_id.is_empty() {
149            return Ok(Response {
150                code: ResCode::InvalidParams as i32,
151                message: "Message ID cannot be empty".into(),
152                data: Vec::new(),
153            });
154        }
155
156        // 这里可以添加实际的消息确认处理逻辑
157        Ok(Response {
158            code: ResCode::Success as i32,
159            message: format!("消息 {} 确认成功", msg_id),
160            data: Vec::new(),
161        })
162    }
163}