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
use crate::client::config::ClientConfig;
use crate::client::handlers::ClientMessageHandler;
use crate::client::message_handler::DefMessageHandler;
use crate::client::sys_handler::DefClientSystemHandler;
use flare_core::error::FlareErr;
use flare_core::error::Result;
use crate::connections::Connection;
use log::{debug, error, warn};
use prost::Message as ProstMessage;
use flare_core::flare_net::net::LoginReq;
use flare_core::flare_net::net::{Command, Message as ProtoMessage, Response};
use std::collections::HashMap;
use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::oneshot;
use tokio::sync::{mpsc, Mutex};
use tokio::time::{interval, sleep, Instant};
use uuid;

const PING_INTERVAL: Duration = Duration::from_secs(30);
const PONG_TIMEOUT: Duration = Duration::from_secs(10);
const RECONNECT_INTERVAL: Duration = Duration::from_secs(5);
const MAX_RECONNECT_ATTEMPTS: u32 = 5;

#[derive(Clone, Debug)]
pub enum ClientState {
    Disconnected,
    Connecting,
    Connected,
    Authenticating,
    Authenticated,
    Reconnecting { attempt: u32 },
}

impl fmt::Display for ClientState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Disconnected => write!(f, "Disconnected"),
            Self::Connecting => write!(f, "Connecting"),
            Self::Connected => write!(f, "Connected"),
            Self::Authenticating => write!(f, "Authenticating"),
            Self::Authenticated => write!(f, "Authenticated"),
            Self::Reconnecting { attempt } => write!(f, "Reconnecting (attempt {})", attempt),
        }
    }
}

pub struct Client<F>
where
    F: Fn() -> Pin<Box<dyn Future<Output = Result<Box<dyn Connection>>> + Send + Sync>> + Send + Sync + 'static,
{
    config: Arc<Mutex<ClientConfig>>,
    connector: Arc<Mutex<F>>,
    handler: Arc<ClientMessageHandler<DefClientSystemHandler, DefMessageHandler>>,
    state: Arc<Mutex<ClientState>>,
    conn: Arc<Mutex<Option<Box<dyn Connection>>>>,
    message_sender: mpsc::Sender<ProtoMessage>,
    last_pong: Arc<Mutex<Instant>>,
    is_running: Arc<Mutex<bool>>,
    pending_requests: Arc<Mutex<HashMap<String, oneshot::Sender<Response>>>>,
}

