veloren_network_protocol/
error.rs

1/// All possible Errors that can happen during Handshake [`InitProtocol`]
2///
3/// [`InitProtocol`]: crate::InitProtocol
4#[derive(Debug, PartialEq, Eq)]
5pub enum InitProtocolError<E: std::fmt::Debug + Send> {
6    Custom(E),
7    /// expected Handshake, didn't get handshake
8    NotHandshake,
9    /// expected Id, didn't get id
10    NotId,
11    WrongMagicNumber([u8; 7]),
12    WrongVersion([u32; 3]),
13}
14
15/// When you return closed you must stay closed!
16#[derive(Debug, PartialEq, Eq)]
17pub enum ProtocolError<E: std::fmt::Debug + Send> {
18    /// Custom Error on the underlying I/O,
19    /// e.g. the TCP, UDP or MPSC connection is dropped by the OS
20    Custom(E),
21    /// Violated indicates the veloren_network_protocol was violated
22    /// the underlying I/O connection is still valid, but the remote side
23    /// send WRONG (e.g. Invalid, or wrong order) data on the protocol layer.
24    Violated,
25}
26
27impl<E: std::fmt::Debug + Send> From<ProtocolError<E>> for InitProtocolError<E> {
28    fn from(err: ProtocolError<E>) -> Self {
29        match err {
30            ProtocolError::Custom(e) => InitProtocolError::Custom(e),
31            ProtocolError::Violated => {
32                unreachable!("not possible as the Init has raw access to the I/O")
33            },
34        }
35    }
36}
37
38impl<E: std::fmt::Debug + Send> core::fmt::Display for InitProtocolError<E> {
39    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
40        match self {
41            InitProtocolError::Custom(e) => write!(f, "custom: {:?}", e),
42            InitProtocolError::NotHandshake => write!(
43                f,
44                "Remote send something which couldn't be parsed as a handshake"
45            ),
46            InitProtocolError::NotId => {
47                write!(f, "Remote send something which couldn't be parsed as an id")
48            },
49            InitProtocolError::WrongMagicNumber(r) => write!(
50                f,
51                "Magic Number doesn't match, remote side send '{:?}' instead of '{:?}'",
52                &r,
53                &crate::types::VELOREN_MAGIC_NUMBER
54            ),
55            InitProtocolError::WrongVersion(r) => write!(
56                f,
57                "Network doesn't match, remote side send '{:?}' we are on '{:?}'",
58                &r,
59                &crate::types::VELOREN_NETWORK_VERSION
60            ),
61        }
62    }
63}
64
65impl<E: std::fmt::Debug + Send> core::fmt::Display for ProtocolError<E> {
66    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
67        match self {
68            ProtocolError::Custom(e) => write!(f, "Channel custom close: {:?}", e),
69            ProtocolError::Violated => write!(f, "Channel protocol violated"),
70        }
71    }
72}
73
74impl<E: std::fmt::Debug + Send> std::error::Error for InitProtocolError<E> {}
75impl<E: std::fmt::Debug + Send> std::error::Error for ProtocolError<E> {}