Compare commits

...

3 commits

Author SHA1 Message Date
griffi-gh 9f5b974f82 increase small 2024-02-01 16:21:10 +01:00
griffi-gh bfdd506ae8 rename guiv2_integration -> kubi_ui_integration 2024-02-01 16:12:51 +01:00
griffi-gh 3a42c293dd refactor net code to use enums 2024-02-01 16:00:32 +01:00
12 changed files with 136 additions and 88 deletions

View file

@ -8,10 +8,10 @@ use kubi_shared::{
ServerToClientMessage,
InitData,
ClientInitData,
C_CLIENT_HELLO,
ClientToServerMessageType,
},
client::{Client, ClientId, Username},
channels::{CHANNEL_AUTH, CHANNEL_SYS_EVT},
channels::Channel,
},
player::{Player, PLAYER_HEALTH},
transform::Transform, entity::{Entity, Health}
@ -37,7 +37,7 @@ pub fn authenticate_players(
let ServerEvent::Receive(client_addr, data) = event else{
continue
};
if !event.is_message_of_type::<C_CLIENT_HELLO>() {
if !event.is_message_of_type::<{ClientToServerMessageType::ClientHello as u8}>() {
continue
}
let Some(client) = server.0.client(client_addr) else {
@ -62,7 +62,7 @@ pub fn authenticate_players(
postcard::to_allocvec(&ServerToClientMessage::ServerFuckOff {
reason: "Incorrect password".into()
}).unwrap().into_boxed_slice(),
CHANNEL_AUTH,
Channel::Auth as usize,
SendMode::Reliable
);
continue
@ -72,7 +72,7 @@ pub fn authenticate_players(
postcard::to_allocvec(&ServerToClientMessage::ServerFuckOff {
reason: "This server is password protected".into()
}).unwrap().into_boxed_slice(),
CHANNEL_AUTH,
Channel::Auth as usize,
SendMode::Reliable
);
continue
@ -88,7 +88,7 @@ pub fn authenticate_players(
postcard::to_allocvec(&ServerToClientMessage::ServerFuckOff {
reason: "Can't find a free spot for you!".into()
}).unwrap().into_boxed_slice(),
CHANNEL_AUTH,
Channel::Auth as usize,
SendMode::Reliable
);
continue
@ -164,7 +164,7 @@ pub fn authenticate_players(
};
other_client.borrow_mut().send(
postcard::to_allocvec(&message).unwrap().into_boxed_slice(),
CHANNEL_SYS_EVT,
Channel::SysEvt as usize,
SendMode::Reliable
);
}
@ -175,7 +175,7 @@ pub fn authenticate_players(
postcard::to_allocvec(&ServerToClientMessage::ServerHello {
init: init_data
}).unwrap().into_boxed_slice(),
CHANNEL_AUTH,
Channel::Auth as usize,
SendMode::Reliable
);

View file

@ -6,8 +6,8 @@ use std::net::SocketAddr;
use kubi_shared::{
networking::{
client::{ClientIdMap, Client},
messages::{ClientToServerMessage, ServerToClientMessage, C_POSITION_CHANGED},
channels::CHANNEL_MOVE
messages::{ClientToServerMessage, ServerToClientMessage, ClientToServerMessageType},
channels::Channel
},
transform::Transform
};
@ -41,9 +41,10 @@ pub fn sync_client_positions(
addrs: View<ClientAddress>,
) {
for event in &events.0 {
let Some(message) = check_message_auth::<C_POSITION_CHANGED>(&server, event, &clients, &addr_map) else {
continue;
};
let Some(message) = check_message_auth
::<{ClientToServerMessageType::PositionChanged as u8}>
(&server, event, &clients, &addr_map) else { continue };
let ClientToServerMessage::PositionChanged { position, velocity: _, direction } = message.message else {
unreachable!()
};
@ -72,7 +73,7 @@ pub fn sync_client_positions(
direction
}
).unwrap().into_boxed_slice(),
CHANNEL_MOVE,
Channel::Move as usize,
SendMode::Reliable
);
}

View file

@ -2,8 +2,8 @@ use shipyard::{Unique, UniqueView, UniqueViewMut, Workload, IntoWorkload, AllSto
use glam::IVec3;
use hashbrown::HashMap;
use kubi_shared::networking::{
messages::{ClientToServerMessage, ServerToClientMessage, C_CHUNK_SUB_REQUEST, C_QUEUE_BLOCK},
channels::{CHANNEL_WORLD, CHANNEL_BLOCK},
messages::{ClientToServerMessage, ServerToClientMessage, ClientToServerMessageType},
channels::Channel,
client::Client,
};
use uflow::{server::RemoteClient, SendMode};
@ -45,7 +45,11 @@ pub fn send_chunk_compressed(
ser_message.truncate(1);
ser_message.append(&mut compressed);
let ser_message = ser_message.into_boxed_slice();
client.borrow_mut().send(ser_message, CHANNEL_WORLD, SendMode::Reliable);
client.borrow_mut().send(
ser_message,
Channel::World as usize,
SendMode::Reliable
);
Ok(())
}
@ -59,9 +63,10 @@ fn process_chunk_requests(
clients: View<Client>
) {
for event in &events.0 {
let Some(message) = check_message_auth::<C_CHUNK_SUB_REQUEST>(&server, event, &clients, &addr_map) else {
continue;
};
let Some(message) = check_message_auth
::<{ClientToServerMessageType::ChunkSubRequest as u8}>
(&server, event, &clients, &addr_map) else { continue };
let ClientToServerMessage::ChunkSubRequest { chunk: chunk_position } = message.message else {
unreachable!()
};
@ -151,9 +156,10 @@ fn process_block_queue_messages(
addrs: View<ClientAddress>,
) {
for event in &events.0 {
let Some(message) = check_message_auth::<C_QUEUE_BLOCK>(&server, event, &clients, &addr_map) else {
continue;
};
let Some(message) = check_message_auth
::<{ClientToServerMessageType::QueueBlock as u8}>
(&server, event, &clients, &addr_map) else { continue };
let ClientToServerMessage::QueueBlock { item } = message.message else { unreachable!() };
//TODO place in our own queue, for now just send to other clients
log::info!("Placed block {:?} at {}", item.block_type, item.position);
@ -172,7 +178,7 @@ fn process_block_queue_messages(
postcard::to_allocvec(
&ServerToClientMessage::QueueBlock { item }
).unwrap().into_boxed_slice(),
CHANNEL_BLOCK,
Channel::Block as usize,
SendMode::Reliable,
);
}

View file

@ -1,6 +1,9 @@
pub const CHANNEL_GENERIC: usize = 0;
pub const CHANNEL_AUTH: usize = 1;
pub const CHANNEL_WORLD: usize = 2;
pub const CHANNEL_BLOCK: usize = 3;
pub const CHANNEL_MOVE: usize = 4;
pub const CHANNEL_SYS_EVT: usize = 5;
#[repr(u8)]
pub enum Channel {
Generic = 0,
Auth = 1,
World = 2,
Block = 3,
Move = 4,
SysEvt = 5,
}

View file

@ -5,60 +5,86 @@ use super::client::ClientId;
pub const PROTOCOL_ID: u16 = 0;
pub const C_CLIENT_HELLO: u8 = 0;
pub const C_POSITION_CHANGED: u8 = 1;
pub const C_CHUNK_SUB_REQUEST: u8 = 2;
pub const C_CHUNK_UNUBSCRIBE: u8 = 3;
pub const C_QUEUE_BLOCK: u8 = 4;
pub trait ToMessageType<T> {
fn message_type(&self) -> T;
}
#[repr(u8)]
#[non_exhaustive]
pub enum ClientToServerMessageType {
ClientHello = 0,
PositionChanged = 1,
ChunkSubRequest = 2,
ChunkUnsubscribe = 3,
QueueBlock = 4,
}
#[derive(Serialize, Deserialize, Clone)]
#[repr(u8)]
#[non_exhaustive]
pub enum ClientToServerMessage {
ClientHello {
username: String,
password: Option<String>,
} = C_CLIENT_HELLO,
} = ClientToServerMessageType::ClientHello as u8,
PositionChanged {
position: Vec3,
velocity: Vec3,
direction: Quat,
} = C_POSITION_CHANGED,
} = ClientToServerMessageType::PositionChanged as u8,
ChunkSubRequest {
chunk: IVec3,
} = C_CHUNK_SUB_REQUEST,
} = ClientToServerMessageType::ChunkSubRequest as u8,
ChunkUnsubscribe {
chunk: IVec3,
} = C_CHUNK_UNUBSCRIBE,
} = ClientToServerMessageType::ChunkUnsubscribe as u8,
QueueBlock {
item: QueuedBlock
} = C_QUEUE_BLOCK,
} = ClientToServerMessageType::QueueBlock as u8,
}
pub const S_SERVER_HELLO: u8 = 0;
pub const S_SERVER_FUCK_OFF: u8 = 1;
pub const S_PLAYER_POSITION_CHANGED: u8 = 2;
pub const S_CHUNK_RESPONSE: u8 = 3;
pub const S_QUEUE_BLOCK: u8 = 4;
pub const S_PLAYER_CONNECTED: u8 = 5;
pub const S_PLAYER_DISCONNECTED: u8 = 6;
impl ToMessageType<ClientToServerMessageType> for ClientToServerMessage {
fn message_type(&self) -> ClientToServerMessageType {
match self {
ClientToServerMessage::ClientHello { .. } => ClientToServerMessageType::ClientHello,
ClientToServerMessage::PositionChanged { .. } => ClientToServerMessageType::PositionChanged,
ClientToServerMessage::ChunkSubRequest { .. } => ClientToServerMessageType::ChunkSubRequest,
ClientToServerMessage::ChunkUnsubscribe { .. } => ClientToServerMessageType::ChunkUnsubscribe,
ClientToServerMessage::QueueBlock { .. } => ClientToServerMessageType::QueueBlock,
}
}
}
#[repr(u8)]
#[non_exhaustive]
pub enum ServerToClientMessageType {
ServerHello = 0,
ServerFuckOff = 1,
PlayerPositionChanged = 2,
ChunkResponse = 3,
QueueBlock = 4,
PlayerConnected = 5,
PlayerDisconnected = 6,
}
#[serde_with::serde_as]
#[derive(Serialize, Deserialize, Clone)]
#[repr(u8)]
#[non_exhaustive]
pub enum ServerToClientMessage {
ServerHello {
init: InitData
} = S_SERVER_HELLO,
} = ServerToClientMessageType::ServerHello as u8,
ServerFuckOff {
reason: String,
} = S_SERVER_FUCK_OFF,
} = ServerToClientMessageType::ServerFuckOff as u8,
PlayerPositionChanged {
client_id: ClientId,
position: Vec3,
direction: Quat,
} = S_PLAYER_POSITION_CHANGED,
} = ServerToClientMessageType::PlayerPositionChanged as u8,
///## WARNING: THIS IS COMPRESSED
///MESSAGES OF THIS TYPE ARE FULLY
@ -69,19 +95,33 @@ pub enum ServerToClientMessage {
#[serde_as(as = "Box<[[[_; CHUNK_SIZE]; CHUNK_SIZE]; CHUNK_SIZE]>")]
data: BlockData,
queued: Vec<QueuedBlock>,
} = S_CHUNK_RESPONSE,
} = ServerToClientMessageType::ChunkResponse as u8,
QueueBlock {
item: QueuedBlock
} = S_QUEUE_BLOCK,
} = ServerToClientMessageType::QueueBlock as u8,
PlayerConnected {
init: ClientInitData
} = S_PLAYER_CONNECTED,
} = ServerToClientMessageType::PlayerConnected as u8,
PlayerDisconnected {
id: ClientId
} = S_PLAYER_DISCONNECTED,
} = ServerToClientMessageType::PlayerDisconnected as u8,
}
impl ToMessageType<ServerToClientMessageType> for ServerToClientMessage {
fn message_type(&self) -> ServerToClientMessageType {
match self {
ServerToClientMessage::ServerHello { .. } => ServerToClientMessageType::ServerHello,
ServerToClientMessage::ServerFuckOff { .. } => ServerToClientMessageType::ServerFuckOff,
ServerToClientMessage::PlayerPositionChanged { .. } => ServerToClientMessageType::PlayerPositionChanged,
ServerToClientMessage::ChunkResponse { .. } => ServerToClientMessageType::ChunkResponse,
ServerToClientMessage::QueueBlock { .. } => ServerToClientMessageType::QueueBlock,
ServerToClientMessage::PlayerConnected { .. } => ServerToClientMessageType::PlayerConnected,
ServerToClientMessage::PlayerDisconnected { .. } => ServerToClientMessageType::PlayerDisconnected,
}
}
}
//---

