scene_cache_interface.h 3.9 KB

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