Add necessary utility functions

This commit is contained in:
Sofia 2026-07-16 04:52:20 +03:00
parent cdab0bb448
commit 44122d77d6
2 changed files with 25 additions and 0 deletions

View File

@ -373,6 +373,10 @@ impl<T: Clone + Serialize + DeserializeOwned + Send + Sync + 'static> Connection
messages
}
pub fn connections(&self) -> Vec<Connection<T>> {
self.connections.values().cloned().collect()
}
}
/// Wrap UdpSocket with some helper methods

View File

@ -207,8 +207,29 @@ impl<T: Clone + Serialize + DeserializeOwned + Send + Sync + 'static> Peer<T> {
self.connection_mgr.send(to, message, false);
}
/// Send a reliable message to every connection currently connected
pub fn broadcast_reliable(&mut self, message: T) {
for connection in self.connections() {
self.connection_mgr
.send(&connection.address, message.clone(), true);
}
}
/// Send an unreliable message to every connection currently connected
pub fn broadcast_unreliable(&mut self, message: T) {
for connection in self.connections() {
self.connection_mgr
.send(&connection.address, message.clone(), false);
}
}
/// Close the Peer, disconnecting all connections.
pub fn close(&mut self) {
self.connection_mgr.close();
}
/// List of all current connections
pub fn connections(&self) -> Vec<Connection<T>> {
self.connection_mgr.connections()
}
}