Protocol
Overview
The Protocol module in this library is responsible for defining the communication protocol used to send messages between the client and server. The Protocol must be shared between client and server, so that the messages can be serialized and deserialized correctly.
Key Concepts
Protocol Trait
A Protocol
contains multiple sub-parts:
-
Input
: Defines the user inputs, which is an enum of all the inputs that the client can send to the server. Input handling can be added by adding theInputPlugin
plugin:app.add_plugins(InputPlugin::<I>::default());
-
LeafwingInput
: (only if the featureleafwing
is enabled) Defines the leafwingActionState
that the client can send to the server. Input handling can be added by adding theLeafwingInputPlugin
plugin:app.add_plugins(LeafwingInputPlugin::<I>::default());
-
MessageRegistry
: Will hold metadata about the all the messages that can be sent over the network. Each message must beSerializable + Deserializeable + Clone
. You can register a message with the commandapp.add_message::<Message1>(ChannelDirection::Bidirectional);
-
Components
: Defines the component protocol, which is an enum of all the components that can be replicated between the client and server. Each component must beSerializable + Clone + Component
. You can register a component with:app.register_component::<PlayerId>(ChannelDirection::ServerToClient) .add_prediction::<PlayerId>(ComponentSyncMode::Once) .add_interpolation::<PlayerId>(ComponentSyncMode::Once);
(You can specify additional behaviour for the component, such as prediction or interpolation.)
-
Channels
: the protocol should also contain a list of channels to be used to send messages. AChannel
defines guarantees about how the packets will be sent over the network: reliably? in-order? etc. You can register a channel with:app.add_channel::<Channel1>(ChannelSettings { mode: ChannelMode::OrderedReliable(ReliableSettings::default()), ..default() });