aboutsummaryrefslogtreecommitdiff
path: root/games/rstnode/rst-core/src/net/endpoint.rs
diff options
context:
space:
mode:
authorKatharina Fey <kookie@spacekookie.de>2021-02-14 00:06:14 +0100
committerKatharina Fey <kookie@spacekookie.de>2021-02-14 00:06:14 +0100
commiteffbdeed66e8de8e769b8ac069926ad1a9110e62 (patch)
treee4522354e53266204aa9962caccde55d8c815092 /games/rstnode/rst-core/src/net/endpoint.rs
parent5dab336049dbc6817e9ff212998690f59f6bbfa8 (diff)
rstnode: refactoring server and client components into rst-coreHEADmaster
* Add an inbox/ outbox system to server components * Define a data flow from Request -> computation -> Update * Create simple handlers to call server or client code for requests
Diffstat (limited to 'games/rstnode/rst-core/src/net/endpoint.rs')
-rw-r--r--games/rstnode/rst-core/src/net/endpoint.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/games/rstnode/rst-core/src/net/endpoint.rs b/games/rstnode/rst-core/src/net/endpoint.rs
new file mode 100644
index 000000000000..0c8e2f912421
--- /dev/null
+++ b/games/rstnode/rst-core/src/net/endpoint.rs
@@ -0,0 +1,49 @@
+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?");
+ }
+ }
+ }
+}