| 1 | /**************************************************************************/ |
| 2 | /* scene_cache_interface.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "scene_cache_interface.h" |
| 32 | |
| 33 | #include "scene_multiplayer.h" |
| 34 | |
| 35 | #include "core/io/marshalls.h" |
| 36 | #include "scene/main/node.h" |
| 37 | #include "scene/main/window.h" |
| 38 | |
| 39 | SceneCacheInterface::NodeCache &SceneCacheInterface::_track(Node *p_node) { |
| 40 | const ObjectID oid = p_node->get_instance_id(); |
| 41 | NodeCache *nc = nodes_cache.getptr(oid); |
| 42 | if (!nc) { |
| 43 | nodes_cache[oid] = NodeCache(); |
| 44 | p_node->connect(SceneStringName(tree_exited), callable_mp(this, &SceneCacheInterface::_remove_node_cache).bind(oid), Object::CONNECT_ONE_SHOT); |
| 45 | } |
| 46 | return nodes_cache[oid]; |
| 47 | } |
| 48 | |
| 49 | void SceneCacheInterface::_remove_node_cache(ObjectID p_oid) { |
| 50 | NodeCache *nc = nodes_cache.getptr(p_oid); |
| 51 | if (!nc) { |
| 52 | return; |
| 53 | } |
| 54 | if (nc->cache_id) { |
| 55 | assigned_ids.erase(nc->cache_id); |
| 56 | } |
| 57 | #if 0 |
| 58 | // TODO: Find a way to cleanup recv_nodes without breaking visibility and RPCs interactions. |
| 59 | for (KeyValue<int, int> &E : nc->recv_ids) { |
| 60 | PeerInfo *pinfo = peers_info.getptr(E.key); |
| 61 | ERR_CONTINUE(!pinfo); |
| 62 | pinfo->recv_nodes.erase(E.value); |
| 63 | } |
| 64 | #endif |
| 65 | for (KeyValue<int, bool> &E : nc->confirmed_peers) { |
| 66 | PeerInfo *pinfo = peers_info.getptr(E.key); |
| 67 | ERR_CONTINUE(!pinfo); |
| 68 | pinfo->sent_nodes.erase(p_oid); |
| 69 | } |
| 70 | nodes_cache.erase(p_oid); |
| 71 | } |
| 72 | |
| 73 | void SceneCacheInterface::on_peer_change(int p_id, bool p_connected) { |
| 74 | if (p_connected) { |
| 75 | peers_info.insert(p_id, PeerInfo()); |
| 76 | } else { |
| 77 | PeerInfo *pinfo = peers_info.getptr(p_id); |
| 78 | ERR_FAIL_NULL(pinfo); // Bug. |
| 79 | for (KeyValue<int, RecvNode> E : pinfo->recv_nodes) { |
| 80 | NodeCache *nc = nodes_cache.getptr(E.value.oid); |
| 81 | if (!nc) { |
| 82 | // Node might have already been deleted locally. |
| 83 | continue; |
| 84 | } |
| 85 | nc->recv_ids.erase(p_id); |
| 86 | } |
| 87 | for (const ObjectID &oid : pinfo->sent_nodes) { |
| 88 | NodeCache *nc = nodes_cache.getptr(oid); |
| 89 | ERR_CONTINUE(!nc); |
| 90 | nc->confirmed_peers.erase(p_id); |
| 91 | } |
| 92 | peers_info.erase(p_id); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void SceneCacheInterface::process_simplify_path(int p_from, const uint8_t *p_packet, int p_packet_len) { |
| 97 | ERR_FAIL_COND(!peers_info.has(p_from)); // Bug. |
| 98 | ERR_FAIL_COND_MSG(p_packet_len < 38, "Invalid packet received. Size too small."); |
| 99 | Node *root_node = SceneTree::get_singleton()->get_root()->get_node(multiplayer->get_root_path()); |
| 100 | ERR_FAIL_NULL(root_node); |
| 101 | int ofs = 1; |
| 102 | |
| 103 | String methods_md5 = String::utf8((const char *)(p_packet + ofs), 32); |
| 104 | ofs += 33; |
| 105 | |
| 106 | int id = decode_uint32(&p_packet[ofs]); |
| 107 | ofs += 4; |
| 108 | |
| 109 | ERR_FAIL_COND_MSG(peers_info[p_from].recv_nodes.has(id), vformat("Duplicate remote cache ID %d for peer %d", id, p_from)); |
| 110 | |
| 111 | String paths = String::utf8((const char *)(p_packet + ofs), p_packet_len - ofs); |
| 112 | |
| 113 | const NodePath path = paths; |
| 114 | |
| 115 | Node *node = root_node->get_node(path); |
| 116 | ERR_FAIL_NULL(node); |
| 117 | const bool valid_rpc_checksum = multiplayer->get_rpc_md5(node) == methods_md5; |
| 118 | if (valid_rpc_checksum == false) { |
| 119 | ERR_PRINT("The rpc node checksum failed. Make sure to have the same methods on both nodes. Node path: " + String(path)); |
| 120 | } |
| 121 | |
| 122 | peers_info[p_from].recv_nodes.insert(id, RecvNode(node->get_instance_id(), path)); |
| 123 | NodeCache &cache = _track(node); |
| 124 | cache.recv_ids.insert(p_from, id); |
| 125 | |
| 126 | // Send ack. |
| 127 | Vector<uint8_t> packet; |
| 128 | packet.resize(1 + 1 + 4); |
| 129 | packet.write[0] = SceneMultiplayer::NETWORK_COMMAND_CONFIRM_PATH; |
| 130 | packet.write[1] = valid_rpc_checksum; |
| 131 | encode_uint32(id, &packet.write[2]); |
| 132 | |
| 133 | Ref<MultiplayerPeer> multiplayer_peer = multiplayer->get_multiplayer_peer(); |
| 134 | ERR_FAIL_COND(multiplayer_peer.is_null()); |
| 135 | |
| 136 | multiplayer_peer->set_transfer_channel(0); |
| 137 | multiplayer_peer->set_transfer_mode(MultiplayerPeer::TRANSFER_MODE_RELIABLE); |
| 138 | multiplayer->send_command(p_from, packet.ptr(), packet.size()); |
| 139 | } |
| 140 | |
| 141 | void SceneCacheInterface::process_confirm_path(int p_from, const uint8_t *p_packet, int p_packet_len) { |
| 142 | ERR_FAIL_COND_MSG(p_packet_len != 6, "Invalid packet received. Size too small."); |
| 143 | Node *root_node = SceneTree::get_singleton()->get_root()->get_node(multiplayer->get_root_path()); |
| 144 | ERR_FAIL_NULL(root_node); |
| 145 | |
| 146 | const bool valid_rpc_checksum = p_packet[1]; |
| 147 | int id = decode_uint32(&p_packet[2]); |
| 148 | |
| 149 | const ObjectID *oid = assigned_ids.getptr(id); |
| 150 | if (oid == nullptr) { |
| 151 | return; // May be trying to confirm a node that was removed. |
| 152 | } |
| 153 | |
| 154 | if (valid_rpc_checksum == false) { |
| 155 | const Node *node = ObjectDB::get_instance<Node>(*oid); |
| 156 | ERR_FAIL_NULL(node); // Bug. |
| 157 | ERR_PRINT("The rpc node checksum failed. Make sure to have the same methods on both nodes. Node path: " + String(node->get_path())); |
| 158 | } |
| 159 | |
| 160 | NodeCache *cache = nodes_cache.getptr(*oid); |
| 161 | ERR_FAIL_NULL(cache); // Bug. |
| 162 | |
| 163 | bool *confirmed = cache->confirmed_peers.getptr(p_from); |
| 164 | ERR_FAIL_NULL_MSG(confirmed, "Invalid packet received. Tries to confirm a node which was not requested."); |
| 165 | *confirmed = true; |
| 166 | } |
| 167 | |
| 168 | Error SceneCacheInterface::_send_confirm_path(Node *p_node, NodeCache &p_cache, const List<int> &p_peers) { |
| 169 | // Encode function name. |
| 170 | const CharString path = String(multiplayer->get_root_path().rel_path_to(p_node->get_path())).utf8(); |
| 171 | const int path_len = encode_cstring(path.get_data(), nullptr); |
| 172 | |
| 173 | // Extract MD5 from rpc methods list. |
| 174 | const String methods_md5 = multiplayer->get_rpc_md5(p_node); |
| 175 | const int methods_md5_len = 33; // 32 + 1 for the `0` that is added by the encoder. |
| 176 | |
| 177 | Vector<uint8_t> packet; |
| 178 | packet.resize(1 + 4 + path_len + methods_md5_len); |
| 179 | int ofs = 0; |
| 180 | |
| 181 | packet.write[ofs] = SceneMultiplayer::NETWORK_COMMAND_SIMPLIFY_PATH; |
| 182 | ofs += 1; |
| 183 | |
| 184 | ofs += encode_cstring(methods_md5.utf8().get_data(), &packet.write[ofs]); |
| 185 | |
| 186 | ofs += encode_uint32(p_cache.cache_id, &packet.write[ofs]); |
| 187 | |
| 188 | ofs += encode_cstring(path.get_data(), &packet.write[ofs]); |
| 189 | |
| 190 | Ref<MultiplayerPeer> multiplayer_peer = multiplayer->get_multiplayer_peer(); |
| 191 | ERR_FAIL_COND_V(multiplayer_peer.is_null(), ERR_BUG); |
| 192 | |
| 193 | Error err = OK; |
| 194 | for (int peer_id : p_peers) { |
| 195 | multiplayer_peer->set_transfer_channel(0); |
| 196 | multiplayer_peer->set_transfer_mode(MultiplayerPeer::TRANSFER_MODE_RELIABLE); |
| 197 | err = multiplayer->send_command(peer_id, packet.ptr(), packet.size()); |
| 198 | ERR_FAIL_COND_V(err != OK, err); |
| 199 | // Insert into confirmed, but as false since it was not confirmed. |
| 200 | p_cache.confirmed_peers.insert(peer_id, false); |
| 201 | ERR_CONTINUE(!peers_info.has(peer_id)); |
| 202 | peers_info[peer_id].sent_nodes.insert(p_node->get_instance_id()); |
| 203 | } |
| 204 | return err; |
| 205 | } |
| 206 | |
| 207 | bool SceneCacheInterface::is_cache_confirmed(Node *p_node, int p_peer) { |
| 208 | ERR_FAIL_NULL_V(p_node, false); |
| 209 | const ObjectID oid = p_node->get_instance_id(); |
| 210 | NodeCache *cache = nodes_cache.getptr(oid); |
| 211 | bool *confirmed = cache ? cache->confirmed_peers.getptr(p_peer) : nullptr; |
| 212 | return confirmed && *confirmed; |
| 213 | } |
| 214 | |
| 215 | int SceneCacheInterface::make_object_cache(Object *p_obj) { |
| 216 | Node *node = Object::cast_to<Node>(p_obj); |
| 217 | ERR_FAIL_NULL_V(node, -1); |
| 218 | NodeCache &cache = _track(node); |
| 219 | if (cache.cache_id == 0) { |
| 220 | cache.cache_id = last_send_cache_id++; |
| 221 | assigned_ids[cache.cache_id] = p_obj->get_instance_id(); |
| 222 | } |
| 223 | return cache.cache_id; |
| 224 | } |
| 225 | |
| 226 | bool SceneCacheInterface::send_object_cache(Object *p_obj, int p_peer_id, int &r_id) { |
| 227 | Node *node = Object::cast_to<Node>(p_obj); |
| 228 | ERR_FAIL_NULL_V(node, false); |
| 229 | // See if the path is cached. |
| 230 | NodeCache &cache = _track(node); |
| 231 | if (cache.cache_id == 0) { |
| 232 | cache.cache_id = last_send_cache_id++; |
| 233 | assigned_ids[cache.cache_id] = p_obj->get_instance_id(); |
| 234 | } |
| 235 | r_id = cache.cache_id; |
| 236 | |
| 237 | bool has_all_peers = true; |
| 238 | List<int> peers_to_add; // If one is missing, take note to add it. |
| 239 | |
| 240 | if (p_peer_id > 0) { |
| 241 | // Fast single peer check. |
| 242 | ERR_FAIL_COND_V_MSG(!peers_info.has(p_peer_id), false, "Peer doesn't exist: " + itos(p_peer_id)); |
| 243 | |
| 244 | bool *confirmed = cache.confirmed_peers.getptr(p_peer_id); |
| 245 | if (!confirmed) { |
| 246 | peers_to_add.push_back(p_peer_id); // Need to also be notified. |
| 247 | has_all_peers = false; |
| 248 | } else if (!(*confirmed)) { |
| 249 | has_all_peers = false; |
| 250 | } |
| 251 | } else { |
| 252 | // Long and painful. |
| 253 | for (KeyValue<int, PeerInfo> &E : peers_info) { |
| 254 | if (p_peer_id < 0 && E.key == -p_peer_id) { |
| 255 | continue; // Continue, excluded. |
| 256 | } |
| 257 | |
| 258 | bool *confirmed = cache.confirmed_peers.getptr(E.key); |
| 259 | if (!confirmed) { |
| 260 | peers_to_add.push_back(E.key); // Need to also be notified. |
| 261 | has_all_peers = false; |
| 262 | } else if (!(*confirmed)) { |
| 263 | has_all_peers = false; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | if (peers_to_add.size()) { |
| 269 | _send_confirm_path(node, cache, peers_to_add); |
| 270 | } |
| 271 | |
| 272 | return has_all_peers; |
| 273 | } |
| 274 | |
| 275 | Object *SceneCacheInterface::get_cached_object(int p_from, uint32_t p_cache_id) { |
| 276 | PeerInfo *pinfo = peers_info.getptr(p_from); |
| 277 | ERR_FAIL_NULL_V(pinfo, nullptr); |
| 278 | |
| 279 | RecvNode *recv_node = pinfo->recv_nodes.getptr(p_cache_id); |
| 280 | ERR_FAIL_NULL_V_MSG(recv_node, nullptr, vformat("ID %d not found in cache of peer %d.", p_cache_id, p_from)); |
| 281 | Node *node = ObjectDB::get_instance<Node>(recv_node->oid); |
| 282 | if (!node) { |
| 283 | // Fallback to path lookup. |
| 284 | Node *root_node = SceneTree::get_singleton()->get_root()->get_node(multiplayer->get_root_path()); |
| 285 | ERR_FAIL_NULL_V(root_node, nullptr); |
| 286 | node = root_node->get_node(recv_node->path); |
| 287 | if (node) { |
| 288 | recv_node->oid = node->get_instance_id(); |
| 289 | } |
| 290 | } |
| 291 | ERR_FAIL_NULL_V_MSG(node, nullptr, vformat("Failed to get cached node from peer %d with cache ID %d.", p_from, p_cache_id)); |
| 292 | return node; |
| 293 | } |
| 294 | |
| 295 | void SceneCacheInterface::clear() { |
| 296 | for (KeyValue<ObjectID, NodeCache> &E : nodes_cache) { |
| 297 | Object *obj = ObjectDB::get_instance(E.key); |
| 298 | ERR_CONTINUE(!obj); |
| 299 | obj->disconnect(SceneStringName(tree_exited), callable_mp(this, &SceneCacheInterface::_remove_node_cache)); |
| 300 | } |
| 301 | peers_info.clear(); |
| 302 | nodes_cache.clear(); |
| 303 | assigned_ids.clear(); |
| 304 | last_send_cache_id = 1; |
| 305 | } |