diff --git a/example/src/main.rs b/example/src/main.rs index 48f5988..ff82bd2 100644 --- a/example/src/main.rs +++ b/example/src/main.rs @@ -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). diff --git a/gns/src/lib.rs b/gns/src/lib.rs index f59e291..8b41c6b 100644 --- a/gns/src/lib.rs +++ b/gns/src/lib.rs @@ -815,7 +815,6 @@ impl<'x, 'y> GnsSocket<'x, 'y, IsCreated> { m_int64: queue as *const _ as i64 } }]; - (addr, options) } @@ -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 { @@ -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