View file

@ -29,7 +29,7 @@ pub(crate) mod delta_time;
pub(crate) mod cursor_lock;
pub(crate) mod control_flow;
pub(crate) mod state;
pub(crate) mod guiv2_integration;
pub(crate) mod kubi_ui_integration;
pub(crate) mod networking;
pub(crate) mod init;
pub(crate) mod color;
@ -76,7 +76,7 @@ use control_flow::{exit_on_esc, insert_control_flow_unique, RequestExit};
use state::{is_ingame, is_ingame_or_loading, is_loading, init_state, update_state, is_connecting};
use networking::{update_networking, update_networking_late, is_multiplayer, disconnect_on_exit, is_singleplayer};
use init::initialize_from_args;
use guiv2_integration::{kubi_ui_init, kubi_ui_begin, kubi_ui_end, kubi_ui_draw};
use kubi_ui_integration::{kubi_ui_init, kubi_ui_begin, kubi_ui_end, kubi_ui_draw};
use loading_screen::update_loading_screen;
use connecting_screen::switch_to_loading_if_connected;
use fixed_timestamp::init_fixed_timestamp_storage;

View file

@ -6,7 +6,7 @@ use crate::{
state::{GameState, NextState},
rendering::WindowSize,
input::RawKbmInputState,
guiv2_integration::UiState,
kubi_ui_integration::UiState,
};
fn render_progressbar(

View file

@ -1,11 +1,9 @@
use shipyard::{UniqueViewMut, View, IntoIter, AllStoragesViewMut};
use uflow::{client::Event as ClientEvent, SendMode};
use kubi_shared::{
networking::{
messages::{ClientToServerMessage, ServerToClientMessage, S_SERVER_HELLO},
use kubi_shared::networking::{
messages::{ClientToServerMessage, ServerToClientMessage, ServerToClientMessageType},
state::ClientJoinState,
channels::CHANNEL_AUTH,
},
channels::Channel,
};
use crate::player::{spawn_local_player_multiplayer, spawn_remote_player_multiplayer};
use super::{UdpClient, NetworkEvent};
@ -27,7 +25,7 @@ pub fn say_hello(
postcard::to_allocvec(
&ClientToServerMessage::ClientHello { username, password }
).unwrap().into_boxed_slice(),
CHANNEL_AUTH,
Channel::Auth as usize,
SendMode::Reliable
);
}
@ -40,7 +38,7 @@ pub fn check_server_hello_response(
let ClientEvent::Receive(data) = &event.0 else {
return None
};
if !event.is_message_of_type::<S_SERVER_HELLO>() {
if !event.is_message_of_type::<{ServerToClientMessageType::ServerHello as u8}>() {
return None
}
let Ok(parsed_message) = postcard::from_bytes(data) else {

View file

@ -4,8 +4,8 @@ use uflow::{SendMode, client::Event as ClientEvent};
use kubi_shared::{
transform::Transform,
networking::{
messages::{ClientToServerMessage, ServerToClientMessage, S_PLAYER_POSITION_CHANGED, S_PLAYER_CONNECTED},
channels::CHANNEL_MOVE,
messages::{ClientToServerMessage, ServerToClientMessage, ServerToClientMessageType},
channels::Channel,
client::ClientIdMap,
},
};
@ -35,7 +35,7 @@ pub fn send_player_movement_events(
velocity: Vec3::ZERO,
direction: *direction
}).unwrap().into_boxed_slice(),
CHANNEL_MOVE,
Channel::Move as usize,
SendMode::TimeSensitive
);
}
@ -51,7 +51,7 @@ pub fn receive_player_movement_events(
continue
};
if !event.is_message_of_type::<S_PLAYER_POSITION_CHANGED>() {
if !event.is_message_of_type::<{ServerToClientMessageType::PlayerPositionChanged as u8}>() {
continue
}
@ -83,7 +83,7 @@ pub fn receive_player_connect_events(
let ClientEvent::Receive(data) = &event.0 else {
return None
};
if !event.is_message_of_type::<S_PLAYER_CONNECTED>() {
if !event.is_message_of_type::<{ServerToClientMessageType::PlayerConnected as u8}>() {
return None
};
let Ok(parsed_message) = postcard::from_bytes(data) else {

View file

@ -4,8 +4,8 @@ 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,
messages::{ClientToServerMessage, ServerToClientMessage, ServerToClientMessageType},
channels::Channel,
},
queue::QueuedBlock
};
@ -31,7 +31,7 @@ pub fn inject_network_responses_into_manager_queue(
events: View<NetworkEvent>
) {
for event in events.iter() {
if event.is_message_of_type::<S_CHUNK_RESPONSE>() {
if event.is_message_of_type::<{ServerToClientMessageType::ChunkResponse as u8}>() {
let NetworkEvent(ClientEvent::Receive(data)) = &event else { unreachable!() };
let packet = decompress_chunk_packet(data).expect("Chunk decode failed");
let ServerToClientMessage::ChunkResponse {
@ -62,7 +62,7 @@ pub fn send_block_place_events(
soft: false
}
}).unwrap().into_boxed_slice(),
CHANNEL_BLOCK,
Channel::Block as usize,
SendMode::Reliable,
);
}
@ -76,7 +76,7 @@ pub fn recv_block_place_events(
let ClientEvent::Receive(data) = &event.0 else {
continue
};
if !event.is_message_of_type::<S_QUEUE_BLOCK>() {
if !event.is_message_of_type::<{ServerToClientMessageType::QueueBlock as u8}>() {
continue
}
let Ok(parsed_message) = postcard::from_bytes(data) else {

View file

@ -17,7 +17,7 @@ use super::{
primitives::cube::CubePrimitive,
};
const SMOL: f32 = 0.0001;
const SMOL: f32 = 0.0025;
pub fn render_selection_box(
lookat: View<LookingAtBlock>,