scene_cache_interface.cpp 9.8 KB

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