flare-im-core 0.1.1

A high performance instant messaging core library for Flare framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
use flare_core::context::{AppContext, AppContextBuilder};
use flare_core::error::{FlareErr, Result};
use crate::connections::Connection;
use crate::server::handlers::{CommandHandler, ServerMessageHandler};
use log::{debug, error, info, warn};
use prost::Message;
use flare_core::flare_net::net::LoginResp;
use flare_core::flare_net::net::{Command, Message as ProtoMessage, Platform, ResCode, Response};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio::time::{interval, Duration};
use crate::server::auth_handler::AuthHandler;
use crate::server::server_handler::ServerHandler;
use crate::server::sys_handler::SystemHandler;

use super::auth_handler::DefAuthHandler;
use super::server_handler::DefServerHandler;
use super::sys_handler::DefSystemHandler;

const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(30);
const CONNECTION_TIMEOUT: Duration = Duration::from_secs(90);

/// 连接信息
#[derive(Clone)]
pub struct ConnectionInfo {
    protocol: String,
    conn_id: String,
    user_id: String,
    platform: Platform,
    language: Option<String>,
    client_id : String,
    remote_addr: String,
    connected_at: chrono::DateTime<chrono::Utc>,
    last_heartbeat: Arc<Mutex<chrono::DateTime<chrono::Utc>>>,
    conn: Arc<Box<dyn Connection>>,
}

impl ConnectionInfo {
    pub fn new(
        conn: Box<dyn Connection>,
        user_id: String,
        platform: Platform,
        client_id: String,
        remote_addr: String,
        protocol: String,
    ) -> Self {
        Self {
            conn_id: conn.id().to_string(),
            user_id,
            platform,
            language: None,
            client_id,
            remote_addr,
            protocol,
            connected_at: chrono::Utc::now(),
            last_heartbeat: Arc::new(Mutex::new(chrono::Utc::now())),
            conn: Arc::new(conn),
        }
    }

    pub async fn send(&self, msg: ProtoMessage) -> Result<()> {
        self.conn.send(msg).await
    }

    pub async fn receive(&self) -> Result<ProtoMessage> {
        self.conn.receive().await
    }

    pub async fn close(&self) -> Result<()> {
        self.conn.close().await
    }
    pub fn get_conn_id(&self) -> String {
        self.conn_id.clone()
    }
    pub fn get_protocol(&self) -> String {
        self.protocol.clone()
    }
    pub fn get_connection_at(&self) -> chrono::DateTime<chrono::Utc> {
        self.connected_at.clone()
    }
}

pub struct Server<S, A, Y>
where
    S: ServerHandler + Send + Sync + 'static,
    A: AuthHandler + Send + Sync + 'static,
    Y: SystemHandler + Send + Sync + 'static,
{
    handler: Arc<ServerMessageHandler<S, A, Y>>,
    connections: Arc<Mutex<HashMap<String, ConnectionInfo>>>, // conn_id -> ConnectionInfo
    user_connections: Arc<Mutex<HashMap<String, Vec<String>>>>, // user_id -> Vec<conn_id>
}

