From 2f82d2ae7136d39c94df8ba7c484507d919411ab Mon Sep 17 00:00:00 2001 From: ice_iix Date: Sun, 5 May 2019 14:32:48 -0700 Subject: [PATCH] Add flag to log network packets for debugging, --network-debug Pass -n or --network-debug to print out the packets from the server as they are received, as well as write to last-packet for later analysis. Useful when debugging network protocol issues. Builds on #90 #114. --- protocol/src/protocol/mod.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/protocol/src/protocol/mod.rs b/protocol/src/protocol/mod.rs index 6ac2f39..be57ae3 100644 --- a/protocol/src/protocol/mod.rs +++ b/protocol/src/protocol/mod.rs @@ -42,6 +42,7 @@ pub const SUPPORTED_PROTOCOLS: [i32; 13] = [477, 452, 451, 404, 340, 316, 315, 2 // TODO: switch to using thread_local storage?, see https://doc.rust-lang.org/std/macro.thread_local.html pub static mut CURRENT_PROTOCOL_VERSION: i32 = SUPPORTED_PROTOCOLS[0]; +pub static mut NETWORK_DEBUG: bool = false; /// Helper macro for defining packets #[macro_export] @@ -923,8 +924,19 @@ impl Conn { Direction::Serverbound => Direction::Clientbound, }; + let network_debug = unsafe { NETWORK_DEBUG }; + + if network_debug { + println!("about to parse id={:x}, dir={:?} state={:?}", id, dir, self.state); + std::fs::File::create("last-packet")?.write_all(buf.get_ref())?; + } + let packet = packet::packet_by_id(self.protocol_version, self.state, dir, id, &mut buf)?; + if network_debug { + println!("packet = {:?}", packet); + } + match packet { Some(val) => { let pos = buf.position() as usize;