scene_cache_interface.cpp 9.6 KB

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