2019-03-03 22:02:38 +00:00
|
|
|
use specs::Entity as EcsEntity;
|
|
|
|
use common::{
|
|
|
|
msg::{ServerMsg, ClientMsg},
|
|
|
|
net::PostBox,
|
|
|
|
};
|
2019-03-04 19:50:26 +00:00
|
|
|
use crate::Error;
|
2019-03-03 22:02:38 +00:00
|
|
|
|
|
|
|
pub struct Client {
|
|
|
|
pub ecs_entity: EcsEntity,
|
|
|
|
pub postbox: PostBox<ServerMsg, ClientMsg>,
|
|
|
|
pub last_ping: f64,
|
|
|
|
}
|
2019-03-04 19:50:26 +00:00
|
|
|
|
|
|
|
pub struct Clients {
|
|
|
|
clients: Vec<Client>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Clients {
|
|
|
|
pub fn empty() -> Self {
|
|
|
|
Self {
|
|
|
|
clients: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add(&mut self, client: Client) {
|
|
|
|
self.clients.push(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove_if<F: FnMut(&mut Client) -> bool>(&mut self, f: F) {
|
|
|
|
self.clients.drain_filter(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn notify_all(&mut self, msg: ServerMsg) {
|
|
|
|
for client in &mut self.clients {
|
|
|
|
// Consume any errors, deal with them later
|
|
|
|
let _ = client.postbox.send(msg.clone());
|
|
|
|
println!("Sending message...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|