use serde::{Deserialize, Serialize};
use tokio::sync::broadcast;
use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConnectionStatus {
Disconnected,
Connecting,
Connected,
Failed(ConnectionError), Retrying { attempt: u32, max_attempts: u32 }, }
pub struct WebsocketProviderOptions {
pub connect: bool,
pub resync_interval: Option<u64>,
pub max_backoff_time: u64,
}
impl Default for WebsocketProviderOptions {
fn default() -> Self {
Self { connect: true, resync_interval: None, max_backoff_time: 2500 }
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoomSnapshot {
pub room_id: String,
pub root_id: String,
pub nodes: HashMap<String, NodeData>,
pub version: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeData {
pub id: String,
pub node_type: String,
pub attrs: HashMap<String, serde_json::Value>,
pub content: Vec<String>,
pub marks: Vec<MarkData>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MarkData {
pub mark_type: String,
pub attrs: HashMap<String, serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StepResult {
pub step_id: String,
pub step_name: String,
pub description: String,
pub timestamp: u64,
pub client_id: String,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ProtocolSyncState {
NotStarted,
Step1Sent,
Step2Received,
Updating,
}
#[derive(Debug, Clone)]
pub enum SyncEvent {
ProtocolStateChanged(ProtocolSyncState),
InitialSyncCompleted { has_data: bool, elapsed_ms: u64 },
DataReceived,
ConnectionChanged(ConnectionStatus),
ConnectionFailed(ConnectionError),
}
pub type SyncEventSender = broadcast::Sender<SyncEvent>;
pub type SyncEventReceiver = broadcast::Receiver<SyncEvent>;
#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq)]
pub enum ConnectionError {
#[error("服务端未启动或无法连接: {0}")]
ServerUnavailable(String),
#[error("连接超时: {0}ms")]
Timeout(u64),
#[error("网络错误: {0}")]
NetworkError(String),
#[error("WebSocket 错误: {0}")]
WebSocketError(String),
}
#[derive(Debug, Clone)]
pub struct ConnectionRetryConfig {
pub max_attempts: u32,
pub initial_delay_ms: u64,
pub max_delay_ms: u64,
pub backoff_multiplier: f64,
}
impl Default for ConnectionRetryConfig {
fn default() -> Self {
Self {
max_attempts: 5,
initial_delay_ms: 1000,
max_delay_ms: 30000,
backoff_multiplier: 2.0,
}
}
}