]> git.scottworley.com Git - tattlekey/commitdiff
server: Combine ago & clock to get original event time
authorScott Worley <scottworley@scottworley.com>
Tue, 10 Oct 2023 22:45:38 +0000 (15:45 -0700)
committerScott Worley <scottworley@scottworley.com>
Wed, 11 Oct 2023 01:50:37 +0000 (18:50 -0700)
server/src/main.rs

index c4c0b19c07f9587a80090e2ea45f7944b6984abc..c31f873ba08a103cc0e6a2f856b2bb8a3debf44b 100644 (file)
@@ -15,6 +15,7 @@
 // along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
 use std::net::UdpSocket;
+use std::time::{Duration, SystemTime};
 
 const MESSAGE_SIZE: usize = 12;
 
@@ -29,17 +30,20 @@ struct MessageKey {
 struct Message {
     key: MessageKey,
     ago: u32,
+    t: SystemTime,
 }
 
 impl From<&[u8; MESSAGE_SIZE]> for Message {
     fn from(value: &[u8; MESSAGE_SIZE]) -> Self {
+        let ago = u32::from_be_bytes(value[8..=11].try_into().expect("I can't count"));
         Self {
             key: MessageKey {
                 epoch: u32::from_be_bytes(value[0..=3].try_into().expect("I can't count")),
                 device: u16::from_be_bytes(value[4..=5].try_into().expect("I can't count")),
                 seq: u16::from_be_bytes(value[6..=7].try_into().expect("I can't count")),
             },
-            ago: u32::from_be_bytes(value[8..=11].try_into().expect("I can't count")),
+            ago,
+            t: SystemTime::now() - Duration::new(ago.into(), 0),
         }
     }
 }