create net lib

This commit is contained in:
griffi-gh 2023-02-01 03:16:23 +01:00
parent dfebd1d5a1
commit 060533f831
11 changed files with 82 additions and 29 deletions

View file

@ -1,5 +1,5 @@
[workspace] [workspace]
members = ["kubi", "kubi-server", "kubi-shared"] members = ["kubi", "kubi-server", "kubi-shared", "kubi-udp"]
resolver = "2" resolver = "2"
[profile.dev] [profile.dev]

View file

@ -11,4 +11,3 @@ strum = { version = "0.24", features = ["derive"] }
bincode = "2.0.0-rc" bincode = "2.0.0-rc"
anyhow = "1.0" anyhow = "1.0"
bracket-noise = "0.8" bracket-noise = "0.8"
#laminar = "0.5.0"

View file

@ -2,8 +2,3 @@ pub mod blocks;
pub mod networking; pub mod networking;
pub mod worldgen; pub mod worldgen;
pub mod chunk; pub mod chunk;
pub(crate) const BINCODE_CONFIG: bincode::config::Configuration<bincode::config::LittleEndian, bincode::config::Varint, bincode::config::SkipFixedArrayLength> = bincode::config::standard()
.with_little_endian()
.with_variable_int_encoding()
.skip_fixed_array_length();

View file

@ -1,3 +1 @@
pub mod messages; pub mod messages;
pub mod client;
pub mod server;

View file

@ -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<Self> {
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(())
}
}

10
kubi-udp/Cargo.toml Normal file
View file

@ -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"

36
kubi-udp/src/client.rs Normal file
View file

@ -0,0 +1,36 @@
use std::{
net::{UdpSocket, SocketAddr},
marker::PhantomData
};
use bincode::{Encode, Decode};
use crate::{BINCODE_CONFIG, packet::ClientPacket};
pub struct Client<T> where T: Encode + Decode {
socket: UdpSocket,
_marker: PhantomData<T>
}
impl<T> Client<T> where T: Encode + Decode {
pub fn connect(addr: SocketAddr) -> anyhow::Result<Self> {
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<T>) -> 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(())
}
}

11
kubi-udp/src/lib.rs Normal file
View file

@ -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::LittleEndian, bincode::config::Varint, bincode::config::SkipFixedArrayLength> = bincode::config::standard()
.with_little_endian()
.with_variable_int_encoding()
.skip_fixed_array_length();

10
kubi-udp/src/packet.rs Normal file
View file

@ -0,0 +1,10 @@
use bincode::{Encode, Decode};
#[repr(u8)]
#[derive(Encode, Decode)]
pub enum ClientPacket<T> where T: Encode + Decode {
Connect,
Disconnect,
Heartbeat,
Data(T)
}

14
kubi-udp/src/server.rs Normal file
View file

@ -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<Self> {
let socket = UdpSocket::bind(addr)?;
socket.set_nonblocking(true)?;
socket.set_broadcast(true)?;
Ok(Self { socket })
}
}