impl<S, A, Y> Server<S, A, Y>
where
    S: ServerHandler + Send + Sync + 'static,
    A: AuthHandler + Send + Sync + 'static,
    Y: SystemHandler + Send + Sync + 'static,
{
    pub fn new(handler: ServerMessageHandler<S, A, Y>) -> Self {
        let server = Self {
            handler: Arc::new(handler),
            connections: Arc::new(Mutex::new(HashMap::new())),
            user_connections: Arc::new(Mutex::new(HashMap::new())),
        };

        // 启动心跳检测
        let connections = server.connections.clone();
        tokio::spawn(async move {
            let mut interval = interval(HEARTBEAT_INTERVAL);
            loop {
                interval.tick().await;
                Self::check_connections(connections.clone()).await;
            }
        });

        server
    }

    /// 添加新连接
    pub async fn add_connection(&self, conn: Box<dyn Connection>) {
        let conn_id = conn.id().to_string();
        let remote_addr = conn.remote_addr().to_string();
        info!("New connection from {}: {}", remote_addr, conn_id);
        // 等待认证消息
        match self.wait_for_auth(&conn).await {
            Ok(login_resp) => {
                let info = ConnectionInfo::new(
                    conn.clone_box(),
                    login_resp.user_id.clone(),
                    conn.platform(),
                    conn.id().to_string(),
                    conn.remote_addr().to_string(),
                    conn.protocol().to_string(),
                );

                // 保存连接信息
                {
                    let mut conns = self.connections.lock().await;
                    conns.insert(conn_id.clone(), info.clone());
                    
                    // 处理新连接
                    let ctx = match self.build_context(
                        AppContextBuilder::new()
                            .user_id(login_resp.user_id.clone())
                            .remote_addr(info.remote_addr.clone())
                            .platform(info.platform as i32)
                            .client_id(info.client_id.clone()),
                        info.conn_id.clone(),
                        info.client_id.clone(),
                    ).await {
                        Some(ctx) => ctx,
                        None => {
                            error!("Failed to build context for new connection");
                            // 发送错误响应
                            if let Err(e) = conn.send(ProtoMessage {
                                command: Command::ServerResponse as i32,
                                data: Response {
                                    code: ResCode::InvalidParams as i32,
                                    message: "Failed to initialize connection context".into(),
                                    data: Vec::new(),
                                }.encode_to_vec(),
                                ..Default::default()
                            }).await {
                                error!("Failed to send error response: {}", e);
                            }
                            // 关闭连接
                            if let Err(e) = conn.close().await {
                                error!("Failed to close connection: {}", e);
                            }
                            return;
                        }
                    };
                        
                    if let Err(e) = self.handler.handle_new_connection(&ctx, &info).await {
                        error!("Failed to handle new connection: {}", e);
                        // 发送错误响应
                        if let Err(send_err) = conn.send(ProtoMessage {
                            command: Command::ServerResponse as i32,
                            data: Response {
                                code: ResCode::InternalError as i32,
                                message: format!("Failed to initialize connection: {}", e),
                                data: Vec::new(),
                            }.encode_to_vec(),
                            ..Default::default()
                        }).await {
                            error!("Failed to send error response: {}", send_err);
                        }
                        // 关闭连接
                        if let Err(close_err) = conn.close().await {
                            error!("Failed to close connection: {}", close_err);
                        }
                        return;
                    }
                }

                // 更新用户连接映射
                {
                    let mut user_conns = self.user_connections.lock().await;
                    user_conns.entry(login_resp.user_id)
                        .or_insert_with(Vec::new)
                        .push(conn_id.clone());
                }

                // 启动消息处理
                self.handle_connection(info).await;
            }
            Err(e) => {
                error!("Authentication failed for {}: {}", remote_addr, e);
                
                // 发送认证失败响应
                if let Err(send_err) = conn.send(ProtoMessage {
                    command: Command::ServerResponse as i32,
                    data: Response {
                        code: e.code() as i32,
                        message: e.to_string(),
                        data: Vec::new(),
                    }.encode_to_vec(),
                    ..Default::default()
                }).await {
                    error!("Failed to send auth error response: {}", send_err);
                }
                
                // 关闭连接
                if let Err(close_err) = conn.close().await {
                    error!("Failed to close connection after auth failure: {}", close_err);
                }
            }
        }
    }

    /// 构建应用上下文
    async fn build_context(
        &self,
        builder: AppContextBuilder,
        conn_id: String,
        client_msg_id: String,
    ) -> Option<AppContext> {
        match builder.build() {
            Ok(ctx) => Some(ctx),
            Err(e) => {
                error!("Failed to build context: {}", e);
                if let Err(send_err) = self.send_response(
                    conn_id,
                    client_msg_id,
                    Response {
                        code: ResCode::InvalidParams as i32,
                        message: "Invalid context parameters".into(),
                        data: Vec::new(),
                    },
                ).await {
                    error!("Failed to send error response: {}", send_err);
                }
                None
            }
        }
    }

    /// 等待认证消息
    async fn wait_for_auth(&self, conn: &Box<dyn Connection>) -> Result<LoginResp> {
        let timeout = tokio::time::sleep(Duration::from_secs(30));
        tokio::pin!(timeout);

        loop {
            tokio::select! {
                msg = conn.receive() => {
                    match msg {
                        Ok(msg) => {
                            match Command::try_from(msg.command) {
                                Ok(Command::Ping) => {
                                    // 处理心跳消息
                                    debug!("Received ping during auth, sending pong");
                                    if let Err(e) = conn.send(ProtoMessage {
                                        command: Command::Pong as i32,
                                        ..Default::default()
                                    }).await {
                                        error!("Failed to send pong: {}", e);
                                    }
                                    continue;
                                }
                                Ok(Command::Pong) => {
                                    // 处理心跳响应
                                    debug!("Received pong during auth, ignoring");
                                    continue;
                                }
                                Ok(Command::Login) => {
                                    // 处理登录请求
                                    let ctx = AppContextBuilder::new()
                                        .remote_addr(conn.remote_addr().to_string())
                                        .command(Some(Command::Login))
                                        .data(msg.data)
                                        .build()?;

                                    match self.handler.handle_auth(&ctx).await {
                                        Ok(response) => {
                                            // 发送登录响应
                                            conn.send(ProtoMessage {
                                                command: Command::ServerResponse as i32,
                                                data: response.data.clone(),
                                                client_id: msg.client_id,
                                                ..Default::default()
                                            }).await?;

                                            // 解析登录响应
                                            if response.code == ResCode::Success as i32 {
                                                if let Ok(login_resp) = LoginResp::decode(&response.data[..]) {
                                                    return Ok(login_resp);
                                                }
                                            }
                                            return Err(FlareErr::AuthError(response.message));
                                        }
                                        Err(e) => return Err(e),
                                    }
                                }
                                _ => {
                                    warn!("Unexpected command during auth: {:?}", msg.command);
                                    continue;
                                }
                            }
                        }
                        Err(e) => return Err(e),
                    }
                }
                _ = &mut timeout => {
                    return Err(FlareErr::AuthError("Authentication timeout".to_string()));
                }
            }
        }
    }

    /// 处理连接
    async fn handle_connection(&self, info: ConnectionInfo) {
        let conn_id = info.conn_id.clone();
        let last_heartbeat = info.last_heartbeat.clone();
        let handler = self.handler.clone();
        let connections = self.connections.clone();
        let info = info.clone();
        let server = Arc::new(ServerHandle {
            handler,
            connections,
        });

        tokio::spawn(async move {
            while let Ok(msg) = info.receive().await {
                debug!("Received message from {}: {:?}", info.remote_addr, msg);
                *last_heartbeat.lock().await = chrono::Utc::now();

                match Command::try_from(msg.command) {
                    Ok(comm) => {
                        if comm == Command::Ping{
                            // 处理心跳消息
                            debug!("Received ping during auth, sending pong");
                            if let Err(e) = server.send_pong(info.conn_id.clone()).await{
                                debug!("Failed to send pong: {}", e)
                            }
                            continue;
                        }
                        if comm ==Command::Pong {
                            // 处理心跳响应
                            debug!("Received pong during auth, ignoring");
                            continue;
                        }
                        let ctx = match server.build_context(
                            AppContextBuilder::new()
                                .user_id(info.user_id.clone())
                                .remote_addr(info.remote_addr.clone())
                                .command(Some(comm))
                                .platform(info.platform as i32)
                                .data(msg.data.clone())
                                .with_language(info.language.clone())
                                .client_id(info.client_id.clone()),
                            info.conn_id.clone(),
                            msg.client_id.clone(),
                        ).await {
                            Some(ctx) => ctx,
                            None => break,
                        };

                        // 处理消息
                        match server.handler.handle_command(&ctx).await {
                            Ok(response) => {
                                if let Err(e) = server.send_response(info.conn_id.clone(), msg.client_id, response).await {
                                    error!("Failed to send response: {}", e);
                                    break;
                                }
                            }
                            Err(e) => {
                                error!("Message handling error: {}", e);
                                if let Err(e) = server.send_response(info.conn_id.clone(), msg.client_id, Response {
                                    code: ResCode::InternalError as i32,
                                    message: e.to_string(),
                                    data: Vec::new(),
                                }).await {
                                    error!("Failed to send error response: {}", e);
                                }
                                break;
                            }
                        }
                    }
                    Err(e) => {
                        error!("Invalid command: {}", e);
                        if let Err(e) = server.send_response(info.conn_id.clone(), msg.client_id, Response {
                            code: ResCode::InvalidCommand as i32,
                            message: "Invalid command".into(),
                            data: Vec::new(),
                        }).await {
                            error!("Failed to send invalid command response: {}", e);
                        }
                        break;
                    }
                }
            }

            let mut conns = server.connections.lock().await;
            conns.remove(&conn_id);
            info!("Connection closed: {}", conn_id);
        });
    }

    /// 检查连接状态
    async fn check_connections(connections: Arc<Mutex<HashMap<String, ConnectionInfo>>>) {
        let mut conns = connections.lock().await;
        let now = chrono::Utc::now();
        
        conns.retain(|conn_id, info| {
            if let Ok(last) = info.last_heartbeat.try_lock() {
                if now.signed_duration_since(*last) > chrono::Duration::seconds(CONNECTION_TIMEOUT.as_secs() as i64) {
                    warn!("Connection {} timed out", conn_id);
                    false
                } else {
                    true
                }
            } else {
                true
            }
        });
    }

    /// 向用户发送消息
    pub async fn send_to_user(&self, user_id: &str, msg: ProtoMessage) -> Result<()> {
        let user_conns = self.user_connections.lock().await;
        if let Some(conn_ids) = user_conns.get(user_id) {
            let conns = self.connections.lock().await;
            for conn_id in conn_ids {
                if let Some(info) = conns.get(conn_id) {
                    if let Err(e) = info.send(msg.clone()).await {
                        warn!("Failed to send message to {}: {}", conn_id, e);
                    }
                }
            }
        }
        Ok(())
    }


    /// 向所有连接广播消息
    pub async fn broadcast(&self, msg: ProtoMessage) -> Result<()> {
        let conns = self.connections.lock().await;
        for (conn_id, info) in conns.iter() {
            if let Err(e) = info.send(msg.clone()).await {
                warn!("Failed to broadcast to {}: {}", conn_id, e);
            }
        }
        Ok(())
    }

    /// 获取连接信息
    pub async fn get_connection_info(&self, conn_id: &str) -> Option<ConnectionInfo> {
        let conns = self.connections.lock().await;
        conns.get(conn_id).cloned()
    }

    /// 获取用户的所有连接
    pub async fn get_user_connections(&self, user_id: &str) -> Vec<ConnectionInfo> {
        let user_conns = self.user_connections.lock().await;
        let conns = self.connections.lock().await;
        
        user_conns.get(user_id)
            .map(|conn_ids| {
                conn_ids.iter()
                    .filter_map(|id| conns.get(id).cloned())
                    .collect()
            })
            .unwrap_or_default()
    }

    pub fn get_handler_mut(&mut self) -> &mut ServerMessageHandler<S, A, Y> {
        Arc::get_mut(&mut self.handler).unwrap()
    }
    /// 发送响应消息
    pub async fn send_response(&self, conn_id: String,client_msg_id:String, response: Response) -> Result<()> {
        let conn = self.connections.lock().await;
        if let Some(info) = conn.get(conn_id.as_str()) {
            info.send(ProtoMessage {
                command: Command::ServerResponse as i32,
                data: response.encode_to_vec(),
                client_id:client_msg_id,
                ..Default::default()
            }).await
        } else {
            debug!("Connection not found: {}", conn_id);
            Err(FlareErr::ConnectionNotFound)
        }
    }
}

