scene_replication_interface.h 5.5 KB

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