You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.2 KiB
49 lines
1.2 KiB
use crate::{server::Server, Id};
|
|
use async_std::{
|
|
net::UdpSocket,
|
|
sync::{Arc, RwLock},
|
|
task,
|
|
};
|
|
use std::{
|
|
collections::BTreeMap,
|
|
net::SocketAddr,
|
|
sync::atomic::{AtomicBool, Ordering},
|
|
};
|
|
|
|
pub struct Endpoint {
|
|
running: AtomicBool,
|
|
socket: UdpSocket,
|
|
bind: String,
|
|
clients: RwLock<BTreeMap<Id, Client>>,
|
|
}
|
|
|
|
pub struct Client {}
|
|
|
|
impl Endpoint {
|
|
pub async fn new(bind: &str) -> Arc<Self> {
|
|
let socket = UdpSocket::bind(bind).await.unwrap();
|
|
Arc::new(Self {
|
|
running: true.into(),
|
|
socket,
|
|
bind: bind.into(),
|
|
clients: Default::default(),
|
|
})
|
|
}
|
|
|
|
/// Stop the endpoint
|
|
pub fn stop(self: &Arc<Self>) {
|
|
self.running.store(false, Ordering::Relaxed);
|
|
}
|
|
|
|
pub async fn listen(self: &Arc<Self>, serv: Arc<Server>) {
|
|
let mut buf = vec![0; 1024];
|
|
|
|
info!("Listening for connections on {}", self.bind);
|
|
while self.running.load(Ordering::Relaxed) {
|
|
let (int, peer) = self.socket.recv_from(&mut buf).await.unwrap();
|
|
if int > 1024 {
|
|
warn!("Read a larger chunk than buffer?");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|