Skip to content

Commit 88263b3

Browse files
committed
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.
1 parent 741c00b commit 88263b3

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub const SUPPORTED_PROTOCOLS: [i32; 13] = [477, 452, 451, 404, 340, 316, 315, 2
4242

4343
// TODO: switch to using thread_local storage?, see https://doc.rust-lang.org/std/macro.thread_local.html
4444
pub static mut CURRENT_PROTOCOL_VERSION: i32 = SUPPORTED_PROTOCOLS[0];
45+
pub static mut NETWORK_DEBUG: bool = false;
4546

4647
/// Helper macro for defining packets
4748
#[macro_export]
@@ -923,8 +924,19 @@ impl Conn {
923924
Direction::Serverbound => Direction::Clientbound,
924925
};
925926

927+
let network_debug = unsafe { NETWORK_DEBUG };
928+
929+
if network_debug {
930+
println!("about to parse id={:x}, dir={:?} state={:?}", id, dir, self.state);
931+
std::fs::File::create("last-packet")?.write_all(buf.get_ref())?;
932+
}
933+
926934
let packet = packet::packet_by_id(self.protocol_version, self.state, dir, id, &mut buf)?;
927935

936+
if network_debug {
937+
println!("packet = {:?}", packet);
938+
}
939+
928940
match packet {
929941
Some(val) => {
930942
let pos = buf.position() as usize;

0 commit comments

Comments
 (0)