//! The inner link of the TeamSpeak side: [`ServerLink`], the trait that //! [`TsLink`] implements, and the [`ConnectOptions`] it connects with. //! //! The engine does not know this vocabulary: it talks to a //! `voelin_proto::link::Session`. The `ts_session` module makes a `Session` //! of a [`ServerLink`] (`LinkSession`), and the engine's tests give it a link //! of their own, an in-memory one. [`TsLink`] connects as a normal TeamSpeak 3 //! or 6 client with `tsclientlib` and //! runs the connection ([`super::voice`]) with everything that rides on it: //! chat, voice, files, pokes, offline messages and, on TeamSpeak 6, the //! stream task. Which product the server is (and what it can do) is decided //! there, from the version it sends, and reported in //! [`LinkEvent::Connected`](crate::link::LinkEvent::Connected). //! //! Everything it uses (`voice.rs`, `book.rs`, `stream.rs`, `versions.rs`, //! `identity.rs`) is TeamSpeak's and leaves this crate together with it: //! the plan moves `TsLink` into the TeamSpeak gateway, `voelin-gateway-ts` //! of VoelinGateWay. use std::sync::Arc; use tokio::sync::{mpsc, watch}; use voelin_stream::PeerConfig; use super::voice; use crate::link::{LinkCmd, LinkCtx, LinkEvent}; /// Starts the connection of a link: the contract of `crate::link` for the /// vocabulary ([`LinkCmd`] in, [`LinkEvent`] out) holds for what a link does /// with them, and the module documentation of `link/session_link.rs` states /// it. /// /// One link serves every session of an engine; [`spawn`](Self::spawn) starts /// one connection. pub(crate) trait ServerLink: Send + Sync + 'static { /// Connect to the server `options` names and run the connection on its /// own task: take commands from `cmds`, report through `events`, and end /// the connection with [`LinkEvent::Disconnected`]. Does not block. fn spawn( &self, options: ConnectOptions, ctx: LinkCtx, cmds: mpsc::UnboundedReceiver, events: mpsc::UnboundedSender, ); } /// What a TeamSpeak connection needs to know: the server, who to be, where /// to join. The reverse of what the engine's `ConnectParams` carry /// (`ts_session::inner_options`). #[derive(Clone, Debug)] pub(crate) struct ConnectOptions { pub address: String, pub nickname: String, /// The identity to connect with; none generates one. pub identity: Option, /// Signed compatibility version; `None` selects the native platform's tuple. pub client_version: Option, pub server_password: Option, /// Channel path to join, e.g. `Lobby/Sub`, or `/`. pub channel: Option, /// The password of `channel`. pub channel_password: Option, /// A privilege key to use when connecting. pub token: Option, /// Open the audio devices (capture and playback). pub audio: bool, /// Network settings for stream peer connections. pub stream_peer: PeerConfig, } impl ConnectOptions { /// Options for `address` and `nickname`, the rest as a test leaves it. #[cfg(test)] pub(crate) fn new(address: impl Into, nickname: impl Into) -> Self { Self { address: address.into(), nickname: nickname.into(), identity: None, client_version: None, server_password: None, channel: None, channel_password: None, token: None, audio: false, stream_peer: PeerConfig::default(), } } } /// The main myTeamSpeak account of an application, as its connections show /// it to the servers: the proof of the myTS ID, and what servers are shown /// of it (its avatar and badges with their certificates, its User Tag). /// /// The senders live here, one pair for every connection of a [`TsLink`]; each /// connection watches the pair through [`Watches`], presents the account it /// finds when it connects, and follows the changes. pub(crate) struct Account { identity: watch::Sender>>, data: watch::Sender>>, } impl Default for Account { /// No account is signed in. fn default() -> Self { Self { identity: watch::Sender::new(None), data: watch::Sender::new(None) } } } impl Account { /// The account proof for every current and future connection. Still no /// account is no change: every notification restarts the handshakes and /// sends `updatemytsid` on every connection. Any account is (each /// connection's proof is signed anew). pub(crate) fn set_identity(&self, identity: Option>) { self.identity.send_if_modified(|current| { if current.is_none() && identity.is_none() { return false; } *current = identity; true }); } /// What the servers are shown of the account: sent to every connection /// that presents the account's myTS ID, once connected and when it /// changes; cleared on a sign-out. The same data is no change. pub(crate) fn set_data(&self, data: Option>) { self.data.send_if_modified(|current| { if *current == data { return false; } *current = data; true }); } /// What one connection watches. It keeps the senders alive for as long as /// it runs: a connection ends with "account source closed" when its /// watches have lost them, so the account outlives the connector that /// made the connection. pub(crate) fn watches(self: &Arc) -> Watches { Watches { identity: self.identity.subscribe(), data: self.data.subscribe(), _keep: Some(self.clone()), } } } /// The account a connection presents and follows ([`Account::watches`]). pub(crate) struct Watches { pub(super) identity: watch::Receiver>>, pub(super) data: watch::Receiver>>, _keep: Option>, } impl Watches { /// Watches of senders that someone else keeps (a test's). #[cfg(test)] pub(super) fn of( identity: watch::Receiver>>, data: watch::Receiver>>, ) -> Self { Self { identity, data, _keep: None } } } /// Connects to TeamSpeak 3 and 6 servers. The link of [`TsConnector`](super::TsConnector). #[derive(Clone, Default)] pub(crate) struct TsLink { account: Arc, } impl TsLink { /// A link that presents no account until it is given one. pub(crate) fn new() -> Self { Self::default() } /// The account the link's connections present. pub(crate) fn account(&self) -> &Arc { &self.account } } impl ServerLink for TsLink { fn spawn( &self, options: ConnectOptions, ctx: LinkCtx, cmds: mpsc::UnboundedReceiver, events: mpsc::UnboundedSender, ) { tokio::spawn(voice::run(options, ctx, self.account.watches(), cmds, events)); } } #[cfg(test)] mod tests { use super::*; /// A myTS ID's proof (the standard Ed25519 base point as the public key). fn identity() -> Arc { let mut private = [0; 32]; private[0] = 1; let mut public = [0x66; 32]; public[0] = 0x58; Arc::new( tsproto::myts::Identity::new(vec![1; 33], 42, public, private, vec![2; 65]).unwrap(), ) } /// An account sets what it holds only when it changes: every notification /// of a watch restarts the handshake of a connection. #[test] fn a_watch_hears_of_a_change_and_not_of_the_same_account_again() { let account = Arc::new(Account::default()); let mut watches = account.watches(); // No account, still none: nothing. account.set_identity(None); account.set_data(None); assert!(!watches.identity.has_changed().unwrap()); assert!(!watches.data.has_changed().unwrap()); let proof = identity(); account.set_identity(Some(proof.clone())); assert!(watches.identity.borrow_and_update().is_some()); // An account again is a change (each connection signs its proof anew), the // same data is none. account.set_identity(Some(proof)); assert!(watches.identity.has_changed().unwrap()); watches.identity.borrow_and_update(); let data = Arc::new(tsclientlib::MytsData::default()); account.set_data(Some(data.clone())); assert!(watches.data.has_changed().unwrap()); watches.data.borrow_and_update(); account.set_data(Some(Arc::new(tsclientlib::MytsData::default()))); assert!(!watches.data.has_changed().unwrap(), "equal data is no change"); // A sign-out is a change. account.set_identity(None); assert!(watches.identity.has_changed().unwrap()); assert!(watches.identity.borrow_and_update().is_none()); } /// A connection outlives the connector that made it (a session handed out /// by a connector that was dropped): its watches keep their senders. #[test] fn a_connection_keeps_the_account_open_when_its_link_is_gone() { let link = TsLink::new(); let watches = link.account().watches(); drop(link); assert!(watches.identity.has_changed().is_ok(), "the identity watch lost its sender"); assert!(watches.data.has_changed().is_ok(), "the data watch lost its sender"); } }