Skip to content

Commit 48bc0d0

Browse files
committed
fix: trace abort and color handling
1 parent e1a926e commit 48bc0d0

File tree

6 files changed

+263
-161
lines changed

6 files changed

+263
-161
lines changed

examples/mvd_parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn parse_file(filename: String) -> Result<bool, Box<dyn Error>> {
4141
},
4242
Err(e) => {
4343
#[cfg(feature = "trace")]
44-
print_message_trace(&mvd.message, false, 0, 2)?;
44+
print_message_trace(&mvd.message, false, 0, 2, false)?;
4545
return Err(Box::new(e));
4646
},
4747
};
@@ -62,7 +62,7 @@ fn parse_file(filename: String) -> Result<bool, Box<dyn Error>> {
6262
}
6363
#[cfg(feature = "trace")]
6464
if false { // if you want to print a trace of each read frame
65-
print_message_trace(&mvd.message, false, 0, 2)?;
65+
print_message_trace(&mvd.message, false, 0, 2, false)?;
6666
}
6767
}
6868
return Ok(true)

examples/trace.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use quakeworld::protocol::message::{Message, MessageFlags, MessageType};
2-
use quakeworld::protocol::types::{ServerClient, Print, ServerMessage};
2+
use quakeworld::protocol::types::{ServerClient, ServerMessage};
33
use quakeworld::utils::trace::print_message_trace;
44
fn main() {
55
let b: Vec<u8> = vec![8, 2, 0x68, 0x65, 0x6c, 0x6c, 0x6f,0x0];
@@ -26,5 +26,5 @@ fn main() {
2626
},
2727
_ => { panic!("its not print!");},
2828
}
29-
print_message_trace(&message, false)?;
29+
let _ = print_message_trace(&message, true, 0, 9, true);
3030
}

src/protocol/message/mod.rs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::Serialize;
66
use crate::utils::ascii_converter::AsciiConverter;
77

88
use crate::protocol::types::*;
9-
use crate::protocol::message::trace::{trace_start, trace_stop, trace_annotate, trace_lock, trace_unlock, MessageTrace, TraceValue, ToTraceValue};
9+
use crate::protocol::message::trace::{trace_start, trace_stop, trace_annotate, trace_abort, trace_lock, trace_unlock, MessageTrace, TraceValue, ToTraceValue};
1010

