mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-12-25 21:28:20 -06:00
implement broadcase and multicast functions
This commit is contained in:
parent
7bb7b9b131
commit
3cd3634618
|
@ -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,17 +137,30 @@ 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
errors
|
||||||
}
|
}
|
||||||
pub fn broadcast_message(&mut self, _message: S) -> anyhow::Result<()> {
|
pub fn broadcast_message(&mut self, message: S) -> Vec<Error> {
|
||||||
todo!()
|
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<()> {
|
||||||
//kick inactive clients
|
//kick inactive clients
|
||||||
self.clients.retain(|&id, client| {
|
self.clients.retain(|&id, client| {
|
||||||
|
|
Loading…
Reference in a new issue