pub mod local; pub mod redis; pub mod nats; use std::collections::HashSet; use async_trait::async_trait; use thiserror::Error; use crate::socket::packet::Packet; #[derive(Error, Debug)] pub enum AdapterError { #[error("Redis error: {0}")] Redis(String), #[error("NATS error: {0}")] Nats(String), #[error("Message bus error: {0}")] MessageBus(String), #[error("Serialization error: {0}")] Serialization(String), #[error("Room error: {0}")] Room(String), } #[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize, PartialEq)] pub struct BroadcastOptions { pub rooms: HashSet, pub except: HashSet, pub flags: BroadcastFlags, } #[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize, PartialEq)] pub struct BroadcastFlags { pub local_only: bool, pub broadcast: bool, } #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] pub struct SocketInfo { pub sid: String, pub namespace: String, pub rooms: HashSet, } #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)] pub enum BusMessage { Broadcast { namespace: String, packet: String, opts: BroadcastOptions, server_id: String, }, SocketJoin { namespace: String, sid: String, room: String, server_id: String, }, SocketLeave { namespace: String, sid: String, room: String, server_id: String, }, SocketDisconnect { namespace: String, sid: String, server_id: String, }, } #[async_trait] pub trait Adapter: Send + Sync + 'static { async fn broadcast(&self, packet: &Packet, opts: &BroadcastOptions) -> Result<(), AdapterError>; async fn add(&self, sid: &str, room: &str, ns: &str) -> Result<(), AdapterError>; async fn del(&self, sid: &str, room: &str, ns: &str) -> Result<(), AdapterError>; async fn del_all(&self, sid: &str, ns: &str) -> Result<(), AdapterError>; async fn fetch_sockets(&self, opts: &BroadcastOptions) -> Result, AdapterError>; async fn socket_rooms(&self, sid: &str) -> Result, AdapterError>; fn server_id(&self) -> &str; async fn close(&self) -> Result<(), AdapterError>; /// Register a socket SID → engine SID mapping in the adapter. /// Must be called when a socket first connects, before any room operations. /// The `ns` parameter is the namespace path this socket belongs to. async fn register(&self, _socket_sid: &str, _engine_sid: &str, _ns: &str) -> Result<(), AdapterError> { Ok(()) } /// Unregister a socket from the adapter, removing all local mappings. async fn unregister(&self, _socket_sid: &str, _ns: &str) -> Result<(), AdapterError> { Ok(()) } } pub use local::LocalAdapter; pub use redis::RedisAdapter; pub use nats::NatsAdapter;