diff --git a/Cargo.toml b/Cargo.toml index 80d3a7e..3ba572e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,4 +2,6 @@ members = [ "board-shared", "board-frontend", + "board-server", + "board-network", ] diff --git a/board-network/Cargo.toml b/board-network/Cargo.toml new file mode 100644 index 0000000..78aa7b1 --- /dev/null +++ b/board-network/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "board-network" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +serde = { version = "1.0.152", features = ["derive"] } +board-shared = { path = "../board-shared" } diff --git a/board-network/src/lib.rs b/board-network/src/lib.rs new file mode 100644 index 0000000..1b800ec --- /dev/null +++ b/board-network/src/lib.rs @@ -0,0 +1 @@ +pub mod protocol; diff --git a/board-network/src/protocol.rs b/board-network/src/protocol.rs new file mode 100644 index 0000000..c850203 --- /dev/null +++ b/board-network/src/protocol.rs @@ -0,0 +1,15 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)] +pub enum ClientMessage { + CreateRoom(String), + JoinRoom(String, String), + Disconnected, +} + +#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] +pub enum ServerMessage { + JoinedRoom { + room_name: String, + }, +} \ No newline at end of file diff --git a/board-server/Cargo.toml b/board-server/Cargo.toml new file mode 100644 index 0000000..f0d8a9e --- /dev/null +++ b/board-server/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "board-server" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +futures = { version = "0.3" } +board-shared = { path = "../board-shared" } +board-network = { path = "../board-network" } +smol = "1.3.0" +async-tungstenite = "0.20.0" +tungstenite = "0.18.0" +anyhow = "1.0.69" diff --git a/board-server/src/main.rs b/board-server/src/main.rs new file mode 100644 index 0000000..eb3404c --- /dev/null +++ b/board-server/src/main.rs @@ -0,0 +1,42 @@ +use anyhow::Result; +use futures::StreamExt; +use smol::Async; +use std::net::{SocketAddr, TcpListener, TcpStream}; +use std::{env, io}; +use tungstenite::Message as WebsocketMessage; + +async fn handle_connection(raw_stream: Async, addr: SocketAddr) -> Result<()> { + println!("[{addr}] Incoming TCP connection"); + let mut ws_stream = async_tungstenite::accept_async(raw_stream).await?; + println!("[{addr}] WebSocket connection established"); + + while let Some(Ok(WebsocketMessage::Text(text))) = ws_stream.next().await { + println!("[{addr}] Received message: {text}"); + } + + println!("[{addr}] Dropping connection"); + Ok(()) +} + +fn main() -> Result<(), io::Error> { + let addr = env::args() + .nth(1) + .unwrap_or_else(|| "0.0.0.0:8080".to_string()) + .parse::() + .expect("Invalid address"); + + smol::block_on(async { + println!("Listening on: {addr}"); + let listener = Async::::bind(addr).expect("Could not create listener"); + + while let Ok((stream, addr)) = listener.accept().await { + smol::spawn(async move { + if let Err(e) = handle_connection(stream, addr).await { + eprintln!("Failed to handle connection from {addr}: {e}"); + } + }) + .detach(); + } + }); + Ok(()) +}