47 lines
2.2 KiB
Rust
47 lines
2.2 KiB
Rust
//! Connection-scoped service fan-out. Media grants are updated before observers receive events.
|
|
use crate::app::connections::{ConnectionEvent, ConnectionSink, SessionKey};
|
|
use super::{Result, media::{MediaService, MediaTarget}, server_icons::IconTransferEvents};
|
|
use std::{collections::HashMap, sync::{Arc, Mutex}};
|
|
use teamspeach_core::protocol::ErrorObj;
|
|
|
|
pub struct ServiceEvents {
|
|
pub media: Arc<MediaService>,
|
|
pub icon_transfers: Arc<IconTransferEvents>,
|
|
keys: Mutex<HashMap<String, SessionKey>>,
|
|
consumer: Arc<dyn ConnectionSink>,
|
|
}
|
|
impl ServiceEvents {
|
|
pub fn new(network_enabled: bool, consumer: Arc<dyn ConnectionSink>) -> Result<Self> {
|
|
Ok(Self { media: Arc::new(MediaService::new(network_enabled)?),
|
|
icon_transfers: Arc::new(IconTransferEvents::default()), keys: Mutex::new(HashMap::new()), consumer })
|
|
}
|
|
pub fn authorize_core_media(&self, key: &SessionKey, target: &MediaTarget, error: &ErrorObj) -> Result<()> {
|
|
self.media.authorize_core_response(key, target, error)
|
|
}
|
|
pub fn close(&self, key: &SessionKey) {
|
|
let mut keys = self.keys.lock().expect("service session state poisoned");
|
|
if keys.get(&key.connection_id) == Some(key) { keys.remove(&key.connection_id); }
|
|
self.media.on_closed(key);
|
|
}
|
|
}
|
|
impl ConnectionSink for ServiceEvents {
|
|
fn on_event(&self, event: ConnectionEvent) {
|
|
if let ConnectionEvent::Control { key, value } = &event {
|
|
let mut keys = self.keys.lock().expect("service session state poisoned");
|
|
if let Some(previous) = keys.get(&key.connection_id) {
|
|
if previous != key { self.media.on_closed(previous); }
|
|
}
|
|
keys.insert(key.connection_id.clone(), key.clone());
|
|
match value["event"].as_str() {
|
|
Some("snapshot") => self.media.on_snapshot(key, value),
|
|
Some("state") if value["state"] == "disconnected" => self.media.on_closed(key),
|
|
_ => {},
|
|
}
|
|
self.icon_transfers.on_control(key, value.clone());
|
|
} else if let ConnectionEvent::Closed { key } | ConnectionEvent::Failure { key: Some(key), terminal: true, .. } = &event {
|
|
self.close(key);
|
|
}
|
|
self.consumer.on_event(event);
|
|
}
|
|
}
|