From 13c6fce6e5facfa541f4f12702a77ac4906e476b Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Thu, 18 May 2023 04:34:33 +0200 Subject: [PATCH] move handshake into separate module --- kubi-shared/src/networking/client.rs | 1 - kubi/src/networking.rs | 63 +++++----------------------- kubi/src/networking/handshake.rs | 56 +++++++++++++++++++++++++ kubi/src/networking/player.rs | 11 +++++ 4 files changed, 77 insertions(+), 54 deletions(-) create mode 100644 kubi/src/networking/handshake.rs diff --git a/kubi-shared/src/networking/client.rs b/kubi-shared/src/networking/client.rs index 39e42c7..ac4029a 100644 --- a/kubi-shared/src/networking/client.rs +++ b/kubi-shared/src/networking/client.rs @@ -1,7 +1,6 @@ use shipyard::Component; pub type ClientId = u16; -pub type ClientKey = u16; #[derive(Component, Clone, Copy, Debug)] pub struct Client(pub ClientId); diff --git a/kubi/src/networking.rs b/kubi/src/networking.rs index 0587656..618bb5e 100644 --- a/kubi/src/networking.rs +++ b/kubi/src/networking.rs @@ -1,23 +1,27 @@ -use glam::Vec3; use shipyard::{Unique, AllStoragesView, UniqueView, UniqueViewMut, Workload, IntoWorkload, EntitiesViewMut, Component, ViewMut, SystemModificator, View, IntoIter, WorkloadModificator}; use glium::glutin::event_loop::ControlFlow; use std::net::SocketAddr; -use uflow::{client::{Client, Config as ClientConfig, Event as ClientEvent}, EndpointConfig, SendMode}; +use uflow::{client::{Client, Config as ClientConfig, Event as ClientEvent}, EndpointConfig}; use kubi_shared::networking::{ - messages::{ClientToServerMessage, ServerToClientMessage, S_SERVER_HELLO}, - state::ClientJoinState, - channels::{CHANNEL_AUTH, CHANNEL_MOVE}, + messages::ServerToClientMessage, + state::ClientJoinState, }; use crate::{ - events::{EventComponent, player_actions::PlayerActionEvent}, + events::EventComponent, control_flow::SetControlFlow, world::tasks::ChunkTaskManager, state::is_ingame_or_loading }; +mod handshake; mod world; mod player; +use handshake::{ + set_client_join_state_to_connected, + say_hello, + check_server_hello_response +}; use world::{ inject_network_responses_into_manager_queue, send_block_place_events, @@ -89,53 +93,6 @@ fn flush_client( client.0.flush(); } -fn set_client_join_state_to_connected( - mut join_state: UniqueViewMut -) { - log::info!("Setting ClientJoinState"); - *join_state = ClientJoinState::Connected; -} - -fn say_hello( - mut client: UniqueViewMut, -) { - log::info!("Authenticating"); - client.0.send( - postcard::to_allocvec( - &ClientToServerMessage::ClientHello { - username: "Sbeve".into(), - password: None - } - ).unwrap().into_boxed_slice(), - CHANNEL_AUTH, - uflow::SendMode::Reliable - ); -} - -fn check_server_hello_response( - network_events: View, - mut join_state: UniqueViewMut -) { - for event in network_events.iter() { - let ClientEvent::Receive(data) = &event.0 else { - continue - }; - if !event.is_message_of_type::() { - continue - } - let Ok(parsed_message) = postcard::from_bytes(data) else { - log::error!("Malformed message"); - continue - }; - let ServerToClientMessage::ServerHello { init: _ } = parsed_message else { - unreachable!() - }; - //TODO handle init data - *join_state = ClientJoinState::Joined; - log::info!("Joined the server!"); - return; - } -} fn handle_disconnect( network_events: View, diff --git a/kubi/src/networking/handshake.rs b/kubi/src/networking/handshake.rs new file mode 100644 index 0000000..9066947 --- /dev/null +++ b/kubi/src/networking/handshake.rs @@ -0,0 +1,56 @@ +use shipyard::{UniqueViewMut, View, IntoIter}; +use uflow::{client::Event as ClientEvent, SendMode}; +use kubi_shared::networking::{ + messages::{ClientToServerMessage, ServerToClientMessage, S_SERVER_HELLO}, + state::ClientJoinState, + channels::CHANNEL_AUTH, +}; +use super::{UdpClient, NetworkEvent}; + +pub fn set_client_join_state_to_connected( + mut join_state: UniqueViewMut +) { + log::info!("Setting ClientJoinState"); + *join_state = ClientJoinState::Connected; +} + +pub fn say_hello( + mut client: UniqueViewMut, +) { + log::info!("Authenticating"); + client.0.send( + postcard::to_allocvec( + &ClientToServerMessage::ClientHello { + username: "Sbeve".into(), + password: None + } + ).unwrap().into_boxed_slice(), + CHANNEL_AUTH, + SendMode::Reliable + ); +} + +pub fn check_server_hello_response( + network_events: View, + mut join_state: UniqueViewMut +) { + for event in network_events.iter() { + let ClientEvent::Receive(data) = &event.0 else { + continue + }; + if !event.is_message_of_type::() { + continue + } + let Ok(parsed_message) = postcard::from_bytes(data) else { + log::error!("Malformed message"); + continue + }; + let ServerToClientMessage::ServerHello { init: _ } = parsed_message else { + unreachable!() + }; + //TODO handle init data + *join_state = ClientJoinState::Joined; + log::info!("Joined the server!"); + return; + } +} diff --git a/kubi/src/networking/player.rs b/kubi/src/networking/player.rs index b10b63b..9d70385 100644 --- a/kubi/src/networking/player.rs +++ b/kubi/src/networking/player.rs @@ -8,6 +8,10 @@ use kubi_shared::networking::{ use crate::events::player_actions::PlayerActionEvent; use super::{UdpClient, NetworkEvent}; +pub fn add_net_player() { + //TODO +} + pub fn send_player_movement_events( actions: View, mut client: UniqueViewMut, @@ -48,3 +52,10 @@ pub fn receive_player_movement_events( //TODO apply position to local player } } + + +pub fn receive_connected_players( + network_events: View, +) { + +}