Skip to main content

steam_client/client/steam_client/
reconnect.rs

1//! Reconnection control on `SteamClient`.
2
3use steam_enums::EResult;
4use tracing::info;
5
6use super::SteamClient;
7use crate::error::SteamError;
8use crate::internal::reconnect::ReconnectState;
9
10impl SteamClient {
11    /// Manually trigger a reconnection.
12    ///
13    /// Closes the current connection (if any) and starts the reconnect sequence.
14    ///
15    /// # Errors
16    /// - `NotLoggedOn` if not currently logged in.
17    /// - `Other` if no stored credentials are available for relog.
18    pub async fn relog(&mut self) -> Result<(), SteamError> {
19        if self.steam_id.is_none() {
20            return Err(SteamError::NotLoggedOn);
21        }
22
23        if self.auth.read().logon_details.is_none() {
24            return Err(SteamError::Other("No stored credentials for relog".to_string()));
25        }
26
27        info!("Initiating manual relog");
28        self.auth.write().relogging = true;
29
30        if let Some(conn) = self.connection.take() {
31            let _ = conn.close().await;
32        }
33
34        self.reconnect_manager.start_reconnection(EResult::NoConnection);
35
36        Ok(())
37    }
38
39    /// Cancel any pending reconnection attempts.
40    pub fn cancel_reconnect(&mut self) {
41        self.reconnect_manager.reset();
42        self.auth.write().relogging = false;
43    }
44
45    /// Check if currently attempting to reconnect.
46    #[must_use]
47    pub fn is_reconnecting(&self) -> bool {
48        self.reconnect_manager.is_reconnecting()
49    }
50
51    /// Get the current reconnection state.
52    #[must_use]
53    pub fn reconnect_state(&self) -> ReconnectState {
54        self.reconnect_manager.state()
55    }
56}