Skip to content

Commit 4520f6b

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 9a6af43 commit 4520f6b

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/main.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,15 @@ impl Game {
166166
}
167167

168168
#[derive(StructOpt, Debug)]
169-
#[structopt(name = "basic")]
169+
#[structopt(name = "Stevenarella")]
170170
struct Opt {
171171
/// Server to connect to
172172
#[structopt(short = "s", long = "server")]
173173
server: Option<String>,
174+
175+
/// Log decoded packets received from network
176+
#[structopt(short = "n", long = "network-debug")]
177+
network_debug: bool,
174178
}
175179

176180
cfg_if! {
@@ -273,6 +277,10 @@ pub fn main() {
273277
};
274278
game.renderer.camera.pos = cgmath::Point3::new(0.5, 13.2, 0.5);
275279

280+
if opt.network_debug {
281+
unsafe { protocol::NETWORK_DEBUG = true; }
282+
}
283+
276284
if opt.server.is_some() {
277285
game.connect_to(&opt.server.unwrap());
278286
}

src/protocol/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)