lightyear_messages/lib.rs
1//! # Lightyear Messages
2//!
3//! This crate provides a [`MessagePlugin`](crate::plugin::MessagePlugin) to handle sending and receiving messages over the network.
4//!
5//! A [`Message`] is simply any type that can be (de)serialized.
6//!
7//! You can add the [`MessageSender<M>`](send::MessageSender) or [`MessageReceiver<M>`](receive::MessageReceiver) components to your Link entity to enable sending and receiving messages of type `M`.
8//!
9//! The crate also provides a [`MessageManager`] component that manages the process of sending and receiving messages for an entity.
10//! It stores a [`RemoteEntityMap`] that holds a mapping between the local and remote entities.
11
12#![no_std]
13
14extern crate alloc;
15#[cfg(feature = "std")]
16extern crate std;
17
18use bevy_ecs::component::{Component, ComponentId};
19use bevy_reflect::Reflect;
20
21use crate::registry::MessageKind;
22use alloc::vec::Vec;
23use lightyear_core::network::NetId;
24use lightyear_serde::entity_map::RemoteEntityMap;
25use lightyear_transport::prelude::Transport;
26
27#[cfg(feature = "client")]
28mod client;
29pub mod multi;
30pub mod plugin;
31pub mod receive;
32mod receive_event;
33pub mod registry;
34pub mod send;
35mod send_trigger;
36#[cfg(feature = "server")]
37pub mod server;
38mod trigger;
39pub mod prelude {
40 pub use crate::plugin::MessageSystems;
41 pub use crate::receive::MessageReceiver;
42 pub use crate::receive_event::RemoteEvent;
43 pub use crate::registry::{AppMessageExt, MessageRegistry};
44 pub use crate::send::MessageSender;
45 pub use crate::send_trigger::EventSender;
46 pub use crate::trigger::AppTriggerExt;
47 pub use crate::{Message, MessageManager};
48
49 #[cfg(feature = "server")]
50 pub use crate::server::ServerMultiMessageSender;
51}
52
53// send-trigger: prepare message TriggerEvent<M> to be sent.
54// if TriggerEvent<M> is added, we update `sender_id` with MessageSender<RemoteMessage<M>>.
55
56// TODO: for now messages must be able to be used as events, since we output them in our message events
57/// A [`Message`] is basically any type that can be (de)serialized over the network.
58///
59/// Every type that can be sent over the network must implement this trait.
60///
61pub trait Message: Send + Sync + 'static {}
62impl<T: Send + Sync + 'static> Message for T {}
63
64// // Internal id that we assign to each message sent over the network
65// wrapping_id!(MessageId);
66// TODO: this conflicts with the MessageId from lightyear_transport! find a different name
67pub type MessageNetId = NetId;
68
69/// Manages sending and receiving messages for an entity.
70///
71/// This component is added to entities that need to send or receive messages.
72/// It keeps track of the [`MessageSender<M>`](send::MessageSender) and [`MessageReceiver<M>`](receive::MessageReceiver) components
73/// attached to the entity, allowing the messaging system to interact with them.
74/// It also holds a [`RemoteEntityMap`] for mapping entities between client and server.
75#[derive(Component, Default, Reflect)]
76#[require(Transport)]
77pub struct MessageManager {
78 /// List of component ids of the [`MessageSender<M>`](send::MessageSender) present on this entity
79 pub(crate) send_messages: Vec<(MessageKind, ComponentId)>,
80 /// List of component ids of the [`TriggerSender<M>`](send_trigger::EventSender) present on this entity
81 pub(crate) send_triggers: Vec<(MessageKind, ComponentId)>,
82 /// List of component ids of the [`MessageReceiver<M>`](receive::MessageReceiver) present on this entity
83 pub(crate) receive_messages: Vec<(MessageKind, ComponentId)>,
84 pub entity_mapper: RemoteEntityMap,
85}