scene_cache_interface.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "core/io/marshalls.h"
  32. #include "scene/main/node.h"
  33. #include "scene/main/window.h"
  34. #include "scene_multiplayer.h"
  35. void SceneCacheInterface::on_peer_change(int p_id, bool p_connected) {
  36. if (p_connected) {
  37. path_get_cache.insert(p_id, PathGetCache());
  38. } else {
  39. // Cleanup get cache.
  40. path_get_cache.erase(p_id);
  41. // Cleanup sent cache.
  42. // Some refactoring is needed to make this faster and do paths GC.
  43. for (const KeyValue<NodePath, PathSentCache> &E : path_send_cache) {
  44. PathSentCache *psc = path_send_cache.getptr(E.key);
  45. psc->confirmed_peers.erase(p_id);
  46. }
  47. }
  48. }
  49. void SceneCacheInterface::process_simplify_path(int p_from, const uint8_t *p_packet, int p_packet_len) {
  50. Node *root_node = SceneTree::get_singleton()->get_root()->get_node(multiplayer->get_root_path());
  51. ERR_FAIL_COND(!root_node);
  52. ERR_FAIL_COND_MSG(p_packet_len < 38, "Invalid packet received. Size too small.");
  53. int ofs = 1;
  54. String methods_md5;
  55. methods_md5.parse_utf8((const char *)(p_packet + ofs), 32);
  56. ofs += 33;
  57. int id = decode_uint32(&p_packet[ofs]);
  58. ofs += 4;
  59. String paths;
  60. paths.parse_utf8((const char *)(p_packet + ofs), p_packet_len - ofs);
  61. NodePath path = paths;
  62. if (!path_get_cache.has(p_from)) {
  63. path_get_cache[p_from] = PathGetCache();
  64. }
  65. Node *node = root_node->get_node(path);
  66. ERR_FAIL_COND(node == nullptr);
  67. const bool valid_rpc_checksum = multiplayer->get_rpc_md5(node) == methods_md5;
  68. if (valid_rpc_checksum == false) {
  69. ERR_PRINT("The rpc node checksum failed. Make sure to have the same methods on both nodes. Node path: " + path);
  70. }
  71. PathGetCache::NodeInfo ni;
  72. ni.path = path;
  73. path_get_cache[p_from].nodes[id] = ni;
  74. // Encode path to send ack.
  75. CharString pname = String(path).utf8();
  76. int len = encode_cstring(pname.get_data(), nullptr);
  77. Vector<uint8_t> packet;
  78. packet.resize(1 + 1 + len);
  79. packet.write[0] = SceneMultiplayer::NETWORK_COMMAND_CONFIRM_PATH;
  80. packet.write[1] = valid_rpc_checksum;
  81. encode_cstring(pname.get_data(), &packet.write[2]);
  82. Ref<MultiplayerPeer> multiplayer_peer = multiplayer->get_multiplayer_peer();
  83. ERR_FAIL_COND(multiplayer_peer.is_null());
  84. multiplayer_peer->set_transfer_channel(0);
  85. multiplayer_peer->set_transfer_mode(MultiplayerPeer::TRANSFER_MODE_RELIABLE);
  86. multiplayer->send_command(p_from, packet.ptr(), packet.size());
  87. }
  88. void SceneCacheInterface::process_confirm_path(int p_from, const uint8_t *p_packet, int p_packet_len) {
  89. ERR_FAIL_COND_MSG(p_packet_len < 3, "Invalid packet received. Size too small.");
  90. const bool valid_rpc_checksum = p_packet[1];
  91. String paths;
  92. paths.parse_utf8((const char *)&p_packet[2], p_packet_len - 2);
  93. NodePath path = paths;
  94. if (valid_rpc_checksum == false) {
  95. ERR_PRINT("The rpc node checksum failed. Make sure to have the same methods on both nodes. Node path: " + path);
  96. }
  97. PathSentCache *psc = path_send_cache.getptr(path);
  98. ERR_FAIL_COND_MSG(!psc, "Invalid packet received. Tries to confirm a path which was not found in cache.");
  99. HashMap<int, bool>::Iterator E = psc->confirmed_peers.find(p_from);
  100. ERR_FAIL_COND_MSG(!E, "Invalid packet received. Source peer was not found in cache for the given path.");
  101. E->value = true;
  102. }
  103. Error SceneCacheInterface::_send_confirm_path(Node *p_node, NodePath p_path, PathSentCache *psc, const List<int> &p_peers) {
  104. // Encode function name.
  105. const CharString path = String(p_path).utf8();
  106. const int path_len = encode_cstring(path.get_data(), nullptr);
  107. // Extract MD5 from rpc methods list.
  108. const String methods_md5 = multiplayer->get_rpc_md5(p_node);
  109. const int methods_md5_len = 33; // 32 + 1 for the `0` that is added by the encoder.
  110. Vector<uint8_t> packet;
  111. packet.resize(1 + 4 + path_len + methods_md5_len);
  112. int ofs = 0;
  113. packet.write[ofs] = SceneMultiplayer::NETWORK_COMMAND_SIMPLIFY_PATH;
  114. ofs += 1;
  115. ofs += encode_cstring(methods_md5.utf8().get_data(), &packet.write[ofs]);
  116. ofs += encode_uint32(psc->id, &packet.write[ofs]);
  117. ofs += encode_cstring(path.get_data(), &packet.write[ofs]);
  118. Ref<MultiplayerPeer> multiplayer_peer = multiplayer->get_multiplayer_peer();
  119. ERR_FAIL_COND_V(multiplayer_peer.is_null(), ERR_BUG);
  120. Error err = OK;
  121. for (int peer_id : p_peers) {
  122. multiplayer_peer->set_transfer_channel(0);
  123. multiplayer_peer->set_transfer_mode(MultiplayerPeer::TRANSFER_MODE_RELIABLE);
  124. err = multiplayer->send_command(peer_id, packet.ptr(), packet.size());
  125. ERR_FAIL_COND_V(err != OK, err);
  126. // Insert into confirmed, but as false since it was not confirmed.
  127. psc->confirmed_peers.insert(peer_id, false);
  128. }
  129. return err;
  130. }
  131. bool SceneCacheInterface::is_cache_confirmed(NodePath p_path, int p_peer) {
  132. const PathSentCache *psc = path_send_cache.getptr(p_path);
  133. ERR_FAIL_COND_V(!psc, false);
  134. HashMap<int, bool>::ConstIterator F = psc->confirmed_peers.find(p_peer);
  135. ERR_FAIL_COND_V(!F, false); // Should never happen.
  136. return F->value;
  137. }
  138. int SceneCacheInterface::make_object_cache(Object *p_obj) {
  139. Node *node = Object::cast_to<Node>(p_obj);
  140. ERR_FAIL_COND_V(!node, -1);
  141. NodePath for_path = multiplayer->get_root_path().rel_path_to(node->get_path());
  142. // See if the path is cached.
  143. PathSentCache *psc = path_send_cache.getptr(for_path);
  144. if (!psc) {
  145. // Path is not cached, create.
  146. path_send_cache[for_path] = PathSentCache();
  147. psc = path_send_cache.getptr(for_path);
  148. psc->id = last_send_cache_id++;
  149. }
  150. return psc->id;
  151. }
  152. bool SceneCacheInterface::send_object_cache(Object *p_obj, int p_peer_id, int &r_id) {
  153. Node *node = Object::cast_to<Node>(p_obj);
  154. ERR_FAIL_COND_V(!node, false);
  155. r_id = make_object_cache(p_obj);
  156. ERR_FAIL_COND_V(r_id < 0, false);
  157. NodePath for_path = multiplayer->get_root_path().rel_path_to(node->get_path());
  158. PathSentCache *psc = path_send_cache.getptr(for_path);
  159. bool has_all_peers = true;
  160. List<int> peers_to_add; // If one is missing, take note to add it.
  161. if (p_peer_id > 0) {
  162. // Fast single peer check.
  163. HashMap<int, bool>::Iterator F = psc->confirmed_peers.find(p_peer_id);
  164. if (!F) {
  165. peers_to_add.push_back(p_peer_id); // Need to also be notified.
  166. has_all_peers = false;
  167. } else if (!F->value) {
  168. has_all_peers = false;
  169. }
  170. } else {
  171. // Long and painful.
  172. for (const int &E : multiplayer->get_connected_peers()) {
  173. if (p_peer_id < 0 && E == -p_peer_id) {
  174. continue; // Continue, excluded.
  175. }
  176. HashMap<int, bool>::Iterator F = psc->confirmed_peers.find(E);
  177. if (!F) {
  178. peers_to_add.push_back(E); // Need to also be notified.
  179. has_all_peers = false;
  180. } else if (!F->value) {
  181. has_all_peers = false;
  182. }
  183. }
  184. }
  185. if (peers_to_add.size()) {
  186. _send_confirm_path(node, for_path, psc, peers_to_add);
  187. }
  188. return has_all_peers;
  189. }
  190. Object *SceneCacheInterface::get_cached_object(int p_from, uint32_t p_cache_id) {
  191. Node *root_node = SceneTree::get_singleton()->get_root()->get_node(multiplayer->get_root_path());
  192. ERR_FAIL_COND_V(!root_node, nullptr);
  193. HashMap<int, PathGetCache>::Iterator E = path_get_cache.find(p_from);
  194. ERR_FAIL_COND_V_MSG(!E, nullptr, vformat("No cache found for peer %d.", p_from));
  195. HashMap<int, PathGetCache::NodeInfo>::Iterator F = E->value.nodes.find(p_cache_id);
  196. ERR_FAIL_COND_V_MSG(!F, nullptr, vformat("ID %d not found in cache of peer %d.", p_cache_id, p_from));
  197. PathGetCache::NodeInfo *ni = &F->value;
  198. Node *node = root_node->get_node(ni->path);
  199. if (!node) {
  200. ERR_PRINT("Failed to get cached path: " + String(ni->path) + ".");
  201. }
  202. return node;
  203. }
  204. void SceneCacheInterface::clear() {
  205. path_get_cache.clear();
  206. path_send_cache.clear();
  207. last_send_cache_id = 1;
  208. }