Have a clear error for when the I/O closes and when some protocol is violated. this should help find the rootcause of a bug.

If its Closed it looks like the TCP connection got dropped/cut off (e.g. OS, Wifi).
If its Violated we for sure know the cause is the messages send/recv in a wrong way
This commit is contained in:
Marcel Märtens 2021-04-07 18:25:46 +02:00
parent 831cd9c5db
commit 4fed9dab83
3 changed files with 13 additions and 4 deletions

View File

@ -11,13 +11,21 @@ pub enum InitProtocolError {
/// When you return closed you must stay closed! /// When you return closed you must stay closed!
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum ProtocolError { pub enum ProtocolError {
/// Closed indicates the underlying I/O got closed
/// e.g. the TCP, UDP or MPSC connection is dropped by the OS
Closed, Closed,
/// Violated indicates the veloren_network_protocol was violated
/// the underlying I/O connection is still valid, but the remote side
/// send WRONG (e.g. Invalid, or wrong order) data on the protocol layer.
Violated,
} }
impl From<ProtocolError> for InitProtocolError { impl From<ProtocolError> for InitProtocolError {
fn from(err: ProtocolError) -> Self { fn from(err: ProtocolError) -> Self {
match err { match err {
ProtocolError::Closed => InitProtocolError::Closed, ProtocolError::Closed => InitProtocolError::Closed,
// not possible as the Init has raw access to the I/O
ProtocolError::Violated => InitProtocolError::Closed,
} }
} }
} }
@ -46,6 +54,7 @@ impl core::fmt::Display for ProtocolError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self { match self {
ProtocolError::Closed => write!(f, "Channel closed"), ProtocolError::Closed => write!(f, "Channel closed"),
ProtocolError::Violated => write!(f, "Channel protocol violated"),
} }
} }
} }

View File

@ -142,7 +142,7 @@ where
} }
Ok(e) Ok(e)
}, },
MpscMsg::InitFrame(_) => Err(ProtocolError::Closed), MpscMsg::InitFrame(_) => Err(ProtocolError::Violated),
} }
} }
} }
@ -164,7 +164,7 @@ where
{ {
async fn recv(&mut self) -> Result<InitFrame, ProtocolError> { async fn recv(&mut self) -> Result<InitFrame, ProtocolError> {
match self.sink.recv().await? { match self.sink.recv().await? {
MpscMsg::Event(_) => Err(ProtocolError::Closed), MpscMsg::Event(_) => Err(ProtocolError::Violated),
MpscMsg::InitFrame(f) => Ok(f), MpscMsg::InitFrame(f) => Ok(f),
} }
} }

View File

@ -266,7 +266,7 @@ where
?mid, ?mid,
"protocol violation by remote side: send Data before Header" "protocol violation by remote side: send Data before Header"
); );
break 'outer Err(ProtocolError::Closed); break 'outer Err(ProtocolError::Violated);
}, },
}; };
m.data.extend_from_slice(&data); m.data.extend_from_slice(&data);
@ -321,7 +321,7 @@ where
return Ok(frame); return Ok(frame);
} }
} }
Err(ProtocolError::Closed) Err(ProtocolError::Violated)
} }
} }