Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Networking improvements #146

Merged
merged 2 commits into from
Dec 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions src/networking_sockets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,20 @@ impl<Manager: 'static> NetworkingSockets<Manager> {
}
}

pub fn get_authentication_status(
&self,
) -> Result<NetworkingAvailability, NetworkingAvailabilityError> {
let mut details: sys::SteamNetAuthenticationStatus_t = unsafe { std::mem::zeroed() };
let auth = unsafe {
sys::SteamAPI_ISteamNetworkingSockets_GetAuthenticationStatus(
self.sockets,
&mut details,
)
};

auth.try_into()
}

/// Returns basic information about the high-level state of the connection.
///
/// Returns false if the connection handle is invalid.
Expand Down Expand Up @@ -859,7 +873,10 @@ impl<Manager: 'static> NetConnection<Manager> {
/// If any messages are returned, you MUST call SteamNetworkingMessage_t::Release() on each
/// of them free up resources after you are done. It is safe to keep the object alive for
/// a little while (put it into some queue, etc), and you may call Release() from any thread.
pub fn receive_messages(&mut self, batch_size: usize) -> Vec<NetworkingMessage<Manager>> {
pub fn receive_messages(
&mut self,
batch_size: usize,
) -> Result<Vec<NetworkingMessage<Manager>>, InvalidHandle> {
if self.message_buffer.capacity() < batch_size {
self.message_buffer
.reserve(batch_size - self.message_buffer.capacity());
Expand All @@ -872,16 +889,20 @@ impl<Manager: 'static> NetConnection<Manager> {
self.message_buffer.as_mut_ptr(),
batch_size as _,
);
if message_count < 0 {
return Err(InvalidHandle);
}
self.message_buffer.set_len(message_count as usize);
}

self.message_buffer
Ok(self
.message_buffer
.drain(..)
.map(|x| NetworkingMessage {
message: x,
_inner: self.inner.clone(),
})
.collect()
.collect())
}

/// Assign a connection to a poll group. Note that a connection may only belong to a
Expand Down Expand Up @@ -909,6 +930,10 @@ impl<Manager: 'static> NetConnection<Manager> {
debug_assert!(was_successful);
}

pub fn run_callbacks(&self) {
unsafe { sys::SteamAPI_ISteamNetworkingSockets_RunCallbacks(self.sockets) }
}

/// Set the connection state to be handled externally. The struct will no longer close the connection on drop.
pub(crate) fn handle_connection(&mut self) {
self.is_handled = true
Expand Down Expand Up @@ -1120,7 +1145,7 @@ mod tests {
std::thread::sleep(::std::time::Duration::from_millis(100));

println!("Receive message");
let messages = to_client.receive_messages(10);
let messages = to_client.receive_messages(10).unwrap();
assert_eq!(messages.len(), 1);
assert_eq!(messages[0].data(), &[1, 1, 2, 5]);

Expand All @@ -1132,7 +1157,7 @@ mod tests {
std::thread::sleep(::std::time::Duration::from_millis(100));

println!("Receive message");
let messages = to_server.receive_messages(10);
let messages = to_server.receive_messages(10).unwrap();
assert_eq!(messages.len(), 1);
assert_eq!(messages[0].data(), &[3, 3, 3, 1]);

Expand All @@ -1147,7 +1172,7 @@ mod tests {
std::thread::sleep(::std::time::Duration::from_millis(1000));

println!("Receive message");
let messages = to_server.receive_messages(10);
let messages = to_server.receive_messages(10).unwrap();
assert_eq!(messages.len(), 1);
assert_eq!(messages[0].data(), &[1, 2, 34, 5]);
}
Expand Down
Loading