#[derive(Debug, Clone)]
pub struct CmppProtocolParams {
pub heartbeat_interval: u64,
pub response_timeout: u64,
pub retry_count: u32,
pub window_size: usize,
pub connect_timeout: u64,
pub read_idle_timeout: u64,
pub verify_authenticator: bool,
}
impl Default for CmppProtocolParams {
fn default() -> Self {
Self {
heartbeat_interval: 180, response_timeout: 60, retry_count: 3, window_size: 16, connect_timeout: 10, read_idle_timeout: 300, verify_authenticator: true, }
}
}
impl CmppProtocolParams {
pub(crate) fn validate(&self) -> Result<(), String> {
if self.heartbeat_interval == 0 {
return Err("heartbeat_interval 不能为 0".to_string());
}
if self.response_timeout == 0 {
return Err("response_timeout 不能为 0".to_string());
}
if self.retry_count == 0 {
return Err("retry_count 不能为 0".to_string());
}
if self.window_size == 0 || self.window_size > 256 {
return Err("window_size 必须在 1 到 256 之间".to_string());
}
if self.connect_timeout == 0 {
return Err("connect_timeout 不能为 0".to_string());
}
if self.read_idle_timeout == 0 {
return Err("read_idle_timeout 不能为 0".to_string());
}
Ok(())
}
}
#[derive(Debug, Clone)]
pub struct CmppConfig {
pub host: String,
pub port: i32,
pub account: String,
pub password: String,
pub version: u8,
pub protocol_params: CmppProtocolParams,
}
impl CmppConfig {
pub(crate) fn validate(&self) -> Result<(), String> {
if self.host.is_empty() {
return Err("host 不能为空".to_string());
}
if self.port < 1 || self.port > 65535 {
return Err("port 必须在 1 到 65535 之间".to_string());
}
if self.account.is_empty() {
return Err("account 不能为空".to_string());
}
if self.account.len() > 6 {
return Err("account 长度不能超过 6 bytes".to_string());
}
if self.password.is_empty() {
return Err("password 不能为空".to_string());
}
if self.version != crate::types::CMPP_VERSION_20 {
return Err("仅支持 CMPP 2.0(version 0x20)".to_string());
}
self.protocol_params.validate()?;
Ok(())
}
}