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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
/*************************************************************************/
/* scene_replication_state.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "scene/multiplayer/scene_replication_state.h"
#include "core/multiplayer/multiplayer_api.h"
#include "scene/multiplayer/multiplayer_spawner.h"
#include "scene/multiplayer/multiplayer_synchronizer.h"
#include "scene/scene_string_names.h"
SceneReplicationState::TrackedNode &SceneReplicationState::_track(const ObjectID &p_id) {
if (!tracked_nodes.has(p_id)) {
tracked_nodes[p_id] = TrackedNode(p_id);
Node *node = Object::cast_to<Node>(ObjectDB::get_instance(p_id));
node->connect(SceneStringNames::get_singleton()->tree_exited, callable_mp(this, &SceneReplicationState::_untrack), varray(p_id), Node::CONNECT_ONESHOT);
}
return tracked_nodes[p_id];
}
void SceneReplicationState::_untrack(const ObjectID &p_id) {
if (tracked_nodes.has(p_id)) {
uint32_t net_id = tracked_nodes[p_id].net_id;
uint32_t peer = tracked_nodes[p_id].remote_peer;
tracked_nodes.erase(p_id);
// If it was spawned by a remote, remove it from the received nodes.
if (peer && peers_info.has(peer)) {
peers_info[peer].recv_nodes.erase(net_id);
}
// If we spawned or synced it, we need to remove it from any peer it was sent to.
if (net_id || peer == 0) {
for (KeyValue<int, PeerInfo> &E : peers_info) {
E.value.known_nodes.erase(p_id);
}
}
}
}
const HashMap<uint32_t, ObjectID> SceneReplicationState::peer_get_remotes(int p_peer) const {
return peers_info.has(p_peer) ? peers_info[p_peer].recv_nodes : HashMap<uint32_t, ObjectID>();
}
bool SceneReplicationState::update_last_node_sync(const ObjectID &p_id, uint16_t p_time) {
TrackedNode *tnode = tracked_nodes.getptr(p_id);
ERR_FAIL_COND_V(!tnode, false);
if (p_time <= tnode->last_sync && tnode->last_sync - p_time < 32767) {
return false;
}
tnode->last_sync = p_time;
return true;
}
bool SceneReplicationState::update_sync_time(const ObjectID &p_id, uint64_t p_msec) {
TrackedNode *tnode = tracked_nodes.getptr(p_id);
ERR_FAIL_COND_V(!tnode, false);
MultiplayerSynchronizer *sync = get_synchronizer(p_id);
if (!sync) {
return false;
}
if (tnode->last_sync_msec == p_msec) {
return true;
}
if (p_msec >= tnode->last_sync_msec + sync->get_replication_interval_msec()) {
tnode->last_sync_msec = p_msec;
return true;
}
return false;
}
const RBSet<ObjectID> SceneReplicationState::get_known_nodes(int p_peer) {
ERR_FAIL_COND_V(!peers_info.has(p_peer), RBSet<ObjectID>());
return peers_info[p_peer].known_nodes;
}
uint32_t SceneReplicationState::get_net_id(const ObjectID &p_id) const {
const TrackedNode *tnode = tracked_nodes.getptr(p_id);
ERR_FAIL_COND_V(!tnode, 0);
return tnode->net_id;
}
void SceneReplicationState::set_net_id(const ObjectID &p_id, uint32_t p_net_id) {
TrackedNode *tnode = tracked_nodes.getptr(p_id);
ERR_FAIL_COND(!tnode);
tnode->net_id = p_net_id;
}
uint32_t SceneReplicationState::ensure_net_id(const ObjectID &p_id) {
TrackedNode *tnode = tracked_nodes.getptr(p_id);
ERR_FAIL_COND_V(!tnode, 0);
if (tnode->net_id == 0) {
tnode->net_id = ++last_net_id;
}
return tnode->net_id;
}
void SceneReplicationState::on_peer_change(int p_peer, bool p_connected) {
if (p_connected) {
peers_info[p_peer] = PeerInfo();
known_peers.insert(p_peer);
} else {
peers_info.erase(p_peer);
known_peers.erase(p_peer);
}
}
void SceneReplicationState::reset() {
peers_info.clear();
known_peers.clear();
// Tracked nodes are cleared on deletion, here we only reset the ids so they can be later re-assigned.
for (KeyValue<ObjectID, TrackedNode> &E : tracked_nodes) {
TrackedNode &tobj = E.value;
tobj.net_id = 0;
tobj.remote_peer = 0;
tobj.last_sync = 0;
}
}
Error SceneReplicationState::config_add_spawn(Node *p_node, MultiplayerSpawner *p_spawner) {
const ObjectID oid = p_node->get_instance_id();
TrackedNode &tobj = _track(oid);
ERR_FAIL_COND_V(tobj.spawner != ObjectID(), ERR_ALREADY_IN_USE);
tobj.spawner = p_spawner->get_instance_id();
spawned_nodes.insert(oid);
// The spawner may be notified after the synchronizer.
path_only_nodes.erase(oid);
return OK;
}
Error SceneReplicationState::config_del_spawn(Node *p_node, MultiplayerSpawner *p_spawner) {
const ObjectID oid = p_node->get_instance_id();
ERR_FAIL_COND_V(!is_tracked(oid), ERR_INVALID_PARAMETER);
TrackedNode &tobj = _track(oid);
ERR_FAIL_COND_V(tobj.spawner != p_spawner->get_instance_id(), ERR_INVALID_PARAMETER);
tobj.spawner = ObjectID();
spawned_nodes.erase(oid);
return OK;
}
Error SceneReplicationState::config_add_sync(Node *p_node, MultiplayerSynchronizer *p_sync) {
const ObjectID oid = p_node->get_instance_id();
TrackedNode &tobj = _track(oid);
ERR_FAIL_COND_V(tobj.synchronizer != ObjectID(), ERR_ALREADY_IN_USE);
tobj.synchronizer = p_sync->get_instance_id();
// If it doesn't have a spawner, we might need to assign ID for this node using it's path.
if (tobj.spawner.is_null()) {
path_only_nodes.insert(oid);
}
return OK;
}
Error SceneReplicationState::config_del_sync(Node *p_node, MultiplayerSynchronizer *p_sync) {
const ObjectID oid = p_node->get_instance_id();
ERR_FAIL_COND_V(!is_tracked(oid), ERR_INVALID_PARAMETER);
TrackedNode &tobj = _track(oid);
ERR_FAIL_COND_V(tobj.synchronizer != p_sync->get_instance_id(), ERR_INVALID_PARAMETER);
tobj.synchronizer = ObjectID();
if (path_only_nodes.has(oid)) {
p_node->disconnect(SceneStringNames::get_singleton()->tree_exited, callable_mp(this, &SceneReplicationState::_untrack));
_untrack(oid);
path_only_nodes.erase(oid);
}
return OK;
}
Error SceneReplicationState::peer_add_node(int p_peer, const ObjectID &p_id) {
if (p_peer) {
ERR_FAIL_COND_V(!peers_info.has(p_peer), ERR_INVALID_PARAMETER);
peers_info[p_peer].known_nodes.insert(p_id);
} else {
for (KeyValue<int, PeerInfo> &E : peers_info) {
E.value.known_nodes.insert(p_id);
}
}
return OK;
}
Error SceneReplicationState::peer_del_node(int p_peer, const ObjectID &p_id) {
if (p_peer) {
ERR_FAIL_COND_V(!peers_info.has(p_peer), ERR_INVALID_PARAMETER);
peers_info[p_peer].known_nodes.erase(p_id);
} else {
for (KeyValue<int, PeerInfo> &E : peers_info) {
E.value.known_nodes.erase(p_id);
}
}
return OK;
}
Node *SceneReplicationState::peer_get_remote(int p_peer, uint32_t p_net_id) {
PeerInfo *info = peers_info.getptr(p_peer);
return info && info->recv_nodes.has(p_net_id) ? Object::cast_to<Node>(ObjectDB::get_instance(info->recv_nodes[p_net_id])) : nullptr;
}
Error SceneReplicationState::peer_add_remote(int p_peer, uint32_t p_net_id, Node *p_node, MultiplayerSpawner *p_spawner) {
ERR_FAIL_COND_V(!p_node || !p_spawner, ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V(!peers_info.has(p_peer), ERR_UNAVAILABLE);
PeerInfo &pinfo = peers_info[p_peer];
ObjectID oid = p_node->get_instance_id();
TrackedNode &tobj = _track(oid);
tobj.spawner = p_spawner->get_instance_id();
tobj.net_id = p_net_id;
tobj.remote_peer = p_peer;
tobj.last_sync = pinfo.last_recv_sync;
// Also track as a remote.
ERR_FAIL_COND_V(pinfo.recv_nodes.has(p_net_id), ERR_ALREADY_IN_USE);
pinfo.recv_nodes[p_net_id] = oid;
return OK;
}
Error SceneReplicationState::peer_del_remote(int p_peer, uint32_t p_net_id, Node **r_node) {
ERR_FAIL_COND_V(!peers_info.has(p_peer), ERR_UNAUTHORIZED);
PeerInfo &info = peers_info[p_peer];
ERR_FAIL_COND_V(!info.recv_nodes.has(p_net_id), ERR_UNAUTHORIZED);
*r_node = Object::cast_to<Node>(ObjectDB::get_instance(info.recv_nodes[p_net_id]));
info.recv_nodes.erase(p_net_id);
return OK;
}
uint16_t SceneReplicationState::peer_sync_next(int p_peer) {
ERR_FAIL_COND_V(!peers_info.has(p_peer), 0);
PeerInfo &info = peers_info[p_peer];
return ++info.last_sent_sync;
}
void SceneReplicationState::peer_sync_recv(int p_peer, uint16_t p_time) {
ERR_FAIL_COND(!peers_info.has(p_peer));
peers_info[p_peer].last_recv_sync = p_time;
}
|