scene_cache_interface.cpp 11 KB

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