scene_replication_state.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*************************************************************************/
  2. /* scene_replication_state.h */
  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. #ifndef SCENE_REPLICATION_STATE_H
  31. #define SCENE_REPLICATION_STATE_H
  32. #include "core/object/ref_counted.h"
  33. #include "multiplayer_spawner.h"
  34. #include "multiplayer_synchronizer.h"
  35. class MultiplayerSpawner;
  36. class MultiplayerSynchronizer;
  37. class Node;
  38. class SceneReplicationState : public RefCounted {
  39. private:
  40. struct TrackedNode {
  41. ObjectID id;
  42. uint32_t net_id = 0;
  43. uint32_t remote_peer = 0;
  44. ObjectID spawner;
  45. ObjectID synchronizer;
  46. uint16_t last_sync = 0;
  47. uint64_t last_sync_msec = 0;
  48. bool operator==(const ObjectID &p_other) { return id == p_other; }
  49. Node *get_node() const { return id.is_valid() ? Object::cast_to<Node>(ObjectDB::get_instance(id)) : nullptr; }
  50. MultiplayerSpawner *get_spawner() const { return spawner.is_valid() ? Object::cast_to<MultiplayerSpawner>(ObjectDB::get_instance(spawner)) : nullptr; }
  51. MultiplayerSynchronizer *get_synchronizer() const { return synchronizer.is_valid() ? Object::cast_to<MultiplayerSynchronizer>(ObjectDB::get_instance(synchronizer)) : nullptr; }
  52. TrackedNode() {}
  53. TrackedNode(const ObjectID &p_id) { id = p_id; }
  54. TrackedNode(const ObjectID &p_id, uint32_t p_net_id) {
  55. id = p_id;
  56. net_id = p_net_id;
  57. }
  58. };
  59. struct PeerInfo {
  60. HashSet<ObjectID> sync_nodes;
  61. HashSet<ObjectID> spawn_nodes;
  62. HashMap<uint32_t, ObjectID> recv_nodes;
  63. uint16_t last_sent_sync = 0;
  64. uint16_t last_recv_sync = 0;
  65. };
  66. HashSet<int> known_peers;
  67. uint32_t last_net_id = 0;
  68. HashMap<ObjectID, TrackedNode> tracked_nodes;
  69. HashMap<int, PeerInfo> peers_info;
  70. HashSet<ObjectID> spawned_nodes;
  71. HashSet<ObjectID> synced_nodes;
  72. TrackedNode &_track(const ObjectID &p_id);
  73. void _untrack(const ObjectID &p_id);
  74. bool is_tracked(const ObjectID &p_id) const { return tracked_nodes.has(p_id); }
  75. public:
  76. const HashSet<int> get_peers() const { return known_peers; }
  77. const HashSet<ObjectID> &get_spawned_nodes() const { return spawned_nodes; }
  78. bool is_spawned_node(const ObjectID &p_id) const { return spawned_nodes.has(p_id); }
  79. const HashSet<ObjectID> &get_synced_nodes() const { return synced_nodes; }
  80. bool is_synced_node(const ObjectID &p_id) const { return synced_nodes.has(p_id); }
  81. MultiplayerSynchronizer *get_synchronizer(const ObjectID &p_id) { return tracked_nodes.has(p_id) ? tracked_nodes[p_id].get_synchronizer() : nullptr; }
  82. MultiplayerSpawner *get_spawner(const ObjectID &p_id) { return tracked_nodes.has(p_id) ? tracked_nodes[p_id].get_spawner() : nullptr; }
  83. Node *get_node(const ObjectID &p_id) { return tracked_nodes.has(p_id) ? tracked_nodes[p_id].get_node() : nullptr; }
  84. bool update_last_node_sync(const ObjectID &p_id, uint16_t p_time);
  85. bool update_sync_time(const ObjectID &p_id, uint64_t p_msec);
  86. uint32_t get_net_id(const ObjectID &p_id) const;
  87. void set_net_id(const ObjectID &p_id, uint32_t p_net_id);
  88. uint32_t ensure_net_id(const ObjectID &p_id);
  89. void reset();
  90. void on_peer_change(int p_peer, bool p_connected);
  91. Error config_add_spawn(Node *p_node, MultiplayerSpawner *p_spawner);
  92. Error config_del_spawn(Node *p_node, MultiplayerSpawner *p_spawner);
  93. Error config_add_sync(Node *p_node, MultiplayerSynchronizer *p_sync);
  94. Error config_del_sync(Node *p_node, MultiplayerSynchronizer *p_sync);
  95. Error peer_add_sync(int p_peer, const ObjectID &p_id);
  96. Error peer_del_sync(int p_peer, const ObjectID &p_id);
  97. const HashSet<ObjectID> get_peer_sync_nodes(int p_peer);
  98. bool is_peer_sync(int p_peer, const ObjectID &p_id) const;
  99. Error peer_add_spawn(int p_peer, const ObjectID &p_id);
  100. Error peer_del_spawn(int p_peer, const ObjectID &p_id);
  101. const HashSet<ObjectID> get_peer_spawn_nodes(int p_peer);
  102. bool is_peer_spawn(int p_peer, const ObjectID &p_id) const;
  103. const HashMap<uint32_t, ObjectID> peer_get_remotes(int p_peer) const;
  104. Node *peer_get_remote(int p_peer, uint32_t p_net_id);
  105. Error peer_add_remote(int p_peer, uint32_t p_net_id, Node *p_node, MultiplayerSpawner *p_spawner);
  106. Error peer_del_remote(int p_peer, uint32_t p_net_id, Node **r_node);
  107. uint16_t peer_sync_next(int p_peer);
  108. void peer_sync_recv(int p_peer, uint16_t p_time);
  109. SceneReplicationState() {}
  110. };
  111. #endif // SCENE_REPLICATION_STATE_H