naia_shared/messages/
message.rs

1use std::{any::Any, collections::HashSet};
2
3use naia_serde::{BitReader, BitWrite, SerdeErr};
4
5use crate::{
6    messages::{
7        message_kinds::{MessageKind, MessageKinds},
8        named::Named,
9    },
10    world::entity::entity_converters::LocalEntityAndGlobalEntityConverterMut,
11    LocalEntityAndGlobalEntityConverter, MessageContainer, RemoteEntity,
12};
13
14// MessageBuilder
15pub trait MessageBuilder: Send + Sync {
16    /// Create new Message from incoming bit stream
17    fn read(
18        &self,
19        reader: &mut BitReader,
20        converter: &dyn LocalEntityAndGlobalEntityConverter,
21    ) -> Result<MessageContainer, SerdeErr>;
22}
23
24// Message
25pub trait Message: Send + Sync + Named + MessageClone + Any {
26    /// Gets the MessageKind of this type
27    fn kind(&self) -> MessageKind;
28    fn to_boxed_any(self: Box<Self>) -> Box<dyn Any>;
29    fn create_builder() -> Box<dyn MessageBuilder>
30    where
31        Self: Sized;
32    fn bit_length(&self, converter: &mut dyn LocalEntityAndGlobalEntityConverterMut) -> u32;
33    fn is_fragment(&self) -> bool;
34    fn is_request(&self) -> bool;
35    /// Writes data into an outgoing byte stream
36    fn write(
37        &self,
38        message_kinds: &MessageKinds,
39        writer: &mut dyn BitWrite,
40        converter: &mut dyn LocalEntityAndGlobalEntityConverterMut,
41    );
42    /// Returns a list of LocalEntities contained within the Message's EntityProperty fields, which are waiting to be converted to GlobalEntities
43    fn relations_waiting(&self) -> Option<HashSet<RemoteEntity>>;
44    /// Converts any LocalEntities contained within the Message's EntityProperty fields to GlobalEntities
45    fn relations_complete(&mut self, converter: &dyn LocalEntityAndGlobalEntityConverter);
46    // /// Returns whether has any EntityRelations
47    // fn has_entity_relations(&self) -> bool;
48    // /// Returns a list of Entities contained within the Message's EntityRelation fields
49    // fn entities(&self) -> Vec<GlobalEntity>;
50}
51
52// Named
53impl Named for Box<dyn Message> {
54    fn name(&self) -> String {
55        self.as_ref().name()
56    }
57}
58
59// MessageClone
60pub trait MessageClone {
61    fn clone_box(&self) -> Box<dyn Message>;
62}
63
64impl<T: 'static + Clone + Message> MessageClone for T {
65    fn clone_box(&self) -> Box<dyn Message> {
66        Box::new(self.clone())
67    }
68}
69
70impl Clone for Box<dyn Message> {
71    fn clone(&self) -> Box<dyn Message> {
72        MessageClone::clone_box(self.as_ref())
73    }
74}