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#ifndef SCENE_REPLICATION_INTERFACE_H
32#define SCENE_REPLICATION_INTERFACE_H
33
34#include "multiplayer_spawner.h"
35#include "multiplayer_synchronizer.h"
36
37#include "core/object/ref_counted.h"
38
39class SceneMultiplayer;
40
41class SceneReplicationInterface : public RefCounted {
42 GDCLASS(SceneReplicationInterface, RefCounted);
43
44private:
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 HashSet<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 PackedByteArray packet_cache;
91 int sync_mtu = 1350; // Highly dependent on underlying protocol.
92 int delta_mtu = 65535;
93
94 TrackedNode &_track(const ObjectID &p_id);
95 void _untrack(const ObjectID &p_id);
96 void _node_ready(const ObjectID &p_oid);
97
98 bool _verify_synchronizer(int p_peer, MultiplayerSynchronizer *p_sync, uint32_t &r_net_id);
99 MultiplayerSynchronizer *_find_synchronizer(int p_peer, uint32_t p_net_ida);
100
101 void _send_sync(int p_peer, const HashSet<ObjectID> p_synchronizers, uint16_t p_sync_net_time, uint64_t p_usec);
102 void _send_delta(int p_peer, const HashSet<ObjectID> p_synchronizers, uint64_t p_usec, const HashMap<ObjectID, uint64_t> p_last_watch_usecs);
103 Error _make_spawn_packet(Node *p_node, MultiplayerSpawner *p_spawner, int &r_len);
104 Error _make_despawn_packet(Node *p_node, int &r_len);
105 Error _send_raw(const uint8_t *p_buffer, int p_size, int p_peer, bool p_reliable);
106
107 void _visibility_changed(int p_peer, ObjectID p_oid);
108 Error _update_sync_visibility(int p_peer, MultiplayerSynchronizer *p_sync);
109 Error _update_spawn_visibility(int p_peer, const ObjectID &p_oid);
110 void _free_remotes(const PeerInfo &p_info);
111
112 template <class T>
113 static T *get_id_as(const ObjectID &p_id) {
114 return p_id.is_valid() ? Object::cast_to<T>(ObjectDB::get_instance(p_id)) : nullptr;
115 }
116
117#ifdef DEBUG_ENABLED
118 _FORCE_INLINE_ void _profile_node_data(const String &p_what, ObjectID p_id, int p_size);
119#endif
120
121public:
122 static void make_default();
123
124 void on_reset();
125 void on_peer_change(int p_id, bool p_connected);
126
127 Error on_spawn(Object *p_obj, Variant p_config);
128 Error on_despawn(Object *p_obj, Variant p_config);
129 Error on_replication_start(Object *p_obj, Variant p_config);
130 Error on_replication_stop(Object *p_obj, Variant p_config);
131 void on_network_process();
132
133 Error on_spawn_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len);
134 Error on_despawn_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len);
135 Error on_sync_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len);
136 Error on_delta_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len);
137
138 bool is_rpc_visible(const ObjectID &p_oid, int p_peer) const;
139
140 void set_max_sync_packet_size(int p_size);
141 int get_max_sync_packet_size() const;
142
143 void set_max_delta_packet_size(int p_size);
144 int get_max_delta_packet_size() const;
145
146 SceneReplicationInterface(SceneMultiplayer *p_multiplayer) {
147 multiplayer = p_multiplayer;
148 }
149};
150
151#endif // SCENE_REPLICATION_INTERFACE_H
152