renet/
error.rs

1use std::fmt;
2
3use crate::packet::SerializationError;
4
5/// Possible reasons for a disconnection.
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub enum DisconnectReason {
8    /// Connection was terminated by the transport layer
9    Transport,
10    /// Connection was terminated by the server
11    DisconnectedByClient,
12    /// Connection was terminated by the server
13    DisconnectedByServer,
14    /// Failed to serialize packet
15    PacketSerialization(SerializationError),
16    /// Failed to deserialize packet
17    PacketDeserialization(SerializationError),
18    /// Received message from channel with invalid id
19    ReceivedInvalidChannelId(u8),
20    /// Error occurred in a send channel
21    SendChannelError { channel_id: u8, error: ChannelError },
22    /// Error occurred in a receive channel
23    ReceiveChannelError { channel_id: u8, error: ChannelError },
24}
25
26/// Possibles errors that can occur in a channel.
27#[derive(Clone, Copy, Debug, PartialEq, Eq)]
28pub enum ChannelError {
29    /// Reliable channel reached maximum allowed memory
30    ReliableChannelMaxMemoryReached,
31    /// Received an invalid slice message in the channel.
32    InvalidSliceMessage,
33}
34
35impl fmt::Display for ChannelError {
36    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
37        use ChannelError::*;
38
39        match *self {
40            ReliableChannelMaxMemoryReached => write!(fmt, "reliable channel memory usage was exausted"),
41            InvalidSliceMessage => write!(fmt, "received an invalid slice packet"),
42        }
43    }
44}
45
46impl fmt::Display for DisconnectReason {
47    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
48        use DisconnectReason::*;
49
50        match *self {
51            Transport => write!(fmt, "connection terminated by the transport layer"),
52            DisconnectedByClient => write!(fmt, "connection terminated by the client"),
53            DisconnectedByServer => write!(fmt, "connection terminated by the server"),
54            PacketSerialization(err) => write!(fmt, "failed to serialize packet: {err}"),
55            PacketDeserialization(err) => write!(fmt, "failed to deserialize packet: {err}"),
56            ReceivedInvalidChannelId(id) => write!(fmt, "received message with invalid channel {id}"),
57            SendChannelError { channel_id, error } => write!(fmt, "send channel {channel_id} with error: {error}"),
58            ReceiveChannelError { channel_id, error } => write!(fmt, "receive channel {channel_id} with error: {error}"),
59        }
60    }
61}
62
63impl std::error::Error for ChannelError {}
64
65#[derive(Debug)]
66pub struct ClientNotFound;
67
68impl std::error::Error for ClientNotFound {}
69
70impl fmt::Display for ClientNotFound {
71    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
72        write!(fmt, "client with given id was not found")
73    }
74}