flare-core 1.0.3

A high-performance, reliable long-connection communication toolkit for Rust, supporting WebSocket and QUIC protocols with features like authentication, device management, serialization negotiation, and protocol racing.
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
//! 简单模式服务端构建器
//!
//! 提供最小实现,使用闭包定义消息处理逻辑,适合快速原型开发和学习。
//!
//! ## 特点
//! - ✅ **使用闭包处理消息和连接事件**:无需实现 trait,直接使用闭包
//! - ✅ **最小依赖**:只提供基本的消息处理功能
//! - ✅ **零配置**:使用默认配置即可运行
//! - ✅ **轻量级**:不包含中间件、管道等高级功能
//! - ✅ **快速上手**:几行代码即可启动服务器
//!
//! ## 适用场景
//! - 快速原型开发
//! - 学习和测试
//! - 小型应用
//! - 需要完全控制消息处理流程的场景
//!
//! ## 架构说明
//!
//! 简单模式基于 `HybridServer`,共享所有核心能力(多协议支持、序列化协商、心跳检测等),
//! 但消息处理逻辑通过闭包定义,不依赖高级抽象。

use crate::common::error::Result;
use crate::common::protocol::{Frame, PayloadCommand};
use crate::server::HybridServer;
use crate::server::builder::{BaseServerBuilderConfig, ServerWrapper};
use crate::server::events::handler::ServerEventHandler;
use crate::server::handle::ServerHandle;
use async_trait::async_trait;
use std::sync::Arc;
use tokio::sync::Mutex;

/// 消息处理上下文
///
/// 提供给消息处理函数的上下文,包含连接信息和服务器操作处理器
pub struct MessageContext {
    /// 连接 ID
    pub connection_id: String,
    /// 服务器操作处理器(轻量级,用于发送消息和连接管理)
    handle: Arc<dyn ServerHandle>,
}

impl MessageContext {
    /// 创建新的消息上下文
    fn new(connection_id: String, handle: Arc<dyn ServerHandle>) -> Self {
        Self {
            connection_id,
            handle,
        }
    }

    /// 向指定连接发送消息
    pub async fn send_to(&self, connection_id: &str, frame: &Frame) -> Result<()> {
        self.handle.send_to(connection_id, frame).await
    }

    /// 向指定用户的所有连接发送消息
    pub async fn send_to_user(&self, user_id: &str, frame: &Frame) -> Result<()> {
        self.handle.send_to_user(user_id, frame).await
    }

    /// 广播消息到所有连接
    pub async fn broadcast(&self, frame: &Frame) -> Result<()> {
        self.handle.broadcast(frame).await
    }

    /// 广播消息到所有连接,排除指定连接
    pub async fn broadcast_except(&self, frame: &Frame, exclude_connection_id: &str) -> Result<()> {
        self.handle
            .broadcast_except(frame, exclude_connection_id)
            .await
    }

    /// 断开指定连接
    pub async fn disconnect(&self, connection_id: &str) -> Result<()> {
        self.handle.disconnect(connection_id).await
    }

    /// 获取连接数量
    pub fn connection_count(&self) -> usize {
        self.handle.connection_count()
    }

    /// 获取用户数量
    pub fn user_count(&self) -> usize {
        self.handle.user_count()
    }
}

/// 消息处理函数类型
pub type MessageHandlerFn = Box<
    dyn for<'a> Fn(
            &'a Frame,
            &'a MessageContext,
        ) -> std::pin::Pin<
            Box<dyn std::future::Future<Output = Result<Option<Frame>>> + Send + 'a>,
        > + Send
        + Sync,
>;

/// 连接事件处理函数类型
pub type OnConnectFn = Box<
    dyn for<'a> Fn(
            &'a str,
            &'a MessageContext,
        )
            -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + Send + 'a>>
        + Send
        + Sync,
>;

/// 断开连接事件处理函数类型
pub type OnDisconnectFn = Box<
    dyn for<'a> Fn(
            &'a str,
            &'a MessageContext,
        )
            -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + Send + 'a>>
        + Send
        + Sync,
>;

