implement broadcase and multicast functions

This commit is contained in:
griffi-gh 2023-02-13 02:57:09 +01:00
parent 95c681d0fc
commit bccd3a8cfd

View file

@ -5,7 +5,7 @@ use std::{
collections::{VecDeque, vec_deque::Drain as DrainDeque}, collections::{VecDeque, vec_deque::Drain as DrainDeque},
io::ErrorKind io::ErrorKind
}; };
use anyhow::{Result, bail}; use anyhow::{Result, Error, bail};
use hashbrown::HashMap; use hashbrown::HashMap;
use nohash_hasher::BuildNoHashHasher; use nohash_hasher::BuildNoHashHasher;
use crate::{ use crate::{
@ -137,15 +137,28 @@ impl<S, R> Server<S, R> where S: Message, R: Message {
Ok(()) Ok(())
} }
pub fn send_message(&mut self, id: ClientId, message: S) -> anyhow::Result<()> { pub fn send_message(&mut self, id: ClientId, message: S) -> Result<()> {
self.send_packet(IdServerPacket(Some(id), ServerPacket::Data(message)))?; self.send_packet(IdServerPacket(Some(id), ServerPacket::Data(message)))?;
Ok(()) Ok(())
} }
pub fn multicast_message(&mut self, _clients: impl IntoIterator<Item = ClientId>, _message: S) { pub fn multicast_message(&mut self, clients: impl IntoIterator<Item = ClientId>, message: S) -> Vec<Error> {
todo!() //TODO use actual udp multicast
let mut errors = Vec::with_capacity(0);
for client in clients {
if let Err(error) = self.send_message(client, message.clone()) {
log::error!("Message broadcast failed for id {client}");
errors.push(error);
} }
pub fn broadcast_message(&mut self, _message: S) -> anyhow::Result<()> { }
todo!() errors
}
pub fn broadcast_message(&mut self, message: S) -> Vec<Error> {
let ids = self.clients.keys().copied().collect::<Vec<ClientId>>();
self.multicast_message(ids, message)
}
pub fn broadcast_message_except(&mut self, except: ClientId, message: S) -> Vec<Error> {
let ids = self.clients.keys().copied().filter(|&k| k != except).collect::<Vec<ClientId>>();
self.multicast_message(ids, message)
} }
pub fn update(&mut self) -> Result<()> { pub fn update(&mut self) -> Result<()> {