diff options
author | metamuffin <metamuffin@disroot.org> | 2024-08-11 00:22:49 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-08-11 00:30:43 +0200 |
commit | f4499b7a0e808d84157e6148afef4f844a7007cf (patch) | |
tree | 354d970af16a4b3d6217575d259cf1afc0faf291 | |
parent | 0f6a824cb4c6d36dda485a939d9c640ec234d802 (diff) | |
download | hurrycurry-f4499b7a0e808d84157e6148afef4f844a7007cf.tar hurrycurry-f4499b7a0e808d84157e6148afef4f844a7007cf.tar.bz2 hurrycurry-f4499b7a0e808d84157e6148afef4f844a7007cf.tar.zst |
factor out client network code to client-lib
-rw-r--r-- | Cargo.lock | 142 | ||||
-rw-r--r-- | pixel-client/Cargo.toml | 3 | ||||
-rw-r--r-- | pixel-client/src/game.rs | 2 | ||||
-rw-r--r-- | pixel-client/src/main.rs | 3 | ||||
-rw-r--r-- | pixel-client/src/menu/main.rs | 2 | ||||
-rw-r--r-- | server/bot/Cargo.toml | 3 | ||||
-rw-r--r-- | server/bot/src/main.rs | 2 | ||||
-rw-r--r-- | server/client-lib/Cargo.toml | 15 | ||||
-rw-r--r-- | server/client-lib/src/lib.rs | 1 | ||||
-rw-r--r-- | server/client-lib/src/network/mod.rs | 4 | ||||
-rw-r--r-- | server/client-lib/src/network/sync.rs (renamed from pixel-client/src/network.rs) | 0 | ||||
-rw-r--r-- | server/client-lib/src/network/tokio.rs | 131 |
12 files changed, 198 insertions, 110 deletions
@@ -68,7 +68,7 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391" dependencies = [ - "windows-sys 0.52.0", + "windows-sys", ] [[package]] @@ -78,7 +78,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" dependencies = [ "anstyle", - "windows-sys 0.52.0", + "windows-sys", ] [[package]] @@ -281,6 +281,7 @@ name = "bot" version = "0.1.0" dependencies = [ "hurrycurry-client-lib", + "log", ] [[package]] @@ -577,7 +578,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys", ] [[package]] @@ -782,7 +783,7 @@ version = "0.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" dependencies = [ - "windows-sys 0.52.0", + "windows-sys", ] [[package]] @@ -812,7 +813,15 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" name = "hurrycurry-client-lib" version = "0.1.0" dependencies = [ + "anyhow", + "bincode", + "futures-util", "hurrycurry-protocol", + "log", + "serde_json", + "tokio", + "tokio-tungstenite", + "tungstenite", ] [[package]] @@ -1003,7 +1012,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e310b3a6b5907f99202fcdb4960ff45b93735d7c7d96b760fcff8db2dc0e103d" dependencies = [ "cfg-if", - "windows-targets 0.52.6", + "windows-targets", ] [[package]] @@ -1077,13 +1086,14 @@ dependencies = [ [[package]] name = "mio" -version = "0.8.11" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c" +checksum = "4569e456d394deccd22ce1c1913e6ea0e54519f577285001215d33557431afe4" dependencies = [ + "hermit-abi", "libc", "wasi", - "windows-sys 0.48.0", + "windows-sys", ] [[package]] @@ -1165,16 +1175,6 @@ dependencies = [ ] [[package]] -name = "num_cpus" -version = "1.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" -dependencies = [ - "hermit-abi", - "libc", -] - -[[package]] name = "object" version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1215,7 +1215,7 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-targets 0.52.6", + "windows-targets", ] [[package]] @@ -1244,6 +1244,7 @@ dependencies = [ "bincode", "clap", "env_logger", + "hurrycurry-client-lib", "hurrycurry-protocol", "log", "rand 0.9.0-alpha.1", @@ -1536,7 +1537,7 @@ dependencies = [ "libc", "spin", "untrusted", - "windows-sys 0.52.0", + "windows-sys", ] [[package]] @@ -1561,7 +1562,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys 0.52.0", + "windows-sys", ] [[package]] @@ -1633,7 +1634,7 @@ version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fbc91545643bcf3a0bbb6569265615222618bdf33ce4ffbbd13c4bbd4c093534" dependencies = [ - "windows-sys 0.52.0", + "windows-sys", ] [[package]] @@ -1810,7 +1811,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys", ] [[package]] @@ -1873,7 +1874,7 @@ dependencies = [ "cfg-if", "fastrand", "rustix", - "windows-sys 0.52.0", + "windows-sys", ] [[package]] @@ -1909,28 +1910,27 @@ dependencies = [ [[package]] name = "tokio" -version = "1.38.0" +version = "1.39.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a" +checksum = "daa4fb1bc778bd6f04cbfc4bb2d06a7396a8f299dc33ea1900cedaa316f467b1" dependencies = [ "backtrace", "bytes", "libc", "mio", - "num_cpus", "parking_lot", "pin-project-lite", "signal-hook-registry", "socket2", "tokio-macros", - "windows-sys 0.48.0", + "windows-sys", ] [[package]] name = "tokio-macros" -version = "2.3.0" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" dependencies = [ "proc-macro2", "quote", @@ -2176,35 +2176,11 @@ dependencies = [ [[package]] name = "windows-sys" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" -dependencies = [ - "windows-targets 0.48.5", -] - -[[package]] -name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.6", -] - -[[package]] -name = "windows-targets" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" -dependencies = [ - "windows_aarch64_gnullvm 0.48.5", - "windows_aarch64_msvc 0.48.5", - "windows_i686_gnu 0.48.5", - "windows_i686_msvc 0.48.5", - "windows_x86_64_gnu 0.48.5", - "windows_x86_64_gnullvm 0.48.5", - "windows_x86_64_msvc 0.48.5", + "windows-targets", ] [[package]] @@ -2213,48 +2189,30 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm 0.52.6", - "windows_aarch64_msvc 0.52.6", - "windows_i686_gnu 0.52.6", + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", "windows_i686_gnullvm", - "windows_i686_msvc 0.52.6", - "windows_x86_64_gnu 0.52.6", - "windows_x86_64_gnullvm 0.52.6", - "windows_x86_64_msvc 0.52.6", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", ] [[package]] name = "windows_aarch64_gnullvm" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" - -[[package]] -name = "windows_aarch64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" - -[[package]] -name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" - -[[package]] -name = "windows_i686_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" @@ -2267,48 +2225,24 @@ checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" - -[[package]] -name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" - -[[package]] -name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" - -[[package]] -name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" - -[[package]] -name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/pixel-client/Cargo.toml b/pixel-client/Cargo.toml index b0333d21..1b610c7c 100644 --- a/pixel-client/Cargo.toml +++ b/pixel-client/Cargo.toml @@ -6,6 +6,9 @@ edition = "2021" [dependencies] sdl2 = { version = "0.37.0", features = ["gfx", "ttf", "mixer"] } hurrycurry-protocol = { path = "../server/protocol" } +hurrycurry-client-lib = { path = "../server/client-lib", default-features = false, features = [ + "sync-network", +] } tungstenite = { version = "0.23.0", features = ["rustls-tls-native-roots"] } serde_json = "1.0.120" bincode = "2.0.0-rc.3" diff --git a/pixel-client/src/game.rs b/pixel-client/src/game.rs index fb9df21c..d4f1e4e4 100644 --- a/pixel-client/src/game.rs +++ b/pixel-client/src/game.rs @@ -17,7 +17,6 @@ */ use crate::{ helper::InterpolateExt, - network::Network, render::{ misc::MiscTextures, sprite::{Sprite, SpriteDraw}, @@ -26,6 +25,7 @@ use crate::{ tilemap::Tilemap, State, }; +use hurrycurry_client_lib::network::sync::Network; use hurrycurry_protocol::{ glam::{IVec2, Vec2}, movement::MovementBase, diff --git a/pixel-client/src/main.rs b/pixel-client/src/main.rs index 963d3fc4..64a6f34f 100644 --- a/pixel-client/src/main.rs +++ b/pixel-client/src/main.rs @@ -18,9 +18,9 @@ #![feature(map_many_mut)] use clap::{Parser, Subcommand}; use game::Game; +use hurrycurry_client_lib::network::sync::Network; use hurrycurry_protocol::glam::Vec2; use menu::main::MainMenu; -use network::Network; use profiler::ProfilerOverlay; use render::Renderer; use sdl2::{event::Event, keyboard::KeyboardState, mouse::MouseState, pixels::Color}; @@ -29,7 +29,6 @@ use std::time::{Duration, Instant}; pub mod game; pub mod helper; pub mod menu; -pub mod network; pub mod profiler; pub mod render; pub mod tilemap; diff --git a/pixel-client/src/menu/main.rs b/pixel-client/src/menu/main.rs index 5c515169..54b0fe9c 100644 --- a/pixel-client/src/menu/main.rs +++ b/pixel-client/src/menu/main.rs @@ -18,11 +18,11 @@ use super::{background::MenuBackground, credits::CreditsMenu, settings::SettingsMenu}; use crate::{ game::Game, - network::Network, render::{sprite::SpriteDraw, AtlasLayout, Renderer}, ui::UiState, State, }; +use hurrycurry_client_lib::network::sync::Network; use hurrycurry_protocol::glam::Vec2; use sdl2::{ keyboard::{KeyboardState, Keycode}, diff --git a/server/bot/Cargo.toml b/server/bot/Cargo.toml index 259095ab..157472ae 100644 --- a/server/bot/Cargo.toml +++ b/server/bot/Cargo.toml @@ -4,4 +4,5 @@ version = "0.1.0" edition = "2021" [dependencies] -hurrycurry-client-lib = { path = "../client-lib" } +hurrycurry-client-lib = { path = "../client-lib", features = ["tokio-network"] } +log = "0.4.22" diff --git a/server/bot/src/main.rs b/server/bot/src/main.rs index e1be64ef..0b625703 100644 --- a/server/bot/src/main.rs +++ b/server/bot/src/main.rs @@ -17,5 +17,5 @@ */ fn main() { - + }
\ No newline at end of file diff --git a/server/client-lib/Cargo.toml b/server/client-lib/Cargo.toml index 6a3e7f98..45361d29 100644 --- a/server/client-lib/Cargo.toml +++ b/server/client-lib/Cargo.toml @@ -5,3 +5,18 @@ edition = "2021" [dependencies] hurrycurry-protocol = { path = "../protocol" } +tungstenite = { version = "0.23.0", optional = true, features = [ + "rustls-tls-native-roots", +] } +tokio-tungstenite = { version = "0.23.1", optional = true } +tokio = { version = "1.39.2", features = ["net"], optional = true } +serde_json = "1.0.120" +bincode = "2.0.0-rc.3" +log = "0.4.22" +anyhow = "1.0.86" +futures-util = { version = "0.3.30", optional = true } + +[features] +default = ["sync-network", "tokio-network"] +sync-network = ["dep:tungstenite"] +tokio-network = ["dep:tokio-tungstenite", "dep:tokio", "dep:futures-util"] diff --git a/server/client-lib/src/lib.rs b/server/client-lib/src/lib.rs index 74f6f10c..649e71b2 100644 --- a/server/client-lib/src/lib.rs +++ b/server/client-lib/src/lib.rs @@ -15,6 +15,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ +pub mod network; pub mod spatial_index; use hurrycurry_protocol::{ diff --git a/server/client-lib/src/network/mod.rs b/server/client-lib/src/network/mod.rs new file mode 100644 index 00000000..fa570170 --- /dev/null +++ b/server/client-lib/src/network/mod.rs @@ -0,0 +1,4 @@ +#[cfg(feature = "sync-network")] +pub mod sync; +#[cfg(feature = "tokio-network")] +pub mod tokio; diff --git a/pixel-client/src/network.rs b/server/client-lib/src/network/sync.rs index e8aa08de..e8aa08de 100644 --- a/pixel-client/src/network.rs +++ b/server/client-lib/src/network/sync.rs diff --git a/server/client-lib/src/network/tokio.rs b/server/client-lib/src/network/tokio.rs new file mode 100644 index 00000000..60cafd95 --- /dev/null +++ b/server/client-lib/src/network/tokio.rs @@ -0,0 +1,131 @@ +/* + Hurry Curry! - a game about cooking + Copyright 2024 metamuffin + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +*/ +use anyhow::Result; +use futures_util::{ + stream::{SplitSink, SplitStream, StreamExt}, + SinkExt, TryStreamExt, +}; +use hurrycurry_protocol::{PacketC, PacketS, BINCODE_CONFIG, VERSION}; +use log::{debug, info, warn}; +use std::sync::atomic::{AtomicBool, Ordering}; +use tokio::{net::TcpStream, sync::RwLock}; +use tokio_tungstenite::{client_async_tls_with_config, MaybeTlsStream, WebSocketStream}; +use tungstenite::{ + client::{uri_mode, IntoClientRequest}, + http::Request, + stream::Mode, + Message, +}; + +pub struct Network { + sock_recv: RwLock<SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>>, + sock_send: RwLock<SplitSink<WebSocketStream<MaybeTlsStream<TcpStream>>, Message>>, + use_bincode: AtomicBool, +} + +impl Network { + pub async fn connect(addr: &str) -> Result<Self> { + let (parts, _) = addr.into_client_request().unwrap().into_parts(); + let mut builder = Request::builder() + .uri(parts.uri.clone().clone()) + .method(parts.method.clone()) + .version(parts.version); + *builder.headers_mut().unwrap() = parts.headers.clone(); + let request = builder.body(()).unwrap(); + + let host = request.uri().host().unwrap(); + let host = if host.starts_with('[') { + &host[1..host.len() - 1] + } else { + host + }; + let port = request + .uri() + .port_u16() + .unwrap_or(match uri_mode(request.uri())? { + Mode::Plain => 27032, + Mode::Tls => 443, + }); + + info!("Connecting: host={host:?} port={port}"); + let stream = TcpStream::connect((host, port)).await?; + stream.set_nodelay(true).unwrap(); + let (sock, _) = client_async_tls_with_config(request, stream, None, None).await?; + info!("Handshake complete."); + let (sock_send, sock_recv) = sock.split(); + Ok(Self { + sock_recv: RwLock::new(sock_recv), + sock_send: RwLock::new(sock_send), + use_bincode: false.into(), + }) + } + + pub async fn receive(&self) -> anyhow::Result<Option<PacketC>> { + let mut g = self.sock_recv.write().await; + loop { + match g.try_next().await? { + Some(Message::Text(packet)) => match serde_json::from_str(&packet) { + Ok(packet) => { + debug!("<- {packet:?}"); + if let PacketC::Version { + minor, + major, + supports_bincode, + } = &packet + { + if *minor == VERSION.0 && *major == VERSION.1 && *supports_bincode { + info!("Binary protocol format enabled."); + self.use_bincode.store(true, Ordering::Relaxed); + } + } + return Ok(Some(packet)); + } + Err(e) => { + warn!("invalid json packet: {e:?}"); + } + }, + Some(Message::Binary(packet)) => { + match bincode::decode_from_slice(&packet, BINCODE_CONFIG) { + Ok((packet, _)) => { + debug!("<- {packet:?}"); + return Ok(Some(packet)); + } + Err(e) => { + warn!("invalid bincode packet: {e:?}"); + } + } + } + _ => (), + }; + } + } + pub async fn send(&self, packet: PacketS) -> anyhow::Result<()> { + debug!("-> {packet:?}"); + let mut g = self.sock_send.write().await; + if self.use_bincode.load(Ordering::Relaxed) { + g.send(Message::Binary( + bincode::encode_to_vec(&packet, BINCODE_CONFIG).unwrap(), + )) + .await?; + } else { + g.send(Message::Text(serde_json::to_string(&packet).unwrap())) + .await?; + } + Ok(()) + } +} |