From aecccc564a9e2c403d75d973d7bbd4aac706b5dd Mon Sep 17 00:00:00 2001 From: Martin Svanberg Date: Sat, 11 May 2024 17:49:32 +0200 Subject: [PATCH] Remove SingleClient --- src/lib.rs | 39 +++++++++----------------------------- src/matchmaking.rs | 4 ++-- src/networking_messages.rs | 4 ++-- src/networking_sockets.rs | 8 ++++---- src/networking_types.rs | 2 +- src/networking_utils.rs | 5 +++-- src/remote_storage.rs | 2 +- src/server.rs | 7 ++----- src/user.rs | 10 +++++----- src/user_stats.rs | 4 ++-- 10 files changed, 31 insertions(+), 54 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d149d31..6b1c62b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,7 +15,6 @@ use core::ffi::c_void; use std::collections::HashMap; use std::ffi::{c_char, CStr, CString}; use std::fmt::{self, Debug, Formatter}; -use std::marker::PhantomData; use std::sync::mpsc::Sender; use std::sync::{Arc, Mutex, Weak}; @@ -83,13 +82,6 @@ impl Clone for Client { } } -/// Allows access parts of the steam api that can only be called -/// on a single thread at any given time. -pub struct SingleClient { - inner: Arc>, - _not_sync: PhantomData<*mut ()>, -} - struct Inner { _manager: Manager, callbacks: Mutex, @@ -118,7 +110,6 @@ unsafe impl Send for Inner {} unsafe impl Sync for Inner {} unsafe impl Send for Client {} unsafe impl Sync for Client {} -unsafe impl Send for SingleClient {} /// Returns true if the app wasn't launched through steam and /// begins relaunching it, the app should exit as soon as possible. @@ -193,10 +184,9 @@ impl Client { /// * The game isn't running on the same user/level as the steam client /// * The user doesn't own a license for the game. /// * The app ID isn't completely set up. - pub fn init() -> SIResult<(Client, SingleClient)> { + pub fn init() -> SIResult> { static_assert_send::>(); static_assert_sync::>(); - static_assert_send::>(); unsafe { let mut err_msg: sys::SteamErrMsg = [0; 1024]; let result = Self::steam_api_init_ex(&mut err_msg); @@ -218,15 +208,7 @@ impl Client { connection_callback: Default::default(), }), }); - Ok(( - Client { - inner: client.clone(), - }, - SingleClient { - inner: client, - _not_sync: PhantomData, - }, - )) + Ok(Client { inner: client }) } } @@ -242,18 +224,17 @@ impl Client { /// * The game isn't running on the same user/level as the steam client /// * The user doesn't own a license for the game. /// * The app ID isn't completely set up. - pub fn init_app>( - app_id: ID, - ) -> SIResult<(Client, SingleClient)> { + pub fn init_app>(app_id: ID) -> SIResult> { let app_id = app_id.into().0.to_string(); std::env::set_var("SteamAppId", &app_id); std::env::set_var("SteamGameId", app_id); Client::init() } } -impl SingleClient + +impl Client where - M: Manager, + Manager: crate::Manager, { /// Runs any currently pending callbacks /// @@ -264,7 +245,7 @@ where /// in order to reduce the latency between recieving events. pub fn run_callbacks(&self) { unsafe { - let pipe = M::get_pipe(); + let pipe = Manager::get_pipe(); sys::SteamAPI_ManualDispatch_RunFrame(pipe); let mut callback = std::mem::zeroed(); while sys::SteamAPI_ManualDispatch_GetNextCallback(pipe, &mut callback) { @@ -297,9 +278,7 @@ where } } } -} -impl Client { /// Registers the passed function as a callback for the /// given type. /// @@ -609,7 +588,7 @@ mod tests { #[test] #[serial] fn basic_test() { - let (client, single) = Client::init().unwrap(); + let client = Client::init().unwrap(); let _cb = client.register_callback(|p: PersonaStateChange| { println!("Got callback: {:?}", p); @@ -641,7 +620,7 @@ mod tests { friends.request_user_information(SteamId(76561198174976054), true); for _ in 0..50 { - single.run_callbacks(); + client.run_callbacks(); ::std::thread::sleep(::std::time::Duration::from_millis(100)); } } diff --git a/src/matchmaking.rs b/src/matchmaking.rs index 199088c..af34606 100644 --- a/src/matchmaking.rs +++ b/src/matchmaking.rs @@ -891,7 +891,7 @@ unsafe impl Callback for LobbyDataUpdate { #[test] #[serial] fn test_lobby() { - let (client, single) = Client::init().unwrap(); + let client = Client::init().unwrap(); let mm = client.matchmaking(); mm.request_lobby_list(|v| { @@ -911,7 +911,7 @@ fn test_lobby() { }); for _ in 0..100 { - single.run_callbacks(); + client.run_callbacks(); ::std::thread::sleep(::std::time::Duration::from_millis(100)); } } diff --git a/src/networking_messages.rs b/src/networking_messages.rs index 1613237..71d7b24 100644 --- a/src/networking_messages.rs +++ b/src/networking_messages.rs @@ -160,7 +160,7 @@ impl NetworkingMessages { /// /// Use the [`SessionRequest`](../networking_messages/struct.SessionRequest.html) to accept or reject the connection. /// - /// Requires regularly calling [`SingleClient.run_callbacks()`](../struct.SingleClient.html#method.run_callbacks). + /// Requires regularly calling [`Client.run_callbacks()`](../struct.Client.html#method.run_callbacks). /// Calling this function more than once will replace the previous callback. /// /// # Example @@ -203,7 +203,7 @@ impl NetworkingMessages { /// Register a callback that will be called whenever a connection fails to be established. /// - /// Requires regularly calling [`SingleClient.run_callbacks()`](../struct.SingleClient.html#method.run_callbacks). + /// Requires regularly calling [`Client.run_callbacks()`](../struct.Client.html#method.run_callbacks). /// Calling this function more than once will replace the previous callback. pub fn session_failed_callback( &self, diff --git a/src/networking_sockets.rs b/src/networking_sockets.rs index b2f3058..1b51c1d 100644 --- a/src/networking_sockets.rs +++ b/src/networking_sockets.rs @@ -1031,7 +1031,7 @@ mod tests { #[test] #[serial] fn test_create_listen_socket_ip() { - let (client, _single) = Client::init().unwrap(); + let client = Client::init().unwrap(); let sockets = client.networking_sockets(); let socket_result = sockets.create_listen_socket_ip( SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), 12345), @@ -1042,7 +1042,7 @@ mod tests { #[test] fn test_socket_connection() { - let (client, single) = Client::init().unwrap(); + let client = Client::init().unwrap(); let sockets = client.networking_sockets(); sockets.init_authentication().unwrap(); @@ -1065,7 +1065,7 @@ mod tests { println!("Run callbacks"); for _ in 0..5 { - single.run_callbacks(); + client.run_callbacks(); std::thread::sleep(::std::time::Duration::from_millis(50)); } @@ -1080,7 +1080,7 @@ mod tests { println!("Run callbacks"); for _ in 0..5 { - single.run_callbacks(); + client.run_callbacks(); std::thread::sleep(::std::time::Duration::from_millis(50)); } diff --git a/src/networking_types.rs b/src/networking_types.rs index f6d442b..6a568b9 100644 --- a/src/networking_types.rs +++ b/src/networking_types.rs @@ -2232,7 +2232,7 @@ mod tests { #[test] fn test_allocate_and_free_message() { - let (client, _single) = Client::init().unwrap(); + let client = Client::init().unwrap(); let utils = client.networking_utils(); // With C buffer diff --git a/src/networking_utils.rs b/src/networking_utils.rs index 47abbf4..6b9f49e 100644 --- a/src/networking_utils.rs +++ b/src/networking_utils.rs @@ -200,8 +200,9 @@ mod tests { #[test] fn test_get_networking_status() { - let (client, single) = Client::init().unwrap(); - std::thread::spawn(move || single.run_callbacks()); + let client = Client::init().unwrap(); + let callback_client = client.clone(); + std::thread::spawn(move || callback_client.run_callbacks()); let utils = client.networking_utils(); let status = utils.detailed_relay_network_status(); diff --git a/src/remote_storage.rs b/src/remote_storage.rs index 8b7d30e..340ea58 100644 --- a/src/remote_storage.rs +++ b/src/remote_storage.rs @@ -303,7 +303,7 @@ pub struct SteamFileInfo { #[serial] fn test_cloud() { use std::io::{Read, Write}; - let (client, _single) = Client::init().unwrap(); + let client = Client::init().unwrap(); let rs = client.remote_storage(); println!("Listing files:"); diff --git a/src/server.rs b/src/server.rs index 5fe2a08..cb76c89 100644 --- a/src/server.rs +++ b/src/server.rs @@ -100,7 +100,7 @@ impl Server { query_port: u16, server_mode: ServerMode, version: &str, - ) -> SIResult<(Server, SingleClient)> { + ) -> SIResult<(Server, Client)> { unsafe { let version = CString::new(version).unwrap(); @@ -148,10 +148,7 @@ impl Server { inner: server.clone(), server: server_raw, }, - SingleClient { - inner: server, - _not_sync: PhantomData, - }, + Client { inner: server }, )) } } diff --git a/src/user.rs b/src/user.rs index 896bdde..40aaacd 100644 --- a/src/user.rs +++ b/src/user.rs @@ -175,7 +175,7 @@ pub enum AuthSessionError { #[test] #[serial] fn test_auth_dll() { - let (client, single) = Client::init().unwrap(); + let client = Client::init().unwrap(); let user = client.user(); let _cb = client.register_callback(|v: AuthSessionTicketResponse| { @@ -194,7 +194,7 @@ fn test_auth_dll() { println!("{:?}", user.begin_authentication_session(id, &ticket)); for _ in 0..20 { - single.run_callbacks(); + client.run_callbacks(); ::std::thread::sleep(::std::time::Duration::from_millis(50)); } @@ -203,7 +203,7 @@ fn test_auth_dll() { user.cancel_authentication_ticket(auth); for _ in 0..20 { - single.run_callbacks(); + client.run_callbacks(); ::std::thread::sleep(::std::time::Duration::from_millis(50)); } @@ -246,7 +246,7 @@ unsafe impl Callback for AuthSessionTicketResponse { #[test] #[serial] fn test_auth_webapi() { - let (client, single) = Client::init().unwrap(); + let client = Client::init().unwrap(); let user = client.user(); let _cb = client.register_callback(|v: TicketForWebApiResponse| { @@ -258,7 +258,7 @@ fn test_auth_webapi() { println!("{:?}", auth); for _ in 0..20 { - single.run_callbacks(); + client.run_callbacks(); ::std::thread::sleep(::std::time::Duration::from_millis(100)); } diff --git a/src/user_stats.rs b/src/user_stats.rs index ed9bfe7..4046c48 100644 --- a/src/user_stats.rs +++ b/src/user_stats.rs @@ -572,7 +572,7 @@ impl Leaderboard { #[ignore] #[serial] fn test() { - let (client, single) = Client::init().unwrap(); + let client = Client::init().unwrap(); let stats = client.user_stats(); @@ -612,7 +612,7 @@ fn test() { ); for _ in 0..50 { - single.run_callbacks(); + client.run_callbacks(); ::std::thread::sleep(::std::time::Duration::from_millis(100)); } }