impl<F> Client<F>
where
    F: Fn() -> Pin<Box<dyn Future<Output = Result<Box<dyn Connection>>> + Send + Sync>> + Send + Sync + 'static,
{
    pub fn new(
        connector: F,
        config: ClientConfig,
    ) -> Self {
        let (tx, rx) = mpsc::channel(100);
        let handler = Arc::new(ClientMessageHandler::default());
        let state = Arc::new(Mutex::new(ClientState::Disconnected));
        let conn = Arc::new(Mutex::new(None));
        let is_running = Arc::new(Mutex::new(true));
        let last_pong = Arc::new(Mutex::new(Instant::now()));
        let pending_requests = Arc::new(Mutex::new(HashMap::new()));

        let client = Self {
            config: Arc::new(Mutex::new(config)),
            connector: Arc::new(Mutex::new(connector)),
            handler,
            state,
            conn,
            message_sender: tx,
            last_pong,
            is_running,
            pending_requests,
        };

        // 启动消息发送任务
        client.spawn_sender(rx);
        
        client
    }

    /// 连接到服务器
    pub async fn connect(&self) -> Result<()> {
        *self.state.lock().await = ClientState::Connecting;
        self.handler.handle_state_change(ClientState::Connecting).await;
        
        // 创建连接
        let connector = self.connector.lock().await;
        let new_conn = (connector)().await?;
        *self.conn.lock().await = Some(new_conn);

        // 启动消息接收循环
        self.spawn_receiver();
        
        // 进行认证
        self.authenticate().await?;

        // 认证成功后启动心跳检测
        self.spawn_keepalive();
        
        // 更新状态
        self.set_state(ClientState::Connected).await;
        Ok(())
    }

    /// 重连
    pub async fn reconnect(&self) -> Result<()> {
        let mut attempt = 0;
        while attempt < self.config.lock().await.max_reconnect_attempts {
            self.set_state(ClientState::Reconnecting { attempt }).await;
            
            match self.connect().await {
                Ok(()) => {
                    // 重连成功后重新进行认证
                    match self.authenticate().await {
                        Ok(()) => {
                            // 认证成功后重新启动心跳检测
                            self.spawn_keepalive();
                            return Ok(());
                        }
                        Err(e) => {
                            error!("Authentication failed after reconnection: {}", e);
                            attempt += 1;
                        }
                    }
                }
                Err(e) => {
                    error!("Reconnection attempt {} failed: {}", attempt, e);
                    attempt += 1;
                }
            }
            sleep(self.config.lock().await.reconnect_interval).await;
        }
        
        self.set_state(ClientState::Disconnected).await;
        Err(anyhow::anyhow!("Max reconnection attempts reached").into())
    }

    /// 关闭连接
    pub async fn close(&self) -> Result<()> {
        *self.is_running.lock().await = false;
        if let Some(conn) = self.conn.lock().await.as_ref() {
            conn.close().await?;
        }
        Ok(())
    }

    /// 等待连接就绪
    pub async fn wait_ready(&self, timeout: Duration) -> Result<()> {
        let start = Instant::now();
        while start.elapsed() < timeout {
            if self.is_connected().await {
                return Ok(());
            }
            sleep(Duration::from_millis(100)).await;
        }
        Err(anyhow::anyhow!("Connection timeout").into())
    }

    /// 获取当前连接
    pub async fn get_connection(&self) -> Result<Box<dyn Connection>> {
        if let Some(conn) = &*self.conn.lock().await {
            Ok(conn.clone_box())
        } else {
            Err(FlareErr::ConnectionNotFound)
        }
    }

    /// 更新连接
    pub async fn update_connection(&self, connection: Box<dyn Connection>, new_config: ClientConfig) -> Result<()> {
        // 更新连接
        let mut conn = self.conn.lock().await;
        *conn = Some(connection);
        drop(conn);

        // 更新配置
        let mut config = self.config.lock().await;
        *config = new_config;
        drop(config);

        // 设置状态为连接中
        self.set_state(ClientState::Connecting).await;

        // 启动消息接收循环
        self.spawn_receiver();

        // 启动心跳检测
        self.spawn_keepalive();

        // 进行认证
        self.authenticate().await?;

        // 设置状态为已连接
        self.set_state(ClientState::Connected).await;

        // 等待连接就绪
        self.wait_ready(Duration::from_secs(5)).await?;

        Ok(())
    }

    /// 发送消息
    pub async fn send(&self, msg: ProtoMessage) -> Result<()> {
        if !*self.is_running.lock().await {
            return Err(anyhow::anyhow!("Client is not running").into());
        }
        self.message_sender.send(msg).await
            .map_err(|e| anyhow::anyhow!("Failed to send message: {}", e))?;
        Ok(())
    }

    /// 发送消息并等待响应
    pub async fn send_wait(&self, msg: ProtoMessage) -> Result<Response> {
        // 创建一个新的可变消息
        let mut new_msg = msg;
        new_msg.client_id = uuid::Uuid::new_v4().to_string();

        // 创建响应通道
        let (tx, rx) = oneshot::channel();
        {
            let mut pending = self.pending_requests.lock().await;
            pending.insert(new_msg.client_id.clone(), tx);
        }

        // 发送消息
        self.send(new_msg).await?;
        // 等待响应
        match rx.await {
            Ok(response) => Ok(response),
            Err(_) => Err(anyhow::anyhow!("Response channel closed").into())
        }
    }

    /// 发送消息并等待响应,带超时
    pub async fn send_wait_timeout(&self, msg: ProtoMessage, timeout: Duration) -> Result<Response> {
        tokio::time::timeout(timeout, self.send_wait(msg))
            .await
            .map_err(|_| FlareErr::ConnectionError("Request timeout".to_string()))?
    }

    /// 获取当前状态
    pub async fn get_state(&self) -> ClientState {
        self.state.lock().await.clone()
    }

    /// 检查连接是否可用
    pub async fn is_connected(&self) -> bool {
        if !*self.is_running.lock().await {
            return false;
        }

        match self.get_state().await {
            ClientState::Connected | ClientState::Authenticated => {
                let last_pong = *self.last_pong.lock().await;
                if last_pong.elapsed() > self.config.lock().await.pong_timeout {
                    return false;
                }

                let ping = ProtoMessage {
                    command: Command::Ping as i32,
                    ..Default::default()
                };

                let conn_ref = Self::get_connection_ref(&self.conn).await;
                if let Some(conn_ref) = conn_ref {
                    match conn_ref.send(ping).await {
                        Ok(_) => true,
                        Err(_) => false,
                    }
                } else {
                    false
                }
            }
            _ => false,
        }
    }

    /// 获取连接状态详情
    pub async fn connection_status(&self) -> ConnectionStatus {
        ConnectionStatus {
            state: self.get_state().await,
            is_running: *self.is_running.lock().await,
            last_pong_elapsed: self.last_pong.lock().await.elapsed(),
            has_active_connection: self.conn.lock().await.is_some(),
        }
    }

    // 认证相关
    async fn authenticate(&self) -> Result<()> {
        self.set_state(ClientState::Authenticating).await;
        let conf = self.config.lock().await;
        let req = LoginReq {
            user_id: conf.user_id.clone(),
            platform: conf.platform as i32,
            client_id: conf.client_id.clone(),
            token: conf.auth_token.clone(),
            ..Default::default()
        };
        let auth_msg = ProtoMessage {
            command: Command::Login as i32,
            data: req.encode_to_vec(),
            ..Default::default()
        };
        
        self.send(auth_msg).await?;
        self.set_state(ClientState::Authenticated).await;
        Ok(())
    }

    // 状态管理
    async fn set_state(&self, new_state: ClientState) {
        let mut state = self.state.lock().await;
        *state = new_state.clone();
        self.handler.handle_state_change(new_state).await;
    }

    // 连接管理
    async fn get_connection_ref(conn: &Arc<Mutex<Option<Box<dyn Connection>>>>) -> Option<Arc<Box<dyn Connection>>> {
        conn.lock().await.as_ref().map(|c| Arc::new(c.clone_box()))
    }

    // 后台任务
    fn spawn_sender(&self, mut rx: mpsc::Receiver<ProtoMessage>) {
        let conn = self.conn.clone();
        let is_running = self.is_running.clone();
        
        tokio::spawn(async move {
            let mut conn_ref = None;
            let mut msg_buffer = Vec::with_capacity(32); // 消息缓冲区
            let mut flush_timer = tokio::time::interval(Duration::from_millis(10)); // 定时刷新
            
            loop {
                tokio::select! {
                    // 接收消息
                    msg = rx.recv() => {
                        match msg {
                            Some(msg) => {
                                if !*is_running.lock().await {
                                    warn!("Client is not running, skipping message send.");
                                    break;
                                }
                                msg_buffer.push(msg);
                                
                                // 缓冲区满时立即发送
                                if msg_buffer.len() >= 32 {
                                    if let Err(e) = Self::flush_messages(&mut conn_ref, &conn, &mut msg_buffer).await {
                                        error!("Failed to flush messages: {}", e);
                                    }
                                }
                            }
                            None => break,
                        }
                    }
                    
                    // 定时刷新缓冲区
                    _ = flush_timer.tick() => {
                        if !msg_buffer.is_empty() {
                            if let Err(e) = Self::flush_messages(&mut conn_ref, &conn, &mut msg_buffer).await {
                                error!("Failed to flush messages: {}", e);
                            }
                        }
                    }
                }
            }
            
            // 退出前确保发送所有消息
            if !msg_buffer.is_empty() {
                if let Err(e) = Self::flush_messages(&mut conn_ref, &conn, &mut msg_buffer).await {
                    error!("Failed to flush remaining messages: {}", e);
                }
            }
        });
    }

    fn spawn_receiver(&self) {
        let conn = self.conn.clone();
        let handler = self.handler.clone();
        let is_running = self.is_running.clone();
        let last_pong = self.last_pong.clone();
        let pending_requests = self.pending_requests.clone();

        tokio::spawn(async move {
            while *is_running.lock().await {
                if let Some(conn_ref) = Self::get_connection_ref(&conn).await {
                    match conn_ref.receive().await {
                        Ok(msg) => {
                            // 处理 PONG 消息
                            if msg.command == Command::Pong as i32 {
                                *last_pong.lock().await = Instant::now();
                                continue;
                            }
                            // 处理服务端Ping
                            if msg.command == Command::Ping as i32 {
                                if let Err(e) = conn_ref.send(ProtoMessage {
                                    command: Command::Pong as i32,
                                    ..Default::default()
                                }).await {
                                    error!("Failed to send Pong message: {}", e);
                                }
                                continue;
                            }
                            // 处理响应消息
                            if msg.command == Command::ServerResponse as i32 {
                                if let Ok(response) = Response::decode(&msg.data[..]) {
                                    // 检查是否有待处理的请求
                                    let mut pending = pending_requests.lock().await;
                                    if let Some(tx) = pending.remove(&msg.client_id) {
                                        let _ = tx.send(response.clone());
                                    }
                                    // 通知消息处理器
                                    handler.on_response(&response).await;
                                }
                                continue;
                            }

                            // 处理其他消息
                            if let Ok(command) = Command::try_from(msg.command) {
                                if let Err(e) = handler.handle_command(command, msg.data).await {
                                    error!("Failed to handle command: {}", e);
                                }
                            }
                        }
                        Err(e) => {
                            error!("Failed to receive message: {}", e);
                            break;
                        }
                    }
                }
                tokio::time::sleep(Duration::from_millis(10)).await;
            }
        });
    }

    fn spawn_keepalive(&self) {
        let conn = self.conn.clone();
        let is_running = self.is_running.clone();
        let state = self.state.clone();
        let handler = self.handler.clone();
        let config = Arc::clone(&self.config);
        
        tokio::spawn(async move {
            let config = config.lock().await;
            let mut interval = interval(config.ping_interval);
            let reconnect_interval = config.reconnect_interval;
            drop(config);

            while *is_running.lock().await {
                interval.tick().await;

                // 检查连接状态
                if !matches!(*state.lock().await, ClientState::Connected | ClientState::Authenticated) {
                    // 如果连接断开,等待一段时间后尝试重连
                    sleep(reconnect_interval).await;
                    handler.handle_state_change(ClientState::Reconnecting { attempt: 0 }).await;
                    continue;
                }
                
                // 发送心跳包
                if let Some(conn_ref) = Self::get_connection_ref(&conn).await {
                    let ping_msg = ProtoMessage {
                        command: Command::Ping as i32,
                        ..Default::default()
                    };
                    
                    if let Err(e) = conn_ref.send(ping_msg).await {
                        error!("Failed to send ping: {}", e);
                        *state.lock().await = ClientState::Disconnected;
                        handler.handle_state_change(ClientState::Disconnected).await;
                    }
                }
            }
        });
    }

    // 消息处理
    async fn flush_messages(
        conn_ref: &mut Option<Arc<Box<dyn Connection>>>,
        conn: &Arc<Mutex<Option<Box<dyn Connection>>>>,
        msg_buffer: &mut Vec<ProtoMessage>
    ) -> Result<()> {
        // 确保连接可用
        if conn_ref.is_none() {
            *conn_ref = Self::get_connection_ref(conn).await;
        }

        if let Some(ref conn) = conn_ref {
            let mut failed = false;
            
            // 批量发送所有消息
            for msg in msg_buffer.drain(..) {
                debug!("Sending message: {:?}", msg);
                if let Err(e) = conn.send(msg).await {
                    error!("Failed to send message: {}", e);
                    failed = true;
                    break;
                }
            }

            if failed {
                *conn_ref = None; // 发送失败时清除连接引用
                msg_buffer.clear(); // 清空缓冲区
            }
        }
        
        Ok(())
    }
}

/// 连接状态详情
#[derive(Debug, Clone)]
pub struct ConnectionStatus {
    pub state: ClientState,
    pub is_running: bool,
    pub last_pong_elapsed: Duration,
    pub has_active_connection: bool,
}

impl fmt::Display for ConnectionStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "State: {}, Running: {}, Last Pong: {:?} ago, Has Connection: {}",
            self.state,
            self.is_running,
            self.last_pong_elapsed,
            self.has_active_connection
        )
    }
}

impl<F> Drop for Client<F>
where
    F: Fn() -> Pin<Box<dyn Future<Output = Result<Box<dyn Connection>>> + Send + Sync>> + Send + Sync + 'static,
{
    fn drop(&mut self) {
        futures::executor::block_on(async {
            *self.is_running.lock().await = false;
        });
    }
}