Skip to content

Commit

Permalink
Merge pull request #179 from msvbg/remove-single-client
Browse files Browse the repository at this point in the history
Remove SingleClient
  • Loading branch information
Noxime authored Aug 2, 2024
2 parents 58feb1b + aecccc5 commit fbb7963
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 54 deletions.
39 changes: 9 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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};

Expand Down Expand Up @@ -87,13 +86,6 @@ impl<Manager> Clone for Client<Manager> {
}
}

/// Allows access parts of the steam api that can only be called
/// on a single thread at any given time.
pub struct SingleClient<Manager = ClientManager> {
inner: Arc<Inner<Manager>>,
_not_sync: PhantomData<*mut ()>,
}

struct Inner<Manager> {
_manager: Manager,
callbacks: Mutex<Callbacks>,
Expand Down Expand Up @@ -122,7 +114,6 @@ unsafe impl<Manager: Send + Sync> Send for Inner<Manager> {}
unsafe impl<Manager: Send + Sync> Sync for Inner<Manager> {}
unsafe impl<Manager: Send + Sync> Send for Client<Manager> {}
unsafe impl<Manager: Send + Sync> Sync for Client<Manager> {}
unsafe impl<Manager: Send + Sync> Send for SingleClient<Manager> {}

/// Returns true if the app wasn't launched through steam and
/// begins relaunching it, the app should exit as soon as possible.
Expand Down Expand Up @@ -167,10 +158,9 @@ impl Client<ClientManager> {
/// * 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<ClientManager>, SingleClient<ClientManager>)> {
pub fn init() -> SIResult<Client<ClientManager>> {
static_assert_send::<Client<ClientManager>>();
static_assert_sync::<Client<ClientManager>>();
static_assert_send::<SingleClient<ClientManager>>();
unsafe {
let mut err_msg: sys::SteamErrMsg = [0; 1024];
let result = Self::steam_api_init_flat(&mut err_msg);
Expand All @@ -192,15 +182,7 @@ impl Client<ClientManager> {
connection_callback: Default::default(),
}),
});
Ok((
Client {
inner: client.clone(),
},
SingleClient {
inner: client,
_not_sync: PhantomData,
},
))
Ok(Client { inner: client })
}
}

Expand All @@ -217,18 +199,17 @@ impl Client<ClientManager> {
/// * 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<ID: Into<AppId>>(
app_id: ID,
) -> SIResult<(Client<ClientManager>, SingleClient<ClientManager>)> {
pub fn init_app<ID: Into<AppId>>(app_id: ID) -> SIResult<Client<ClientManager>> {
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<M> SingleClient<M>

impl<Manager> Client<Manager>
where
M: Manager,
Manager: crate::Manager,
{
/// Runs any currently pending callbacks
///
Expand All @@ -239,7 +220,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) {
Expand Down Expand Up @@ -272,9 +253,7 @@ where
}
}
}
}

impl<Manager> Client<Manager> {
/// Registers the passed function as a callback for the
/// given type.
///
Expand Down Expand Up @@ -608,7 +587,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);
Expand Down Expand Up @@ -640,7 +619,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));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/matchmaking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ unsafe impl Callback for LobbyChatMsg {
#[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| {
Expand All @@ -1021,7 +1021,7 @@ fn test_lobby() {
});

for _ in 0..100 {
single.run_callbacks();
client.run_callbacks();
::std::thread::sleep(::std::time::Duration::from_millis(100));
}
}
4 changes: 2 additions & 2 deletions src/networking_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl<Manager: 'static> NetworkingMessages<Manager> {
///
/// 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
Expand Down Expand Up @@ -203,7 +203,7 @@ impl<Manager: 'static> NetworkingMessages<Manager> {

/// 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,
Expand Down
8 changes: 4 additions & 4 deletions src/networking_sockets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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();
Expand All @@ -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));
}

Expand All @@ -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));
}

Expand Down
2 changes: 1 addition & 1 deletion src/networking_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/networking_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/remote_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:");
Expand Down
7 changes: 2 additions & 5 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Server {
query_port: u16,
server_mode: ServerMode,
version: &str,
) -> SIResult<(Server, SingleClient<ServerManager>)> {
) -> SIResult<(Server, Client<ServerManager>)> {
unsafe {
let version = CString::new(version).unwrap();

Expand Down Expand Up @@ -148,10 +148,7 @@ impl Server {
inner: server.clone(),
server: server_raw,
},
SingleClient {
inner: server,
_not_sync: PhantomData,
},
Client { inner: server },
))
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand All @@ -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));
}

Expand All @@ -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));
}

Expand Down Expand Up @@ -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| {
Expand All @@ -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));
}

Expand Down
4 changes: 2 additions & 2 deletions src/user_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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));
}
}

0 comments on commit fbb7963

Please sign in to comment.