1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*!
Provides a [`ProtocolMessage`] enum that is a wrapper around all the possible messages that can be sent over the network
*/
use crate::_reexport::{InputMessage, ShouldBeInterpolated, ShouldBePredicted, TickManager};
use crate::connection::events::ConnectionEvents;
use crate::prelude::{EntityMap, MapEntities, Tick};
use crate::protocol::channel::ChannelKind;
use crate::protocol::Protocol;
use crate::shared::ping::message::SyncMessage;
use crate::shared::replication::{ReplicationMessage, ReplicationMessageData};
use crate::shared::time_manager::TimeManager;
use crate::utils::named::Named;
use serde::{Deserialize, Serialize};
use tracing::{info, info_span, trace, trace_span};
// pub enum InternalMessage<P: Protocol> {
// InputMessage(InputMessage<P::Input>),
// }
//
// pub enum InternalReplication<P: Protocol> {
// ShouldBePredicted(ShouldBePredicted),
// ShouldBeInterpolated(ShouldBeInterpolated),
// }
#[cfg_attr(feature = "debug", derive(Debug))]
#[derive(Serialize, Deserialize, Clone)]
pub enum ProtocolMessage<P: Protocol> {
Message(P::Message),
Replication(ReplicationMessage<P::Components, P::ComponentKinds>),
// the reason why we include sync here instead of doing another MessageManager is so that
// the sync messages can be added to packets that have other messages
Sync(SyncMessage),
}
impl<P: Protocol> MapEntities for ProtocolMessage<P> {
fn map_entities(&mut self, entity_map: &EntityMap) {
match self {
ProtocolMessage::Message(x) => {
x.map_entities(entity_map);
}
ProtocolMessage::Replication(x) => {
x.map_entities(entity_map);
}
ProtocolMessage::Sync(x) => {
x.map_entities(entity_map);
}
}
}
}
impl<P: Protocol> ProtocolMessage<P> {
pub(crate) fn emit_send_logs(&self, channel_name: &str) {
match self {
ProtocolMessage::Message(message) => {
let message_name = message.name();
trace!(channel = ?channel_name, message = ?message_name, "Sending message");
#[cfg(metrics)]
metrics::increment_counter!("send_message", "channel" => channel_name, "message" => message_name);
}
ProtocolMessage::Replication(message) => {
let _span = info_span!("send replication message", channel = ?channel_name, group_id = ?message.group_id);
#[cfg(metrics)]
metrics::increment_counter!("send_replication_actions");
match &message.data {
ReplicationMessageData::Actions(m) => {
for (entity, actions) in &m.actions {
let _span = info_span!("send replication actions", ?entity);
if actions.spawn {
trace!("Send entity spawn");
#[cfg(metrics)]
metrics::increment_counter!("send_entity_spawn");
}
if actions.despawn {
trace!("Send entity despawn");
#[cfg(metrics)]
metrics::increment_counter!("send_entity_despawn");
}
if !actions.insert.is_empty() {
let components = actions
.insert
.iter()
.map(|c| c.into())
.collect::<Vec<P::ComponentKinds>>();
trace!(?components, "Sending component insert");
#[cfg(metrics)]
{
for component in components {
metrics::increment_counter!("send_component_insert", "component" => kind);
}
}
}
if !actions.remove.is_empty() {
trace!(?actions.remove, "Sending component remove");
#[cfg(metrics)]
{
for kind in actions.remove {
metrics::increment_counter!("send_component_remove", "component" => kind);
}
}
}
if !actions.updates.is_empty() {
let components = actions
.updates
.iter()
.map(|c| c.into())
.collect::<Vec<P::ComponentKinds>>();
trace!(?components, "Sending component update");
#[cfg(metrics)]
{
for component in components {
metrics::increment_counter!("send_component_update", "component" => kind);
}
}
}
}
}
ReplicationMessageData::Updates(m) => {
for (entity, updates) in &m.updates {
let _span = info_span!("send replication updates", ?entity);
let components = updates
.iter()
.map(|c| c.into())
.collect::<Vec<P::ComponentKinds>>();
trace!(?components, "Sending component update");
#[cfg(metrics)]
{
for component in components {
metrics::increment_counter!("send_component_update", "component" => kind);
}
}
}
}
}
}
ProtocolMessage::Sync(message) => match message {
SyncMessage::Ping(_) => {
trace!(channel = ?channel_name, "Sending ping");
#[cfg(metrics)]
metrics::increment_counter!("send_ping", "channel" => channel_name);
}
SyncMessage::Pong(_) => {
trace!(channel = ?channel_name, "Sending pong");
#[cfg(metrics)]
metrics::increment_counter!("send_pong", "channel" => channel_name);
}
},
}
}
// pub(crate) fn push_to_events(
// self,
// channel_kind: ChannelKind,
// events: &mut ConnectionEvents<P>,
// entity_map: &EntityMap,
// time_manager: &TimeManager,
// // tick of the remote when they sent that message. This means this message represents
// // the state of the remote world for this tick
// tick: Tick,
// ) {
// let _span = trace_span!("receive");
// match self {
// ProtocolMessage::Message(message) => {
// events.push_message(channel_kind, message);
// }
// ProtocolMessage::Replication(replication) => match replication {
// ReplicationMessage::SpawnEntity(entity, components) => {
// // convert the remote entity to the local before sending to events
// // if we can't find the local entity, just use the remote
// let local_entity = *entity_map.get_local(entity).unwrap_or(&entity);
// events.push_spawn(local_entity);
// for component in components {
// let kind: P::ComponentKinds = (&component).into();
// events.push_insert_component(local_entity, kind, tick);
// }
// }
// ReplicationMessage::DespawnEntity(entity) => {
// let local_entity = *entity_map.get_local(entity).unwrap_or(&entity);
// events.push_despawn(local_entity);
// }
// ReplicationMessage::InsertComponent(entity, components) => {
// let local_entity = *entity_map.get_local(entity).unwrap_or(&entity);
// for component in components {
// let kind: P::ComponentKinds = (&component).into();
// events.push_insert_component(local_entity, kind, tick);
// }
// }
// ReplicationMessage::RemoveComponent(entity, component_kinds) => {
// let local_entity = *entity_map.get_local(entity).unwrap_or(&entity);
// for component_kind in component_kinds {
// events.push_remove_component(local_entity, component_kind, tick);
// }
// }
// ReplicationMessage::EntityUpdate(entity, components) => {
// let local_entity = *entity_map.get_local(entity).unwrap_or(&entity);
// for component in components {
// let kind: P::ComponentKinds = (&component).into();
// events.push_update_component(local_entity, kind, tick);
// }
// }
// },
// _ => {}
// }
// }
}