scene_cache_interface.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*************************************************************************/
  2. /* scene_cache_interface.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #ifdef DEBUG_ENABLED
  85. multiplayer->profile_bandwidth("out", packet.size());
  86. #endif
  87. multiplayer_peer->set_transfer_channel(0);
  88. multiplayer_peer->set_transfer_mode(MultiplayerPeer::TRANSFER_MODE_RELIABLE);
  89. multiplayer_peer->set_target_peer(p_from);
  90. multiplayer_peer->put_packet(packet.ptr(), packet.size());
  91. }
  92. void SceneCacheInterface::process_confirm_path(int p_from, const uint8_t *p_packet, int p_packet_len) {
  93. ERR_FAIL_COND_MSG(p_packet_len < 3, "Invalid packet received. Size too small.");
  94. const bool valid_rpc_checksum = p_packet[1];
  95. String paths;
  96. paths.parse_utf8((const char *)&p_packet[2], p_packet_len - 2);
  97. NodePath path = paths;
  98. if (valid_rpc_checksum == false) {
  99. ERR_PRINT("The rpc node checksum failed. Make sure to have the same methods on both nodes. Node path: " + path);
  100. }
  101. PathSentCache *psc = path_send_cache.getptr(path);
  102. ERR_FAIL_COND_MSG(!psc, "Invalid packet received. Tries to confirm a path which was not found in cache.");
  103. HashMap<int, bool>::Iterator E = psc->confirmed_peers.find(p_from);
  104. ERR_FAIL_COND_MSG(!E, "Invalid packet received. Source peer was not found in cache for the given path.");
  105. E->value = true;
  106. }
  107. Error SceneCacheInterface::_send_confirm_path(Node *p_node, NodePath p_path, PathSentCache *psc, const List<int> &p_peers) {
  108. // Encode function name.
  109. const CharString path = String(p_path).utf8();
  110. const int path_len = encode_cstring(path.get_data(), nullptr);
  111. // Extract MD5 from rpc methods list.
  112. const String methods_md5 = multiplayer->get_rpc_md5(p_node);
  113. const int methods_md5_len = 33; // 32 + 1 for the `0` that is added by the encoder.
  114. Vector<uint8_t> packet;
  115. packet.resize(1 + 4 + path_len + methods_md5_len);
  116. int ofs = 0;
  117. packet.write[ofs] = SceneMultiplayer::NETWORK_COMMAND_SIMPLIFY_PATH;
  118. ofs += 1;
  119. ofs += encode_cstring(methods_md5.utf8().get_data(), &packet.write[ofs]);
  120. ofs += encode_uint32(psc->id, &packet.write[ofs]);
  121. ofs += encode_cstring(path.get_data(), &packet.write[ofs]);
  122. Ref<MultiplayerPeer> multiplayer_peer = multiplayer->get_multiplayer_peer();
  123. ERR_FAIL_COND_V(multiplayer_peer.is_null(), ERR_BUG);
  124. #ifdef DEBUG_ENABLED
  125. multiplayer->profile_bandwidth("out", packet.size() * p_peers.size());
  126. #endif
  127. Error err = OK;
  128. for (int peer_id : p_peers) {
  129. multiplayer_peer->set_target_peer(peer_id);
  130. multiplayer_peer->set_transfer_channel(0);
  131. multiplayer_peer->set_transfer_mode(MultiplayerPeer::TRANSFER_MODE_RELIABLE);
  132. err = multiplayer_peer->put_packet(packet.ptr(), packet.size());
  133. ERR_FAIL_COND_V(err != OK, err);
  134. // Insert into confirmed, but as false since it was not confirmed.
  135. psc->confirmed_peers.insert(peer_id, false);
  136. }
  137. return err;
  138. }
  139. bool SceneCacheInterface::is_cache_confirmed(NodePath p_path, int p_peer) {
  140. const PathSentCache *psc = path_send_cache.getptr(p_path);
  141. ERR_FAIL_COND_V(!psc, false);
  142. HashMap<int, bool>::ConstIterator F = psc->confirmed_peers.find(p_peer);
  143. ERR_FAIL_COND_V(!F, false); // Should never happen.
  144. return F->value;
  145. }
  146. int SceneCacheInterface::make_object_cache(Object *p_obj) {
  147. Node *node = Object::cast_to<Node>(p_obj);
  148. ERR_FAIL_COND_V(!node, -1);
  149. NodePath for_path = multiplayer->get_root_path().rel_path_to(node->get_path());
  150. // See if the path is cached.
  151. PathSentCache *psc = path_send_cache.getptr(for_path);
  152. if (!psc) {
  153. // Path is not cached, create.
  154. path_send_cache[for_path] = PathSentCache();
  155. psc = path_send_cache.getptr(for_path);
  156. psc->id = last_send_cache_id++;
  157. }
  158. return psc->id;
  159. }
  160. bool SceneCacheInterface::send_object_cache(Object *p_obj, int p_peer_id, int &r_id) {
  161. Node *node = Object::cast_to<Node>(p_obj);
  162. ERR_FAIL_COND_V(!node, false);
  163. r_id = make_object_cache(p_obj);
  164. ERR_FAIL_COND_V(r_id < 0, false);
  165. NodePath for_path = multiplayer->get_root_path().rel_path_to(node->get_path());
  166. PathSentCache *psc = path_send_cache.getptr(for_path);
  167. bool has_all_peers = true;
  168. List<int> peers_to_add; // If one is missing, take note to add it.
  169. if (p_peer_id > 0) {
  170. // Fast single peer check.
  171. HashMap<int, bool>::Iterator F = psc->confirmed_peers.find(p_peer_id);
  172. if (!F) {
  173. peers_to_add.push_back(p_peer_id); // Need to also be notified.
  174. has_all_peers = false;
  175. } else if (!F->value) {
  176. has_all_peers = false;
  177. }
  178. } else {
  179. // Long and painful.
  180. for (const int &E : multiplayer->get_connected_peers()) {
  181. if (p_peer_id < 0 && E == -p_peer_id) {
  182. continue; // Continue, excluded.
  183. }
  184. if (p_peer_id > 0 && E != p_peer_id) {
  185. continue; // Continue, not for this peer.
  186. }
  187. HashMap<int, bool>::Iterator F = psc->confirmed_peers.find(E);
  188. if (!F) {
  189. peers_to_add.push_back(E); // Need to also be notified.
  190. has_all_peers = false;
  191. } else if (!F->value) {
  192. has_all_peers = false;
  193. }
  194. }
  195. }
  196. if (peers_to_add.size()) {
  197. _send_confirm_path(node, for_path, psc, peers_to_add);
  198. }
  199. return has_all_peers;
  200. }
  201. Object *SceneCacheInterface::get_cached_object(int p_from, uint32_t p_cache_id) {
  202. Node *root_node = SceneTree::get_singleton()->get_root()->get_node(multiplayer->get_root_path());
  203. ERR_FAIL_COND_V(!root_node, nullptr);
  204. HashMap<int, PathGetCache>::Iterator E = path_get_cache.find(p_from);
  205. ERR_FAIL_COND_V_MSG(!E, nullptr, vformat("No cache found for peer %d.", p_from));
  206. HashMap<int, PathGetCache::NodeInfo>::Iterator F = E->value.nodes.find(p_cache_id);
  207. ERR_FAIL_COND_V_MSG(!F, nullptr, vformat("ID %d not found in cache of peer %d.", p_cache_id, p_from));
  208. PathGetCache::NodeInfo *ni = &F->value;
  209. Node *node = root_node->get_node(ni->path);
  210. if (!node) {
  211. ERR_PRINT("Failed to get cached path: " + String(ni->path) + ".");
  212. }
  213. return node;
  214. }
  215. void SceneCacheInterface::clear() {
  216. path_get_cache.clear();
  217. path_send_cache.clear();
  218. last_send_cache_id = 1;
  219. }