/// 简化的连接处理器(仅用于存储闭包和 handle)
struct SimpleConnectionHandler {
    message_handler: Option<MessageHandlerFn>,
    on_connect: Option<OnConnectFn>,
    on_disconnect: Option<OnDisconnectFn>,
    handle: Arc<Mutex<Option<Arc<dyn ServerHandle>>>>,
}

impl SimpleConnectionHandler {
    async fn set_handle(&self, handle: Arc<dyn ServerHandle>) {
        *self.handle.lock().await = Some(handle);
    }
}

/// 将 SimpleConnectionHandler 适配为 ServerEventHandler
struct SimpleEventHandlerAdapter {
    handler: Arc<SimpleConnectionHandler>,
}

#[async_trait]
impl ServerEventHandler for SimpleEventHandlerAdapter {
    async fn handle_message(
        &self,
        command: &PayloadCommand,
        connection_id: &str,
    ) -> Result<Option<Frame>> {
        let handle = {
            let handle_guard = self.handler.handle.lock().await;
            handle_guard.clone()
        };

        let context = if let Some(ref handle) = handle {
            MessageContext::new(connection_id.to_string(), Arc::clone(handle))
        } else {
            return Err(crate::common::error::FlareError::general_error(
                "Server handle is not available",
            ));
        };

        let frame = Frame {
            message_id: command.message_id.clone(),
            command: Some(crate::common::protocol::flare::core::commands::Command {
                r#type: Some(
                    crate::common::protocol::flare::core::commands::command::Type::Payload(
                        command.clone(),
                    ),
                ),
            }),
            metadata: std::collections::HashMap::new(),
            reliability: crate::common::protocol::Reliability::AtLeastOnce as i32,
            timestamp: 0,
        };

        if let Some(ref message_handler) = self.handler.message_handler {
            message_handler(&frame, &context).await
        } else {
            Ok(None)
        }
    }

    async fn on_connect(&self, connection_id: &str) -> Result<()> {
        let handle = {
            let handle_guard = self.handler.handle.lock().await;
            handle_guard.clone()
        };

        let context = if let Some(ref handle) = handle {
            MessageContext::new(connection_id.to_string(), Arc::clone(handle))
        } else {
            return Err(crate::common::error::FlareError::general_error(
                "Server handle is not available",
            ));
        };

        if let Some(ref on_connect) = self.handler.on_connect {
            on_connect(connection_id, &context).await
        } else {
            Ok(())
        }
    }

    async fn on_disconnect(&self, connection_id: &str, _reason: Option<&str>) -> Result<()> {
        let handle = {
            let handle_guard = self.handler.handle.lock().await;
            handle_guard.clone()
        };

        let context = if let Some(ref handle) = handle {
            MessageContext::new(connection_id.to_string(), Arc::clone(handle))
        } else {
            return Err(crate::common::error::FlareError::general_error(
                "Server handle is not available",
            ));
        };

        if let Some(ref on_disconnect) = self.handler.on_disconnect {
            on_disconnect(connection_id, &context).await
        } else {
            Ok(())
        }
    }
}

/// 简化的服务器实例
///
/// 提供简化的接口,自动处理服务器引用
pub struct SimpleServer {
    wrapper: ServerWrapper,
    handler: Arc<SimpleConnectionHandler>,
    handle: Arc<dyn ServerHandle>,
}

impl SimpleServer {
    /// 启动服务器
    pub async fn start(&mut self) -> Result<()> {
        // 设置 ServerHandle
        self.handler.set_handle(Arc::clone(&self.handle)).await;
        self.wrapper.start().await
    }

    /// 停止服务器
    pub async fn stop(&mut self) -> Result<()> {
        self.wrapper.stop().await
    }

    /// 检查服务器是否运行
    pub fn is_running(&self) -> bool {
        self.wrapper.is_running()
    }

    /// 获取连接数量
    pub fn connection_count(&self) -> usize {
        self.wrapper.connection_count()
    }

    /// 获取用户数量
    pub fn user_count(&self) -> usize {
        self.wrapper.user_count()
    }

    /// 获取 ServerHandle(用于消息发送和连接管理)
    pub fn handle(&self) -> Arc<dyn ServerHandle> {
        Arc::clone(&self.handle)
    }

    /// 向指定连接发送消息
    pub async fn send_to(&self, connection_id: &str, frame: &Frame) -> Result<()> {
        self.wrapper.send_to(connection_id, frame).await
    }

