mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-11-29 02:08:48 -06:00
client
This commit is contained in:
parent
63b4091ddd
commit
99b2848775
|
@ -1,41 +1,54 @@
|
||||||
use std::{
|
use std::{
|
||||||
net::{UdpSocket, SocketAddr},
|
net::{UdpSocket, SocketAddr},
|
||||||
marker::PhantomData
|
marker::PhantomData, time::Instant
|
||||||
};
|
};
|
||||||
use bincode::{Encode, Decode};
|
use bincode::{Encode, Decode};
|
||||||
use crate::{BINCODE_CONFIG, packet::ClientPacket};
|
use crate::{BINCODE_CONFIG, packet::ClientPacket};
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub struct ClientConfig {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
pub struct Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
||||||
|
addr: SocketAddr,
|
||||||
|
config: ClientConfig,
|
||||||
socket: UdpSocket,
|
socket: UdpSocket,
|
||||||
|
last_heartbeat: Instant,
|
||||||
_s: PhantomData<*const S>,
|
_s: PhantomData<*const S>,
|
||||||
_r: PhantomData<*const R>,
|
_r: PhantomData<*const R>,
|
||||||
}
|
}
|
||||||
impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
||||||
pub fn connect(addr: SocketAddr) -> anyhow::Result<Self> {
|
pub fn connect(addr: SocketAddr, config: ClientConfig) -> anyhow::Result<Self> {
|
||||||
let bind_addr: SocketAddr = "127.0.0.1:0".parse().unwrap();
|
let bind_addr: SocketAddr = "127.0.0.1:0".parse().unwrap();
|
||||||
let socket = UdpSocket::bind(bind_addr)?;
|
let socket = UdpSocket::bind(bind_addr)?;
|
||||||
socket.set_nonblocking(true)?;
|
socket.set_nonblocking(true)?;
|
||||||
socket.connect(addr)?;
|
socket.connect(addr)?;
|
||||||
let client = Self {
|
let client = Self {
|
||||||
|
addr,
|
||||||
|
config,
|
||||||
socket,
|
socket,
|
||||||
|
last_heartbeat: Instant::now(),
|
||||||
_s: PhantomData,
|
_s: PhantomData,
|
||||||
_r: PhantomData,
|
_r: PhantomData,
|
||||||
};
|
};
|
||||||
client.send_packet(&ClientPacket::Connect)?;
|
client.send_raw_packet(&ClientPacket::Connect)?;
|
||||||
Ok(client)
|
Ok(client)
|
||||||
}
|
}
|
||||||
//maybe move generics here if possible?
|
fn send_raw_packet(&self, packet: &ClientPacket<S>) -> anyhow::Result<()> {
|
||||||
fn send_packet(&self, packet: &ClientPacket<S>) -> anyhow::Result<()> {
|
|
||||||
let bytes = bincode::encode_to_vec(packet, BINCODE_CONFIG)?;
|
let bytes = bincode::encode_to_vec(packet, BINCODE_CONFIG)?;
|
||||||
self.socket.send(&bytes)?;
|
self.socket.send(&bytes)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
pub fn send_message(&self, message: S) -> anyhow::Result<()> {
|
pub fn send_message(&self, message: S) -> anyhow::Result<()> {
|
||||||
self.send_packet(&ClientPacket::Data(message))?;
|
self.send_raw_packet(&ClientPacket::Data(message))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
pub fn disconnect(self) -> anyhow::Result<()> {
|
pub fn disconnect(self) -> anyhow::Result<()> {
|
||||||
self.send_packet(&ClientPacket::Disconnect)?;
|
self.send_raw_packet(&ClientPacket::Disconnect)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
pub fn update(&mut self) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ pub(crate) mod packet;
|
||||||
pub use server::Server;
|
pub use server::Server;
|
||||||
pub use client::Client;
|
pub use client::Client;
|
||||||
|
|
||||||
|
//pub(crate) trait Serializable: bincode::Encode + bincode::Decode {}
|
||||||
pub(crate) const BINCODE_CONFIG: bincode::config::Configuration<bincode::config::LittleEndian, bincode::config::Varint, bincode::config::SkipFixedArrayLength> = bincode::config::standard()
|
pub(crate) const BINCODE_CONFIG: bincode::config::Configuration<bincode::config::LittleEndian, bincode::config::Varint, bincode::config::SkipFixedArrayLength> = bincode::config::standard()
|
||||||
.with_little_endian()
|
.with_little_endian()
|
||||||
.with_variable_int_encoding()
|
.with_variable_int_encoding()
|
||||||
|
|
Loading…
Reference in a new issue