diff options
Diffstat (limited to 'server')
-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 | 148 | ||||
-rw-r--r-- | server/client-lib/src/network/tokio.rs | 131 |
7 files changed, 302 insertions, 2 deletions
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/server/client-lib/src/network/sync.rs b/server/client-lib/src/network/sync.rs new file mode 100644 index 00000000..e8aa08de --- /dev/null +++ b/server/client-lib/src/network/sync.rs @@ -0,0 +1,148 @@ +/* + 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 hurrycurry_protocol::{PacketC, PacketS, BINCODE_CONFIG, VERSION}; +use log::{debug, info, warn}; +use std::{collections::VecDeque, net::TcpStream}; +use tungstenite::{ + client::{uri_mode, IntoClientRequest}, + client_tls_with_config, + handshake::client::Request, + stream::{MaybeTlsStream, Mode}, + util::NonBlockingError, + Message, WebSocket, +}; + +pub struct Network { + sock: WebSocket<MaybeTlsStream<TcpStream>>, + pub queue_in: VecDeque<PacketC>, + pub queue_out: VecDeque<PacketS>, + use_bincode: bool, +} + +impl Network { + pub 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))?; + stream.set_nodelay(true).unwrap(); + + let (mut sock, _) = client_tls_with_config(request, stream, None, None).unwrap(); + + match sock.get_mut() { + MaybeTlsStream::Plain(s) => s.set_nonblocking(true).unwrap(), + MaybeTlsStream::Rustls(s) => s.sock.set_nonblocking(true).unwrap(), + _ => todo!(), + }; + + info!("Handshake complete."); + Ok(Self { + sock, + use_bincode: false, + queue_in: VecDeque::new(), + queue_out: VecDeque::new(), + }) + } + + pub fn poll(&mut self) { + loop { + self.queue_in.extend(match self.sock.read() { + Ok(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 = true; + } + } + Some(packet) + } + Err(e) => { + warn!("invalid json packet: {e:?}"); + None + } + }, + Ok(Message::Binary(packet)) => { + match bincode::decode_from_slice(&packet, BINCODE_CONFIG) { + Ok((packet, _)) => { + debug!("<- {packet:?}"); + Some(packet) + } + Err(e) => { + warn!("invalid bincode packet: {e:?}"); + None + } + } + } + Ok(_) => None, + Err(e) => { + if let Some(e) = e.into_non_blocking() { + warn!("{e:?}"); + None + } else { + break; + } + } + }); + } + + for packet in self.queue_out.drain(..) { + debug!("-> {packet:?}"); + if self.use_bincode { + self.sock + .write(Message::Binary( + bincode::encode_to_vec(&packet, BINCODE_CONFIG).unwrap(), + )) + .unwrap(); + } else { + self.sock + .write(Message::Text(serde_json::to_string(&packet).unwrap())) + .unwrap(); + } + } + + self.sock.flush().unwrap(); + } +} 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(()) + } +} |