diff options
| author | metamuffin <metamuffin@disroot.org> | 2024-06-17 00:04:34 +0200 | 
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2024-06-17 00:04:34 +0200 | 
| commit | dd4b88a86a99f028bdfe66fef3c66170629f3cdc (patch) | |
| tree | 6e9acd5148b18220cfe7de83b80cf47b1aef3449 | |
| parent | c10d66de1bd2ef04e4010223dcd82443a5d558f0 (diff) | |
| download | hurrycurry-dd4b88a86a99f028bdfe66fef3c66170629f3cdc.tar hurrycurry-dd4b88a86a99f028bdfe66fef3c66170629f3cdc.tar.bz2 hurrycurry-dd4b88a86a99f028bdfe66fef3c66170629f3cdc.tar.zst | |
establish ws
| -rw-r--r-- | Cargo.lock | 270 | ||||
| -rw-r--r-- | server/Cargo.toml | 2 | ||||
| -rw-r--r-- | server/src/game.rs | 2 | ||||
| -rw-r--r-- | server/src/main.rs | 81 | ||||
| -rw-r--r-- | test-client/index.html | 22 | ||||
| -rw-r--r-- | test-client/main.ts | 34 | ||||
| -rw-r--r-- | test-client/protocol.ts | 4 | 
7 files changed, 391 insertions, 24 deletions
| @@ -109,6 +109,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index"  checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1"  [[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]]  name = "bytes"  version = "1.6.0"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -133,6 +148,41 @@ source = "registry+https://github.com/rust-lang/crates.io-index"  checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422"  [[package]] +name = "cpufeatures" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "data-encoding" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8566979429cf69b49a5c740c60791108e86440e8be149bbea4fe54d2c32d6e2" + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]]  name = "env_filter"  version = "0.1.0"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -156,6 +206,77 @@ dependencies = [  ]  [[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "futures-core" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" + +[[package]] +name = "futures-macro" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" + +[[package]] +name = "futures-task" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" + +[[package]] +name = "futures-util" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" +dependencies = [ + "futures-core", + "futures-macro", + "futures-sink", + "futures-task", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]]  name = "gimli"  version = "0.29.0"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -177,6 +298,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index"  checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"  [[package]] +name = "http" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "httparse" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0e7a4dd27b9476dc40cb050d3632d3bba3a70ddbff012285f7f8559a1e7e545" + +[[package]]  name = "humantime"  version = "2.1.0"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -291,6 +429,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index"  checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02"  [[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]]  name = "proc-macro2"  version = "1.0.85"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -309,6 +459,36 @@ dependencies = [  ]  [[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]]  name = "redox_syscall"  version = "0.5.2"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -396,6 +576,17 @@ dependencies = [  ]  [[package]] +name = "sha1" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]]  name = "signal-hook-registry"  version = "1.4.2"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -405,6 +596,15 @@ dependencies = [  ]  [[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + +[[package]]  name = "smallvec"  version = "1.13.2"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -432,6 +632,26 @@ dependencies = [  ]  [[package]] +name = "thiserror" +version = "1.0.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]]  name = "tokio"  version = "1.38.0"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -462,16 +682,54 @@ dependencies = [  ]  [[package]] +name = "tokio-tungstenite" +version = "0.23.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6989540ced10490aaf14e6bad2e3d33728a2813310a0c71d1574304c49631cd" +dependencies = [ + "futures-util", + "log", + "tokio", + "tungstenite", +] + +[[package]] +name = "tungstenite" +version = "0.23.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e2e2ce1e47ed2994fd43b04c8f618008d4cabdd5ee34027cf14f9d918edd9c8" +dependencies = [ + "byteorder", + "bytes", + "data-encoding", + "http", + "httparse", + "log", + "rand", + "sha1", + "thiserror", + "utf-8", +] + +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + +[[package]]  name = "undercooked"  version = "0.1.0"  dependencies = [   "anyhow",   "env_logger", + "futures-util",   "glam",   "log",   "serde",   "serde_json",   "tokio", + "tokio-tungstenite",  ]  [[package]] @@ -481,12 +739,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index"  checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"  [[package]] +name = "utf-8" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" + +[[package]]  name = "utf8parse"  version = "0.2.2"  source = "registry+https://github.com/rust-lang/crates.io-index"  checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"  [[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]]  name = "wasi"  version = "0.11.0+wasi-snapshot-preview1"  source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/server/Cargo.toml b/server/Cargo.toml index c0e3dae2..27e6f4b7 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -11,3 +11,5 @@ anyhow = "1.0.86"  serde = { version = "1.0.203", features = ["derive"] }  tokio = { version = "1.38.0", features = ["full"] }  serde_json = "1.0.117" +tokio-tungstenite = "0.23.1" +futures-util = "0.3.30" diff --git a/server/src/game.rs b/server/src/game.rs index 0d1e8c3b..458a796b 100644 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -3,8 +3,6 @@ use anyhow::{anyhow, Result};  use glam::UVec2;  use std::collections::{HashMap, VecDeque}; -struct ItemData {} -  struct TileData {      items: Vec<ID>,      active: bool, diff --git a/server/src/main.rs b/server/src/main.rs index 06786c05..0987e9ef 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,4 +1,5 @@  use anyhow::Result; +use futures_util::{SinkExt, StreamExt};  use log::{debug, info};  use std::{sync::Arc, time::Duration};  use tokio::{ @@ -8,6 +9,7 @@ use tokio::{      sync::{broadcast, RwLock},      time::sleep,  }; +use tokio_tungstenite::tungstenite::Message;  use undercooked::{      game::Game,      protocol::{PacketC, PacketS}, @@ -16,8 +18,13 @@ use undercooked::{  #[tokio::main]  async fn main() -> Result<()> {      env_logger::init_from_env("LOG"); -    let listener = TcpListener::bind("0.0.0.0:27031").await?; -    info!("listening on {}", listener.local_addr()?); +    let raw_listener = TcpListener::bind("0.0.0.0:27031").await?; +    let ws_listener = TcpListener::bind("0.0.0.0:27032").await?; +    info!( +        "listening for line-based tcp on {}", +        raw_listener.local_addr()? +    ); +    info!("listening for websockets on {}", ws_listener.local_addr()?);      let game = Arc::new(RwLock::new(Game::new()));      let (tx, rx) = broadcast::channel::<PacketC>(1024); @@ -37,28 +44,58 @@ async fn main() -> Result<()> {      }      for id in 0.. { -        let (sock, addr) = listener.accept().await?; -        let (read, mut write) = sock.into_split(); -        let game = game.clone(); -        let mut rx = rx.resubscribe(); -        info!("{addr} connected"); -        spawn(async move { -            while let Ok(packet) = rx.recv().await { -                write -                    .write_all(serde_json::to_string(&packet).unwrap().as_bytes()) -                    .await -                    .unwrap(); -                write.write_all(b"\n").await.unwrap(); +        tokio::select! { +            r = raw_listener.accept() => { +                let (sock, addr) = r?; +                let (read, mut write) = sock.into_split(); +                let game = game.clone(); +                let mut rx = rx.resubscribe(); +                info!("{addr} connected"); +                spawn(async move { +                    while let Ok(packet) = rx.recv().await { +                        write +                            .write_all(serde_json::to_string(&packet).unwrap().as_bytes()) +                            .await +                            .unwrap(); +                        write.write_all(b"\n").await.unwrap(); +                    } +                }); +                spawn(async move { +                    let mut read = BufReader::new(read).lines(); +                    while let Ok(Some(line)) = read.next_line().await { +                        let packet: PacketS = serde_json::from_str(&line).unwrap(); +                        debug!("<- {id} {packet:?}"); +                        game.write().await.packet_in(id, packet).unwrap(); +                    } +                });              } -        }); -        spawn(async move { -            let mut read = BufReader::new(read).lines(); -            while let Ok(Some(line)) = read.next_line().await { -                let packet: PacketS = serde_json::from_str(&line).unwrap(); -                debug!("<- {id} {packet:?}"); -                game.write().await.packet_in(id, packet).unwrap(); +            r = ws_listener.accept() => { +                // let (sock, addr) = ws_listener.accept().await?; +                let (sock, addr) = r?; +                let sock = tokio_tungstenite::accept_async(sock).await?; +                let (mut write, mut read) = sock.split(); +                let game = game.clone(); +                let mut rx = rx.resubscribe(); +                info!("{addr} connected via ws"); +                spawn(async move { +                    while let Ok(packet) = rx.recv().await { +                        write +                            .send(tokio_tungstenite::tungstenite::Message::Text( +                                serde_json::to_string(&packet).unwrap(), +                            )) +                            .await +                            .unwrap(); +                    } +                }); +                spawn(async move { +                    while let Some(Ok(Message::Text(message))) = read.next().await { +                        let packet: PacketS = serde_json::from_str(&message).unwrap(); +                        debug!("<- {id} {packet:?}"); +                        game.write().await.packet_in(id, packet).unwrap(); +                    } +                });              } -        }); +        }      }      Ok(()) diff --git a/test-client/index.html b/test-client/index.html new file mode 100644 index 00000000..12079351 --- /dev/null +++ b/test-client/index.html @@ -0,0 +1,22 @@ +<!DOCTYPE html> +<html lang="en"> +    <head> +        <meta charset="UTF-8" /> +        <meta name="viewport" content="width=device-width, initial-scale=1.0" /> +        <title>Undercooked - Test Client</title> +        <script src="./main.js" type="module"></script> +        <style> +            body { +                margin: 0px; +                overflow: hidden; +                background-color: black; +            } +            noscript { +                color: white; +            } +        </style> +    </head> +    <body> +        <noscript>the game is written in javascript btw.</noscript> +    </body> +</html> diff --git a/test-client/main.ts b/test-client/main.ts new file mode 100644 index 00000000..bc0872a6 --- /dev/null +++ b/test-client/main.ts @@ -0,0 +1,34 @@ +/// <reference lib="dom" /> + +let ctx: CanvasRenderingContext2D; +let canvas: HTMLCanvasElement; +let ws: WebSocket +document.addEventListener("DOMContentLoaded", () => { +    ws = new WebSocket(`${window.location.protocol.endsWith("s:") ? "wss" : "ws"}://${window.location.hostname}:27032/`) +    ws.onerror = console.error +    ws.onmessage = m => { +        console.log(JSON.parse(m.data)); +    } +    ws.onclose = () => console.warn("close") +    ws.onopen = () => console.warn("open") + +    canvas = document.createElement("canvas"); +    document.body.append(canvas) +    ctx = canvas.getContext("2d")! +    resize() +    globalThis.addEventListener("resize", resize) +    draw() +}) + +function resize() { +    canvas.width = globalThis.innerWidth +    canvas.height = globalThis.innerHeight +} + +function draw() { +    ctx.fillStyle = "black" +    ctx.fillRect(0, 0, canvas.width, canvas.height) + +    requestAnimationFrame(draw) +} + diff --git a/test-client/protocol.ts b/test-client/protocol.ts new file mode 100644 index 00000000..9761f152 --- /dev/null +++ b/test-client/protocol.ts @@ -0,0 +1,4 @@ + + +export type PacketS = {} +export type PacketC = {} | 
