2023-02-05 19:15:19 -06:00
|
|
|
use std::{net::{UdpSocket, SocketAddr}, time::Instant};
|
|
|
|
use hashbrown::HashMap;
|
|
|
|
use nohash_hasher::BuildNoHashHasher;
|
|
|
|
use crate::{BINCODE_CONFIG, common::{ClientId, MAX_CLIENTS}};
|
|
|
|
|
|
|
|
pub struct ConnectedClient {
|
|
|
|
id: ClientId,
|
|
|
|
addr: SocketAddr,
|
|
|
|
timeout: Instant,
|
|
|
|
}
|
2023-01-31 20:16:23 -06:00
|
|
|
|
|
|
|
pub struct Server {
|
|
|
|
socket: UdpSocket,
|
2023-02-05 19:15:19 -06:00
|
|
|
clients: HashMap<ClientId, ConnectedClient, BuildNoHashHasher<ClientId>>
|
2023-01-31 20:16:23 -06:00
|
|
|
}
|
|
|
|
impl Server {
|
|
|
|
pub fn bind(addr: SocketAddr) -> anyhow::Result<Self> {
|
|
|
|
let socket = UdpSocket::bind(addr)?;
|
|
|
|
socket.set_nonblocking(true)?;
|
|
|
|
socket.set_broadcast(true)?;
|
2023-02-05 19:15:19 -06:00
|
|
|
Ok(Self {
|
|
|
|
socket,
|
|
|
|
clients: HashMap::with_capacity_and_hasher(MAX_CLIENTS, BuildNoHashHasher::default())
|
|
|
|
})
|
2023-01-31 20:16:23 -06:00
|
|
|
}
|
2023-02-05 19:40:45 -06:00
|
|
|
pub fn update(&mut self) {
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
if self.socket.recv(&mut buf).is_ok() {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
2023-01-31 20:16:23 -06:00
|
|
|
}
|