Skip to main content

flare_core/server/transports/
hybrid.rs

1//! 混合服务端接口
2//!
3//! 支持单个协议或多协议同时监听
4//! 统一管理连接和心跳检测,简化服务器实现
5
6use super::Server;
7use super::server_core::ServerCore;
8use crate::common::config_types::TransportProtocol;
9use crate::common::error::Result;
10use crate::common::protocol::Frame;
11use crate::server::config::ServerConfig;
12use crate::server::handle::ServerHandle;
13use async_trait::async_trait;
14use std::sync::Arc;
15use std::sync::atomic::{AtomicBool, Ordering};
16use tokio::sync::Mutex;
17use tracing::error;
18
19#[cfg(feature = "quic")]
20use super::quic::QUICServer;
21#[cfg(feature = "tcp")]
22use super::tcp::TCPServer;
23#[cfg(feature = "websocket")]
24use super::websocket::WebSocketServer;
25
26/// 混合服务端
27///
28/// 支持单个协议或多协议同时监听
29/// 统一管理连接和心跳检测,简化服务器实现
30pub struct HybridServer {
31    /// 内部服务器列表
32    servers: Vec<Arc<Mutex<Box<dyn Server>>>>,
33    /// 使用的协议列表
34    protocols: Vec<TransportProtocol>,
35    /// 是否正在运行
36    is_running: Arc<AtomicBool>,
37    /// 服务器核心功能(统一管理连接和心跳)
38    core: Option<Arc<ServerCore>>,
39    /// 配置(用于启动心跳检测)
40    config: ServerConfig,
41}
42
43impl HybridServer {
44    /// 创建新的混合服务端
45    ///
46    /// # 参数
47    /// - `config`: 服务端配置
48    ///
49    /// # 返回
50    /// 混合服务端实例
51    pub fn new(config: ServerConfig) -> Result<Self> {
52        Self::with_connection_manager(config, None, None, None, None)
53    }
54
55    /// 使用指定的连接管理器创建混合服务端
56    ///
57    /// # 参数
58    /// - `config`: 服务端配置
59    /// - `connection_manager`: 可选的连接管理器,如果为 None,则创建新的并统一管理
60    /// - `device_manager`: 可选的设备管理器,如果为 None 且配置中指定了设备冲突策略,则自动创建
61    /// - `event_handler`: 可选的事件处理器
62    /// - `authenticator`: 可选的认证器,如果配置中启用认证,必须提供
63    ///
64    /// # 返回
65    /// 混合服务端实例
66    pub fn with_connection_manager(
67        config: ServerConfig,
68        connection_manager: Option<Arc<crate::server::connection::ConnectionManager>>,
69        device_manager: Option<Arc<crate::server::device::DeviceManager>>,
70        event_handler: Option<Arc<dyn crate::server::events::handler::ServerEventHandler>>,
71        authenticator: Option<Arc<dyn crate::server::auth::Authenticator>>,
72    ) -> Result<Self> {
73        Self::with_connection_manager_and_pipeline(
74            config,
75            connection_manager,
76            device_manager,
77            event_handler,
78            authenticator,
79            Vec::new(),
80            Vec::new(),
81        )
82    }
83
84    /// 使用指定的连接管理器和消息管道创建混合服务端
85    ///
86    /// # 参数
87    /// - `config`: 服务端配置
88    /// - `connection_manager`: 可选的连接管理器
89    /// - `device_manager`: 可选的设备管理器
90    /// - `event_handler`: 可选的事件处理器
91    /// - `authenticator`: 可选的认证器
92    /// - `middlewares`: 中间件列表
93    /// - `processors`: 处理器列表
94    ///
95    /// # 返回
96    /// 混合服务端实例
97    pub fn with_connection_manager_and_pipeline(
98        config: ServerConfig,
99        connection_manager: Option<Arc<crate::server::connection::ConnectionManager>>,
100        device_manager: Option<Arc<crate::server::device::DeviceManager>>,
101        event_handler: Option<Arc<dyn crate::server::events::handler::ServerEventHandler>>,
102        authenticator: Option<Arc<dyn crate::server::auth::Authenticator>>,
103        middlewares: Vec<crate::common::message::pipeline::ArcMessageMiddleware>,
104        processors: Vec<crate::common::message::pipeline::ArcMessageProcessor>,
105    ) -> Result<Self> {
106        // 创建服务器核心,统一管理连接和心跳
107        let mut core = ServerCore::new(&config, connection_manager.clone());
108
109        // 确定设备管理器:优先使用传入的,否则根据配置创建
110        let final_device_manager = if let Some(dm) = device_manager {
111            Some(dm)
112        } else if config.device_conflict_strategy
113            != crate::common::device::DeviceConflictStrategy::AllowAll
114        {
115            Some(Arc::new(crate::server::device::DeviceManager::new(
116                config.device_conflict_strategy.clone(),
117            )))
118        } else {
119            None
120        };
121
122        core = core
123            .with_device_manager(final_device_manager)
124            .with_event_handler(event_handler)
125            .with_authenticator(authenticator);
126
127        // 添加中间件和处理器(在包装为 Arc 之前)
128        // 使用 tokio::task::block_in_place 来允许在异步运行时中阻塞当前线程
129        // 这样可以避免 "Cannot start a runtime from within a runtime" 错误
130        if !middlewares.is_empty() || !processors.is_empty() {
131            tokio::task::block_in_place(|| {
132                let handle = tokio::runtime::Handle::try_current().map_err(|_| {
133                    crate::common::error::FlareError::general_error(
134                        "Tokio runtime not available".to_string(),
135                    )
136                })?;
137
138                handle.block_on(async {
139                    for middleware in middlewares {
140                        core.add_middleware(middleware).await;
141                    }
142                    for processor in processors {
143                        core.add_processor(processor).await;
144                    }
145                });
146                Ok::<(), crate::common::error::FlareError>(())
147            })
148            .map_err(|e| {
149                crate::common::error::FlareError::general_error(format!(
150                    "Failed to add middlewares/processors: {}",
151                    e
152                ))
153            })?;
154        }
155
156        // 将 ServerCore 包装为 Arc,以便共享给 WebSocketServer 和 QUICServer
157        let shared_core = Arc::new(core);
158
159        let protocols = config.get_protocols();
160        let mut servers = Vec::new();
161        let mut effective_protocols = Vec::new();
162        let has_websocket = protocols.contains(&TransportProtocol::WebSocket);
163
164        for protocol in &protocols {
165            let mut server_config = config.clone();
166            server_config.transport = *protocol;
167            server_config.transports = None;
168
169            // 使用配置的协议地址,如果没有配置则使用默认地址
170            let bind_address = config.get_protocol_address(protocol);
171            server_config.bind_address = bind_address;
172
173            let server_result: Result<Box<dyn Server>> = match protocol {
174                TransportProtocol::WebSocket => {
175                    #[cfg(feature = "websocket")]
176                    {
177                        Ok(Box::new(WebSocketServer::with_shared_core(
178                            server_config,
179                            shared_core.clone(),
180                        )))
181                    }
182                    #[cfg(not(feature = "websocket"))]
183                    {
184                        let _ = (server_config, &shared_core);
185                        Err(crate::common::error::FlareError::operation_not_supported(
186                            "WebSocket server feature is disabled",
187                        ))
188                    }
189                }
190                TransportProtocol::QUIC => {
191                    #[cfg(feature = "quic")]
192                    {
193                        QUICServer::with_shared_core(server_config, shared_core.clone())
194                            .map(|s| Box::new(s) as Box<dyn Server>)
195                    }
196                    #[cfg(not(feature = "quic"))]
197                    {
198                        let _ = (server_config, &shared_core);
199                        Err(crate::common::error::FlareError::operation_not_supported(
200                            "QUIC server feature is disabled",
201                        ))
202                    }
203                }
204                TransportProtocol::TCP => {
205                    #[cfg(feature = "tcp")]
206                    {
207                        Ok(Box::new(TCPServer::with_shared_core(
208                            server_config,
209                            shared_core.clone(),
210                        )))
211                    }
212                    #[cfg(not(feature = "tcp"))]
213                    {
214                        let _ = (server_config, &shared_core);
215                        Err(crate::common::error::FlareError::operation_not_supported(
216                            "TCP server feature is disabled",
217                        ))
218                    }
219                }
220            };
221
222            match server_result {
223                Ok(server) => {
224                    servers.push(Arc::new(Mutex::new(server)));
225                    effective_protocols.push(*protocol);
226                }
227                Err(e) if *protocol == TransportProtocol::QUIC && has_websocket => {
228                    tracing::warn!(
229                        "QUIC server unavailable ({e}), continuing with WebSocket only (set FLARE_WS_ONLY=1 to skip QUIC bind attempts)"
230                    );
231                }
232                Err(e) => return Err(e),
233            }
234        }
235
236        if effective_protocols.is_empty() {
237            return Err(crate::common::error::FlareError::connection_failed(
238                "No transport servers could be started".to_string(),
239            ));
240        }
241
242        Ok(Self {
243            servers,
244            protocols: effective_protocols,
245            is_running: Arc::new(AtomicBool::new(false)),
246            core: Some(shared_core),
247            config,
248        })
249    }
250
251    /// 获取使用的协议列表
252    pub fn protocols(&self) -> &[TransportProtocol] {
253        &self.protocols
254    }
255
256    /// 获取 ServerCore 的引用(用于创建 ServerHandle)
257    pub fn core(&self) -> Option<&Arc<ServerCore>> {
258        self.core.as_ref()
259    }
260
261    /// 获取 ServerCore 的可变引用(用于修改)
262    pub fn core_mut(&mut self) -> Option<&mut Arc<ServerCore>> {
263        self.core.as_mut()
264    }
265}
266
267#[async_trait::async_trait]
268impl Server for HybridServer {
269    async fn start(&mut self) -> Result<()> {
270        // 启动心跳检测(统一管理)
271        if let Some(ref mut core) = self.core {
272            core.start_heartbeat(&self.config);
273        }
274
275        let mut started_count = 0;
276        let mut errors = Vec::new();
277
278        // 启动所有服务器
279        for server in &self.servers {
280            let mut s = server.lock().await;
281            match s.start().await {
282                Ok(_) => {
283                    started_count += 1;
284                }
285                Err(e) => {
286                    error!("Failed to start server: {:?}", e);
287                    errors.push(e);
288                }
289            }
290        }
291
292        // 如果所有服务器都启动失败,返回错误
293        if started_count == 0 && !errors.is_empty() {
294            self.is_running.store(false, Ordering::SeqCst);
295            return Err(errors.remove(0));
296        }
297
298        // 如果至少有一个服务器启动成功,标记为运行状态
299        if started_count > 0 {
300            self.is_running.store(true, Ordering::SeqCst);
301        }
302
303        Ok(())
304    }
305
306    async fn stop(&mut self) -> Result<()> {
307        self.is_running.store(false, Ordering::SeqCst);
308
309        // 停止心跳检测
310        if let Some(ref mut core) = self.core {
311            core.stop_heartbeat();
312        }
313
314        // 停止所有服务器
315        for server in &self.servers {
316            let mut s = server.lock().await;
317            if let Err(e) = s.stop().await {
318                error!("Failed to stop server: {:?}", e);
319            }
320        }
321
322        Ok(())
323    }
324
325    fn is_running(&self) -> bool {
326        self.is_running.load(Ordering::SeqCst)
327    }
328}
329
330/// 让 HybridServer 实现 ServerHandle trait
331/// 这样可以在任何需要发送消息的地方注入 HybridServer 的 ServerCore,而不需要整个 Server
332#[async_trait]
333impl ServerHandle for HybridServer {
334    async fn send_to(&self, connection_id: &str, frame: &Frame) -> Result<()> {
335        // 直接通过 ServerCore(实现了 ServerHandle)发送消息
336        if let Some(ref core) = self.core {
337            return ServerHandle::send_to(&**core, connection_id, frame).await;
338        }
339        Err(crate::common::error::FlareError::protocol_error(
340            "ServerCore not initialized".to_string(),
341        ))
342    }
343
344    async fn send_to_user(&self, user_id: &str, frame: &Frame) -> Result<()> {
345        // 直接通过 ServerCore(实现了 ServerHandle)发送消息
346        if let Some(ref core) = self.core {
347            return ServerHandle::send_to_user(&**core, user_id, frame).await;
348        }
349        Err(crate::common::error::FlareError::protocol_error(
350            "ServerCore not initialized".to_string(),
351        ))
352    }
353
354    async fn broadcast(&self, frame: &Frame) -> Result<()> {
355        // 直接通过 ServerCore(实现了 ServerHandle)广播消息
356        if let Some(ref core) = self.core {
357            return ServerHandle::broadcast(&**core, frame).await;
358        }
359        Err(crate::common::error::FlareError::protocol_error(
360            "ServerCore not initialized".to_string(),
361        ))
362    }
363
364    async fn broadcast_except(&self, frame: &Frame, exclude_connection_id: &str) -> Result<()> {
365        // 直接通过 ServerCore(实现了 ServerHandle)广播消息
366        if let Some(ref core) = self.core {
367            return ServerHandle::broadcast_except(&**core, frame, exclude_connection_id).await;
368        }
369        Err(crate::common::error::FlareError::protocol_error(
370            "ServerCore not initialized".to_string(),
371        ))
372    }
373
374    async fn disconnect(&self, connection_id: &str) -> Result<()> {
375        // 直接通过 ServerCore(实现了 ServerHandle)断开连接
376        if let Some(ref core) = self.core {
377            return ServerHandle::disconnect(&**core, connection_id).await;
378        }
379        Err(crate::common::error::FlareError::protocol_error(
380            "ServerCore not initialized".to_string(),
381        ))
382    }
383
384    fn connection_count(&self) -> usize {
385        // 直接通过 ServerCore(实现了 ServerHandle)获取连接数量
386        if let Some(ref core) = self.core {
387            return ServerHandle::connection_count(&**core);
388        }
389        0
390    }
391
392    fn user_count(&self) -> usize {
393        // 直接通过 ServerCore(实现了 ServerHandle)获取用户数量
394        if let Some(ref core) = self.core {
395            return ServerHandle::user_count(&**core);
396        }
397        0
398    }
399}