[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:
parent
d80ce0c52a
commit
61cf7d180c
3 changed files with 36 additions and 27 deletions
|
@ -44,9 +44,8 @@ void SceneCacheInterface::on_peer_change(int p_id, bool p_connected) {
|
||||||
path_get_cache.erase(p_id);
|
path_get_cache.erase(p_id);
|
||||||
// Cleanup sent cache.
|
// Cleanup sent cache.
|
||||||
// Some refactoring is needed to make this faster and do paths GC.
|
// Some refactoring is needed to make this faster and do paths GC.
|
||||||
for (const KeyValue<NodePath, PathSentCache> &E : path_send_cache) {
|
for (KeyValue<ObjectID, PathSentCache> &E : path_send_cache) {
|
||||||
PathSentCache *psc = path_send_cache.getptr(E.key);
|
E.value.confirmed_peers.erase(p_id);
|
||||||
psc->confirmed_peers.erase(p_id);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,7 +66,7 @@ void SceneCacheInterface::process_simplify_path(int p_from, const uint8_t *p_pac
|
||||||
String paths;
|
String paths;
|
||||||
paths.parse_utf8((const char *)(p_packet + ofs), p_packet_len - ofs);
|
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)) {
|
if (!path_get_cache.has(p_from)) {
|
||||||
path_get_cache[p_from] = PathGetCache();
|
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;
|
PathGetCache::NodeInfo ni;
|
||||||
ni.path = path;
|
ni.path = node->get_path();
|
||||||
|
|
||||||
path_get_cache[p_from].nodes[id] = ni;
|
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) {
|
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.");
|
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];
|
const bool valid_rpc_checksum = p_packet[1];
|
||||||
|
|
||||||
String paths;
|
String paths;
|
||||||
paths.parse_utf8((const char *)&p_packet[2], p_packet_len - 2);
|
paths.parse_utf8((const char *)&p_packet[2], p_packet_len - 2);
|
||||||
|
|
||||||
NodePath path = paths;
|
const NodePath path = paths;
|
||||||
|
|
||||||
if (valid_rpc_checksum == false) {
|
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);
|
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.");
|
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);
|
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;
|
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.
|
// 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);
|
const int path_len = encode_cstring(path.get_data(), nullptr);
|
||||||
|
|
||||||
// Extract MD5 from rpc methods list.
|
// Extract MD5 from rpc methods list.
|
||||||
|
@ -163,8 +167,9 @@ Error SceneCacheInterface::_send_confirm_path(Node *p_node, NodePath p_path, Pat
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SceneCacheInterface::is_cache_confirmed(NodePath p_path, int p_peer) {
|
bool SceneCacheInterface::is_cache_confirmed(Node *p_node, int p_peer) {
|
||||||
const PathSentCache *psc = path_send_cache.getptr(p_path);
|
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);
|
ERR_FAIL_NULL_V(psc, false);
|
||||||
HashMap<int, bool>::ConstIterator F = psc->confirmed_peers.find(p_peer);
|
HashMap<int, bool>::ConstIterator F = psc->confirmed_peers.find(p_peer);
|
||||||
ERR_FAIL_COND_V(!F, false); // Should never happen.
|
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) {
|
int SceneCacheInterface::make_object_cache(Object *p_obj) {
|
||||||
Node *node = Object::cast_to<Node>(p_obj);
|
Node *node = Object::cast_to<Node>(p_obj);
|
||||||
ERR_FAIL_NULL_V(node, -1);
|
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.
|
// See if the path is cached.
|
||||||
PathSentCache *psc = path_send_cache.getptr(for_path);
|
PathSentCache *psc = path_send_cache.getptr(oid);
|
||||||
if (!psc) {
|
if (!psc) {
|
||||||
// Path is not cached, create.
|
// Path is not cached, create.
|
||||||
path_send_cache[for_path] = PathSentCache();
|
path_send_cache[oid] = PathSentCache();
|
||||||
psc = path_send_cache.getptr(for_path);
|
psc = path_send_cache.getptr(oid);
|
||||||
psc->id = last_send_cache_id++;
|
psc->id = last_send_cache_id++;
|
||||||
}
|
}
|
||||||
return psc->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) {
|
bool SceneCacheInterface::send_object_cache(Object *p_obj, int p_peer_id, int &r_id) {
|
||||||
Node *node = Object::cast_to<Node>(p_obj);
|
Node *node = Object::cast_to<Node>(p_obj);
|
||||||
ERR_FAIL_NULL_V(node, false);
|
ERR_FAIL_NULL_V(node, false);
|
||||||
|
const ObjectID oid = node->get_instance_id();
|
||||||
r_id = make_object_cache(p_obj);
|
// See if the path is cached.
|
||||||
ERR_FAIL_COND_V(r_id < 0, false);
|
PathSentCache *psc = path_send_cache.getptr(oid);
|
||||||
NodePath for_path = multiplayer->get_root_path().rel_path_to(node->get_path());
|
if (!psc) {
|
||||||
PathSentCache *psc = path_send_cache.getptr(for_path);
|
// 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;
|
bool has_all_peers = true;
|
||||||
List<int> peers_to_add; // If one is missing, take note to add it.
|
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()) {
|
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;
|
return has_all_peers;
|
||||||
|
|
|
@ -58,12 +58,12 @@ private:
|
||||||
HashMap<int, NodeInfo> nodes;
|
HashMap<int, NodeInfo> nodes;
|
||||||
};
|
};
|
||||||
|
|
||||||
HashMap<NodePath, PathSentCache> path_send_cache;
|
HashMap<ObjectID, PathSentCache> path_send_cache;
|
||||||
HashMap<int, PathGetCache> path_get_cache;
|
HashMap<int, PathGetCache> path_get_cache;
|
||||||
int last_send_cache_id = 1;
|
int last_send_cache_id = 1;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Error _send_confirm_path(Node *p_node, NodePath p_path, PathSentCache *psc, const List<int> &p_peers);
|
Error _send_confirm_path(Node *p_node, PathSentCache *psc, const List<int> &p_peers);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void clear();
|
void clear();
|
||||||
|
@ -75,7 +75,7 @@ public:
|
||||||
bool send_object_cache(Object *p_obj, int p_target, int &p_id);
|
bool send_object_cache(Object *p_obj, int p_target, int &p_id);
|
||||||
int make_object_cache(Object *p_obj);
|
int make_object_cache(Object *p_obj);
|
||||||
Object *get_cached_object(int p_from, uint32_t p_cache_id);
|
Object *get_cached_object(int p_from, uint32_t p_cache_id);
|
||||||
bool is_cache_confirmed(NodePath p_path, int p_peer);
|
bool is_cache_confirmed(Node *p_path, int p_peer);
|
||||||
|
|
||||||
SceneCacheInterface(SceneMultiplayer *p_multiplayer) { multiplayer = p_multiplayer; }
|
SceneCacheInterface(SceneMultiplayer *p_multiplayer) { multiplayer = p_multiplayer; }
|
||||||
};
|
};
|
||||||
|
|
|
@ -443,15 +443,14 @@ void SceneRPCInterface::_send_rpc(Node *p_node, int p_to, uint16_t p_rpc_id, con
|
||||||
// Not all verified path, so send one by one.
|
// Not all verified path, so send one by one.
|
||||||
|
|
||||||
// Append path at the end, since we will need it for some packets.
|
// Append path at the end, since we will need it for some packets.
|
||||||
NodePath from_path = multiplayer->get_root_path().rel_path_to(p_node->get_path());
|
CharString pname = String(multiplayer->get_root_path().rel_path_to(p_node->get_path())).utf8();
|
||||||
CharString pname = String(from_path).utf8();
|
|
||||||
int path_len = encode_cstring(pname.get_data(), nullptr);
|
int path_len = encode_cstring(pname.get_data(), nullptr);
|
||||||
MAKE_ROOM(ofs + path_len);
|
MAKE_ROOM(ofs + path_len);
|
||||||
encode_cstring(pname.get_data(), &(packet_cache.write[ofs]));
|
encode_cstring(pname.get_data(), &(packet_cache.write[ofs]));
|
||||||
|
|
||||||
// Not all verified path, so check which needs the longer packet.
|
// Not all verified path, so check which needs the longer packet.
|
||||||
for (const int P : targets) {
|
for (const int P : targets) {
|
||||||
bool confirmed = multiplayer->get_path_cache()->is_cache_confirmed(from_path, P);
|
bool confirmed = multiplayer->get_path_cache()->is_cache_confirmed(p_node, P);
|
||||||
if (confirmed) {
|
if (confirmed) {
|
||||||
// This one confirmed path, so use id.
|
// This one confirmed path, so use id.
|
||||||
encode_uint32(psc_id, &(packet_cache.write[1]));
|
encode_uint32(psc_id, &(packet_cache.write[1]));
|
||||||
|
|
Loading…
Reference in a new issue