From 4fed9dab832c2bd6f36f66dd62f2189b0a0ce200 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=A4rtens?= Date: Wed, 7 Apr 2021 18:25:46 +0200 Subject: [PATCH] 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 --- network/protocol/src/error.rs | 9 +++++++++ network/protocol/src/mpsc.rs | 4 ++-- network/protocol/src/tcp.rs | 4 ++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/network/protocol/src/error.rs b/network/protocol/src/error.rs index 089208b24f..27199197d2 100644 --- a/network/protocol/src/error.rs +++ b/network/protocol/src/error.rs @@ -11,13 +11,21 @@ pub enum InitProtocolError { /// When you return closed you must stay closed! #[derive(Debug, PartialEq)] 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, + /// 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 for InitProtocolError { fn from(err: ProtocolError) -> Self { match err { 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 { match self { ProtocolError::Closed => write!(f, "Channel closed"), + ProtocolError::Violated => write!(f, "Channel protocol violated"), } } } diff --git a/network/protocol/src/mpsc.rs b/network/protocol/src/mpsc.rs index 1f3219bab2..f4a5eee1cd 100644 --- a/network/protocol/src/mpsc.rs +++ b/network/protocol/src/mpsc.rs @@ -142,7 +142,7 @@ where } Ok(e) }, - MpscMsg::InitFrame(_) => Err(ProtocolError::Closed), + MpscMsg::InitFrame(_) => Err(ProtocolError::Violated), } } } @@ -164,7 +164,7 @@ where { async fn recv(&mut self) -> Result { match self.sink.recv().await? { - MpscMsg::Event(_) => Err(ProtocolError::Closed), + MpscMsg::Event(_) => Err(ProtocolError::Violated), MpscMsg::InitFrame(f) => Ok(f), } } diff --git a/network/protocol/src/tcp.rs b/network/protocol/src/tcp.rs index e78741fc01..4e80736342 100644 --- a/network/protocol/src/tcp.rs +++ b/network/protocol/src/tcp.rs @@ -266,7 +266,7 @@ where ?mid, "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); @@ -321,7 +321,7 @@ where return Ok(frame); } } - Err(ProtocolError::Closed) + Err(ProtocolError::Violated) } }