2
0

scene_cache_interface.cpp 11 KB

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