Recognize timestamps
authorunC0Rr
Wed, 14 Nov 2018 16:32:42 +0100
changeset 14210 1c55c54013c4
parent 14209 ab1ef7074e07
child 14211 0f7b92397b9e
Recognize timestamps
rust/hedgewars-engine-messages/src/messages.rs
rust/hedgewars-engine-messages/src/parser.rs
--- a/rust/hedgewars-engine-messages/src/messages.rs	Wed Nov 14 11:09:01 2018 +0100
+++ b/rust/hedgewars-engine-messages/src/messages.rs	Wed Nov 14 16:32:42 2018 +0100
@@ -23,6 +23,7 @@
     Skip,
     TeamControlGained(String),
     TeamControlLost(String),
+    TimeWrap,
 }
 
 #[derive(Debug, PartialEq)]
--- a/rust/hedgewars-engine-messages/src/parser.rs	Wed Nov 14 11:09:01 2018 +0100
+++ b/rust/hedgewars-engine-messages/src/parser.rs	Wed Nov 14 16:32:42 2018 +0100
@@ -1,13 +1,9 @@
-use nom::*;
+use nom::{*};
 use std::str;
 
 use super::messages::{*, EngineMessage::*, UnsyncedEngineMessage::*, SyncedEngineMessage::*, ConfigEngineMessage::*, KeystrokeAction::*};
 
-named!(length_specifier<&[u8], u16>, alt!(
-    verify!(map!(take!(1), |a : &[u8]| a[0] as u16), |l| l < 64)
-    | map!(take!(2), |a| (a[0] as u16 - 64) * 256 + a[1] as u16 + 64)
-    )
-);
+
 
 named!(unrecognized_message<&[u8], EngineMessage>,
     do_parse!(rest >> (Unknown))
@@ -15,10 +11,21 @@
 
 named!(string_tail<&[u8], String>, map!(map_res!(rest, str::from_utf8), String::from));
 
+named!(length_without_timestamp<&[u8], usize>,
+    map_opt!(rest_len, |l| if l > 2 { Some(l - 2) } else { None } )
+);
+
 named!(synced_message<&[u8], SyncedEngineMessage>, alt!(
-      do_parse!(tag!("+l") >> (Left(Press)))
+      do_parse!(tag!("L") >> (Left(Press)))
 ));
 
+named!(timestamped_message<&[u8], (SyncedEngineMessage, u16)>,
+    do_parse!(msg: length_value!(length_without_timestamp, synced_message)
+        >> timestamp: be_u16
+        >> ((msg, timestamp))
+    )
+);
+
 named!(unsynced_message<&[u8], UnsyncedEngineMessage>, alt!(
       do_parse!(tag!("?") >> (Ping))
     | do_parse!(tag!("!") >> (Ping))
@@ -29,18 +36,29 @@
     do_parse!(tag!("C") >> (ConfigRequest))
 ));
 
+named!(unwrapped_message<&[u8], EngineMessage>,
+    alt!(
+        map!(timestamped_message, |(m, t)| Synced(m, t as u32))
+        | do_parse!(tag!("#") >> (Synced(TimeWrap, 65535)))
+        | map!(unsynced_message, |m| Unsynced(m))
+        | map!(config_message, |m| Config(m))
+        | unrecognized_message
+));
+
+
+
+named!(length_specifier<&[u8], u16>, alt!(
+    verify!(map!(take!(1), |a : &[u8]| a[0] as u16), |l| l < 64)
+    | map!(take!(2), |a| (a[0] as u16 - 64) * 256 + a[1] as u16 + 64)
+    )
+);
+
 named!(empty_message<&[u8], EngineMessage>,
     do_parse!(tag!("\0") >> (Empty))
 );
 
-named!(non_empty_message<&[u8], EngineMessage>, length_value!(length_specifier,
-    alt!(
-          map!(synced_message, |m| Synced(m, 0))
-        | map!(unsynced_message, |m| Unsynced(m))
-        | map!(config_message, |m| Config(m))
-        | unrecognized_message
-    )
-));
+named!(non_empty_message<&[u8], EngineMessage>,
+    length_value!(length_specifier, unwrapped_message));
 
 named!(message<&[u8], EngineMessage>, alt!(
       empty_message
@@ -61,7 +79,7 @@
 
 #[test]
 fn parse_synced_messages() {
-    assert_eq!(message(b"\x04+l\x01\x01"), Ok((&b""[..], Synced(Left(Press), 0))));
+    assert_eq!(message(b"\x03L\x01\x02"), Ok((&b""[..], Synced(Left(Press), 258))));
 }
 
 #[test]