separate networking, fix warnings

This commit is contained in:
griffi-gh 2023-03-13 01:56:33 +01:00
parent 6375b397e9
commit 4361eff76f
8 changed files with 110 additions and 93 deletions

View file

@ -1,4 +1,4 @@
use shipyard::{Component, EntityId, Unique, Workload, AllStoragesView}; use shipyard::{Component, EntityId, Unique, AllStoragesView};
use hashbrown::HashMap; use hashbrown::HashMap;
use nohash_hasher::BuildNoHashHasher; use nohash_hasher::BuildNoHashHasher;
use std::net::SocketAddr; use std::net::SocketAddr;

View file

@ -1,7 +1,6 @@
use shipyard::{World, Workload, IntoWorkload}; use shipyard::{World, Workload, IntoWorkload};
use std::{thread, time::Duration}; use std::{thread, time::Duration};
mod util;
mod config; mod config;
mod server; mod server;
mod client; mod client;

View file

@ -1,3 +0,0 @@
pub fn log_error(error: anyhow::Error) {
log::error!("{}", error);
}

View file

@ -1,24 +1,27 @@
use shipyard::{Unique, AllStoragesView, UniqueView, UniqueViewMut, Workload, IntoWorkload, EntitiesViewMut, Component, ViewMut, SystemModificator, View, IntoIter, WorkloadModificator}; use shipyard::{Unique, AllStoragesView, UniqueView, UniqueViewMut, Workload, IntoWorkload, EntitiesViewMut, Component, ViewMut, SystemModificator, View, IntoIter, WorkloadModificator};
use glium::glutin::event_loop::ControlFlow; use glium::glutin::event_loop::ControlFlow;
use std::net::SocketAddr; use std::net::SocketAddr;
use uflow::{client::{Client, Config as ClientConfig, Event as ClientEvent}, SendMode}; use uflow::client::{Client, Config as ClientConfig, Event as ClientEvent};
use lz4_flex::decompress_size_prepended; use kubi_shared::networking::{
use anyhow::{Result, Context}; messages::{ClientToServerMessage, ServerToClientMessage, S_SERVER_HELLO},
use kubi_shared::{
networking::{
messages::{ClientToServerMessage, ServerToClientMessage, S_SERVER_HELLO, S_CHUNK_RESPONSE, S_QUEUE_BLOCK},
state::ClientJoinState, state::ClientJoinState,
channels::{CHANNEL_AUTH, CHANNEL_BLOCK}, channels::CHANNEL_AUTH,
},
queue::QueuedBlock
}; };
use crate::{ use crate::{
events::{EventComponent, player_actions::PlayerActionEvent}, events::EventComponent,
control_flow::SetControlFlow, control_flow::SetControlFlow,
world::{tasks::{ChunkTaskResponse, ChunkTaskManager}, queue::BlockUpdateQueue}, world::tasks::ChunkTaskManager,
state::is_ingame_or_loading state::is_ingame_or_loading
}; };
mod world;
use world::{
inject_network_responses_into_manager_queue,
send_block_place_events,
recv_block_place_events,
};
#[derive(Unique, Clone, Copy, PartialEq, Eq)] #[derive(Unique, Clone, Copy, PartialEq, Eq)]
pub enum GameType { pub enum GameType {
Singleplayer, Singleplayer,
@ -124,78 +127,6 @@ fn check_server_hello_response(
} }
} }
//TODO multithreaded decompression
fn decompress_chunk_packet(data: &Box<[u8]>) -> Result<ServerToClientMessage> {
let mut decompressed = decompress_size_prepended(&data[1..])?;
decompressed.insert(0, data[0]);
Ok(postcard::from_bytes(&decompressed).ok().context("Deserialization failed")?)
}
//TODO get rid of this, this is awfulll
fn inject_network_responses_into_manager_queue(
manager: UniqueView<ChunkTaskManager>,
events: View<NetworkEvent>
) {
for event in events.iter() {
if event.is_message_of_type::<S_CHUNK_RESPONSE>() {
let NetworkEvent(ClientEvent::Receive(data)) = &event else { unreachable!() };
let packet = decompress_chunk_packet(data).expect("Chunk decode failed");
let ServerToClientMessage::ChunkResponse {
chunk, data, queued
} = packet else { unreachable!() };
manager.add_sussy_response(ChunkTaskResponse::LoadedChunk {
position: chunk,
chunk_data: data,
queued
});
}
}
}
fn send_block_place_events(
action_events: View<PlayerActionEvent>,
mut client: UniqueViewMut<UdpClient>,
) {
for event in action_events.iter() {
let PlayerActionEvent::UpdatedBlock { position, block } = event else {
continue
};
client.0.send(
postcard::to_allocvec(&ClientToServerMessage::QueueBlock {
item: QueuedBlock {
position: *position,
block_type: *block,
soft: false
}
}).unwrap().into_boxed_slice(),
CHANNEL_BLOCK,
SendMode::Reliable,
);
}
}
fn recv_block_place_events(
mut queue: UniqueViewMut<BlockUpdateQueue>,
network_events: View<NetworkEvent>,
) {
for event in network_events.iter() {
let ClientEvent::Receive(data) = &event.0 else {
continue
};
if !event.is_message_of_type::<S_QUEUE_BLOCK>() {
continue
}
let Ok(parsed_message) = postcard::from_bytes(data) else {
log::error!("Malformed message");
continue
};
let ServerToClientMessage::QueueBlock { item } = parsed_message else {
unreachable!()
};
queue.push(item);
}
}
pub fn update_networking() -> Workload { pub fn update_networking() -> Workload {
( (
connect_client.run_if_missing_unique::<UdpClient>(), connect_client.run_if_missing_unique::<UdpClient>(),

View file

@ -0,0 +1,91 @@
use shipyard::{UniqueView, UniqueViewMut, View, IntoIter};
use uflow::{client::Event as ClientEvent, SendMode};
use lz4_flex::decompress_size_prepended;
use anyhow::{Result, Context};
use kubi_shared::{
networking::{
messages::{ClientToServerMessage, ServerToClientMessage, S_CHUNK_RESPONSE, S_QUEUE_BLOCK},
channels::CHANNEL_BLOCK,
},
queue::QueuedBlock
};
use crate::{
events::player_actions::PlayerActionEvent,
world::{
tasks::{ChunkTaskResponse, ChunkTaskManager},
queue::BlockUpdateQueue
},
};
use super::{NetworkEvent, UdpClient};
//TODO multithreaded decompression
fn decompress_chunk_packet(data: &Box<[u8]>) -> Result<ServerToClientMessage> {
let mut decompressed = decompress_size_prepended(&data[1..])?;
decompressed.insert(0, data[0]);
Ok(postcard::from_bytes(&decompressed).ok().context("Deserialization failed")?)
}
//TODO get rid of this, this is awfulll
pub fn inject_network_responses_into_manager_queue(
manager: UniqueView<ChunkTaskManager>,
events: View<NetworkEvent>
) {
for event in events.iter() {
if event.is_message_of_type::<S_CHUNK_RESPONSE>() {
let NetworkEvent(ClientEvent::Receive(data)) = &event else { unreachable!() };
let packet = decompress_chunk_packet(data).expect("Chunk decode failed");
let ServerToClientMessage::ChunkResponse {
chunk, data, queued
} = packet else { unreachable!() };
manager.add_sussy_response(ChunkTaskResponse::LoadedChunk {
position: chunk,
chunk_data: data,
queued
});
}
}
}
pub fn send_block_place_events(
action_events: View<PlayerActionEvent>,
mut client: UniqueViewMut<UdpClient>,
) {
for event in action_events.iter() {
let PlayerActionEvent::UpdatedBlock { position, block } = event else {
continue
};
client.0.send(
postcard::to_allocvec(&ClientToServerMessage::QueueBlock {
item: QueuedBlock {
position: *position,
block_type: *block,
soft: false
}
}).unwrap().into_boxed_slice(),
CHANNEL_BLOCK,
SendMode::Reliable,
);
}
}
pub fn recv_block_place_events(
mut queue: UniqueViewMut<BlockUpdateQueue>,
network_events: View<NetworkEvent>,
) {
for event in network_events.iter() {
let ClientEvent::Receive(data) = &event.0 else {
continue
};
if !event.is_message_of_type::<S_QUEUE_BLOCK>() {
continue
}
let Ok(parsed_message) = postcard::from_bytes(data) else {
log::error!("Malformed message");
continue
};
let ServerToClientMessage::QueueBlock { item } = parsed_message else {
unreachable!()
};
queue.push(item);
}
}

View file

@ -1,4 +1,4 @@
use shipyard::{Component, AllStoragesViewMut, View, IntoIter}; use shipyard::{Component, AllStoragesViewMut};
use crate::{ use crate::{
transform::Transform, transform::Transform,
camera::Camera, camera::Camera,
@ -15,7 +15,7 @@ pub fn spawn_player (
mut storages: AllStoragesViewMut mut storages: AllStoragesViewMut
) { ) {
log::info!("spawning player"); log::info!("spawning player");
let entity_id = storages.add_entity(( storages.add_entity((
Player, Player,
MainPlayer, MainPlayer,
Transform::default(), Transform::default(),

View file

@ -1,6 +1,6 @@
use shipyard::{NonSendSync, UniqueView, Unique, AllStoragesView}; use shipyard::{NonSendSync, UniqueView, Unique, AllStoragesView};
use glium::{texture::{SrgbTexture2dArray, MipmapsOption}, Program}; use glium::{texture::{SrgbTexture2dArray, MipmapsOption}, Program};
use kubi_shared::block::{Block, BlockTexture}; use kubi_shared::block::BlockTexture;
use crate::rendering::Renderer; use crate::rendering::Renderer;
mod texture; mod texture;

View file

@ -2,7 +2,6 @@ use glam::{IVec3, ivec3};
use glium::{VertexBuffer, IndexBuffer, index::PrimitiveType}; use glium::{VertexBuffer, IndexBuffer, index::PrimitiveType};
use kubi_shared::networking::messages::ClientToServerMessage; use kubi_shared::networking::messages::ClientToServerMessage;
use shipyard::{View, UniqueView, UniqueViewMut, IntoIter, Workload, IntoWorkload, NonSendSync, track}; use shipyard::{View, UniqueView, UniqueViewMut, IntoIter, Workload, IntoWorkload, NonSendSync, track};
use kubi_shared::queue::QueuedBlock;
use uflow::SendMode; use uflow::SendMode;
use crate::{ use crate::{
player::MainPlayer, player::MainPlayer,