    /// 向指定用户的所有连接发送消息
    pub async fn send_to_user(&self, user_id: &str, frame: &Frame) -> Result<()> {
        self.wrapper.send_to_user(user_id, frame).await
    }

    /// 广播消息到所有连接
    pub async fn broadcast(&self, frame: &Frame) -> Result<()> {
        self.wrapper.broadcast(frame).await
    }

    /// 广播消息到所有连接,排除指定连接
    pub async fn broadcast_except(&self, frame: &Frame, exclude_connection_id: &str) -> Result<()> {
        self.wrapper
            .broadcast_except(frame, exclude_connection_id)
            .await
    }

    /// 断开指定连接
    pub async fn disconnect(&self, connection_id: &str) -> Result<()> {
        self.wrapper.disconnect(connection_id).await
    }

    /// 获取协议列表
    pub fn protocols(&self) -> Vec<crate::common::config_types::TransportProtocol> {
        self.wrapper.protocols()
    }
}

/// 简单模式服务端构建器
///
/// 提供最小实现,使用闭包定义消息处理逻辑,适合快速原型开发和学习。
///
/// ## 设计原则
///
/// - **公共逻辑统一处理**:基于 `HybridServer`,共享所有核心能力(多协议、序列化协商、心跳等)
/// - **最小抽象**:消息处理通过闭包定义,不依赖 trait 实现
/// - **零配置**:使用默认配置即可运行
///
/// ## 使用方式
///
/// 使用闭包定义消息处理和连接事件处理逻辑,无需实现 trait。
pub struct ServerBuilder {
    base: BaseServerBuilderConfig,
    message_handler: Option<MessageHandlerFn>,
    on_connect: Option<OnConnectFn>,
    on_disconnect: Option<OnDisconnectFn>,
}

impl ServerBuilder {
    /// 创建新的服务端构建器
    ///
    /// # 参数
    /// - `bind_address`: 监听地址,例如 "0.0.0.0:8080"
    pub fn new(bind_address: impl Into<String>) -> Self {
        Self {
            base: BaseServerBuilderConfig::new(bind_address),
            message_handler: None,
            on_connect: None,
            on_disconnect: None,
        }
    }

    /// 设置认证器(如果启用认证,必须提供)
    ///
    /// 如果设置了认证器,还需要在配置中启用认证:
    /// ```rust,ignore
    /// .enable_auth()
    /// .with_authenticator(authenticator)
    /// ```
    pub fn with_authenticator(
        mut self,
        authenticator: Arc<dyn crate::server::auth::Authenticator>,
    ) -> Self {
        self.base = self.base.with_authenticator(authenticator);
        self
    }

    /// 启用认证
    pub fn enable_auth(mut self) -> Self {
        self.base = self.base.enable_auth();
        self
    }

    /// 设置认证超时时间
    pub fn with_auth_timeout(mut self, timeout: std::time::Duration) -> Self {
        self.base = self.base.with_auth_timeout(timeout);
        self
    }

