flare_core/server/builder/
common.rs1use crate::common::error::Result;
12use crate::common::protocol::Frame;
13use crate::server::connection::ConnectionManagerTrait;
14use crate::server::handle::{DefaultServerHandle, ServerHandle};
15use crate::server::{HybridServer, Server};
16use std::sync::Arc;
17use tokio::sync::Mutex;
18
19pub struct ServerWrapper {
29 server: Arc<Mutex<HybridServer>>,
30}
31
32impl ServerWrapper {
33 pub fn new(server: HybridServer) -> Self {
35 Self {
36 server: Arc::new(Mutex::new(server)),
37 }
38 }
39
40 pub fn server(&self) -> &Arc<Mutex<HybridServer>> {
42 &self.server
43 }
44
45 pub fn get_server_handle_components(&self) -> Option<Arc<dyn ConnectionManagerTrait>> {
50 tokio::task::block_in_place(|| {
51 let s = self.server.blocking_lock();
52 s.core().map(|core| core.connection_manager_trait())
53 })
54 }
55
56 pub fn get_server_handle(&self) -> Option<Arc<dyn ServerHandle>> {
61 self.get_server_handle_components().map(|manager_trait| {
64 Arc::new(DefaultServerHandle::new(manager_trait)) as Arc<dyn ServerHandle>
65 })
66 }
67
68 pub async fn start(&self) -> Result<()> {
70 let mut s = self.server.lock().await;
71 s.start().await
72 }
73
74 pub async fn stop(&self) -> Result<()> {
76 let mut s = self.server.lock().await;
77 s.stop().await
78 }
79
80 pub fn is_running(&self) -> bool {
82 tokio::task::block_in_place(|| {
83 let s = self.server.blocking_lock();
84 s.is_running()
85 })
86 }
87
88 pub fn connection_count(&self) -> usize {
90 tokio::task::block_in_place(|| {
91 let s = self.server.blocking_lock();
92 ServerHandle::connection_count(&*s)
93 })
94 }
95
96 pub fn user_count(&self) -> usize {
98 tokio::task::block_in_place(|| {
99 let s = self.server.blocking_lock();
100 ServerHandle::user_count(&*s)
101 })
102 }
103
104 pub async fn send_to(&self, connection_id: &str, frame: &Frame) -> Result<()> {
106 let s = self.server.lock().await;
107 ServerHandle::send_to(&*s, connection_id, frame).await
108 }
109
110 pub async fn send_to_user(&self, user_id: &str, frame: &Frame) -> Result<()> {
112 let s = self.server.lock().await;
113 ServerHandle::send_to_user(&*s, user_id, frame).await
114 }
115
116 pub async fn broadcast(&self, frame: &Frame) -> Result<()> {
118 let s = self.server.lock().await;
119 ServerHandle::broadcast(&*s, frame).await
120 }
121
122 pub async fn broadcast_except(&self, frame: &Frame, exclude_connection_id: &str) -> Result<()> {
124 let s = self.server.lock().await;
125 ServerHandle::broadcast_except(&*s, frame, exclude_connection_id).await
126 }
127
128 pub async fn disconnect(&self, connection_id: &str) -> Result<()> {
130 let s = self.server.lock().await;
131 ServerHandle::disconnect(&*s, connection_id).await
132 }
133
134 pub fn protocols(&self) -> Vec<crate::common::config_types::TransportProtocol> {
136 tokio::task::block_in_place(|| {
137 let s = self.server.blocking_lock();
138 s.protocols().to_vec()
139 })
140 }
141}
142
143pub fn validate_auth_config(
148 config: &crate::server::config::ServerConfig,
149 authenticator: &Option<Arc<dyn crate::server::auth::Authenticator>>,
150) -> Result<()> {
151 if config.auth_enabled && authenticator.is_none() {
152 return Err(crate::common::error::FlareError::localized(
153 crate::common::error::ErrorCode::ConfigurationError,
154 "认证已启用但未提供认证器,请使用 with_authenticator() 设置认证器",
155 ));
156 }
157 Ok(())
158}
159
160pub fn create_message_parser(
165 config: &crate::server::config::ServerConfig,
166) -> crate::common::MessageParser {
167 crate::common::MessageParser::new(
168 config.default_serialization_format,
169 config.default_compression.clone(),
170 config.default_encryption.clone(),
171 )
172}