lib: Make CMSession users poll the context

This commit is contained in:
Artemis Tosini 2024-09-05 04:18:35 +00:00
parent 5e7d4c6d98
commit f05f8315cb
Signed by: artemist
GPG key ID: ADFFE553DCBB831E
3 changed files with 15 additions and 7 deletions

View file

@ -22,7 +22,9 @@ pub async fn main() -> eyre::Result<()> {
let servers = vapore::selection::bootstrap_find_servers().await?;
log::debug!("Found servers: {:?}", servers);
let session = vapore::connection::CMSession::connect(&servers[0]).await?;
let (session, context) = vapore::connection::CMSession::connect(&servers[0]).await?;
tokio::spawn(context);
session.send_notification(
EMsg::k_EMsgClientHello,

View file

@ -30,7 +30,9 @@ pub async fn main() -> eyre::Result<()> {
let servers = vapore::selection::bootstrap_find_servers().await?;
log::debug!("Found servers: {:?}", servers);
let session = CMSession::connect(&servers[0]).await?;
let (session, context) = CMSession::connect(&servers[0]).await?;
tokio::spawn(context);
session.send_notification(
EMsg::k_EMsgClientHello,

View file

@ -71,7 +71,12 @@ impl CMSessionInner {
}
}
struct CMContext {
/// Task to manage a single Connection Manager socket.
/// Functions on the matching [CMSession] objects will use this Context
/// to send and recieve messages.
/// Users _must_ poll this to make any progress, e.g. with `tokio::spawn(context)`
#[must_use = "Messages will not be sent or received unless context is polled"]
pub struct CMContext {
socket: WebSocketStream<ConnectStream>,
session: Arc<Mutex<CMSessionInner>>,
}
@ -204,7 +209,8 @@ pub struct CMSession {
}
impl CMSession {
pub async fn connect(server: &str) -> Result<Self, ClientError> {
/// Connect to a given Steam Connection Manager server
pub async fn connect(server: &str) -> Result<(Self, CMContext), ClientError> {
let (socket, _) = connect_async(server)
.await
.with_context(|_| WebSocketConnectSnafu {
@ -229,13 +235,11 @@ impl CMSession {
session: inner_wrapped.clone(),
};
tokio::spawn(context);
let session = Self {
inner: inner_wrapped,
};
Ok(session)
Ok((session, context))
}
pub fn begin_heartbeat(&self, interval: u32) {