    /// 设置消息处理函数
    ///
    /// # 参数
    /// - `handler`: 消息处理函数,接收 `Frame` 和 `MessageContext`,返回 `Option<Frame>`(可选回复)
    pub fn on_message<F>(mut self, handler: F) -> Self
    where
        F: for<'a> Fn(
                &'a Frame,
                &'a MessageContext,
            ) -> std::pin::Pin<
                Box<dyn std::future::Future<Output = Result<Option<Frame>>> + Send + 'a>,
            > + Send
            + Sync
            + 'static,
    {
        self.message_handler = Some(Box::new(move |frame, ctx| handler(frame, ctx)));
        self
    }

    /// 设置连接建立事件处理函数
    ///
    /// # 参数
    /// - `handler`: 连接建立处理函数,接收 connection_id 和 MessageContext
    pub fn on_connect<F>(mut self, handler: F) -> Self
    where
        F: for<'a> Fn(
                &'a str,
                &'a MessageContext,
            ) -> std::pin::Pin<
                Box<dyn std::future::Future<Output = Result<()>> + Send + 'a>,
            > + Send
            + Sync
            + 'static,
    {
        self.on_connect = Some(Box::new(move |conn_id, ctx| handler(conn_id, ctx)));
        self
    }

    /// 设置连接断开事件处理函数
    ///
    /// # 参数
    /// - `handler`: 连接断开处理函数,接收 connection_id 和 MessageContext
    pub fn on_disconnect<F>(mut self, handler: F) -> Self
    where
        F: for<'a> Fn(
                &'a str,
                &'a MessageContext,
            ) -> std::pin::Pin<
                Box<dyn std::future::Future<Output = Result<()>> + Send + 'a>,
            > + Send
            + Sync
            + 'static,
    {
        self.on_disconnect = Some(Box::new(move |conn_id, ctx| handler(conn_id, ctx)));
        self
    }

    /// 设置传输协议
    pub fn with_protocol(
        mut self,
        protocol: crate::common::config_types::TransportProtocol,
    ) -> Self {
        self.base = self.base.with_protocol(protocol);
        self
    }

    /// 启用多协议监听
    pub fn with_protocols(
        mut self,
        protocols: Vec<crate::common::config_types::TransportProtocol>,
    ) -> Self {
        self.base = self.base.with_protocols(protocols);
        self
    }

    /// 设置最大连接数
    pub fn with_max_connections(mut self, max: usize) -> Self {
        self.base = self.base.with_max_connections(max);
        self
    }

    /// 设置握手超时时间
    pub fn with_handshake_timeout(mut self, timeout: std::time::Duration) -> Self {
        self.base = self.base.with_handshake_timeout(timeout);
        self
    }

    /// 设置最大并发握手数
    pub fn with_max_handshake_concurrency(mut self, max: usize) -> Self {
        self.base = self.base.with_max_handshake_concurrency(max);
        self
    }

    /// 设置单次连接写入超时时间
    pub fn with_write_timeout(mut self, timeout: std::time::Duration) -> Self {
        self.base = self.base.with_write_timeout(timeout);
        self
    }

    /// 设置 fanout 发送最大并发度
    pub fn with_fanout_concurrency(mut self, max: usize) -> Self {
        self.base = self.base.with_fanout_concurrency(max);
        self
    }

    /// 设置心跳配置
    pub fn with_heartbeat(
        mut self,
        heartbeat: crate::common::config_types::HeartbeatConfig,
    ) -> Self {
        self.base = self.base.with_heartbeat(heartbeat);
        self
    }

    /// 设置 TLS 配置
    pub fn with_tls(mut self, tls: crate::common::config_types::TlsConfig) -> Self {
        self.base = self.base.with_tls(tls);
        self
    }

    /// 设置默认序列化格式(用于协商,默认 Protobuf)
    pub fn with_default_format(
        mut self,
        format: crate::common::protocol::SerializationFormat,
    ) -> Self {
        self.base = self.base.with_default_format(format);
        self
    }

    /// 设置默认压缩算法(用于协商,默认 None)
    pub fn with_default_compression(
        mut self,
        compression: crate::common::compression::CompressionAlgorithm,
    ) -> Self {
        self.base = self.base.with_default_compression(compression);
        self
    }

    /// 构建服务端
    ///
    /// # 返回
    /// 返回配置好的 SimpleServer 实例
    pub fn build(self) -> Result<SimpleServer> {
        let handler = Arc::new(SimpleConnectionHandler {
            message_handler: self.message_handler,
            on_connect: self.on_connect,
            on_disconnect: self.on_disconnect,
            handle: Arc::new(Mutex::new(None)),
        });

        let event_handler: Arc<dyn crate::server::events::handler::ServerEventHandler> =
            Arc::new(SimpleEventHandlerAdapter {
                handler: handler.clone(),
            });

        let server = HybridServer::with_connection_manager(
            self.base.config,
            None,
            None,
            Some(event_handler),
            self.base.authenticator,
        )?;

        // 使用 ServerWrapper 统一管理
        let wrapper = ServerWrapper::new(server);

        // 创建 ServerHandle(通过 ServerWrapper)
        let handle = wrapper.get_server_handle().ok_or_else(|| {
            crate::common::error::FlareError::general_error("Failed to create ServerHandle")
        })?;

        Ok(SimpleServer {
            wrapper,
            handler,
            handle,
        })
    }
}