scene_cache_interface.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. #include "scene_cache_interface.h"
  31. #include "scene_multiplayer.h"
  32. #include "core/io/marshalls.h"
  33. #include "scene/main/node.h"
  34. #include "scene/main/window.h"
  35. SceneCacheInterface::NodeCache &SceneCacheInterface::_track(Node *p_node) {
  36. const ObjectID oid = p_node->get_instance_id();
  37. NodeCache *nc = nodes_cache.getptr(oid);
  38. if (!nc) {
  39. nodes_cache[oid] = NodeCache();
  40. p_node->connect(SceneStringName(tree_exited), callable_mp(this, &SceneCacheInterface::_remove_node_cache).bind(oid), Object::CONNECT_ONE_SHOT);
  41. }
  42. return nodes_cache[oid];
  43. }
  44. void SceneCacheInterface::_remove_node_cache(ObjectID p_oid) {
  45. NodeCache *nc = nodes_cache.getptr(p_oid);
  46. if (!nc) {
  47. return;
  48. }
  49. if (nc->cache_id) {
  50. assigned_ids.erase(nc->cache_id);
  51. }
  52. #if 0
  53. // TODO: Find a way to cleanup recv_nodes without breaking visibility and RPCs interactions.
  54. for (KeyValue<int, int> &E : nc->recv_ids) {
  55. PeerInfo *pinfo = peers_info.getptr(E.key);
  56. ERR_CONTINUE(!pinfo);
  57. pinfo->recv_nodes.erase(E.value);
  58. }
  59. #endif
  60. for (KeyValue<int, bool> &E : nc->confirmed_peers) {
  61. PeerInfo *pinfo = peers_info.getptr(E.key);
  62. ERR_CONTINUE(!pinfo);
  63. pinfo->sent_nodes.erase(p_oid);
  64. }
  65. nodes_cache.erase(p_oid);
  66. }
  67. void SceneCacheInterface::on_peer_change(int p_id, bool p_connected) {
  68. if (p_connected) {
  69. peers_info.insert(p_id, PeerInfo());
  70. } else {
  71. PeerInfo *pinfo = peers_info.getptr(p_id);
  72. ERR_FAIL_NULL(pinfo); // Bug.
  73. for (KeyValue<int, RecvNode> E : pinfo->recv_nodes) {
  74. NodeCache *nc = nodes_cache.getptr(E.value.oid);
  75. if (!nc) {
  76. // Node might have already been deleted locally.
  77. continue;
  78. }
  79. nc->recv_ids.erase(p_id);
  80. }
  81. for (const ObjectID &oid : pinfo->sent_nodes) {
  82. NodeCache *nc = nodes_cache.getptr(oid);
  83. ERR_CONTINUE(!nc);
  84. nc->confirmed_peers.erase(p_id);
  85. }
  86. peers_info.erase(p_id);
  87. }
  88. }
  89. void SceneCacheInterface::process_simplify_path(int p_from, const uint8_t *p_packet, int p_packet_len) {
  90. ERR_FAIL_COND(!peers_info.has(p_from)); // Bug.
  91. ERR_FAIL_COND_MSG(p_packet_len < 38, "Invalid packet received. Size too small.");
  92. Node *root_node = SceneTree::get_singleton()->get_root()->get_node(multiplayer->get_root_path());
  93. ERR_FAIL_NULL(root_node);
  94. int ofs = 1;
  95. String methods_md5;
  96. methods_md5.parse_utf8((const char *)(p_packet + ofs), 32);
  97. ofs += 33;
  98. int id = decode_uint32(&p_packet[ofs]);
  99. ofs += 4;
  100. ERR_FAIL_COND_MSG(peers_info[p_from].recv_nodes.has(id), vformat("Duplicate remote cache ID %d for peer %d", id, p_from));
  101. String paths;
  102. paths.parse_utf8((const char *)(p_packet + ofs), p_packet_len - ofs);
  103. const NodePath path = paths;
  104. Node *node = root_node->get_node(path);
  105. ERR_FAIL_NULL(node);
  106. const bool valid_rpc_checksum = multiplayer->get_rpc_md5(node) == methods_md5;
  107. if (valid_rpc_checksum == false) {
  108. ERR_PRINT("The rpc node checksum failed. Make sure to have the same methods on both nodes. Node path: " + path);
  109. }
  110. peers_info[p_from].recv_nodes.insert(id, RecvNode(node->get_instance_id(), path));
  111. NodeCache &cache = _track(node);
  112. cache.recv_ids.insert(p_from, id);
  113. // Send ack.
  114. Vector<uint8_t> packet;
  115. packet.resize(1 + 1 + 4);
  116. packet.write[0] = SceneMultiplayer::NETWORK_COMMAND_CONFIRM_PATH;
  117. packet.write[1] = valid_rpc_checksum;
  118. encode_uint32(id, &packet.write[2]);
  119. Ref<MultiplayerPeer> multiplayer_peer = multiplayer->get_multiplayer_peer();
  120. ERR_FAIL_COND(multiplayer_peer.is_null());
  121. multiplayer_peer->set_transfer_channel(0);
  122. multiplayer_peer->set_transfer_mode(MultiplayerPeer::TRANSFER_MODE_RELIABLE);
  123. multiplayer->send_command(p_from, packet.ptr(), packet.size());
  124. }
  125. void SceneCacheInterface::process_confirm_path(int p_from, const uint8_t *p_packet, int p_packet_len) {
  126. ERR_FAIL_COND_MSG(p_packet_len != 6, "Invalid packet received. Size too small.");
  127. Node *root_node = SceneTree::get_singleton()->get_root()->get_node(multiplayer->get_root_path());
  128. ERR_FAIL_NULL(root_node);
  129. const bool valid_rpc_checksum = p_packet[1];
  130. int id = decode_uint32(&p_packet[2]);
  131. const ObjectID *oid = assigned_ids.getptr(id);
  132. if (oid == nullptr) {
  133. return; // May be trying to confirm a node that was removed.
  134. }
  135. if (valid_rpc_checksum == false) {
  136. const Node *node = Object::cast_to<Node>(ObjectDB::get_instance(*oid));
  137. ERR_FAIL_NULL(node); // Bug.
  138. ERR_PRINT("The rpc node checksum failed. Make sure to have the same methods on both nodes. Node path: " + node->get_path());
  139. }
  140. NodeCache *cache = nodes_cache.getptr(*oid);
  141. ERR_FAIL_NULL(cache); // Bug.
  142. bool *confirmed = cache->confirmed_peers.getptr(p_from);
  143. ERR_FAIL_NULL_MSG(confirmed, "Invalid packet received. Tries to confirm a node which was not requested.");
  144. *confirmed = true;
  145. }
  146. Error SceneCacheInterface::_send_confirm_path(Node *p_node, NodeCache &p_cache, const List<int> &p_peers) {
  147. // Encode function name.
  148. const CharString path = String(multiplayer->get_root_path().rel_path_to(p_node->get_path())).utf8();
  149. const int path_len = encode_cstring(path.get_data(), nullptr);
  150. // Extract MD5 from rpc methods list.
  151. const String methods_md5 = multiplayer->get_rpc_md5(p_node);
  152. const int methods_md5_len = 33; // 32 + 1 for the `0` that is added by the encoder.
  153. Vector<uint8_t> packet;
  154. packet.resize(1 + 4 + path_len + methods_md5_len);
  155. int ofs = 0;
  156. packet.write[ofs] = SceneMultiplayer::NETWORK_COMMAND_SIMPLIFY_PATH;
  157. ofs += 1;
  158. ofs += encode_cstring(methods_md5.utf8().get_data(), &packet.write[ofs]);
  159. ofs += encode_uint32(p_cache.cache_id, &packet.write[ofs]);
  160. ofs += encode_cstring(path.get_data(), &packet.write[ofs]);
  161. Ref<MultiplayerPeer> multiplayer_peer = multiplayer->get_multiplayer_peer();
  162. ERR_FAIL_COND_V(multiplayer_peer.is_null(), ERR_BUG);
  163. Error err = OK;
  164. for (int peer_id : p_peers) {
  165. multiplayer_peer->set_transfer_channel(0);
  166. multiplayer_peer->set_transfer_mode(MultiplayerPeer::TRANSFER_MODE_RELIABLE);
  167. err = multiplayer->send_command(peer_id, packet.ptr(), packet.size());
  168. ERR_FAIL_COND_V(err != OK, err);
  169. // Insert into confirmed, but as false since it was not confirmed.
  170. p_cache.confirmed_peers.insert(peer_id, false);
  171. ERR_CONTINUE(!peers_info.has(peer_id));
  172. peers_info[peer_id].sent_nodes.insert(p_node->get_instance_id());
  173. }
  174. return err;
  175. }
  176. bool SceneCacheInterface::is_cache_confirmed(Node *p_node, int p_peer) {
  177. ERR_FAIL_NULL_V(p_node, false);
  178. const ObjectID oid = p_node->get_instance_id();
  179. NodeCache *cache = nodes_cache.getptr(oid);
  180. bool *confirmed = cache ? cache->confirmed_peers.getptr(p_peer) : nullptr;
  181. return confirmed && *confirmed;
  182. }
  183. int SceneCacheInterface::make_object_cache(Object *p_obj) {
  184. Node *node = Object::cast_to<Node>(p_obj);
  185. ERR_FAIL_NULL_V(node, -1);
  186. NodeCache &cache = _track(node);
  187. if (cache.cache_id == 0) {
  188. cache.cache_id = last_send_cache_id++;
  189. assigned_ids[cache.cache_id] = p_obj->get_instance_id();
  190. }
  191. return cache.cache_id;
  192. }
  193. bool SceneCacheInterface::send_object_cache(Object *p_obj, int p_peer_id, int &r_id) {
  194. Node *node = Object::cast_to<Node>(p_obj);
  195. ERR_FAIL_NULL_V(node, false);
  196. // See if the path is cached.
  197. NodeCache &cache = _track(node);
  198. if (cache.cache_id == 0) {
  199. cache.cache_id = last_send_cache_id++;
  200. assigned_ids[cache.cache_id] = p_obj->get_instance_id();
  201. }
  202. r_id = cache.cache_id;
  203. bool has_all_peers = true;
  204. List<int> peers_to_add; // If one is missing, take note to add it.
  205. if (p_peer_id > 0) {
  206. // Fast single peer check.
  207. ERR_FAIL_COND_V_MSG(!peers_info.has(p_peer_id), false, "Peer doesn't exist: " + itos(p_peer_id));
  208. bool *confirmed = cache.confirmed_peers.getptr(p_peer_id);
  209. if (!confirmed) {
  210. peers_to_add.push_back(p_peer_id); // Need to also be notified.
  211. has_all_peers = false;
  212. } else if (!(*confirmed)) {
  213. has_all_peers = false;
  214. }
  215. } else {
  216. // Long and painful.
  217. for (KeyValue<int, PeerInfo> &E : peers_info) {
  218. if (p_peer_id < 0 && E.key == -p_peer_id) {
  219. continue; // Continue, excluded.
  220. }
  221. bool *confirmed = cache.confirmed_peers.getptr(E.key);
  222. if (!confirmed) {
  223. peers_to_add.push_back(E.key); // Need to also be notified.
  224. has_all_peers = false;
  225. } else if (!(*confirmed)) {
  226. has_all_peers = false;
  227. }
  228. }
  229. }
  230. if (peers_to_add.size()) {
  231. _send_confirm_path(node, cache, peers_to_add);
  232. }
  233. return has_all_peers;
  234. }
  235. Object *SceneCacheInterface::get_cached_object(int p_from, uint32_t p_cache_id) {
  236. PeerInfo *pinfo = peers_info.getptr(p_from);
  237. ERR_FAIL_NULL_V(pinfo, nullptr);
  238. RecvNode *recv_node = pinfo->recv_nodes.getptr(p_cache_id);
  239. ERR_FAIL_NULL_V_MSG(recv_node, nullptr, vformat("ID %d not found in cache of peer %d.", p_cache_id, p_from));
  240. Node *node = Object::cast_to<Node>(ObjectDB::get_instance(recv_node->oid));
  241. if (!node) {
  242. // Fallback to path lookup.
  243. Node *root_node = SceneTree::get_singleton()->get_root()->get_node(multiplayer->get_root_path());
  244. ERR_FAIL_NULL_V(root_node, nullptr);
  245. node = root_node->get_node(recv_node->path);
  246. if (node) {
  247. recv_node->oid = node->get_instance_id();
  248. }
  249. }
  250. 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));
  251. return node;
  252. }
  253. void SceneCacheInterface::clear() {
  254. for (KeyValue<ObjectID, NodeCache> &E : nodes_cache) {
  255. Object *obj = ObjectDB::get_instance(E.key);
  256. ERR_CONTINUE(!obj);
  257. obj->disconnect(SceneStringName(tree_exited), callable_mp(this, &SceneCacheInterface::_remove_node_cache));
  258. }
  259. peers_info.clear();
  260. nodes_cache.clear();
  261. assigned_ids.clear();
  262. last_send_cache_id = 1;
  263. }