mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-11-25 16:28:42 -06:00
create net lib
This commit is contained in:
parent
a7a20093d6
commit
728c468a70
|
@ -1,5 +1,5 @@
|
|||
[workspace]
|
||||
members = ["kubi", "kubi-server", "kubi-shared"]
|
||||
members = ["kubi", "kubi-server", "kubi-shared", "kubi-udp"]
|
||||
resolver = "2"
|
||||
|
||||
[profile.dev]
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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::LittleEndian, bincode::config::Varint, bincode::config::SkipFixedArrayLength> = bincode::config::standard()
|
||||
.with_little_endian()
|
||||
.with_variable_int_encoding()
|
||||
.skip_fixed_array_length();
|
||||
|
|
|
@ -1,3 +1 @@
|
|||
pub mod messages;
|
||||
pub mod client;
|
||||
pub mod server;
|
||||
|
|
|
@ -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
10
kubi-udp/Cargo.toml
Normal 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
36
kubi-udp/src/client.rs
Normal 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
11
kubi-udp/src/lib.rs
Normal 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
10
kubi-udp/src/packet.rs
Normal 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
14
kubi-udp/src/server.rs
Normal 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 })
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue