| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "multiplayer_spawner.h" |
| 34 |
#include "multiplayer_synchronizer.h" |
| 35 |
|
| 36 |
#include "core/object/ref_counted.h" |
| 37 |
#include "core/templates/rb_set.h" |
| 38 |
|
| 39 |
class SceneMultiplayer; |
| 40 |
class SceneCacheInterface; |
| 41 |
|
| 42 |
class SceneReplicationInterface : public RefCounted { |
| 43 |
GDCLASS(SceneReplicationInterface, RefCounted); |
| 44 |
|
| 45 |
private: |
| 46 |
struct TrackedNode { |
| 47 |
ObjectID id; |
| 48 |
uint32_t net_id = 0; |
| 49 |
uint32_t remote_peer = 0; |
| 50 |
ObjectID spawner; |
| 51 |
HashSet<ObjectID> synchronizers; |
| 52 |
|
| 53 |
bool operator==(const ObjectID &p_other) { return id == p_other; } |
| 54 |
|
| 55 |
TrackedNode() {} |
| 56 |
TrackedNode(const ObjectID &p_id) { id = p_id; } |
| 57 |
TrackedNode(const ObjectID &p_id, uint32_t p_net_id) { |
| 58 |
id = p_id; |
| 59 |
net_id = p_net_id; |
| 60 |
} |
| 61 |
}; |
| 62 |
|
| 63 |
struct PeerInfo { |
| 64 |
HashSet<ObjectID> sync_nodes; |
| 65 |
HashSet<ObjectID> spawn_nodes; |
| 66 |
HashMap<ObjectID, uint64_t> last_watch_usecs; |
| 67 |
HashMap<uint32_t, ObjectID> recv_sync_ids; |
| 68 |
HashMap<uint32_t, ObjectID> recv_nodes; |
| 69 |
uint16_t last_sent_sync = 0; |
| 70 |
}; |
| 71 |
|
| 72 |
|
| 73 |
HashMap<int, PeerInfo> peers_info; |
| 74 |
uint32_t last_net_id = 0; |
| 75 |
HashMap<ObjectID, TrackedNode> tracked_nodes; |
| 76 |
RBSet<ObjectID> spawned_nodes; |
| 77 |
HashSet<ObjectID> sync_nodes; |
| 78 |
|
| 79 |
|
| 80 |
HashSet<ObjectID> spawn_queue; |
| 81 |
|
| 82 |
|
| 83 |
ObjectID pending_spawn; |
| 84 |
int pending_spawn_remote = 0; |
| 85 |
const uint8_t *pending_buffer = nullptr; |
| 86 |
int pending_buffer_size = 0; |
| 87 |
List<uint32_t> pending_sync_net_ids; |
| 88 |
|
| 89 |
|
| 90 |
SceneMultiplayer *multiplayer = nullptr; |
| 91 |
SceneCacheInterface *multiplayer_cache = nullptr; |
| 92 |
PackedByteArray packet_cache; |
| 93 |
int sync_mtu = 1350; |
| 94 |
int delta_mtu = 65535; |
| 95 |
|
| 96 |
TrackedNode &_track(const ObjectID &p_id); |
| 97 |
void _untrack(const ObjectID &p_id); |
| 98 |
void _node_ready(const ObjectID &p_oid); |
| 99 |
|
| 100 |
bool _has_authority(const Node *p_node); |
| 101 |
bool _verify_synchronizer(int p_peer, MultiplayerSynchronizer *p_sync, uint32_t &r_net_id); |
| 102 |
MultiplayerSynchronizer *_find_synchronizer(int p_peer, uint32_t p_net_ida); |
| 103 |
|
| 104 |
void _send_sync(int p_peer, const HashSet<ObjectID> &p_synchronizers, uint16_t p_sync_net_time, uint64_t p_usec); |
| 105 |
void _send_delta(int p_peer, const HashSet<ObjectID> &p_synchronizers, uint64_t p_usec, const HashMap<ObjectID, uint64_t> &p_last_watch_usecs); |
| 106 |
Error _make_spawn_packet(Node *p_node, MultiplayerSpawner *p_spawner, int &r_len); |
| 107 |
Error _make_despawn_packet(Node *p_node, int &r_len); |
| 108 |
Error _send_raw(const uint8_t *p_buffer, int p_size, int p_peer, bool p_reliable); |
| 109 |
|
| 110 |
void _visibility_changed(int p_peer, ObjectID p_oid); |
| 111 |
Error _update_sync_visibility(int p_peer, MultiplayerSynchronizer *p_sync); |
| 112 |
Error _update_spawn_visibility(int p_peer, const ObjectID &p_oid); |
| 113 |
void _free_remotes(const PeerInfo &p_info); |
| 114 |
|
| 115 |
template <typename T> |
| 116 |
static T *get_id_as(const ObjectID &p_id) { |
| 117 |
return p_id.is_valid() ? ObjectDB::get_instance<T>(p_id) : nullptr; |
| 118 |
} |
| 119 |
|
| 120 |
#ifdef DEBUG_ENABLED |
| 121 |
_FORCE_INLINE_ void _profile_node_data(const String &p_what, ObjectID p_id, int p_size); |
| 122 |
#endif |
| 123 |
|
| 124 |
public: |
| 125 |
static void make_default(); |
| 126 |
|
| 127 |
void on_reset(); |
| 128 |
void on_peer_change(int p_id, bool p_connected); |
| 129 |
|
| 130 |
Error on_spawn(Object *p_obj, Variant p_config); |
| 131 |
Error on_despawn(Object *p_obj, Variant p_config); |
| 132 |
Error on_replication_start(Object *p_obj, Variant p_config); |
| 133 |
Error on_replication_stop(Object *p_obj, Variant p_config); |
| 134 |
void on_network_process(); |
| 135 |
|
| 136 |
Error on_spawn_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len); |
| 137 |
Error on_despawn_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len); |
| 138 |
Error on_sync_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len); |
| 139 |
Error on_delta_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len); |
| 140 |
|
| 141 |
bool is_rpc_visible(const ObjectID &p_oid, int p_peer) const; |
| 142 |
|
| 143 |
void set_max_sync_packet_size(int p_size); |
| 144 |
int get_max_sync_packet_size() const; |
| 145 |
|
| 146 |
void set_max_delta_packet_size(int p_size); |
| 147 |
int get_max_delta_packet_size() const; |
| 148 |
|
| 149 |
SceneReplicationInterface(SceneMultiplayer *p_multiplayer, SceneCacheInterface *p_cache) { |
| 150 |
multiplayer = p_multiplayer; |
| 151 |
multiplayer_cache = p_cache; |
| 152 |
} |
| 153 |
}; |