Skip to content

Commit

Permalink
feat(api): introduce GnsConfig and GnsUtils::set_global_config_value
Browse files Browse the repository at this point in the history
  • Loading branch information
hussein-aitlahcen committed Nov 1, 2022
1 parent 96bfed4 commit 8daa356
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
9 changes: 9 additions & 0 deletions example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ fn server(port: u16) {
// **unwrap** must be banned in production.
let gns_utils = GnsUtils::new().unwrap();

// Add 1000ms ping to everyone connecting.
// **unwrap** must be banned in production.
gns_utils
.set_global_config_value(
ESteamNetworkingConfigValue::k_ESteamNetworkingConfig_FakePacketLag_Recv,
GnsConfig::Int32(1000),
)
.unwrap();

// Minimalistic server state.
// Map from client -> nickname.
// The nickname is autogenerated (nonce incremented for each new connection).
Expand Down
41 changes: 40 additions & 1 deletion gns/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,6 @@ impl<'x, 'y> GnsSocket<'x, 'y, IsCreated> {
m_int64: queue as *const _ as i64
}
}];

(addr, options)
}

Expand Down Expand Up @@ -912,6 +911,14 @@ impl<'x, 'y> GnsSocket<'x, 'y, IsClient> {
}
}

/// The configuration value used to define configure global variables in [`GnsUtils::set_global_config_value`]
pub enum GnsConfig<'a> {
Float(f32),
Int32(u32),
String(&'a str),
Ptr(*mut c_void),
}

pub struct GnsUtils(*mut ISteamNetworkingUtils);

impl Drop for GnsUtils {
Expand Down Expand Up @@ -968,6 +975,38 @@ impl GnsUtils {
GnsNetworkMessage::new(message_ptr, conn, flags, payload)
}

/// Set a global configuration value, i.e. k_ESteamNetworkingConfig_FakePacketLag_Send => 1000 ms
#[inline]
pub fn set_global_config_value<'a>(
&self,
typ: ESteamNetworkingConfigValue,
value: GnsConfig<'a>,
) -> Result<(), ()> {
let result = match value {
GnsConfig::Float(x) => unsafe {
SteamAPI_ISteamNetworkingUtils_SetGlobalConfigValueFloat(self.0, typ, x)
},
GnsConfig::Int32(x) => unsafe {
SteamAPI_ISteamNetworkingUtils_SetGlobalConfigValueInt32(self.0, typ, x as i32)
},
GnsConfig::String(x) => unsafe {
SteamAPI_ISteamNetworkingUtils_SetGlobalConfigValueString(
self.0,
typ,
CString::new(x).expect("str; qed;").as_c_str().as_ptr(),
)
},
GnsConfig::Ptr(x) => unsafe {
SteamAPI_ISteamNetworkingUtils_SetGlobalConfigValuePtr(self.0, typ, x)
},
};
if result {
Ok(())
} else {
Err(())
}
}

#[inline]
pub unsafe fn into_inner(self) -> *mut ISteamNetworkingUtils {
self.0
Expand Down

0 comments on commit 8daa356

Please sign in to comment.