renet2/
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
26impl std::error::Error for DisconnectReason {}
27
28/// Possibles errors that can occur in a channel.
29#[derive(Clone, Copy, Debug, PartialEq, Eq)]
30pub enum ChannelError {
31    /// Reliable channel reached maximum allowed memory
32    ReliableChannelMaxMemoryReached,
33    /// Received an invalid slice message in the channel.
34    InvalidSliceMessage,
35}
36
37impl fmt::Display for ChannelError {
38    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
39        use ChannelError::*;
40
41        match *self {
42            ReliableChannelMaxMemoryReached => write!(fmt, "reliable channel memory usage was exhausted"),
43            InvalidSliceMessage => write!(fmt, "received an invalid slice packet"),
44        }
45    }
46}
47
48impl fmt::Display for DisconnectReason {
49    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
50        use DisconnectReason::*;
51
52        match *self {
53            Transport => write!(fmt, "connection terminated by the transport layer"),
54            DisconnectedByClient => write!(fmt, "connection terminated by the client"),
55            DisconnectedByServer => write!(fmt, "connection terminated by the server"),
56            PacketSerialization(err) => write!(fmt, "failed to serialize packet: {err}"),
57            PacketDeserialization(err) => write!(fmt, "failed to deserialize packet: {err}"),
58            ReceivedInvalidChannelId(id) => write!(fmt, "received message with invalid channel {id}"),
59            SendChannelError { channel_id, error } => write!(fmt, "send channel {channel_id} with error: {error}"),
60            ReceiveChannelError { channel_id, error } => write!(fmt, "receive channel {channel_id} with error: {error}"),
61        }
62    }
63}
64
65impl std::error::Error for ChannelError {}
66
67#[derive(Debug)]
68pub struct ClientNotFound;
69
70impl std::error::Error for ClientNotFound {}
71
72impl fmt::Display for ClientNotFound {
73    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
74        write!(fmt, "client with given id was not found")
75    }
76}