mod callback; mod event; mod host; mod native; mod runner; use std::pin::Pin; use std::sync::Arc; use std::task::{Context, Poll}; use std::time::Duration; use futures_util::Stream; use session::Session; use tokio::sync::{Notify, mpsc, oneshot, watch}; use tokio::task::JoinHandle; pub use callback::{CallbackReplier, TrouterCallback}; pub use event::{ EventKind, MessageEvent, PresenceUpdate, RealtimeEvent, StatusEvent, StatusKind, TrouterEndpoint, TypingEvent, decode_payload, }; pub use host::{DEFAULT_TROUTER_HOST, is_trouter_host}; use crate::error::Result; use native::NativeTrouter; use runner::{Runner, attach}; const STOP_TIMEOUT: Duration = Duration::from_secs(15); #[derive(Debug, Clone)] pub struct RealtimeConfig { pub host: Option, pub default_host: String, pub discover_host: bool, pub ensure_interval: Duration, pub navigation_settle: Duration, pub max_reattach_backoff: Duration, pub instance: InstanceNames, pub forward_callbacks: bool, pub ringable: bool, pub initially_active: bool, pub native: bool, /// Wakes the native reconnect backoff early, e.g. from the banner's Retry action. pub retry: Option>, } /// Page globals of one Trouter client; a second client in the same tab needs its own names. #[derive(Debug, Clone, PartialEq, Eq)] pub struct InstanceNames { pub global: String, pub binding: String, pub endpoint_storage_key: String, } impl Default for InstanceNames { fn default() -> Self { InstanceNames { global: runner::DEFAULT_GLOBAL_NAME.to_owned(), binding: runner::DEFAULT_BINDING_NAME.to_owned(), endpoint_storage_key: runner::DEFAULT_EPID_KEY.to_owned(), } } } impl Default for RealtimeConfig { fn default() -> Self { RealtimeConfig { host: None, default_host: DEFAULT_TROUTER_HOST.to_owned(), discover_host: true, ensure_interval: Duration::from_secs(30), navigation_settle: Duration::from_millis(1500), max_reattach_backoff: Duration::from_secs(30), instance: InstanceNames::default(), forward_callbacks: false, ringable: false, initially_active: false, native: std::env::var_os("TEAMS_PAGE_TROUTER").is_none(), retry: None, } } } pub struct Realtime { host: String, events: mpsc::UnboundedReceiver, callbacks: Option>, replier: CallbackReplier, activity: watch::Sender, resend: Arc, stop: Option>>, task: JoinHandle<()>, } impl Realtime { /// Works on a parked tab (same origin, localStorage readable); the CDP attachment keeps it from being parked. pub async fn start(session: &Session) -> Result { Self::start_with(session, RealtimeConfig::default()).await } pub async fn start_with(session: &Session, config: RealtimeConfig) -> Result { let session = session.clone(); let host = host::resolve(&session, &config).await; let (output, events) = mpsc::unbounded_channel(); let (callback_output, callbacks) = mpsc::unbounded_channel(); let (reply_sender, replies) = mpsc::unbounded_channel(); let (activity, activity_receiver) = watch::channel(config.initially_active); let resend = Arc::new(Notify::new()); let (stop, stop_receiver) = oneshot::channel(); let pipes = runner::CallbackPipes { output: callback_output, replies, }; let task = if config.native { let mut trouter = NativeTrouter::new( session, config, host.clone(), output, pipes, activity_receiver, resend.clone(), ); trouter.token().await?; tokio::spawn(trouter.run(stop_receiver)) } else { let script = runner::PageScript::new(&config); let attached = attach(&session, &script).await?; let mut runner = Runner::new( session, config, script, host.clone(), output, pipes, activity_receiver, resend.clone(), ); if let Err(error) = runner.ensure().await { attached.shutdown().await; return Err(error.into()); } tokio::spawn(runner.run(attached, stop_receiver)) }; Ok(Realtime { host, events, callbacks: Some(callbacks), replier: CallbackReplier { sender: reply_sender, }, activity, resend, stop: Some(stop), task, }) } pub fn host(&self) -> &str { &self.host } pub async fn recv(&mut self) -> Option { self.events.recv().await } /// Callbacks only flow when `forward_callbacks` is set; the receiver can be taken once. pub fn take_callbacks(&mut self) -> Option> { self.callbacks.take() } pub fn callback_replier(&self) -> CallbackReplier { self.replier.clone() } pub fn set_activity(&self, active: bool) { self.activity.send_replace(active); } pub fn resend_activity(&self) { self.resend.notify_one(); } /// Deletes the registration and closes the socket. pub async fn stop(mut self) { if let Some(stop) = self.stop.take() { let (done, finished) = oneshot::channel(); if stop.send(done).is_ok() { let _ = tokio::time::timeout(STOP_TIMEOUT, finished).await; } } let _ = self.task.await; } } impl Stream for Realtime { type Item = RealtimeEvent; fn poll_next( mut self: Pin<&mut Self>, context: &mut Context<'_>, ) -> Poll> { self.events.poll_recv(context) } }