mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-12-22 20:08:20 -06:00
wip server
This commit is contained in:
parent
b940a9d9d6
commit
4cece2a876
|
@ -9,20 +9,48 @@ pub struct ConnectedClient {
|
||||||
timeout: Instant,
|
timeout: Instant,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub struct ServerConfig {
|
||||||
|
pub max_clients: usize,
|
||||||
|
}
|
||||||
|
impl Default for ServerConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
max_clients: MAX_CLIENTS,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Server {
|
pub struct Server {
|
||||||
socket: UdpSocket,
|
socket: UdpSocket,
|
||||||
clients: HashMap<ClientId, ConnectedClient, BuildNoHashHasher<ClientId>>
|
clients: HashMap<ClientId, ConnectedClient, BuildNoHashHasher<u8>>,
|
||||||
|
config: ServerConfig,
|
||||||
}
|
}
|
||||||
impl Server {
|
impl Server {
|
||||||
pub fn bind(addr: SocketAddr) -> anyhow::Result<Self> {
|
pub fn bind(addr: SocketAddr, config: ServerConfig) -> anyhow::Result<Self> {
|
||||||
|
assert!(config.max_clients <= MAX_CLIENTS);
|
||||||
let socket = UdpSocket::bind(addr)?;
|
let socket = UdpSocket::bind(addr)?;
|
||||||
socket.set_nonblocking(true)?;
|
socket.set_nonblocking(true)?;
|
||||||
//socket.set_broadcast(true)?;
|
//socket.set_broadcast(true)?;
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
|
config,
|
||||||
socket,
|
socket,
|
||||||
clients: HashMap::with_capacity_and_hasher(MAX_CLIENTS, BuildNoHashHasher::default())
|
clients: HashMap::with_capacity_and_hasher(MAX_CLIENTS, BuildNoHashHasher::default())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
/// Returns None if there are no free spots left
|
||||||
|
fn connect_client(&mut self) -> Option<ClientId> {
|
||||||
|
let id = (1..=self.config.max_clients)
|
||||||
|
.map(|x| ClientId::new(x as _).unwrap())
|
||||||
|
.find(|i| self.clients.contains_key(i))?;
|
||||||
|
self.clients.insert(id, ConnectedClient {
|
||||||
|
id,
|
||||||
|
addr: "0.0.0.0:0".parse().unwrap(),
|
||||||
|
timeout: Instant::now(),
|
||||||
|
});
|
||||||
|
todo!();
|
||||||
|
Some(id)
|
||||||
|
}
|
||||||
pub fn update(&mut self) {
|
pub fn update(&mut self) {
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
if self.socket.recv(&mut buf).is_ok() {
|
if self.socket.recv(&mut buf).is_ok() {
|
||||||
|
|
Loading…
Reference in a new issue