-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to manual dispatch API + general overhaul
Non-exhaustive list of additional changes: - Increase reliability when fetching a huge number of leaderboards concurrently, by limiting the number of concurrent requests - Add logging using the tracing crate - The build script was changed so we now link to the copy of the Steamworks dynamic library the build script makes. Previously we linked to the original files in the redistributagle_bin directory. - Previously, the `ClientInner` struct had an unsafe impl for `Send` and `Sync`, because it directly held a raw pointer for each Steamworks interface, which aren't `Send` and `Sync`. Now, we instead wrap each pointer in a newtype with the unsafe impls for `Send` and `Sync`. This is safer, because the compiler will make sure everything else in the `ClientInner` struct is `Send` and `Sync`. We also add a static assertion that the `Client` struct is `Send` and `Sync`. - No longer use or depend on an async executor - Use Steamwork's `ISteamUtils::SetWarningMessageHook` to log messages and errors received from the Steam API - Remove `Sync` bound on returned `impl Future`s for consistency, as it's not usefull, and can't always be satisfied - Update README and add an example - Update deps closes #2
- Loading branch information
1 parent
c10fa1e
commit 1a45a04
Showing
14 changed files
with
435 additions
and
309 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
mod persona_state_change; | ||
|
||
pub use persona_state_change::*; | ||
|
||
use az::WrappingCast; | ||
use futures::Stream; | ||
use parking_lot::Mutex; | ||
use slotmap::DenseSlotMap; | ||
use std::{convert::TryFrom, mem}; | ||
use steamworks_sys as sys; | ||
|
||
pub(crate) type CallbackStorage<T> = | ||
Mutex<DenseSlotMap<slotmap::DefaultKey, futures::channel::mpsc::UnboundedSender<T>>>; | ||
|
||
pub(crate) unsafe fn dispatch_callbacks( | ||
callback_dispatchers: &CallbackDispatchers, | ||
callback_msg: sys::CallbackMsg_t, | ||
) { | ||
match callback_msg.m_iCallback.wrapping_cast() { | ||
sys::PersonaStateChange_t_k_iCallback => callback_dispatchers | ||
.persona_state_change | ||
.dispatch(callback_msg.m_pubParam, callback_msg.m_cubParam), | ||
sys::SteamShutdown_t_k_iCallback => callback_dispatchers | ||
.steam_shutdown | ||
.dispatch(callback_msg.m_pubParam, callback_msg.m_cubParam), | ||
_ => {} | ||
} | ||
} | ||
|
||
pub(crate) fn register_to_receive_callback<T: Clone + Send + 'static>( | ||
dispatcher: &impl CallbackDispatcher<MappedCallbackData = T>, | ||
) -> impl Stream<Item = T> + Send { | ||
let (tx, rx) = futures::channel::mpsc::unbounded(); | ||
dispatcher.storage().lock().insert(tx); | ||
rx | ||
} | ||
|
||
#[derive(Debug, Default)] | ||
pub(crate) struct CallbackDispatchers { | ||
pub(crate) persona_state_change: PersonaStateChangeDispatcher, | ||
pub(crate) steam_shutdown: SteamShutdownDispatcher, | ||
} | ||
|
||
impl CallbackDispatchers { | ||
pub(crate) fn new() -> Self { | ||
Self::default() | ||
} | ||
} | ||
|
||
pub(crate) trait CallbackDispatcher { | ||
type RawCallbackData; | ||
type MappedCallbackData: Clone + Send + 'static; | ||
|
||
fn storage(&self) -> &CallbackStorage<Self::MappedCallbackData>; | ||
fn map_callback_data(raw: &Self::RawCallbackData) -> Self::MappedCallbackData; | ||
|
||
unsafe fn dispatch(&self, callback_data: *const u8, callback_data_len: i32) { | ||
assert!(!callback_data.is_null()); | ||
assert_eq!( | ||
callback_data.align_offset(mem::align_of::<Self::RawCallbackData>()), | ||
0 | ||
); | ||
assert_eq!( | ||
usize::try_from(callback_data_len).unwrap(), | ||
mem::size_of::<Self::RawCallbackData>() | ||
); | ||
|
||
let raw = &*(callback_data as *const Self::RawCallbackData); | ||
let mapped = Self::map_callback_data(raw); | ||
|
||
let mut storage = self.storage().lock(); | ||
storage.retain(|_key, tx| match tx.unbounded_send(mapped.clone()) { | ||
Err(e) if e.is_disconnected() => false, | ||
Err(e) => panic!(e), | ||
Ok(()) => true, | ||
}); | ||
} | ||
} | ||
|
||
#[derive(Debug, Default)] | ||
pub(crate) struct SteamShutdownDispatcher(CallbackStorage<()>); | ||
|
||
impl CallbackDispatcher for SteamShutdownDispatcher { | ||
type RawCallbackData = sys::SteamShutdown_t; | ||
type MappedCallbackData = (); | ||
|
||
fn storage(&self) -> &CallbackStorage<()> { | ||
&self.0 | ||
} | ||
|
||
fn map_callback_data(_raw: &sys::SteamShutdown_t) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.