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 /pixel-client | |
| parent | 0f6a824cb4c6d36dda485a939d9c640ec234d802 (diff) | |
| download | hurrycurry-f4499b7a0e808d84157e6148afef4f844a7007cf.tar hurrycurry-f4499b7a0e808d84157e6148afef4f844a7007cf.tar.bz2 hurrycurry-f4499b7a0e808d84157e6148afef4f844a7007cf.tar.zst  | |
factor out client network code to client-lib
Diffstat (limited to 'pixel-client')
| -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-- | pixel-client/src/network.rs | 148 | 
5 files changed, 6 insertions, 152 deletions
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/pixel-client/src/network.rs b/pixel-client/src/network.rs deleted file mode 100644 index e8aa08de..00000000 --- a/pixel-client/src/network.rs +++ /dev/null @@ -1,148 +0,0 @@ -/* -    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(); -    } -}  |