Add peer visibility to MultiplayerSynchronizer.

MultiplayerSynchronizers can now be configured to limit their visibility
to a subset of the connected peers, if the synchronized node was spawned
by a MultiplayerSpawner (either automatically or via custom spawn) the
given node will also be despawned remotely.

The replication system doesn't have the logic to handle subspawn
directly, but it is possible to handle them appropriately by manually
updating the visibility of the parent before changing the one of the
nested spawns via the "update_visibility" function.

The visibility of each MultiplayerSynchronizer can be controlled by
adding or remove filters via "[add|remove]_visibility_filter(callable)".

To further optimize the network code, visibility filters can be configured
to be automatically updated during idle or physics frame, or set to always
require manual update (via the "update_visibility" function).
This commit is contained in:
Fabio Alessandrelli
2022-07-09 20:45:30 +02:00
parent 351197bfe4
commit ddee5f6050
15 changed files with 482 additions and 128 deletions

View File

@@ -56,7 +56,8 @@ void SceneReplicationState::_untrack(const ObjectID &p_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);
E.value.sync_nodes.erase(p_id);
E.value.spawn_nodes.erase(p_id);
}
}
}
@@ -93,11 +94,6 @@ bool SceneReplicationState::update_sync_time(const ObjectID &p_id, uint64_t p_ms
return false;
}
const HashSet<ObjectID> SceneReplicationState::get_known_nodes(int p_peer) {
ERR_FAIL_COND_V(!peers_info.has(p_peer), HashSet<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);
@@ -147,8 +143,6 @@ Error SceneReplicationState::config_add_spawn(Node *p_node, MultiplayerSpawner *
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;
}
@@ -159,6 +153,9 @@ Error SceneReplicationState::config_del_spawn(Node *p_node, MultiplayerSpawner *
ERR_FAIL_COND_V(tobj.spawner != p_spawner->get_instance_id(), ERR_INVALID_PARAMETER);
tobj.spawner = ObjectID();
spawned_nodes.erase(oid);
for (KeyValue<int, PeerInfo> &E : peers_info) {
E.value.spawn_nodes.erase(oid);
}
return OK;
}
@@ -167,10 +164,7 @@ Error SceneReplicationState::config_add_sync(Node *p_node, MultiplayerSynchroniz
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);
}
synced_nodes.insert(oid);
return OK;
}
@@ -180,38 +174,57 @@ Error SceneReplicationState::config_del_sync(Node *p_node, MultiplayerSynchroniz
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);
synced_nodes.erase(oid);
for (KeyValue<int, PeerInfo> &E : peers_info) {
E.value.sync_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);
}
}
Error SceneReplicationState::peer_add_sync(int p_peer, const ObjectID &p_id) {
ERR_FAIL_COND_V(!peers_info.has(p_peer), ERR_INVALID_PARAMETER);
peers_info[p_peer].sync_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);
}
}
Error SceneReplicationState::peer_del_sync(int p_peer, const ObjectID &p_id) {
ERR_FAIL_COND_V(!peers_info.has(p_peer), ERR_INVALID_PARAMETER);
peers_info[p_peer].sync_nodes.erase(p_id);
return OK;
}
const HashSet<ObjectID> SceneReplicationState::get_peer_sync_nodes(int p_peer) {
ERR_FAIL_COND_V(!peers_info.has(p_peer), HashSet<ObjectID>());
return peers_info[p_peer].sync_nodes;
}
bool SceneReplicationState::is_peer_sync(int p_peer, const ObjectID &p_id) const {
ERR_FAIL_COND_V(!peers_info.has(p_peer), false);
return peers_info[p_peer].sync_nodes.has(p_id);
}
Error SceneReplicationState::peer_add_spawn(int p_peer, const ObjectID &p_id) {
ERR_FAIL_COND_V(!peers_info.has(p_peer), ERR_INVALID_PARAMETER);
peers_info[p_peer].spawn_nodes.insert(p_id);
return OK;
}
Error SceneReplicationState::peer_del_spawn(int p_peer, const ObjectID &p_id) {
ERR_FAIL_COND_V(!peers_info.has(p_peer), ERR_INVALID_PARAMETER);
peers_info[p_peer].spawn_nodes.erase(p_id);
return OK;
}
const HashSet<ObjectID> SceneReplicationState::get_peer_spawn_nodes(int p_peer) {
ERR_FAIL_COND_V(!peers_info.has(p_peer), HashSet<ObjectID>());
return peers_info[p_peer].spawn_nodes;
}
bool SceneReplicationState::is_peer_spawn(int p_peer, const ObjectID &p_id) const {
ERR_FAIL_COND_V(!peers_info.has(p_peer), false);
return peers_info[p_peer].spawn_nodes.has(p_id);
}
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;