|
@@ -1,41 +1,78 @@
|
|
|
use std::{
|
|
|
any::Any,
|
|
|
+ collections::HashMap,
|
|
|
hash::{Hash, Hasher},
|
|
|
};
|
|
|
|
|
|
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
|
|
-use std::collections::HashMap;
|
|
|
+
|
|
|
+use crate::prelude::*;
|
|
|
|
|
|
const MESSAGE_MAGIC: u64 = 0x1234123412341234;
|
|
|
|
|
|
+pub trait MessageParams: 'static {
|
|
|
+ const NAME: &'static str;
|
|
|
+ const ENCRYPTED: bool = true;
|
|
|
+ const SIGNED: bool = false;
|
|
|
+}
|
|
|
+
|
|
|
+#[derive(Clone, Copy, Debug)]
|
|
|
+pub(crate) struct SavedMessageParams {
|
|
|
+ pub(crate) name: &'static str,
|
|
|
+ pub(crate) encrypted: bool,
|
|
|
+ pub(crate) signed: bool,
|
|
|
+}
|
|
|
+
|
|
|
+impl SavedMessageParams {
|
|
|
+ fn save<M: MessageParams>() -> Self {
|
|
|
+ Self {
|
|
|
+ name: M::NAME,
|
|
|
+ encrypted: M::ENCRYPTED,
|
|
|
+ signed: M::SIGNED,
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
fn message_type<M: MessageParams>() -> u16 {
|
|
|
let mut hasher = std::collections::hash_map::DefaultHasher::new();
|
|
|
M::NAME.hash(&mut hasher);
|
|
|
(hasher.finish() & 0xffff) as u16
|
|
|
}
|
|
|
|
|
|
-#[derive(Serialize, Deserialize)]
|
|
|
+#[derive(Serialize, Deserialize, Debug)]
|
|
|
pub struct Message {
|
|
|
magic: u64,
|
|
|
- ty: u16,
|
|
|
- data: Vec<u8>,
|
|
|
+ pub(crate) ty: u16,
|
|
|
+ pub(crate) crypto_header: crate::crypto::PacketHeader,
|
|
|
+ pub(crate) data: Vec<u8>,
|
|
|
+
|
|
|
+ #[serde(skip_serializing)]
|
|
|
+ #[serde(skip_deserializing)]
|
|
|
+ pub(crate) saved_params: Option<SavedMessageParams>,
|
|
|
+
|
|
|
+ #[serde(skip_serializing)]
|
|
|
+ #[serde(skip_deserializing)]
|
|
|
+ pub(crate) parsed: Option<Box<dyn Any>>,
|
|
|
}
|
|
|
|
|
|
impl Message {
|
|
|
- pub fn build<M: MessageParams + Serialize>(from: M) -> Self {
|
|
|
+ pub fn build<M: 'static + MessageParams + Serialize>(from: M) -> Self {
|
|
|
Self {
|
|
|
magic: MESSAGE_MAGIC,
|
|
|
ty: message_type::<M>(),
|
|
|
- data: vec![],
|
|
|
+ crypto_header: Default::default(),
|
|
|
+ data: bincode::serialize(&from).expect("couldn't serialize message"),
|
|
|
+ saved_params: Some(SavedMessageParams::save::<M>()),
|
|
|
+ parsed: Some(Box::new(from)),
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- pub fn is_type<M: MessageParams>(&self) -> bool {
|
|
|
+ pub fn is_type<M: MessageParams + DeserializeOwned>(&self) -> bool {
|
|
|
self.ty == message_type::<M>()
|
|
|
}
|
|
|
|
|
|
- pub fn downcast<M: MessageParams + DeserializeOwned>(&self) -> Option<M> {
|
|
|
- todo!()
|
|
|
+ pub fn downcast<M: MessageParams + DeserializeOwned>(&self) -> Option<&M> {
|
|
|
+ self.parsed.as_ref()?.downcast_ref()
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -45,28 +82,63 @@ pub struct MessageRegistry {
|
|
|
deser: HashMap<u16, Box<Deserializer>>,
|
|
|
}
|
|
|
|
|
|
-pub trait MessageParams {
|
|
|
- const NAME: &'static str;
|
|
|
- const ENCRYPTED: bool = true;
|
|
|
-}
|
|
|
-
|
|
|
impl MessageRegistry {
|
|
|
- pub fn add_message_type<MT: MessageParams + Serialize + DeserializeOwned>(&mut self) {
|
|
|
+ pub fn add_message_type<MT: std::fmt::Debug + MessageParams + Serialize + DeserializeOwned>(&mut self) {
|
|
|
let derived_typeid = message_type::<MT>();
|
|
|
self.deser.insert(
|
|
|
derived_typeid,
|
|
|
Box::new(|data| {
|
|
|
- let boxed: Box<dyn Any> = Box::new(bincode::deserialize(data).ok()?);
|
|
|
- Some(boxed)
|
|
|
+ Some(Box::new(bincode::deserialize::<MT>(data).ok()?))
|
|
|
}),
|
|
|
);
|
|
|
}
|
|
|
|
|
|
- pub(crate) fn deserialize(&self, data: &[u8]) -> Option<Box<dyn std::any::Any>> {
|
|
|
- let msg_type = u16::from_le_bytes(data[0..1].try_into().unwrap());
|
|
|
- match self.deser.get(&msg_type) {
|
|
|
- Some(f) => f(&data[2..]),
|
|
|
- None => None,
|
|
|
+ pub(crate) fn deserialize(&self, message: &mut Message) -> Option<Box<dyn std::any::Any>> {
|
|
|
+ match message.crypto_header {
|
|
|
+ crate::crypto::PacketHeader::NoCrypto => {}
|
|
|
+ _ => {
|
|
|
+ log::error!("given packet with crypto still applied to deserialize!");
|
|
|
+ return None;
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+ (self.deser.get(&message.ty)?)(&message.data)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#[derive(Default)]
|
|
|
+pub(crate) struct ParsePacket {}
|
|
|
+
|
|
|
+impl ParsePacket {}
|
|
|
+
|
|
|
+impl Service for std::rc::Rc<ParsePacket> {
|
|
|
+ fn register_channels(&self, eroot: &mut super::EventRoot) {
|
|
|
+ eroot
|
|
|
+ .priority_channel::<fleck_core::ReceivePacketChannel>()
|
|
|
+ .with::<order::Preprocessing>()
|
|
|
+ .subscribe(self, ParsePacket::process_incoming);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl ParsePacket {
|
|
|
+ fn process_incoming(&self, api: &crate::Fleck, event: &mut FleckEvent<FleckPacket>) {
|
|
|
+ // try deserializing
|
|
|
+ let message: Option<crate::msg::Message> =
|
|
|
+ bincode::deserialize(event.data.data.as_ref().unwrap()).ok();
|
|
|
+ if message.is_none() {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ let mut message = message.unwrap();
|
|
|
+ // get message registry from api and add parsed Message
|
|
|
+ message.parsed = api.services().message_registry().deserialize(&mut message);
|
|
|
+ // if the parse failed, stop processing this packet.
|
|
|
+ if message.parsed.is_none() {
|
|
|
+ event.emptied = true;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // remove raw data from packet
|
|
|
+ event.data.data.take();
|
|
|
+
|
|
|
+ event.data.msg = Some(message);
|
|
|
}
|
|
|
}
|