|
@@ -1,7 +1,7 @@
|
|
use std::{
|
|
use std::{
|
|
any::Any,
|
|
any::Any,
|
|
collections::HashMap,
|
|
collections::HashMap,
|
|
- hash::{Hash, Hasher},
|
|
|
|
|
|
+ hash::{Hash, Hasher}, cell::RefCell, rc::Rc,
|
|
};
|
|
};
|
|
|
|
|
|
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
|
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
|
@@ -40,11 +40,16 @@ fn message_type<M: MessageParams>() -> u16 {
|
|
}
|
|
}
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
-pub struct Message {
|
|
|
|
- magic: u64,
|
|
|
|
|
|
+pub struct MessageContent {
|
|
pub(crate) ty: u16,
|
|
pub(crate) ty: u16,
|
|
- // pub(crate) crypto_header: crate::crypto::PacketHeader,
|
|
|
|
pub(crate) data: Vec<u8>,
|
|
pub(crate) data: Vec<u8>,
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
+pub struct Message {
|
|
|
|
+ magic: u64,
|
|
|
|
+ pub(crate) crypto_header: fleck_core::crypto::PacketHeader,
|
|
|
|
+ pub(crate) content: MessageContent,
|
|
|
|
|
|
#[serde(skip_serializing)]
|
|
#[serde(skip_serializing)]
|
|
#[serde(skip_deserializing)]
|
|
#[serde(skip_deserializing)]
|
|
@@ -63,9 +68,11 @@ impl Message {
|
|
pub fn build<M: 'static + MessageParams + Serialize>(from: M) -> Self {
|
|
pub fn build<M: 'static + MessageParams + Serialize>(from: M) -> Self {
|
|
Self {
|
|
Self {
|
|
magic: MESSAGE_MAGIC,
|
|
magic: MESSAGE_MAGIC,
|
|
- ty: message_type::<M>(),
|
|
|
|
- // crypto_header: Default::default(),
|
|
|
|
- data: bincode::serialize(&from).expect("couldn't serialize message"),
|
|
|
|
|
|
+ crypto_header: Default::default(),
|
|
|
|
+ content: MessageContent {
|
|
|
|
+ ty: message_type::<M>(),
|
|
|
|
+ data: bincode::serialize(&from).expect("couldn't serialize message"),
|
|
|
|
+ },
|
|
saved_params: Some(SavedMessageParams::save::<M>()),
|
|
saved_params: Some(SavedMessageParams::save::<M>()),
|
|
parsed: Some(Box::new(from)),
|
|
parsed: Some(Box::new(from)),
|
|
peer: None,
|
|
peer: None,
|
|
@@ -73,7 +80,7 @@ impl Message {
|
|
}
|
|
}
|
|
|
|
|
|
pub fn is_type<M: MessageParams + DeserializeOwned>(&self) -> bool {
|
|
pub fn is_type<M: MessageParams + DeserializeOwned>(&self) -> bool {
|
|
- self.ty == message_type::<M>()
|
|
|
|
|
|
+ self.content.ty == message_type::<M>()
|
|
}
|
|
}
|
|
|
|
|
|
pub fn downcast<M: MessageParams + DeserializeOwned>(&self) -> Option<&M> {
|
|
pub fn downcast<M: MessageParams + DeserializeOwned>(&self) -> Option<&M> {
|
|
@@ -81,33 +88,42 @@ impl Message {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+pub struct MessageChannelTag;
|
|
|
|
+pub type MessageChannel<M> = (MessageChannelTag, M);
|
|
|
|
+
|
|
type Deserializer = dyn Fn(&[u8]) -> Option<Box<dyn std::any::Any>>;
|
|
type Deserializer = dyn Fn(&[u8]) -> Option<Box<dyn std::any::Any>>;
|
|
#[derive(Default)]
|
|
#[derive(Default)]
|
|
-pub struct MessageRegistry {
|
|
|
|
- deser: HashMap<u16, Box<Deserializer>>,
|
|
|
|
|
|
+pub struct MessageService {
|
|
|
|
+ deser: RefCell<HashMap<u16, Box<Deserializer>>>,
|
|
}
|
|
}
|
|
|
|
|
|
-impl MessageRegistry {
|
|
|
|
|
|
+impl Service for Rc<MessageService> {
|
|
|
|
+ fn setup(&self, api: &crate::Fleck) {
|
|
|
|
+ api.channel::<fleck_core::ReceivePacketChannel>().sub_opt(fleck_core::ReceiveOrder::Parse, self, MessageService::parse);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl MessageService {
|
|
pub fn add_message_type<MT: std::fmt::Debug + MessageParams + Serialize + DeserializeOwned>(
|
|
pub fn add_message_type<MT: std::fmt::Debug + MessageParams + Serialize + DeserializeOwned>(
|
|
- &mut self,
|
|
|
|
|
|
+ &self, api: &crate::Fleck
|
|
) {
|
|
) {
|
|
let derived_typeid = message_type::<MT>();
|
|
let derived_typeid = message_type::<MT>();
|
|
- self.deser.insert(
|
|
|
|
|
|
+ self.deser.borrow_mut().insert(
|
|
derived_typeid,
|
|
derived_typeid,
|
|
Box::new(|data| Some(Box::new(bincode::deserialize::<MT>(data).ok()?))),
|
|
Box::new(|data| Some(Box::new(bincode::deserialize::<MT>(data).ok()?))),
|
|
);
|
|
);
|
|
|
|
+
|
|
|
|
+ api.create_channel::<MessageChannel<MT>>();
|
|
}
|
|
}
|
|
|
|
|
|
- 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;
|
|
|
|
- }
|
|
|
|
- }*/
|
|
|
|
|
|
+ fn parse(&self, api: &crate::Fleck, mut msg: Message) -> Option<Message> {
|
|
|
|
+ // try deserializing
|
|
|
|
+ msg.parsed = Some(self.deserialize(&mut msg)?);
|
|
|
|
+ None
|
|
|
|
+ }
|
|
|
|
|
|
- (self.deser.get(&message.ty)?)(&message.data)
|
|
|
|
|
|
+ fn deserialize(&self, message: &mut Message) -> Option<Box<dyn std::any::Any>> {
|
|
|
|
+ (self.deser.borrow().get(&message.content.ty)?)(&message.content.data)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|