scene_replication_interface.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**************************************************************************/
  2. /* scene_replication_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_REPLICATION_INTERFACE_H
  31. #define SCENE_REPLICATION_INTERFACE_H
  32. #include "multiplayer_spawner.h"
  33. #include "multiplayer_synchronizer.h"
  34. #include "core/object/ref_counted.h"
  35. class SceneMultiplayer;
  36. class SceneCacheInterface;
  37. class SceneReplicationInterface : public RefCounted {
  38. GDCLASS(SceneReplicationInterface, 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. HashSet<ObjectID> synchronizers;
  46. bool operator==(const ObjectID &p_other) { return id == p_other; }
  47. TrackedNode() {}
  48. TrackedNode(const ObjectID &p_id) { id = p_id; }
  49. TrackedNode(const ObjectID &p_id, uint32_t p_net_id) {
  50. id = p_id;
  51. net_id = p_net_id;
  52. }
  53. };
  54. struct PeerInfo {
  55. HashSet<ObjectID> sync_nodes;
  56. HashSet<ObjectID> spawn_nodes;
  57. HashMap<ObjectID, uint64_t> last_watch_usecs;
  58. HashMap<uint32_t, ObjectID> recv_sync_ids;
  59. HashMap<uint32_t, ObjectID> recv_nodes;
  60. uint16_t last_sent_sync = 0;
  61. };
  62. // Replication state.
  63. HashMap<int, PeerInfo> peers_info;
  64. uint32_t last_net_id = 0;
  65. HashMap<ObjectID, TrackedNode> tracked_nodes;
  66. HashSet<ObjectID> spawned_nodes;
  67. HashSet<ObjectID> sync_nodes;
  68. // Pending local spawn information (handles spawning nested nodes during ready).
  69. HashSet<ObjectID> spawn_queue;
  70. // Pending remote spawn information.
  71. ObjectID pending_spawn;
  72. int pending_spawn_remote = 0;
  73. const uint8_t *pending_buffer = nullptr;
  74. int pending_buffer_size = 0;
  75. List<uint32_t> pending_sync_net_ids;
  76. // Replicator config.
  77. SceneMultiplayer *multiplayer = nullptr;
  78. SceneCacheInterface *multiplayer_cache = nullptr;
  79. PackedByteArray packet_cache;
  80. int sync_mtu = 1350; // Highly dependent on underlying protocol.
  81. int delta_mtu = 65535;
  82. TrackedNode &_track(const ObjectID &p_id);
  83. void _untrack(const ObjectID &p_id);
  84. void _node_ready(const ObjectID &p_oid);
  85. bool _has_authority(const Node *p_node);
  86. bool _verify_synchronizer(int p_peer, MultiplayerSynchronizer *p_sync, uint32_t &r_net_id);
  87. MultiplayerSynchronizer *_find_synchronizer(int p_peer, uint32_t p_net_ida);
  88. void _send_sync(int p_peer, const HashSet<ObjectID> &p_synchronizers, uint16_t p_sync_net_time, uint64_t p_usec);
  89. void _send_delta(int p_peer, const HashSet<ObjectID> &p_synchronizers, uint64_t p_usec, const HashMap<ObjectID, uint64_t> &p_last_watch_usecs);
  90. Error _make_spawn_packet(Node *p_node, MultiplayerSpawner *p_spawner, int &r_len);
  91. Error _make_despawn_packet(Node *p_node, int &r_len);
  92. Error _send_raw(const uint8_t *p_buffer, int p_size, int p_peer, bool p_reliable);
  93. void _visibility_changed(int p_peer, ObjectID p_oid);
  94. Error _update_sync_visibility(int p_peer, MultiplayerSynchronizer *p_sync);
  95. Error _update_spawn_visibility(int p_peer, const ObjectID &p_oid);
  96. void _free_remotes(const PeerInfo &p_info);
  97. template <typename T>
  98. static T *get_id_as(const ObjectID &p_id) {
  99. return p_id.is_valid() ? Object::cast_to<T>(ObjectDB::get_instance(p_id)) : nullptr;
  100. }
  101. #ifdef DEBUG_ENABLED
  102. _FORCE_INLINE_ void _profile_node_data(const String &p_what, ObjectID p_id, int p_size);
  103. #endif
  104. public:
  105. static void make_default();
  106. void on_reset();
  107. void on_peer_change(int p_id, bool p_connected);
  108. Error on_spawn(Object *p_obj, Variant p_config);
  109. Error on_despawn(Object *p_obj, Variant p_config);
  110. Error on_replication_start(Object *p_obj, Variant p_config);
  111. Error on_replication_stop(Object *p_obj, Variant p_config);
  112. void on_network_process();
  113. Error on_spawn_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len);
  114. Error on_despawn_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len);
  115. Error on_sync_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len);
  116. Error on_delta_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len);
  117. bool is_rpc_visible(const ObjectID &p_oid, int p_peer) const;
  118. void set_max_sync_packet_size(int p_size);
  119. int get_max_sync_packet_size() const;
  120. void set_max_delta_packet_size(int p_size);
  121. int get_max_delta_packet_size() const;
  122. SceneReplicationInterface(SceneMultiplayer *p_multiplayer, SceneCacheInterface *p_cache) {
  123. multiplayer = p_multiplayer;
  124. multiplayer_cache = p_cache;
  125. }
  126. };
  127. #endif // SCENE_REPLICATION_INTERFACE_H