You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-04 12:00:25 +00:00
[MP] Optimize multiplayer NodePath caching
Only use paths during network transfer. Use ObjectID instead of NodePaths for storing the Node <-> NetID relations locally.
This commit is contained in:
@@ -44,9 +44,8 @@ void SceneCacheInterface::on_peer_change(int p_id, bool p_connected) {
|
||||
path_get_cache.erase(p_id);
|
||||
// Cleanup sent cache.
|
||||
// Some refactoring is needed to make this faster and do paths GC.
|
||||
for (const KeyValue<NodePath, PathSentCache> &E : path_send_cache) {
|
||||
PathSentCache *psc = path_send_cache.getptr(E.key);
|
||||
psc->confirmed_peers.erase(p_id);
|
||||
for (KeyValue<ObjectID, PathSentCache> &E : path_send_cache) {
|
||||
E.value.confirmed_peers.erase(p_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -67,7 +66,7 @@ void SceneCacheInterface::process_simplify_path(int p_from, const uint8_t *p_pac
|
||||
String paths;
|
||||
paths.parse_utf8((const char *)(p_packet + ofs), p_packet_len - ofs);
|
||||
|
||||
NodePath path = paths;
|
||||
const NodePath path = paths;
|
||||
|
||||
if (!path_get_cache.has(p_from)) {
|
||||
path_get_cache[p_from] = PathGetCache();
|
||||
@@ -81,7 +80,7 @@ void SceneCacheInterface::process_simplify_path(int p_from, const uint8_t *p_pac
|
||||
}
|
||||
|
||||
PathGetCache::NodeInfo ni;
|
||||
ni.path = path;
|
||||
ni.path = node->get_path();
|
||||
|
||||
path_get_cache[p_from].nodes[id] = ni;
|
||||
|
||||
@@ -106,19 +105,24 @@ void SceneCacheInterface::process_simplify_path(int p_from, const uint8_t *p_pac
|
||||
|
||||
void SceneCacheInterface::process_confirm_path(int p_from, const uint8_t *p_packet, int p_packet_len) {
|
||||
ERR_FAIL_COND_MSG(p_packet_len < 3, "Invalid packet received. Size too small.");
|
||||
Node *root_node = SceneTree::get_singleton()->get_root()->get_node(multiplayer->get_root_path());
|
||||
ERR_FAIL_NULL(root_node);
|
||||
|
||||
const bool valid_rpc_checksum = p_packet[1];
|
||||
|
||||
String paths;
|
||||
paths.parse_utf8((const char *)&p_packet[2], p_packet_len - 2);
|
||||
|
||||
NodePath path = paths;
|
||||
const NodePath path = paths;
|
||||
|
||||
if (valid_rpc_checksum == false) {
|
||||
ERR_PRINT("The rpc node checksum failed. Make sure to have the same methods on both nodes. Node path: " + path);
|
||||
}
|
||||
|
||||
PathSentCache *psc = path_send_cache.getptr(path);
|
||||
Node *node = root_node->get_node(path);
|
||||
ERR_FAIL_NULL(node);
|
||||
|
||||
PathSentCache *psc = path_send_cache.getptr(node->get_instance_id());
|
||||
ERR_FAIL_NULL_MSG(psc, "Invalid packet received. Tries to confirm a path which was not found in cache.");
|
||||
|
||||
HashMap<int, bool>::Iterator E = psc->confirmed_peers.find(p_from);
|
||||
@@ -126,9 +130,9 @@ void SceneCacheInterface::process_confirm_path(int p_from, const uint8_t *p_pack
|
||||
E->value = true;
|
||||
}
|
||||
|
||||
Error SceneCacheInterface::_send_confirm_path(Node *p_node, NodePath p_path, PathSentCache *psc, const List<int> &p_peers) {
|
||||
Error SceneCacheInterface::_send_confirm_path(Node *p_node, PathSentCache *psc, const List<int> &p_peers) {
|
||||
// Encode function name.
|
||||
const CharString path = String(p_path).utf8();
|
||||
const CharString path = String(multiplayer->get_root_path().rel_path_to(p_node->get_path())).utf8();
|
||||
const int path_len = encode_cstring(path.get_data(), nullptr);
|
||||
|
||||
// Extract MD5 from rpc methods list.
|
||||
@@ -163,8 +167,9 @@ Error SceneCacheInterface::_send_confirm_path(Node *p_node, NodePath p_path, Pat
|
||||
return err;
|
||||
}
|
||||
|
||||
bool SceneCacheInterface::is_cache_confirmed(NodePath p_path, int p_peer) {
|
||||
const PathSentCache *psc = path_send_cache.getptr(p_path);
|
||||
bool SceneCacheInterface::is_cache_confirmed(Node *p_node, int p_peer) {
|
||||
ERR_FAIL_NULL_V(p_node, false);
|
||||
const PathSentCache *psc = path_send_cache.getptr(p_node->get_instance_id());
|
||||
ERR_FAIL_NULL_V(psc, false);
|
||||
HashMap<int, bool>::ConstIterator F = psc->confirmed_peers.find(p_peer);
|
||||
ERR_FAIL_COND_V(!F, false); // Should never happen.
|
||||
@@ -174,13 +179,13 @@ bool SceneCacheInterface::is_cache_confirmed(NodePath p_path, int p_peer) {
|
||||
int SceneCacheInterface::make_object_cache(Object *p_obj) {
|
||||
Node *node = Object::cast_to<Node>(p_obj);
|
||||
ERR_FAIL_NULL_V(node, -1);
|
||||
NodePath for_path = multiplayer->get_root_path().rel_path_to(node->get_path());
|
||||
const ObjectID oid = node->get_instance_id();
|
||||
// See if the path is cached.
|
||||
PathSentCache *psc = path_send_cache.getptr(for_path);
|
||||
PathSentCache *psc = path_send_cache.getptr(oid);
|
||||
if (!psc) {
|
||||
// Path is not cached, create.
|
||||
path_send_cache[for_path] = PathSentCache();
|
||||
psc = path_send_cache.getptr(for_path);
|
||||
path_send_cache[oid] = PathSentCache();
|
||||
psc = path_send_cache.getptr(oid);
|
||||
psc->id = last_send_cache_id++;
|
||||
}
|
||||
return psc->id;
|
||||
@@ -189,11 +194,16 @@ int SceneCacheInterface::make_object_cache(Object *p_obj) {
|
||||
bool SceneCacheInterface::send_object_cache(Object *p_obj, int p_peer_id, int &r_id) {
|
||||
Node *node = Object::cast_to<Node>(p_obj);
|
||||
ERR_FAIL_NULL_V(node, false);
|
||||
|
||||
r_id = make_object_cache(p_obj);
|
||||
ERR_FAIL_COND_V(r_id < 0, false);
|
||||
NodePath for_path = multiplayer->get_root_path().rel_path_to(node->get_path());
|
||||
PathSentCache *psc = path_send_cache.getptr(for_path);
|
||||
const ObjectID oid = node->get_instance_id();
|
||||
// See if the path is cached.
|
||||
PathSentCache *psc = path_send_cache.getptr(oid);
|
||||
if (!psc) {
|
||||
// Path is not cached, create.
|
||||
path_send_cache[oid] = PathSentCache();
|
||||
psc = path_send_cache.getptr(oid);
|
||||
psc->id = last_send_cache_id++;
|
||||
}
|
||||
r_id = psc->id;
|
||||
|
||||
bool has_all_peers = true;
|
||||
List<int> peers_to_add; // If one is missing, take note to add it.
|
||||
@@ -225,7 +235,7 @@ bool SceneCacheInterface::send_object_cache(Object *p_obj, int p_peer_id, int &r
|
||||
}
|
||||
|
||||
if (peers_to_add.size()) {
|
||||
_send_confirm_path(node, for_path, psc, peers_to_add);
|
||||
_send_confirm_path(node, psc, peers_to_add);
|
||||
}
|
||||
|
||||
return has_all_peers;
|
||||
|
||||
Reference in New Issue
Block a user