server/src/state.rs

56 lines
1 KiB
Rust

use dashmap::DashMap;
use shared::UserId;
use std::sync::Arc;
use tokio::sync::broadcast;
#[derive(Debug, Clone)]
pub struct AppState {
pub rooms: Arc<DashMap<String, Room>>,
}
#[derive(Debug, Clone)]
pub struct Room {
pub id: String,
pub peers: DashMap<UserId, Peer>,
// Channel for broadcasting control messages within the room
pub tx: broadcast::Sender<RoomMessage>,
}
#[derive(Clone, Debug)]
pub struct RoomMessage {
pub from_user_id: UserId,
pub content: RoomMessageContent,
}
#[derive(Clone, Debug)]
pub enum RoomMessageContent {
Control(shared::ControlMsg),
Media(Vec<u8>),
}
#[derive(Debug, Clone)]
pub struct Peer {
pub id: UserId,
pub display_name: String,
}
impl AppState {
pub fn new() -> Self {
Self {
rooms: Arc::new(DashMap::new()),
}
}
}
impl Room {
pub fn new(id: String) -> Self {
let (tx, _) = broadcast::channel(1024);
Self {
id,
peers: DashMap::new(),
tx,
}
}
}