1111
pub mod errors;
1212
pub mod trace;
@@ -72,7 +72,13 @@ macro_rules! endian_read {
7272
pub fn [< read_$ty >] (&mut self, readahead: bool) -> Result<$ty, MessageError> {
7373
const TYPE_SIZE:usize = std::mem::size_of::<$ty>();
7474
trace_start!(self, readahead);
75-
self.check_read_size(TYPE_SIZE)?;
75+
match self.check_read_size(TYPE_SIZE) {
76+
Ok(()) => {},
77+
Err(e) => {
78+
trace_abort!(self);
79+
return Err(e);
80+
},
81+
}
7682
let mut a: [u8; TYPE_SIZE] = [0; TYPE_SIZE];
7783
for n in 0..TYPE_SIZE {
7884
a[n] = self.buffer[self.start + self.position + n ];
@@ -297,7 +303,6 @@ impl Message {
297303
} else if b == 0 {
298304
break
299305
}
300-
301306
buf.push(b);
302307
}
303308
trace_unlock!(self);
@@ -497,16 +502,25 @@ impl Message {
497502
Ok(t) => t,
498503
Err(e) => {
499504
match e {
500-
MessageError::ReadBeyondSize(_,_,_) => break,
501-
_ => return Err(e),
505+
MessageError::ReadBeyondSize(_,_,_) => {
506+
break
507+
},
508+
_ => {
509+
let p = Packet::Connected(Connected{
510+
sequence,
511+
sequence_ack,
512+
messages,
513+
});
514+
trace_stop!(self, p);
515+
return Err(e)
516+
},
502517
}
503518
},
504519
};
505520

506521
let cmd = match ServerClient::try_from(t) {
507522
Ok(cmd) => cmd,
508523
Err(_) => {
509-
510524
let p = Packet::Connected(Connected{
511525
sequence,
512526
sequence_ack,
@@ -516,7 +530,19 @@ impl Message {
516530
return Err(MessageError::UnknownType(t));
517531
}
518532
};
519-
let ret = cmd.read_message(self)?;
533+
534+
let ret = match cmd.read_message(self) {
535+
Ok(ret) => ret,
536+
Err(e) => {
537+
let p = Packet::Connected(Connected{
538+
sequence,
539+
sequence_ack,
540+
messages,
541+
});
542+
trace_stop!(self, p);
543+
return Err(e);
544+
}
545+
};
520546
messages.push(ret);
521547
}
522548
let p = Packet::Connected(Connected{
@@ -539,8 +565,7 @@ impl Message {
539565
trace_stop!(self, _packet_type, U8);
540566
match packet_type {
541567
CommandCode::S2cChallenge => {
542-
let p = self.read_packet_s2c_challenge();
543-
return p;
568+
self.read_packet_s2c_challenge()
544569
}
545570
CommandCode::S2cConnection => {
546571
Ok(Packet::ConnectionLessServerConnection)

src/protocol/message/trace.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub struct ReadTrace {
1212
pub start: usize,
1313
pub stop: usize,
1414
pub readahead: bool,
15+
pub aborted: bool,
1516
pub function: String,
1617
pub annotation: Option<String>,
1718
pub read: Vec<ReadTrace>,
@@ -90,10 +91,29 @@ impl Message {
9091
read: vec![],
9192
value: TraceValue::None,
9293
annotation,
94+
aborted: false,
9395
};
9496
self.trace.stack.push(res)
9597
}
9698

99+
#[cfg(feature = "trace")]
100+
pub fn read_trace_abort(&mut self) {
101+
if !self.trace.enabled {
102+
return;
103+
}
104+
if let Some(mut trace) = self.trace.stack.pop() {
105+
trace.aborted = true;
106+
trace.stop = self.position;
107+
108+
let len = self.trace.stack.len();
109+
if len > 0 {
110+
self.trace.stack[len-1].read.push(trace);
111+
} else {
112+
self.trace.read.push(trace);
113+
}
114+
}
115+
}
116+
97117
#[cfg(feature = "trace")]
98118
pub fn read_trace_stop(&mut self, value: TraceValue) {
99119
if !self.trace.enabled {
@@ -159,6 +179,21 @@ macro_rules! trace_stop{
159179
}
160180
pub(crate) use trace_stop;
161181

182+
#[cfg(not(feature = "trace"))]
183+
macro_rules! trace_abort {
184+
}
185+
186+
#[cfg(feature = "trace")]
187+
macro_rules! trace_abort {
188+
($self:expr) => {
189+
if $self.trace.enabled && !$self.trace.locked {
190+
$self.read_trace_abort();
191+
}
192+
}
193+
}
194+
195+
pub(crate) use trace_abort;
196+
162197
#[cfg(not(feature = "trace"))]
163198
macro_rules! trace_annotate {
164199
}

src/protocol/types.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,13 +1652,10 @@ pub struct Intermission {
16521652
pub angle: AngleVector,
16531653
}
16541654

1655-
16561655
#[derive(Debug, PartialEq, Eq, PartialOrd, ParseMessage, Serialize, Clone)]
16571656
pub struct Disconnect {
16581657
}
16591658

1660-
1661-
16621659
#[derive(Debug, Eq, PartialEq, PartialOrd, Ord, TryFromPrimitive, Display, Serialize, Clone)]
16631660
#[repr(u8)]
16641661
pub enum TempEntityType {

0 commit comments

Comments
 (0)