Skip to main content

flare_core/server/builder/
base.rs

1//! 服务端构建器基类
2//!
3//! 提供所有构建器共享的配置方法,体现"公共逻辑统一处理"的设计原则。
4//!
5//! ## 设计原则
6//!
7//! - **统一配置接口**:所有构建器(简单模式、观察者模式、Flare 模式)共享相同的配置方法
8//! - **减少代码重复**:通过组合模式,让各个构建器复用配置逻辑
9//! - **类型安全**:使用 Rust 类型系统确保配置的正确性
10
11use crate::common::compression::CompressionAlgorithm;
12use crate::common::config_types::{HeartbeatConfig, TlsConfig, TransportProtocol};
13use crate::common::device::DeviceConflictStrategy;
14use crate::common::encryption::EncryptionAlgorithm;
15use crate::common::protocol::SerializationFormat;
16use crate::server::config::ServerConfig;
17use std::sync::Arc;
18use std::time::Duration;
19
20/// 服务端构建器基类
21///
22/// 提供所有构建器共享的配置方法
23/// 使用组合模式,让各个构建器可以复用这些配置逻辑
24pub struct BaseServerBuilderConfig {
25    pub config: ServerConfig,
26    pub authenticator: Option<Arc<dyn crate::server::auth::Authenticator>>,
27}
28
29impl BaseServerBuilderConfig {
30    /// 创建新的构建器配置
31    pub fn new(bind_address: impl Into<String>) -> Self {
32        Self {
33            config: ServerConfig::new(bind_address.into()),
34            authenticator: None,
35        }
36    }
37
38    /// 设置认证器(如果启用认证,必须提供)
39    #[must_use]
40    pub fn with_authenticator(
41        mut self,
42        authenticator: Arc<dyn crate::server::auth::Authenticator>,
43    ) -> Self {
44        self.authenticator = Some(authenticator);
45        self
46    }
47
48    /// 启用认证
49    #[must_use]
50    pub fn enable_auth(mut self) -> Self {
51        self.config = self.config.enable_auth();
52        self
53    }
54
55    /// 设置认证超时时间
56    #[must_use]
57    pub fn with_auth_timeout(mut self, timeout: Duration) -> Self {
58        self.config = self.config.with_auth_timeout(timeout);
59        self
60    }
61
62    /// 设置传输协议
63    #[must_use]
64    pub fn with_protocol(mut self, protocol: TransportProtocol) -> Self {
65        self.config.transport = protocol;
66        self
67    }
68
69    /// 启用多协议监听
70    #[must_use]
71    pub fn with_protocols(mut self, protocols: Vec<TransportProtocol>) -> Self {
72        self.config = self.config.with_protocols(protocols);
73        self
74    }
75
76    /// 为特定协议设置监听地址
77    #[must_use]
78    pub fn with_protocol_address(mut self, protocol: TransportProtocol, address: String) -> Self {
79        self.config = self.config.with_protocol_address(protocol, address);
80        self
81    }
82
83    /// 设置最大连接数
84    #[must_use]
85    pub fn with_max_connections(mut self, max: usize) -> Self {
86        self.config = self.config.with_max_connections(max);
87        self
88    }
89
90    /// 设置握手超时时间
91    #[must_use]
92    pub fn with_handshake_timeout(mut self, timeout: Duration) -> Self {
93        self.config = self.config.with_handshake_timeout(timeout);
94        self
95    }
96
97    /// 设置最大并发握手数
98    #[must_use]
99    pub fn with_max_handshake_concurrency(mut self, max: usize) -> Self {
100        self.config = self.config.with_max_handshake_concurrency(max);
101        self
102    }
103
104    /// 设置单次连接写入超时时间
105    #[must_use]
106    pub fn with_write_timeout(mut self, timeout: Duration) -> Self {
107        self.config = self.config.with_write_timeout(timeout);
108        self
109    }
110
111    /// 设置 fanout 发送最大并发度
112    #[must_use]
113    pub fn with_fanout_concurrency(mut self, max: usize) -> Self {
114        self.config = self.config.with_fanout_concurrency(max);
115        self
116    }
117
118    /// 设置连接超时
119    #[must_use]
120    pub fn with_connection_timeout(mut self, timeout: Duration) -> Self {
121        self.config = self.config.with_connection_timeout(timeout);
122        self
123    }
124
125    /// 设置心跳配置
126    #[must_use]
127    pub fn with_heartbeat(mut self, heartbeat: HeartbeatConfig) -> Self {
128        self.config = self.config.with_heartbeat(heartbeat);
129        self
130    }
131
132    /// 设置 TLS 配置
133    #[must_use]
134    pub fn with_tls(mut self, tls: TlsConfig) -> Self {
135        self.config = self.config.with_tls(tls);
136        self
137    }
138
139    /// 设置默认序列化格式(用于协商,默认 Protobuf)
140    #[must_use]
141    pub fn with_default_format(mut self, format: SerializationFormat) -> Self {
142        self.config = self.config.with_format(format);
143        self
144    }
145
146    /// 设置默认压缩算法(用于协商,默认 None)
147    #[must_use]
148    pub fn with_default_compression(mut self, compression: CompressionAlgorithm) -> Self {
149        self.config = self.config.with_compression(compression);
150        self
151    }
152
153    /// 设置默认加密算法(用于协商,默认 None)
154    #[must_use]
155    pub fn with_default_encryption(mut self, encryption: EncryptionAlgorithm) -> Self {
156        self.config = self.config.with_encryption(encryption);
157        self
158    }
159
160    /// 设置设备冲突策略
161    #[must_use]
162    pub fn with_device_conflict_strategy(mut self, strategy: DeviceConflictStrategy) -> Self {
163        self.config = self.config.with_device_conflict_strategy(strategy);
164        self
165    }
166}