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.
This commit is contained in:
ice_iix 2019-05-05 14:32:48 -07:00
parent 9a6af430df
commit 4520f6bdae
2 changed files with 21 additions and 1 deletions

View File

@ -166,11 +166,15 @@ impl Game {
} }
#[derive(StructOpt, Debug)] #[derive(StructOpt, Debug)]
#[structopt(name = "basic")] #[structopt(name = "Stevenarella")]
struct Opt { struct Opt {
/// Server to connect to /// Server to connect to
#[structopt(short = "s", long = "server")] #[structopt(short = "s", long = "server")]
server: Option<String>, server: Option<String>,
/// Log decoded packets received from network
#[structopt(short = "n", long = "network-debug")]
network_debug: bool,
} }
cfg_if! { cfg_if! {
@ -273,6 +277,10 @@ pub fn main() {
}; };
game.renderer.camera.pos = cgmath::Point3::new(0.5, 13.2, 0.5); game.renderer.camera.pos = cgmath::Point3::new(0.5, 13.2, 0.5);
if opt.network_debug {
unsafe { protocol::NETWORK_DEBUG = true; }
}
if opt.server.is_some() { if opt.server.is_some() {
game.connect_to(&opt.server.unwrap()); game.connect_to(&opt.server.unwrap());
} }

View File

@ -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 // 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 CURRENT_PROTOCOL_VERSION: i32 = SUPPORTED_PROTOCOLS[0];
pub static mut NETWORK_DEBUG: bool = false;
/// Helper macro for defining packets /// Helper macro for defining packets
#[macro_export] #[macro_export]
@ -923,8 +924,19 @@ impl Conn {
Direction::Serverbound => Direction::Clientbound, 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)?; let packet = packet::packet_by_id(self.protocol_version, self.state, dir, id, &mut buf)?;
if network_debug {
println!("packet = {:?}", packet);
}
match packet { match packet {
Some(val) => { Some(val) => {
let pos = buf.position() as usize; let pos = buf.position() as usize;