diff --git a/Cargo.toml b/Cargo.toml index 078f0ba..c0cf813 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["kubi", "kubi-server", "kubi-shared"] +members = ["kubi", "kubi-server", "kubi-shared", "kubi-udp"] resolver = "2" [profile.dev] diff --git a/kubi-shared/Cargo.toml b/kubi-shared/Cargo.toml index d3d65bb..c42eb3b 100644 --- a/kubi-shared/Cargo.toml +++ b/kubi-shared/Cargo.toml @@ -11,4 +11,3 @@ strum = { version = "0.24", features = ["derive"] } bincode = "2.0.0-rc" anyhow = "1.0" bracket-noise = "0.8" -#laminar = "0.5.0" diff --git a/kubi-shared/src/lib.rs b/kubi-shared/src/lib.rs index 8fb7339..dd0e7a5 100644 --- a/kubi-shared/src/lib.rs +++ b/kubi-shared/src/lib.rs @@ -2,8 +2,3 @@ pub mod blocks; pub mod networking; pub mod worldgen; pub mod chunk; - -pub(crate) const BINCODE_CONFIG: bincode::config::Configuration = bincode::config::standard() - .with_little_endian() - .with_variable_int_encoding() - .skip_fixed_array_length(); diff --git a/kubi-shared/src/networking.rs b/kubi-shared/src/networking.rs index 8a4e7ed..ba63992 100644 --- a/kubi-shared/src/networking.rs +++ b/kubi-shared/src/networking.rs @@ -1,3 +1 @@ pub mod messages; -pub mod client; -pub mod server; diff --git a/kubi-shared/src/networking/client.rs b/kubi-shared/src/networking/client.rs deleted file mode 100644 index 82d0ddd..0000000 --- a/kubi-shared/src/networking/client.rs +++ /dev/null @@ -1,20 +0,0 @@ -use std::net::{UdpSocket, SocketAddr}; -use super::messages::ClientToServerMessage; - -use crate::BINCODE_CONFIG; - -pub struct Client { - socket: UdpSocket -} -impl Client { - pub fn bind(addr: SocketAddr) -> anyhow::Result { - Ok(Self { - socket: UdpSocket::bind(addr)? - }) - } - pub fn send(&self, message: ClientToServerMessage) -> anyhow::Result<()> { - let bytes = bincode::encode_to_vec(message, BINCODE_CONFIG)?; - self.socket.send(&bytes)?; - Ok(()) - } -} diff --git a/kubi-shared/src/networking/server.rs b/kubi-shared/src/networking/server.rs deleted file mode 100644 index e69de29..0000000 diff --git a/kubi-udp/Cargo.toml b/kubi-udp/Cargo.toml new file mode 100644 index 0000000..8a64827 --- /dev/null +++ b/kubi-udp/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "kubi-udp" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +bincode = "2.0.0-rc" +anyhow = "1.0" diff --git a/kubi-udp/src/client.rs b/kubi-udp/src/client.rs new file mode 100644 index 0000000..e98a8b9 --- /dev/null +++ b/kubi-udp/src/client.rs @@ -0,0 +1,36 @@ +use std::{ + net::{UdpSocket, SocketAddr}, + marker::PhantomData +}; +use bincode::{Encode, Decode}; +use crate::{BINCODE_CONFIG, packet::ClientPacket}; + +pub struct Client where T: Encode + Decode { + socket: UdpSocket, + _marker: PhantomData +} +impl Client where T: Encode + Decode { + pub fn connect(addr: SocketAddr) -> anyhow::Result { + let bind_addr: SocketAddr = "127.0.0.1:0".parse().unwrap(); + let socket = UdpSocket::bind(bind_addr)?; + socket.set_nonblocking(true)?; + socket.connect(addr)?; + Ok(Self { + socket, + _marker: PhantomData + }) + } + fn send_packet(&self, packet: &ClientPacket) -> anyhow::Result<()> { + let bytes = bincode::encode_to_vec(packet, BINCODE_CONFIG)?; + self.socket.send(&bytes)?; + Ok(()) + } + pub fn send(&self, message: T) -> anyhow::Result<()> { + self.send_packet(&ClientPacket::Data(message))?; + Ok(()) + } + pub fn disconnect(&self) -> anyhow::Result<()> { + self.send_packet(&ClientPacket::Disconnect)?; + Ok(()) + } +} diff --git a/kubi-udp/src/lib.rs b/kubi-udp/src/lib.rs new file mode 100644 index 0000000..caf5110 --- /dev/null +++ b/kubi-udp/src/lib.rs @@ -0,0 +1,11 @@ +pub(crate) mod client; +pub(crate) mod server; +pub(crate) mod packet; + +pub use server::Server; +pub use client::Client; + +pub(crate) const BINCODE_CONFIG: bincode::config::Configuration = bincode::config::standard() + .with_little_endian() + .with_variable_int_encoding() + .skip_fixed_array_length(); diff --git a/kubi-udp/src/packet.rs b/kubi-udp/src/packet.rs new file mode 100644 index 0000000..49ca38d --- /dev/null +++ b/kubi-udp/src/packet.rs @@ -0,0 +1,10 @@ +use bincode::{Encode, Decode}; + +#[repr(u8)] +#[derive(Encode, Decode)] +pub enum ClientPacket where T: Encode + Decode { + Connect, + Disconnect, + Heartbeat, + Data(T) +} diff --git a/kubi-udp/src/server.rs b/kubi-udp/src/server.rs new file mode 100644 index 0000000..9935fc3 --- /dev/null +++ b/kubi-udp/src/server.rs @@ -0,0 +1,14 @@ +use std::net::{UdpSocket, SocketAddr}; +use crate::BINCODE_CONFIG; + +pub struct Server { + socket: UdpSocket, +} +impl Server { + pub fn bind(addr: SocketAddr) -> anyhow::Result { + let socket = UdpSocket::bind(addr)?; + socket.set_nonblocking(true)?; + socket.set_broadcast(true)?; + Ok(Self { socket }) + } +}