diff options
author | Henauxg <19689618+Henauxg@users.noreply.github.com> | 2022-11-09 16:36:25 +0100 |
---|---|---|
committer | Henauxg <19689618+Henauxg@users.noreply.github.com> | 2022-11-09 16:36:25 +0100 |
commit | ae8a6fc20637b971cb583ac87b12f455bc07e9f6 (patch) | |
tree | 4d8c4ea289e4407f4d8d32396ae1989b8dc4be8a | |
parent | 2991e10fcd3ae49865136b9c4dbed84eab8468cd (diff) |
Use thiserror in QuinnetError and return QuinnetErrors with serevr API
-rw-r--r-- | src/lib.rs | 17 | ||||
-rw-r--r-- | src/server.rs | 64 |
2 files changed, 45 insertions, 36 deletions
@@ -7,18 +7,19 @@ pub mod server; pub type ClientId = u64; -/// Enum with possibles errors that can occur. -#[derive(Debug)] +/// Enum with possibles errors that can occur in Bevy Quinnet +#[derive(thiserror::Error, Debug)] pub enum QuinnetError { - /// Failed serialization + #[error("Client with id `{0}` is unknown")] + UnknownClient(ClientId), + #[error("Failed serialization")] Serialization, - /// Failed deserialization + #[error("Failed deserialization")] Deserialization, - /// The data could not be sent on the channel because the channel is - /// currently full and sending would require blocking. + #[error("The data could not be sent on the channel because the channel is currently full and sending would require blocking")] FullQueue, - /// The receive half of the channel was explicitly closed or has been - /// dropped. + #[error("The receiving half of the channel was explicitly closed or has been dropped")] ChannelClosed, + #[error("The hosts file is invalid")] InvalidHostFile, } diff --git a/src/server.rs b/src/server.rs index 162156e..ab2f9e9 100644 --- a/src/server.rs +++ b/src/server.rs @@ -169,20 +169,13 @@ impl Server { } } - pub fn disconnect_client(&mut self, client_id: ClientId) { + pub fn disconnect_client(&mut self, client_id: ClientId) -> Result<(), QuinnetError> { match self.clients.remove(&client_id) { - Some(client_connection) => { - if let Err(_) = client_connection.close_sender.send(()) { - error!( - "Failed to close client streams & resources while disconnecting client {}", - client_id - ) - } - } - None => error!( - "Failed to disconnect client {}, client not found", - client_id - ), + Some(client_connection) => match client_connection.close_sender.send(()) { + Ok(_) => Ok(()), + Err(_) => Err(QuinnetError::ChannelClosed), + }, + None => Err(QuinnetError::UnknownClient(client_id)), } } @@ -204,7 +197,7 @@ impl Server { message: T, ) -> Result<(), QuinnetError> { match bincode::serialize(&message) { - Ok(payload) => Ok(self.send_payload(client_id, payload)), + Ok(payload) => Ok(self.send_payload(client_id, payload)?), Err(_) => Err(QuinnetError::Serialization), } } @@ -216,9 +209,8 @@ impl Server { ) -> Result<(), QuinnetError> { match bincode::serialize(&message) { Ok(payload) => { - // TODO Fix: Error handling for id in client_ids { - self.send_payload(*id, payload.clone()); + self.send_payload(*id, payload.clone())?; } Ok(()) } @@ -231,31 +223,47 @@ impl Server { message: T, ) -> Result<(), QuinnetError> { match bincode::serialize(&message) { - Ok(payload) => Ok(self.broadcast_payload(payload)), + Ok(payload) => Ok(self.broadcast_payload(payload)?), Err(_) => Err(QuinnetError::Serialization), } } - pub fn broadcast_payload<T: Into<Bytes> + Clone>(&mut self, payload: T) { - // TODO Fix: Error handling + pub fn broadcast_payload<T: Into<Bytes> + Clone>( + &mut self, + payload: T, + ) -> Result<(), QuinnetError> { for (_, client_connection) in self.clients.iter() { - client_connection - .sender - .try_send(payload.clone().into()) - .unwrap(); + match client_connection.sender.try_send(payload.clone().into()) { + Ok(_) => {} + Err(err) => match err { + mpsc::error::TrySendError::Full(_) => return Err(QuinnetError::FullQueue), + mpsc::error::TrySendError::Closed(_) => { + return Err(QuinnetError::ChannelClosed) + } + }, + }; } + Ok(()) } - pub fn send_payload<T: Into<Bytes>>(&mut self, client_id: ClientId, payload: T) { - // TODO Fix: Error handling + pub fn send_payload<T: Into<Bytes>>( + &mut self, + client_id: ClientId, + payload: T, + ) -> Result<(), QuinnetError> { if let Some(client) = self.clients.get(&client_id) { - client.sender.try_send(payload.into()).unwrap(); + match client.sender.try_send(payload.into()) { + Ok(_) => Ok(()), + Err(err) => match err { + mpsc::error::TrySendError::Full(_) => Err(QuinnetError::FullQueue), + mpsc::error::TrySendError::Closed(_) => Err(QuinnetError::ChannelClosed), + }, + } } else { - warn!("Failed to send payload to unknown client {}", client_id) + Err(QuinnetError::UnknownClient(client_id)) } } - //TODO Clean: Consider receiving payloads for a specified client pub fn receive_payload(&mut self) -> Result<Option<ClientPayload>, QuinnetError> { match self.receiver.try_recv() { Ok(msg) => Ok(Some(msg)), |