Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/multiplayer/scene_replication_interface.h
10277 views
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
31
#pragma once
32
33
#include "multiplayer_spawner.h"
34
#include "multiplayer_synchronizer.h"
35
36
#include "core/object/ref_counted.h"
37
38
class SceneMultiplayer;
39
class SceneCacheInterface;
40
41
class SceneReplicationInterface : public RefCounted {
42
GDCLASS(SceneReplicationInterface, RefCounted);
43
44
private:
45
struct TrackedNode {
46
ObjectID id;
47
uint32_t net_id = 0;
48
uint32_t remote_peer = 0;
49
ObjectID spawner;
50
HashSet<ObjectID> synchronizers;
51
52
bool operator==(const ObjectID &p_other) { return id == p_other; }
53
54
TrackedNode() {}
55
TrackedNode(const ObjectID &p_id) { id = p_id; }
56
TrackedNode(const ObjectID &p_id, uint32_t p_net_id) {
57
id = p_id;
58
net_id = p_net_id;
59
}
60
};
61
62
struct PeerInfo {
63
HashSet<ObjectID> sync_nodes;
64
HashSet<ObjectID> spawn_nodes;
65
HashMap<ObjectID, uint64_t> last_watch_usecs;
66
HashMap<uint32_t, ObjectID> recv_sync_ids;
67
HashMap<uint32_t, ObjectID> recv_nodes;
68
uint16_t last_sent_sync = 0;
69
};
70
71
// Replication state.
72
HashMap<int, PeerInfo> peers_info;
73
uint32_t last_net_id = 0;
74
HashMap<ObjectID, TrackedNode> tracked_nodes;
75
RBSet<ObjectID> spawned_nodes;
76
HashSet<ObjectID> sync_nodes;
77
78
// Pending local spawn information (handles spawning nested nodes during ready).
79
HashSet<ObjectID> spawn_queue;
80
81
// Pending remote spawn information.
82
ObjectID pending_spawn;
83
int pending_spawn_remote = 0;
84
const uint8_t *pending_buffer = nullptr;
85
int pending_buffer_size = 0;
86
List<uint32_t> pending_sync_net_ids;
87
88
// Replicator config.
89
SceneMultiplayer *multiplayer = nullptr;
90
SceneCacheInterface *multiplayer_cache = nullptr;
91
PackedByteArray packet_cache;
92
int sync_mtu = 1350; // Highly dependent on underlying protocol.
93
int delta_mtu = 65535;
94
95
TrackedNode &_track(const ObjectID &p_id);
96
void _untrack(const ObjectID &p_id);
97
void _node_ready(const ObjectID &p_oid);
98
99
bool _has_authority(const Node *p_node);
100
bool _verify_synchronizer(int p_peer, MultiplayerSynchronizer *p_sync, uint32_t &r_net_id);
101
MultiplayerSynchronizer *_find_synchronizer(int p_peer, uint32_t p_net_ida);
102
103
void _send_sync(int p_peer, const HashSet<ObjectID> &p_synchronizers, uint16_t p_sync_net_time, uint64_t p_usec);
104
void _send_delta(int p_peer, const HashSet<ObjectID> &p_synchronizers, uint64_t p_usec, const HashMap<ObjectID, uint64_t> &p_last_watch_usecs);
105
Error _make_spawn_packet(Node *p_node, MultiplayerSpawner *p_spawner, int &r_len);
106
Error _make_despawn_packet(Node *p_node, int &r_len);
107
Error _send_raw(const uint8_t *p_buffer, int p_size, int p_peer, bool p_reliable);
108
109
void _visibility_changed(int p_peer, ObjectID p_oid);
110
Error _update_sync_visibility(int p_peer, MultiplayerSynchronizer *p_sync);
111
Error _update_spawn_visibility(int p_peer, const ObjectID &p_oid);
112
void _free_remotes(const PeerInfo &p_info);
113
114
template <typename T>
115
static T *get_id_as(const ObjectID &p_id) {
116
return p_id.is_valid() ? ObjectDB::get_instance<T>(p_id) : nullptr;
117
}
118
119
#ifdef DEBUG_ENABLED
120
_FORCE_INLINE_ void _profile_node_data(const String &p_what, ObjectID p_id, int p_size);
121
#endif
122
123
public:
124
static void make_default();
125
126
void on_reset();
127
void on_peer_change(int p_id, bool p_connected);
128
129
Error on_spawn(Object *p_obj, Variant p_config);
130
Error on_despawn(Object *p_obj, Variant p_config);
131
Error on_replication_start(Object *p_obj, Variant p_config);
132
Error on_replication_stop(Object *p_obj, Variant p_config);
133
void on_network_process();
134
135
Error on_spawn_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len);
136
Error on_despawn_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len);
137
Error on_sync_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len);
138
Error on_delta_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len);
139
140
bool is_rpc_visible(const ObjectID &p_oid, int p_peer) const;
141
142
void set_max_sync_packet_size(int p_size);
143
int get_max_sync_packet_size() const;
144
145
void set_max_delta_packet_size(int p_size);
146
int get_max_delta_packet_size() const;
147
148
SceneReplicationInterface(SceneMultiplayer *p_multiplayer, SceneCacheInterface *p_cache) {
149
multiplayer = p_multiplayer;
150
multiplayer_cache = p_cache;
151
}
152
};
153
154