scene_cache_interface.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**************************************************************************/
  2. /* scene_cache_interface.h */
  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. #pragma once
  31. #include "core/object/ref_counted.h"
  32. class Node;
  33. class SceneMultiplayer;
  34. class SceneCacheInterface : public RefCounted {
  35. GDCLASS(SceneCacheInterface, RefCounted);
  36. private:
  37. SceneMultiplayer *multiplayer = nullptr;
  38. //path sent caches
  39. struct NodeCache {
  40. int cache_id = 0;
  41. HashMap<int, int> recv_ids; // peer id, remote cache id
  42. HashMap<int, bool> confirmed_peers; // peer id, confirmed
  43. };
  44. struct RecvNode {
  45. ObjectID oid;
  46. NodePath path;
  47. RecvNode(const ObjectID &p_oid, const NodePath &p_path) {
  48. oid = p_oid;
  49. path = p_path;
  50. }
  51. };
  52. struct PeerInfo {
  53. HashMap<int, RecvNode> recv_nodes; // remote cache id, (ObjectID, NodePath)
  54. HashSet<ObjectID> sent_nodes;
  55. };
  56. HashMap<ObjectID, NodeCache> nodes_cache;
  57. HashMap<int, ObjectID> assigned_ids;
  58. HashMap<int, PeerInfo> peers_info;
  59. int last_send_cache_id = 1;
  60. void _remove_node_cache(ObjectID p_oid);
  61. NodeCache &_track(Node *p_node);
  62. protected:
  63. Error _send_confirm_path(Node *p_node, NodeCache &p_cache, const List<int> &p_peers);
  64. public:
  65. void clear();
  66. void on_peer_change(int p_id, bool p_connected);
  67. void process_simplify_path(int p_from, const uint8_t *p_packet, int p_packet_len);
  68. void process_confirm_path(int p_from, const uint8_t *p_packet, int p_packet_len);
  69. // Returns true if all peers have cached path.
  70. bool send_object_cache(Object *p_obj, int p_target, int &p_id);
  71. int make_object_cache(Object *p_obj);
  72. Object *get_cached_object(int p_from, uint32_t p_cache_id);
  73. bool is_cache_confirmed(Node *p_path, int p_peer);
  74. SceneCacheInterface(SceneMultiplayer *p_multiplayer) { multiplayer = p_multiplayer; }
  75. };