use anyhow::{...}

This commit is contained in:
griffi-gh 2023-02-03 19:39:22 +01:00
parent 0bd48bf46f
commit 4a699c34f3

View file

@ -1,3 +1,4 @@
use anyhow::{Result, bail};
use std::{ use std::{
net::{UdpSocket, SocketAddr}, net::{UdpSocket, SocketAddr},
marker::PhantomData, time::{Instant, Duration} marker::PhantomData, time::{Instant, Duration}
@ -35,7 +36,7 @@ pub struct Client<S, R> where S: Encode + Decode, R: Encode + Decode {
_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 new(addr: SocketAddr, config: ClientConfig) -> anyhow::Result<Self> { pub fn new(addr: SocketAddr, config: ClientConfig) -> 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)?;
@ -52,9 +53,9 @@ impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
_r: PhantomData, _r: PhantomData,
}) })
} }
pub fn connect(&mut self) -> anyhow::Result<()> { pub fn connect(&mut self) -> Result<()> {
if self.status != ClientStatus::Disconnected { if self.status != ClientStatus::Disconnected {
anyhow::bail!("Not Disconnected"); bail!("Not Disconnected");
} }
self.status = ClientStatus::Connecting; self.status = ClientStatus::Connecting;
self.timeout = Instant::now(); self.timeout = Instant::now();
@ -63,30 +64,30 @@ impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
self.send_raw_packet(ClientPacket::Connect)?; self.send_raw_packet(ClientPacket::Connect)?;
Ok(()) Ok(())
} }
fn send_raw_packet(&self, packet: ClientPacket<S>) -> anyhow::Result<()> { fn send_raw_packet(&self, packet: ClientPacket<S>) -> Result<()> {
let id_packet = IdClientPacket(self.client_id, packet); let id_packet = IdClientPacket(self.client_id, packet);
let bytes = bincode::encode_to_vec(id_packet, BINCODE_CONFIG)?; let bytes = bincode::encode_to_vec(id_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) -> Result<()> {
self.send_raw_packet(ClientPacket::Data(message))?; self.send_raw_packet(ClientPacket::Data(message))?;
Ok(()) Ok(())
} }
fn disconnect_inner(&mut self, reason: DisconnectReason) -> anyhow::Result<()> { fn disconnect_inner(&mut self, reason: DisconnectReason) -> Result<()> {
self.send_raw_packet(ClientPacket::Disconnect)?; self.send_raw_packet(ClientPacket::Disconnect)?;
self.status = ClientStatus::Disconnected; self.status = ClientStatus::Disconnected;
self.disconnect_reason = DisconnectReason::ClientDisconnected; self.disconnect_reason = reason;
Ok(()) Ok(())
} }
pub fn disconnect(&mut self) -> anyhow::Result<()> { pub fn disconnect(&mut self) -> Result<()> {
if self.status != ClientStatus::Connected { if self.status != ClientStatus::Connected {
anyhow::bail!("Not Connected"); bail!("Not Connected");
} }
self.disconnect_inner(DisconnectReason::ClientDisconnected)?; self.disconnect_inner(DisconnectReason::ClientDisconnected)?;
Ok(()) Ok(())
} }
pub fn update(&mut self) -> anyhow::Result<()> { pub fn update(&mut self) -> Result<()> {
if self.status == ClientStatus::Disconnected { if self.status == ClientStatus::Disconnected {
return Ok(()) return Ok(())
} }