scene_replication_interface.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*************************************************************************/
  2. /* scene_replication_interface.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_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. _FORCE_INLINE_ MultiplayerSpawner *get_spawner() const { return spawner.is_valid() ? Object::cast_to<MultiplayerSpawner>(ObjectDB::get_instance(spawner)) : nullptr; }
  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<uint32_t, ObjectID> recv_sync_ids;
  58. HashMap<uint32_t, ObjectID> recv_nodes;
  59. uint16_t last_sent_sync = 0;
  60. };
  61. // Replication state.
  62. HashMap<int, PeerInfo> peers_info;
  63. uint32_t last_net_id = 0;
  64. HashMap<ObjectID, TrackedNode> tracked_nodes;
  65. HashSet<ObjectID> spawned_nodes;
  66. HashSet<ObjectID> sync_nodes;
  67. // Pending spawn informations.
  68. ObjectID pending_spawn;
  69. int pending_spawn_remote = 0;
  70. const uint8_t *pending_buffer = nullptr;
  71. int pending_buffer_size = 0;
  72. List<uint32_t> pending_sync_net_ids;
  73. // Replicator config.
  74. SceneMultiplayer *multiplayer = nullptr;
  75. PackedByteArray packet_cache;
  76. int sync_mtu = 1350; // Highly dependent on underlying protocol.
  77. TrackedNode &_track(const ObjectID &p_id);
  78. void _untrack(const ObjectID &p_id);
  79. void _send_sync(int p_peer, const HashSet<ObjectID> p_synchronizers, uint16_t p_sync_net_time, uint64_t p_msec);
  80. Error _make_spawn_packet(Node *p_node, MultiplayerSpawner *p_spawner, int &r_len);
  81. Error _make_despawn_packet(Node *p_node, int &r_len);
  82. Error _send_raw(const uint8_t *p_buffer, int p_size, int p_peer, bool p_reliable);
  83. void _visibility_changed(int p_peer, ObjectID p_oid);
  84. Error _update_sync_visibility(int p_peer, MultiplayerSynchronizer *p_sync);
  85. Error _update_spawn_visibility(int p_peer, const ObjectID &p_oid);
  86. void _free_remotes(const PeerInfo &p_info);
  87. template <class T>
  88. static T *get_id_as(const ObjectID &p_id) {
  89. return p_id.is_valid() ? Object::cast_to<T>(ObjectDB::get_instance(p_id)) : nullptr;
  90. }
  91. public:
  92. static void make_default();
  93. void on_reset();
  94. void on_peer_change(int p_id, bool p_connected);
  95. Error on_spawn(Object *p_obj, Variant p_config);
  96. Error on_despawn(Object *p_obj, Variant p_config);
  97. Error on_replication_start(Object *p_obj, Variant p_config);
  98. Error on_replication_stop(Object *p_obj, Variant p_config);
  99. void on_network_process();
  100. Error on_spawn_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len);
  101. Error on_despawn_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len);
  102. Error on_sync_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len);
  103. SceneReplicationInterface(SceneMultiplayer *p_multiplayer) {
  104. multiplayer = p_multiplayer;
  105. }
  106. };
  107. #endif // SCENE_REPLICATION_INTERFACE_H