use crate::common::error::Result;
use crate::common::protocol::Frame;
use crate::server::connection::ConnectionManagerTrait;
use crate::server::handle::{DefaultServerHandle, ServerHandle};
use crate::server::{HybridServer, Server};
use std::sync::Arc;
use tokio::sync::Mutex;
pub struct ServerWrapper {
server: Arc<Mutex<HybridServer>>,
}
impl ServerWrapper {
pub fn new(server: HybridServer) -> Self {
Self {
server: Arc::new(Mutex::new(server)),
}
}
pub fn server(&self) -> &Arc<Mutex<HybridServer>> {
&self.server
}
pub fn get_server_handle_components(&self) -> Option<Arc<dyn ConnectionManagerTrait>> {
tokio::task::block_in_place(|| {
let s = self.server.blocking_lock();
s.core().map(|core| core.connection_manager_trait())
})
}
pub fn get_server_handle(&self) -> Option<Arc<dyn ServerHandle>> {
self.get_server_handle_components().map(|manager_trait| {
Arc::new(DefaultServerHandle::new(manager_trait)) as Arc<dyn ServerHandle>
})
}
pub async fn start(&self) -> Result<()> {
let mut s = self.server.lock().await;
s.start().await
}
pub async fn stop(&self) -> Result<()> {
let mut s = self.server.lock().await;
s.stop().await
}
pub fn is_running(&self) -> bool {
tokio::task::block_in_place(|| {
let s = self.server.blocking_lock();
s.is_running()
})
}
pub fn connection_count(&self) -> usize {
tokio::task::block_in_place(|| {
let s = self.server.blocking_lock();
ServerHandle::connection_count(&*s)
})
}
pub fn user_count(&self) -> usize {
tokio::task::block_in_place(|| {
let s = self.server.blocking_lock();
ServerHandle::user_count(&*s)
})
}
pub async fn send_to(&self, connection_id: &str, frame: &Frame) -> Result<()> {
let s = self.server.lock().await;
ServerHandle::send_to(&*s, connection_id, frame).await
}
pub async fn send_to_user(&self, user_id: &str, frame: &Frame) -> Result<()> {
let s = self.server.lock().await;
ServerHandle::send_to_user(&*s, user_id, frame).await
}
pub async fn broadcast(&self, frame: &Frame) -> Result<()> {
let s = self.server.lock().await;
ServerHandle::broadcast(&*s, frame).await
}
pub async fn broadcast_except(&self, frame: &Frame, exclude_connection_id: &str) -> Result<()> {
let s = self.server.lock().await;
ServerHandle::broadcast_except(&*s, frame, exclude_connection_id).await
}
pub async fn disconnect(&self, connection_id: &str) -> Result<()> {
let s = self.server.lock().await;
ServerHandle::disconnect(&*s, connection_id).await
}
pub fn protocols(&self) -> Vec<crate::common::config_types::TransportProtocol> {
tokio::task::block_in_place(|| {
let s = self.server.blocking_lock();
s.protocols().to_vec()
})
}
}
pub fn validate_auth_config(
config: &crate::server::config::ServerConfig,
authenticator: &Option<Arc<dyn crate::server::auth::Authenticator>>,
) -> Result<()> {
if config.auth_enabled && authenticator.is_none() {
return Err(crate::common::error::FlareError::localized(
crate::common::error::ErrorCode::ConfigurationError,
"认证已启用但未提供认证器,请使用 with_authenticator() 设置认证器",
));
}
Ok(())
}
pub fn create_message_parser(
config: &crate::server::config::ServerConfig,
) -> crate::common::MessageParser {
crate::common::MessageParser::new(
config.default_serialization_format,
config.default_compression.clone(),
config.default_encryption.clone(),
)
}