// 新增一个辅助结构体来处理生命周期问题
struct ServerHandle<S, A, Y>
where
    S: ServerHandler + Send + Sync + 'static,
    A: AuthHandler + Send + Sync + 'static,
    Y: SystemHandler + Send + Sync + 'static,
{
    handler: Arc<ServerMessageHandler<S, A, Y>>,
    connections: Arc<Mutex<HashMap<String, ConnectionInfo>>>,
}

impl<S, A, Y> ServerHandle<S, A, Y>
where
    S: ServerHandler + Send + Sync + 'static,
    A: AuthHandler + Send + Sync + 'static,
    Y: SystemHandler + Send + Sync + 'static,
{
    async fn build_context(&self, builder: AppContextBuilder, conn_id: String, client_msg_id: String) -> Option<AppContext> {
        match builder
            .with_conn_id(conn_id)
            .with_client_msg_id(client_msg_id)
            .build() 
        {
            Ok(ctx) => Some(ctx),
            Err(_) => None
        }
    }

    async fn send_response(&self, conn_id: String, client_msg_id: String, response: Response) -> Result<()> {
        let conn = self.connections.lock().await;
        if let Some(info) = conn.get(conn_id.as_str()) {
            info.send(ProtoMessage {
                command: Command::ServerResponse as i32,
                data: response.encode_to_vec(),
                client_id: client_msg_id,
                ..Default::default()
            }).await
        } else {
            debug!("Connection not found: {}", conn_id);
            Err(FlareErr::ConnectionNotFound)
        }
    }
    //发送pong
    async fn send_pong(&self, conn_id: String)->Result<()> {
        let conn = self.connections.lock().await;
        if let Some(info) = conn.get(conn_id.as_str()) {
            info.send(ProtoMessage {
                command: Command::Pong as i32,
                data: "pong".into(),
                ..Default::default()
            }).await
        } else {
            debug!("Connection not found: {}", conn_id);
            Err(FlareErr::ConnectionNotFound)
        }
    }
}

impl Default for Server<DefServerHandler, DefAuthHandler, DefSystemHandler> {
    fn default() -> Self {
        Self::new(ServerMessageHandler::<DefServerHandler, DefAuthHandler, DefSystemHandler>::default())
    }
}