821537186e
- Reorganized import statements in adapter tests for better readability - Replaced or_insert_with(Vec::new) with or_default() in test closures - Updated Cargo.lock with new dependency versions and checksums - Added TLS features to tonic dependency configuration - Included sqlx, chrono, and uuid dependencies with specific features - Added jsonwebtoken and arc-swap as project dependencies - Reformatted assertion statements to comply with line length limits - Adjusted base64 import order in engine codec module - Updated protobuf include statement formatting
113 lines
3.2 KiB
Rust
113 lines
3.2 KiB
Rust
pub mod local;
|
|
pub mod nats;
|
|
pub mod redis;
|
|
|
|
use std::collections::HashSet;
|
|
use std::sync::Arc;
|
|
|
|
use async_trait::async_trait;
|
|
use thiserror::Error;
|
|
|
|
use crate::socket::packet::Packet;
|
|
|
|
/// Alias for cross-node broadcast callback functions.
|
|
pub type LocalBroadcastFn = Arc<dyn Fn(&Packet, &BroadcastOptions) + Send + Sync + 'static>;
|
|
/// Alias for local send-to-socket callback functions.
|
|
pub type LocalSendFn = Arc<dyn Fn(&str, &Packet) -> Result<(), String> + Send + Sync>;
|
|
|
|
#[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<String>,
|
|
pub except: HashSet<String>,
|
|
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<String>,
|
|
}
|
|
|
|
#[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<Vec<SocketInfo>, AdapterError>;
|
|
async fn socket_rooms(&self, sid: &str) -> Result<HashSet<String>, 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 nats::NatsAdapter;
|
|
pub use redis::RedisAdapter;
|