scene_replication_interface.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. /**************************************************************************/
  2. /* scene_replication_interface.cpp */
  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. #include "scene_replication_interface.h"
  31. #include "scene_multiplayer.h"
  32. #include "core/debugger/engine_debugger.h"
  33. #include "core/io/marshalls.h"
  34. #include "core/os/os.h"
  35. #include "scene/main/node.h"
  36. #define MAKE_ROOM(m_amount) \
  37. if (packet_cache.size() < m_amount) \
  38. packet_cache.resize(m_amount);
  39. #ifdef DEBUG_ENABLED
  40. _FORCE_INLINE_ void SceneReplicationInterface::_profile_node_data(const String &p_what, ObjectID p_id, int p_size) {
  41. if (EngineDebugger::is_profiling("multiplayer:replication")) {
  42. Array values = { p_what, p_id, p_size };
  43. EngineDebugger::profiler_add_frame_data("multiplayer:replication", values);
  44. }
  45. }
  46. #endif
  47. SceneReplicationInterface::TrackedNode &SceneReplicationInterface::_track(const ObjectID &p_id) {
  48. if (!tracked_nodes.has(p_id)) {
  49. tracked_nodes[p_id] = TrackedNode(p_id);
  50. Node *node = get_id_as<Node>(p_id);
  51. node->connect(SceneStringName(tree_exited), callable_mp(this, &SceneReplicationInterface::_untrack).bind(p_id), Node::CONNECT_ONE_SHOT);
  52. }
  53. return tracked_nodes[p_id];
  54. }
  55. void SceneReplicationInterface::_untrack(const ObjectID &p_id) {
  56. if (!tracked_nodes.has(p_id)) {
  57. return;
  58. }
  59. uint32_t net_id = tracked_nodes[p_id].net_id;
  60. uint32_t peer = tracked_nodes[p_id].remote_peer;
  61. tracked_nodes.erase(p_id);
  62. // If it was spawned by a remote, remove it from the received nodes.
  63. if (peer && peers_info.has(peer)) {
  64. peers_info[peer].recv_nodes.erase(net_id);
  65. }
  66. // If we spawned or synced it, we need to remove it from any peer it was sent to.
  67. if (net_id || peer == 0) {
  68. for (KeyValue<int, PeerInfo> &E : peers_info) {
  69. E.value.spawn_nodes.erase(p_id);
  70. }
  71. }
  72. }
  73. void SceneReplicationInterface::_free_remotes(const PeerInfo &p_info) {
  74. for (const KeyValue<uint32_t, ObjectID> &E : p_info.recv_nodes) {
  75. Node *node = tracked_nodes.has(E.value) ? get_id_as<Node>(E.value) : nullptr;
  76. ERR_CONTINUE(!node);
  77. node->queue_free();
  78. }
  79. }
  80. bool SceneReplicationInterface::_has_authority(const Node *p_node) {
  81. return multiplayer->has_multiplayer_peer() && p_node->get_multiplayer_authority() == multiplayer->get_unique_id();
  82. }
  83. void SceneReplicationInterface::on_peer_change(int p_id, bool p_connected) {
  84. if (p_connected) {
  85. peers_info[p_id] = PeerInfo();
  86. for (const ObjectID &oid : spawned_nodes) {
  87. _update_spawn_visibility(p_id, oid);
  88. }
  89. for (const ObjectID &oid : sync_nodes) {
  90. _update_sync_visibility(p_id, get_id_as<MultiplayerSynchronizer>(oid));
  91. }
  92. } else {
  93. ERR_FAIL_COND(!peers_info.has(p_id));
  94. _free_remotes(peers_info[p_id]);
  95. peers_info.erase(p_id);
  96. }
  97. }
  98. void SceneReplicationInterface::on_reset() {
  99. for (const KeyValue<int, PeerInfo> &E : peers_info) {
  100. _free_remotes(E.value);
  101. }
  102. peers_info.clear();
  103. // Tracked nodes are cleared on deletion, here we only reset the ids so they can be later re-assigned.
  104. for (KeyValue<ObjectID, TrackedNode> &E : tracked_nodes) {
  105. TrackedNode &tobj = E.value;
  106. tobj.net_id = 0;
  107. tobj.remote_peer = 0;
  108. }
  109. for (const ObjectID &oid : sync_nodes) {
  110. MultiplayerSynchronizer *sync = get_id_as<MultiplayerSynchronizer>(oid);
  111. ERR_CONTINUE(!sync);
  112. sync->reset();
  113. }
  114. last_net_id = 0;
  115. }
  116. void SceneReplicationInterface::on_network_process() {
  117. // Prevent endless stalling in case of unforeseen spawn errors.
  118. if (spawn_queue.size()) {
  119. ERR_PRINT("An error happened during last spawn, this usually means the 'ready' signal was not emitted by the spawned node.");
  120. for (const ObjectID &oid : spawn_queue) {
  121. Node *node = get_id_as<Node>(oid);
  122. ERR_CONTINUE(!node);
  123. if (node->is_connected(SceneStringName(ready), callable_mp(this, &SceneReplicationInterface::_node_ready))) {
  124. node->disconnect(SceneStringName(ready), callable_mp(this, &SceneReplicationInterface::_node_ready));
  125. }
  126. }
  127. spawn_queue.clear();
  128. }
  129. // Process syncs.
  130. uint64_t usec = OS::get_singleton()->get_ticks_usec();
  131. for (KeyValue<int, PeerInfo> &E : peers_info) {
  132. const HashSet<ObjectID> to_sync = E.value.sync_nodes;
  133. if (to_sync.is_empty()) {
  134. continue; // Nothing to sync
  135. }
  136. uint16_t sync_net_time = ++E.value.last_sent_sync;
  137. _send_sync(E.key, to_sync, sync_net_time, usec);
  138. _send_delta(E.key, to_sync, usec, E.value.last_watch_usecs);
  139. }
  140. }
  141. Error SceneReplicationInterface::on_spawn(Object *p_obj, Variant p_config) {
  142. Node *node = Object::cast_to<Node>(p_obj);
  143. ERR_FAIL_COND_V(!node || p_config.get_type() != Variant::OBJECT, ERR_INVALID_PARAMETER);
  144. MultiplayerSpawner *spawner = Object::cast_to<MultiplayerSpawner>(p_config.get_validated_object());
  145. ERR_FAIL_NULL_V(spawner, ERR_INVALID_PARAMETER);
  146. // Track node.
  147. const ObjectID oid = node->get_instance_id();
  148. TrackedNode &tobj = _track(oid);
  149. // Spawn state needs to be callected after "ready", but the spawn order follows "enter_tree".
  150. ERR_FAIL_COND_V(tobj.spawner != ObjectID(), ERR_ALREADY_IN_USE);
  151. tobj.spawner = spawner->get_instance_id();
  152. spawn_queue.insert(oid);
  153. node->connect(SceneStringName(ready), callable_mp(this, &SceneReplicationInterface::_node_ready).bind(oid), Node::CONNECT_ONE_SHOT);
  154. return OK;
  155. }
  156. void SceneReplicationInterface::_node_ready(const ObjectID &p_oid) {
  157. ERR_FAIL_COND(!spawn_queue.has(p_oid)); // Bug.
  158. // If we are a nested spawn, we need to wait until the parent is ready.
  159. if (p_oid != *(spawn_queue.begin())) {
  160. return;
  161. }
  162. for (const ObjectID &oid : spawn_queue) {
  163. ERR_CONTINUE(!tracked_nodes.has(oid));
  164. TrackedNode &tobj = tracked_nodes[oid];
  165. MultiplayerSpawner *spawner = get_id_as<MultiplayerSpawner>(tobj.spawner);
  166. ERR_CONTINUE(!spawner);
  167. spawned_nodes.insert(oid);
  168. if (_has_authority(spawner)) {
  169. _update_spawn_visibility(0, oid);
  170. }
  171. }
  172. spawn_queue.clear();
  173. }
  174. Error SceneReplicationInterface::on_despawn(Object *p_obj, Variant p_config) {
  175. Node *node = Object::cast_to<Node>(p_obj);
  176. ERR_FAIL_COND_V(!node || p_config.get_type() != Variant::OBJECT, ERR_INVALID_PARAMETER);
  177. MultiplayerSpawner *spawner = Object::cast_to<MultiplayerSpawner>(p_config.get_validated_object());
  178. ERR_FAIL_COND_V(!p_obj || !spawner, ERR_INVALID_PARAMETER);
  179. // Forcibly despawn to all peers that knowns me.
  180. int len = 0;
  181. Error err = _make_despawn_packet(node, len);
  182. ERR_FAIL_COND_V(err != OK, ERR_BUG);
  183. const ObjectID oid = p_obj->get_instance_id();
  184. for (const KeyValue<int, PeerInfo> &E : peers_info) {
  185. if (!E.value.spawn_nodes.has(oid)) {
  186. continue;
  187. }
  188. _send_raw(packet_cache.ptr(), len, E.key, true);
  189. }
  190. // Also remove spawner tracking from the replication state.
  191. ERR_FAIL_COND_V(!tracked_nodes.has(oid), ERR_INVALID_PARAMETER);
  192. TrackedNode &tobj = _track(oid);
  193. ERR_FAIL_COND_V(tobj.spawner != spawner->get_instance_id(), ERR_INVALID_PARAMETER);
  194. tobj.spawner = ObjectID();
  195. spawned_nodes.erase(oid);
  196. for (KeyValue<int, PeerInfo> &E : peers_info) {
  197. E.value.spawn_nodes.erase(oid);
  198. }
  199. return OK;
  200. }
  201. Error SceneReplicationInterface::on_replication_start(Object *p_obj, Variant p_config) {
  202. Node *node = Object::cast_to<Node>(p_obj);
  203. ERR_FAIL_COND_V(!node || p_config.get_type() != Variant::OBJECT, ERR_INVALID_PARAMETER);
  204. MultiplayerSynchronizer *sync = Object::cast_to<MultiplayerSynchronizer>(p_config.get_validated_object());
  205. ERR_FAIL_NULL_V(sync, ERR_INVALID_PARAMETER);
  206. // Add to synchronizer list.
  207. TrackedNode &tobj = _track(p_obj->get_instance_id());
  208. const ObjectID sid = sync->get_instance_id();
  209. tobj.synchronizers.insert(sid);
  210. sync_nodes.insert(sid);
  211. // Update visibility.
  212. sync->connect(SceneStringName(visibility_changed), callable_mp(this, &SceneReplicationInterface::_visibility_changed).bind(sync->get_instance_id()));
  213. _update_sync_visibility(0, sync);
  214. if (pending_spawn == p_obj->get_instance_id() && sync->get_multiplayer_authority() == pending_spawn_remote) {
  215. // Try to apply synchronizer Net ID
  216. ERR_FAIL_COND_V_MSG(pending_sync_net_ids.is_empty(), ERR_INVALID_DATA, vformat("The MultiplayerSynchronizer at path \"%s\" is unable to process the pending spawn since it has no network ID. This might happen when changing the multiplayer authority during the \"_ready\" callback. Make sure to only change the authority of multiplayer synchronizers during \"_enter_tree\" or the \"_spawn_custom\" callback of their multiplayer spawner.", sync->get_path()));
  217. ERR_FAIL_COND_V(!peers_info.has(pending_spawn_remote), ERR_INVALID_DATA);
  218. uint32_t net_id = pending_sync_net_ids.front()->get();
  219. pending_sync_net_ids.pop_front();
  220. peers_info[pending_spawn_remote].recv_sync_ids[net_id] = sync->get_instance_id();
  221. sync->set_net_id(net_id);
  222. // Try to apply spawn state (before ready).
  223. if (pending_buffer_size > 0) {
  224. ERR_FAIL_COND_V(!node || !sync->get_replication_config_ptr(), ERR_UNCONFIGURED);
  225. int consumed = 0;
  226. const List<NodePath> props = sync->get_replication_config_ptr()->get_spawn_properties();
  227. Vector<Variant> vars;
  228. vars.resize(props.size());
  229. Error err = MultiplayerAPI::decode_and_decompress_variants(vars, pending_buffer, pending_buffer_size, consumed);
  230. ERR_FAIL_COND_V(err, err);
  231. if (consumed > 0) {
  232. pending_buffer += consumed;
  233. pending_buffer_size -= consumed;
  234. err = MultiplayerSynchronizer::set_state(props, node, vars);
  235. ERR_FAIL_COND_V(err, err);
  236. }
  237. }
  238. }
  239. return OK;
  240. }
  241. Error SceneReplicationInterface::on_replication_stop(Object *p_obj, Variant p_config) {
  242. Node *node = Object::cast_to<Node>(p_obj);
  243. ERR_FAIL_COND_V(!node || p_config.get_type() != Variant::OBJECT, ERR_INVALID_PARAMETER);
  244. MultiplayerSynchronizer *sync = Object::cast_to<MultiplayerSynchronizer>(p_config.get_validated_object());
  245. ERR_FAIL_NULL_V(sync, ERR_INVALID_PARAMETER);
  246. sync->disconnect(SceneStringName(visibility_changed), callable_mp(this, &SceneReplicationInterface::_visibility_changed));
  247. // Untrack synchronizer.
  248. const ObjectID oid = node->get_instance_id();
  249. const ObjectID sid = sync->get_instance_id();
  250. ERR_FAIL_COND_V(!tracked_nodes.has(oid), ERR_INVALID_PARAMETER);
  251. TrackedNode &tobj = _track(oid);
  252. tobj.synchronizers.erase(sid);
  253. sync_nodes.erase(sid);
  254. for (KeyValue<int, PeerInfo> &E : peers_info) {
  255. E.value.sync_nodes.erase(sid);
  256. E.value.last_watch_usecs.erase(sid);
  257. if (sync->get_net_id()) {
  258. E.value.recv_sync_ids.erase(sync->get_net_id());
  259. }
  260. }
  261. return OK;
  262. }
  263. void SceneReplicationInterface::_visibility_changed(int p_peer, ObjectID p_sid) {
  264. MultiplayerSynchronizer *sync = get_id_as<MultiplayerSynchronizer>(p_sid);
  265. ERR_FAIL_NULL(sync); // Bug.
  266. Node *node = sync->get_root_node();
  267. ERR_FAIL_NULL(node); // Bug.
  268. const ObjectID oid = node->get_instance_id();
  269. if (spawned_nodes.has(oid) && p_peer != multiplayer->get_unique_id()) {
  270. _update_spawn_visibility(p_peer, oid);
  271. }
  272. _update_sync_visibility(p_peer, sync);
  273. }
  274. bool SceneReplicationInterface::is_rpc_visible(const ObjectID &p_oid, int p_peer) const {
  275. if (!tracked_nodes.has(p_oid)) {
  276. return true; // Untracked nodes are always visible to RPCs.
  277. }
  278. ERR_FAIL_COND_V(p_peer < 0, false);
  279. const TrackedNode &tnode = tracked_nodes[p_oid];
  280. if (tnode.synchronizers.is_empty()) {
  281. return true; // No synchronizers means no visibility restrictions.
  282. }
  283. if (tnode.remote_peer && uint32_t(p_peer) == tnode.remote_peer) {
  284. return true; // RPCs on spawned nodes are always visible to spawner.
  285. } else if (spawned_nodes.has(p_oid)) {
  286. // It's a spawned node we control, this can be fast.
  287. if (p_peer) {
  288. return peers_info.has(p_peer) && peers_info[p_peer].spawn_nodes.has(p_oid);
  289. } else {
  290. for (const KeyValue<int, PeerInfo> &E : peers_info) {
  291. if (!E.value.spawn_nodes.has(p_oid)) {
  292. return false; // Not public.
  293. }
  294. }
  295. return true; // All peers have this node.
  296. }
  297. } else {
  298. // Cycle object synchronizers to check visibility.
  299. for (const ObjectID &sid : tnode.synchronizers) {
  300. MultiplayerSynchronizer *sync = get_id_as<MultiplayerSynchronizer>(sid);
  301. ERR_CONTINUE(!sync);
  302. // RPC visibility is composed using OR when multiple synchronizers are present.
  303. // Note that we don't really care about authority here which may lead to unexpected
  304. // results when using multiple synchronizers to control the same node.
  305. if (sync->is_visible_to(p_peer)) {
  306. return true;
  307. }
  308. }
  309. return false; // Not visible.
  310. }
  311. }
  312. Error SceneReplicationInterface::_update_sync_visibility(int p_peer, MultiplayerSynchronizer *p_sync) {
  313. ERR_FAIL_NULL_V(p_sync, ERR_BUG);
  314. if (!_has_authority(p_sync) || p_peer == multiplayer->get_unique_id()) {
  315. return OK;
  316. }
  317. const ObjectID &sid = p_sync->get_instance_id();
  318. bool is_visible = p_sync->is_visible_to(p_peer);
  319. if (p_peer == 0) {
  320. for (KeyValue<int, PeerInfo> &E : peers_info) {
  321. // Might be visible to this specific peer.
  322. bool is_visible_to_peer = is_visible || p_sync->is_visible_to(E.key);
  323. if (is_visible_to_peer == E.value.sync_nodes.has(sid)) {
  324. continue;
  325. }
  326. if (is_visible_to_peer) {
  327. E.value.sync_nodes.insert(sid);
  328. } else {
  329. E.value.sync_nodes.erase(sid);
  330. E.value.last_watch_usecs.erase(sid);
  331. }
  332. }
  333. return OK;
  334. } else {
  335. ERR_FAIL_COND_V(!peers_info.has(p_peer), ERR_INVALID_PARAMETER);
  336. if (is_visible == peers_info[p_peer].sync_nodes.has(sid)) {
  337. return OK;
  338. }
  339. if (is_visible) {
  340. peers_info[p_peer].sync_nodes.insert(sid);
  341. } else {
  342. peers_info[p_peer].sync_nodes.erase(sid);
  343. peers_info[p_peer].last_watch_usecs.erase(sid);
  344. }
  345. return OK;
  346. }
  347. }
  348. Error SceneReplicationInterface::_update_spawn_visibility(int p_peer, const ObjectID &p_oid) {
  349. const TrackedNode *tnode = tracked_nodes.getptr(p_oid);
  350. ERR_FAIL_NULL_V(tnode, ERR_BUG);
  351. MultiplayerSpawner *spawner = get_id_as<MultiplayerSpawner>(tnode->spawner);
  352. Node *node = get_id_as<Node>(p_oid);
  353. ERR_FAIL_NULL_V(node, ERR_BUG);
  354. ERR_FAIL_NULL_V(spawner, ERR_BUG);
  355. ERR_FAIL_COND_V(!_has_authority(spawner), ERR_BUG);
  356. ERR_FAIL_COND_V(!tracked_nodes.has(p_oid), ERR_BUG);
  357. const HashSet<ObjectID> synchronizers = tracked_nodes[p_oid].synchronizers;
  358. bool is_visible = true;
  359. for (const ObjectID &sid : synchronizers) {
  360. MultiplayerSynchronizer *sync = get_id_as<MultiplayerSynchronizer>(sid);
  361. ERR_CONTINUE(!sync);
  362. if (!_has_authority(sync)) {
  363. continue;
  364. }
  365. // Spawn visibility is composed using OR when multiple synchronizers are present.
  366. if (sync->is_visible_to(p_peer)) {
  367. is_visible = true;
  368. break;
  369. }
  370. is_visible = false;
  371. }
  372. // Spawn (and despawn) when needed.
  373. HashSet<int> to_spawn;
  374. HashSet<int> to_despawn;
  375. if (p_peer) {
  376. ERR_FAIL_COND_V(!peers_info.has(p_peer), ERR_INVALID_PARAMETER);
  377. if (is_visible == peers_info[p_peer].spawn_nodes.has(p_oid)) {
  378. return OK;
  379. }
  380. if (is_visible) {
  381. to_spawn.insert(p_peer);
  382. } else {
  383. to_despawn.insert(p_peer);
  384. }
  385. } else {
  386. // Check visibility for each peers.
  387. for (const KeyValue<int, PeerInfo> &E : peers_info) {
  388. if (is_visible) {
  389. // This is fast, since the object is visible to everyone, we don't need to check each peer.
  390. if (E.value.spawn_nodes.has(p_oid)) {
  391. // Already spawned.
  392. continue;
  393. }
  394. to_spawn.insert(E.key);
  395. } else {
  396. // Need to check visibility for each peer.
  397. _update_spawn_visibility(E.key, p_oid);
  398. }
  399. }
  400. }
  401. if (to_spawn.size()) {
  402. int len = 0;
  403. _make_spawn_packet(node, spawner, len);
  404. for (int pid : to_spawn) {
  405. ERR_CONTINUE(!peers_info.has(pid));
  406. int path_id;
  407. multiplayer_cache->send_object_cache(spawner, pid, path_id);
  408. _send_raw(packet_cache.ptr(), len, pid, true);
  409. peers_info[pid].spawn_nodes.insert(p_oid);
  410. }
  411. }
  412. if (to_despawn.size()) {
  413. int len = 0;
  414. _make_despawn_packet(node, len);
  415. for (int pid : to_despawn) {
  416. ERR_CONTINUE(!peers_info.has(pid));
  417. peers_info[pid].spawn_nodes.erase(p_oid);
  418. _send_raw(packet_cache.ptr(), len, pid, true);
  419. }
  420. }
  421. return OK;
  422. }
  423. Error SceneReplicationInterface::_send_raw(const uint8_t *p_buffer, int p_size, int p_peer, bool p_reliable) {
  424. ERR_FAIL_COND_V(!p_buffer || p_size < 1, ERR_INVALID_PARAMETER);
  425. Ref<MultiplayerPeer> peer = multiplayer->get_multiplayer_peer();
  426. ERR_FAIL_COND_V(peer.is_null(), ERR_UNCONFIGURED);
  427. peer->set_transfer_channel(0);
  428. peer->set_transfer_mode(p_reliable ? MultiplayerPeer::TRANSFER_MODE_RELIABLE : MultiplayerPeer::TRANSFER_MODE_UNRELIABLE);
  429. return multiplayer->send_command(p_peer, p_buffer, p_size);
  430. }
  431. Error SceneReplicationInterface::_make_spawn_packet(Node *p_node, MultiplayerSpawner *p_spawner, int &r_len) {
  432. ERR_FAIL_COND_V(!multiplayer || !p_node || !p_spawner, ERR_BUG);
  433. const ObjectID oid = p_node->get_instance_id();
  434. TrackedNode *tnode = tracked_nodes.getptr(oid);
  435. ERR_FAIL_NULL_V(tnode, ERR_INVALID_PARAMETER);
  436. if (tnode->net_id == 0) {
  437. // Ensure the node has an ID.
  438. tnode->net_id = ++last_net_id;
  439. }
  440. uint32_t nid = tnode->net_id;
  441. ERR_FAIL_COND_V(!nid, ERR_UNCONFIGURED);
  442. // Prepare custom arg and scene_id
  443. uint8_t scene_id = p_spawner->find_spawnable_scene_index_from_object(oid);
  444. bool is_custom = scene_id == MultiplayerSpawner::INVALID_ID;
  445. Variant spawn_arg = p_spawner->get_spawn_argument(oid);
  446. int spawn_arg_size = 0;
  447. if (is_custom) {
  448. Error err = MultiplayerAPI::encode_and_compress_variant(spawn_arg, nullptr, spawn_arg_size, false);
  449. ERR_FAIL_COND_V(err, err);
  450. }
  451. // Prepare spawn state.
  452. List<NodePath> state_props;
  453. List<uint32_t> sync_ids;
  454. const HashSet<ObjectID> synchronizers = tnode->synchronizers;
  455. for (const ObjectID &sid : synchronizers) {
  456. MultiplayerSynchronizer *sync = get_id_as<MultiplayerSynchronizer>(sid);
  457. if (!_has_authority(sync)) {
  458. continue;
  459. }
  460. ERR_CONTINUE(!sync);
  461. ERR_FAIL_NULL_V(sync->get_replication_config_ptr(), ERR_BUG);
  462. for (const NodePath &prop : sync->get_replication_config_ptr()->get_spawn_properties()) {
  463. state_props.push_back(prop);
  464. }
  465. // Ensure the synchronizer has an ID.
  466. if (sync->get_net_id() == 0) {
  467. sync->set_net_id(++last_net_id);
  468. }
  469. sync_ids.push_back(sync->get_net_id());
  470. }
  471. int state_size = 0;
  472. Vector<Variant> state_vars;
  473. Vector<const Variant *> state_varp;
  474. if (state_props.size()) {
  475. Error err = MultiplayerSynchronizer::get_state(state_props, p_node, state_vars, state_varp);
  476. ERR_FAIL_COND_V_MSG(err != OK, err, "Unable to retrieve spawn state.");
  477. err = MultiplayerAPI::encode_and_compress_variants(state_varp.ptrw(), state_varp.size(), nullptr, state_size);
  478. ERR_FAIL_COND_V_MSG(err != OK, err, "Unable to encode spawn state.");
  479. }
  480. // Encode scene ID, path ID, net ID, node name.
  481. int path_id = multiplayer_cache->make_object_cache(p_spawner);
  482. CharString cname = p_node->get_name().operator String().utf8();
  483. int nlen = encode_cstring(cname.get_data(), nullptr);
  484. MAKE_ROOM(1 + 1 + 4 + 4 + 4 + 4 * sync_ids.size() + 4 + nlen + (is_custom ? 4 + spawn_arg_size : 0) + state_size);
  485. uint8_t *ptr = packet_cache.ptrw();
  486. ptr[0] = (uint8_t)SceneMultiplayer::NETWORK_COMMAND_SPAWN;
  487. ptr[1] = scene_id;
  488. int ofs = 2;
  489. ofs += encode_uint32(path_id, &ptr[ofs]);
  490. ofs += encode_uint32(nid, &ptr[ofs]);
  491. ofs += encode_uint32(sync_ids.size(), &ptr[ofs]);
  492. ofs += encode_uint32(nlen, &ptr[ofs]);
  493. for (uint32_t snid : sync_ids) {
  494. ofs += encode_uint32(snid, &ptr[ofs]);
  495. }
  496. ofs += encode_cstring(cname.get_data(), &ptr[ofs]);
  497. // Write args
  498. if (is_custom) {
  499. ofs += encode_uint32(spawn_arg_size, &ptr[ofs]);
  500. Error err = MultiplayerAPI::encode_and_compress_variant(spawn_arg, &ptr[ofs], spawn_arg_size, false);
  501. ERR_FAIL_COND_V(err, err);
  502. ofs += spawn_arg_size;
  503. }
  504. // Write state.
  505. if (state_size) {
  506. Error err = MultiplayerAPI::encode_and_compress_variants(state_varp.ptrw(), state_varp.size(), &ptr[ofs], state_size);
  507. ERR_FAIL_COND_V(err, err);
  508. ofs += state_size;
  509. }
  510. r_len = ofs;
  511. return OK;
  512. }
  513. Error SceneReplicationInterface::_make_despawn_packet(Node *p_node, int &r_len) {
  514. const ObjectID oid = p_node->get_instance_id();
  515. const TrackedNode *tnode = tracked_nodes.getptr(oid);
  516. ERR_FAIL_NULL_V(tnode, ERR_INVALID_PARAMETER);
  517. MAKE_ROOM(5);
  518. uint8_t *ptr = packet_cache.ptrw();
  519. ptr[0] = (uint8_t)SceneMultiplayer::NETWORK_COMMAND_DESPAWN;
  520. int ofs = 1;
  521. uint32_t nid = tnode->net_id;
  522. ofs += encode_uint32(nid, &ptr[ofs]);
  523. r_len = ofs;
  524. return OK;
  525. }
  526. Error SceneReplicationInterface::on_spawn_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len) {
  527. ERR_FAIL_COND_V_MSG(p_buffer_len < 18, ERR_INVALID_DATA, "Invalid spawn packet received");
  528. int ofs = 1; // The spawn/despawn command.
  529. uint8_t scene_id = p_buffer[ofs];
  530. ofs += 1;
  531. uint32_t node_target = decode_uint32(&p_buffer[ofs]);
  532. ofs += 4;
  533. MultiplayerSpawner *spawner = Object::cast_to<MultiplayerSpawner>(multiplayer_cache->get_cached_object(p_from, node_target));
  534. ERR_FAIL_NULL_V(spawner, ERR_DOES_NOT_EXIST);
  535. ERR_FAIL_COND_V(p_from != spawner->get_multiplayer_authority(), ERR_UNAUTHORIZED);
  536. uint32_t net_id = decode_uint32(&p_buffer[ofs]);
  537. ofs += 4;
  538. uint32_t sync_len = decode_uint32(&p_buffer[ofs]);
  539. ofs += 4;
  540. uint32_t name_len = decode_uint32(&p_buffer[ofs]);
  541. ofs += 4;
  542. ERR_FAIL_COND_V_MSG(name_len + (sync_len * 4) > uint32_t(p_buffer_len - ofs), ERR_INVALID_DATA, vformat("Invalid spawn packet size: %d, wants: %d", p_buffer_len, ofs + name_len + (sync_len * 4)));
  543. List<uint32_t> sync_ids;
  544. for (uint32_t i = 0; i < sync_len; i++) {
  545. sync_ids.push_back(decode_uint32(&p_buffer[ofs]));
  546. ofs += 4;
  547. }
  548. ERR_FAIL_COND_V_MSG(name_len < 1, ERR_INVALID_DATA, "Zero spawn name size.");
  549. // We need to make sure no trickery happens here, but we want to allow autogenerated ("@") node names.
  550. const String name = String::utf8((const char *)&p_buffer[ofs], name_len);
  551. ERR_FAIL_COND_V_MSG(name.validate_node_name() != name, ERR_INVALID_DATA, vformat("Invalid node name received: '%s'. Make sure to add nodes via 'add_child(node, true)' remotely.", name));
  552. ofs += name_len;
  553. // Check that we can spawn.
  554. Node *parent = spawner->get_node_or_null(spawner->get_spawn_path());
  555. ERR_FAIL_NULL_V(parent, ERR_UNCONFIGURED);
  556. ERR_FAIL_COND_V(parent->has_node(name), ERR_INVALID_DATA);
  557. Node *node = nullptr;
  558. if (scene_id == MultiplayerSpawner::INVALID_ID) {
  559. // Custom spawn.
  560. ERR_FAIL_COND_V(p_buffer_len - ofs < 4, ERR_INVALID_DATA);
  561. uint32_t arg_size = decode_uint32(&p_buffer[ofs]);
  562. ofs += 4;
  563. ERR_FAIL_COND_V(arg_size > uint32_t(p_buffer_len - ofs), ERR_INVALID_DATA);
  564. Variant v;
  565. Error err = MultiplayerAPI::decode_and_decompress_variant(v, &p_buffer[ofs], arg_size, nullptr, false);
  566. ERR_FAIL_COND_V(err != OK, err);
  567. ofs += arg_size;
  568. node = spawner->instantiate_custom(v);
  569. } else {
  570. // Scene based spawn.
  571. node = spawner->instantiate_scene(scene_id);
  572. }
  573. ERR_FAIL_NULL_V(node, ERR_UNAUTHORIZED);
  574. node->set_name(name);
  575. // Add and track remote
  576. ERR_FAIL_COND_V(!peers_info.has(p_from), ERR_UNAVAILABLE);
  577. ERR_FAIL_COND_V(peers_info[p_from].recv_nodes.has(net_id), ERR_ALREADY_IN_USE);
  578. ObjectID oid = node->get_instance_id();
  579. TrackedNode &tobj = _track(oid);
  580. tobj.spawner = spawner->get_instance_id();
  581. tobj.net_id = net_id;
  582. tobj.remote_peer = p_from;
  583. peers_info[p_from].recv_nodes[net_id] = oid;
  584. // The initial state will be applied during the sync config (i.e. before _ready).
  585. pending_spawn = node->get_instance_id();
  586. pending_spawn_remote = p_from;
  587. pending_buffer_size = p_buffer_len - ofs;
  588. pending_buffer = pending_buffer_size > 0 ? &p_buffer[ofs] : nullptr;
  589. pending_sync_net_ids = sync_ids;
  590. parent->add_child(node);
  591. spawner->emit_signal(SNAME("spawned"), node);
  592. pending_spawn = ObjectID();
  593. pending_spawn_remote = 0;
  594. pending_buffer = nullptr;
  595. pending_buffer_size = 0;
  596. if (pending_sync_net_ids.size()) {
  597. pending_sync_net_ids.clear();
  598. ERR_FAIL_V(ERR_INVALID_DATA); // Should have been consumed.
  599. }
  600. return OK;
  601. }
  602. Error SceneReplicationInterface::on_despawn_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len) {
  603. ERR_FAIL_COND_V_MSG(p_buffer_len < 5, ERR_INVALID_DATA, "Invalid spawn packet received");
  604. int ofs = 1; // The spawn/despawn command.
  605. uint32_t net_id = decode_uint32(&p_buffer[ofs]);
  606. ofs += 4;
  607. // Untrack remote
  608. ERR_FAIL_COND_V(!peers_info.has(p_from), ERR_UNAUTHORIZED);
  609. PeerInfo &pinfo = peers_info[p_from];
  610. ERR_FAIL_COND_V(!pinfo.recv_nodes.has(net_id), ERR_UNAUTHORIZED);
  611. Node *node = get_id_as<Node>(pinfo.recv_nodes[net_id]);
  612. ERR_FAIL_NULL_V(node, ERR_BUG);
  613. pinfo.recv_nodes.erase(net_id);
  614. const ObjectID oid = node->get_instance_id();
  615. ERR_FAIL_COND_V(!tracked_nodes.has(oid), ERR_BUG);
  616. MultiplayerSpawner *spawner = get_id_as<MultiplayerSpawner>(tracked_nodes[oid].spawner);
  617. ERR_FAIL_NULL_V(spawner, ERR_DOES_NOT_EXIST);
  618. ERR_FAIL_COND_V(p_from != spawner->get_multiplayer_authority(), ERR_UNAUTHORIZED);
  619. if (node->get_parent() != nullptr) {
  620. node->get_parent()->remove_child(node);
  621. }
  622. node->queue_free();
  623. spawner->emit_signal(SNAME("despawned"), node);
  624. return OK;
  625. }
  626. bool SceneReplicationInterface::_verify_synchronizer(int p_peer, MultiplayerSynchronizer *p_sync, uint32_t &r_net_id) {
  627. r_net_id = p_sync->get_net_id();
  628. if (r_net_id == 0 || (r_net_id & 0x80000000)) {
  629. int path_id = 0;
  630. bool verified = multiplayer_cache->send_object_cache(p_sync, p_peer, path_id);
  631. ERR_FAIL_COND_V_MSG(path_id < 0, false, "This should never happen!");
  632. if (r_net_id == 0) {
  633. // First time path based ID.
  634. r_net_id = path_id | 0x80000000;
  635. p_sync->set_net_id(r_net_id | 0x80000000);
  636. }
  637. return verified;
  638. }
  639. return true;
  640. }
  641. MultiplayerSynchronizer *SceneReplicationInterface::_find_synchronizer(int p_peer, uint32_t p_net_id) {
  642. MultiplayerSynchronizer *sync = nullptr;
  643. if (p_net_id & 0x80000000) {
  644. sync = Object::cast_to<MultiplayerSynchronizer>(multiplayer_cache->get_cached_object(p_peer, p_net_id & 0x7FFFFFFF));
  645. } else if (peers_info[p_peer].recv_sync_ids.has(p_net_id)) {
  646. const ObjectID &sid = peers_info[p_peer].recv_sync_ids[p_net_id];
  647. sync = get_id_as<MultiplayerSynchronizer>(sid);
  648. }
  649. return sync;
  650. }
  651. void SceneReplicationInterface::_send_delta(int p_peer, const HashSet<ObjectID> &p_synchronizers, uint64_t p_usec, const HashMap<ObjectID, uint64_t> &p_last_watch_usecs) {
  652. MAKE_ROOM(/* header */ 1 + /* element */ 4 + 8 + 4 + delta_mtu);
  653. uint8_t *ptr = packet_cache.ptrw();
  654. ptr[0] = SceneMultiplayer::NETWORK_COMMAND_SYNC | (1 << SceneMultiplayer::CMD_FLAG_0_SHIFT);
  655. int ofs = 1;
  656. for (const ObjectID &oid : p_synchronizers) {
  657. MultiplayerSynchronizer *sync = get_id_as<MultiplayerSynchronizer>(oid);
  658. ERR_CONTINUE(!sync || !sync->get_replication_config_ptr() || !_has_authority(sync));
  659. uint32_t net_id;
  660. if (!_verify_synchronizer(p_peer, sync, net_id)) {
  661. continue;
  662. }
  663. uint64_t last_usec = p_last_watch_usecs.has(oid) ? p_last_watch_usecs[oid] : 0;
  664. uint64_t indexes;
  665. List<Variant> delta = sync->get_delta_state(p_usec, last_usec, indexes);
  666. if (!delta.size()) {
  667. continue; // Nothing to update.
  668. }
  669. Vector<const Variant *> varp;
  670. varp.resize(delta.size());
  671. const Variant **vptr = varp.ptrw();
  672. int i = 0;
  673. for (const Variant &v : delta) {
  674. vptr[i] = &v;
  675. i++;
  676. }
  677. int size;
  678. Error err = MultiplayerAPI::encode_and_compress_variants(vptr, varp.size(), nullptr, size);
  679. ERR_CONTINUE_MSG(err != OK, "Unable to encode delta state.");
  680. ERR_CONTINUE_MSG(size > delta_mtu, vformat("Synchronizer delta bigger than MTU will not be sent (%d > %d): %s", size, delta_mtu, sync->get_path()));
  681. if (ofs + 4 + 8 + 4 + size > delta_mtu) {
  682. // Send what we got, and reset write.
  683. _send_raw(packet_cache.ptr(), ofs, p_peer, true);
  684. ofs = 1;
  685. }
  686. if (size) {
  687. ofs += encode_uint32(sync->get_net_id(), &ptr[ofs]);
  688. ofs += encode_uint64(indexes, &ptr[ofs]);
  689. ofs += encode_uint32(size, &ptr[ofs]);
  690. MultiplayerAPI::encode_and_compress_variants(vptr, varp.size(), &ptr[ofs], size);
  691. ofs += size;
  692. }
  693. #ifdef DEBUG_ENABLED
  694. _profile_node_data("delta_out", oid, size);
  695. #endif
  696. peers_info[p_peer].last_watch_usecs[oid] = p_usec;
  697. }
  698. if (ofs > 1) {
  699. // Got some left over to send.
  700. _send_raw(packet_cache.ptr(), ofs, p_peer, true);
  701. }
  702. }
  703. Error SceneReplicationInterface::on_delta_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len) {
  704. int ofs = 1;
  705. while (ofs + 4 + 8 + 4 < p_buffer_len) {
  706. uint32_t net_id = decode_uint32(&p_buffer[ofs]);
  707. ofs += 4;
  708. uint64_t indexes = decode_uint64(&p_buffer[ofs]);
  709. ofs += 8;
  710. uint32_t size = decode_uint32(&p_buffer[ofs]);
  711. ofs += 4;
  712. ERR_FAIL_COND_V(size > uint32_t(p_buffer_len - ofs), ERR_INVALID_DATA);
  713. MultiplayerSynchronizer *sync = _find_synchronizer(p_from, net_id);
  714. Node *node = sync ? sync->get_root_node() : nullptr;
  715. if (!sync || sync->get_multiplayer_authority() != p_from || !node) {
  716. ofs += size;
  717. ERR_CONTINUE_MSG(true, "Ignoring delta for non-authority or invalid synchronizer.");
  718. }
  719. List<NodePath> props = sync->get_delta_properties(indexes);
  720. ERR_FAIL_COND_V(props.is_empty(), ERR_INVALID_DATA);
  721. Vector<Variant> vars;
  722. vars.resize(props.size());
  723. int consumed = 0;
  724. Error err = MultiplayerAPI::decode_and_decompress_variants(vars, p_buffer + ofs, size, consumed);
  725. ERR_FAIL_COND_V(err != OK, err);
  726. ERR_FAIL_COND_V(uint32_t(consumed) != size, ERR_INVALID_DATA);
  727. err = MultiplayerSynchronizer::set_state(props, node, vars);
  728. ERR_FAIL_COND_V(err != OK, err);
  729. ofs += size;
  730. sync->emit_signal(SNAME("delta_synchronized"));
  731. #ifdef DEBUG_ENABLED
  732. _profile_node_data("delta_in", sync->get_instance_id(), size);
  733. #endif
  734. }
  735. return OK;
  736. }
  737. void SceneReplicationInterface::_send_sync(int p_peer, const HashSet<ObjectID> &p_synchronizers, uint16_t p_sync_net_time, uint64_t p_usec) {
  738. MAKE_ROOM(/* header */ 3 + /* element */ 4 + 4 + sync_mtu);
  739. uint8_t *ptr = packet_cache.ptrw();
  740. ptr[0] = SceneMultiplayer::NETWORK_COMMAND_SYNC;
  741. int ofs = 1;
  742. ofs += encode_uint16(p_sync_net_time, &ptr[1]);
  743. // Can only send updates for already notified nodes.
  744. // This is a lazy implementation, we could optimize much more here with by grouping by replication config.
  745. for (const ObjectID &oid : p_synchronizers) {
  746. MultiplayerSynchronizer *sync = get_id_as<MultiplayerSynchronizer>(oid);
  747. ERR_CONTINUE(!sync || !sync->get_replication_config_ptr() || !_has_authority(sync));
  748. if (!sync->update_outbound_sync_time(p_usec)) {
  749. continue; // nothing to sync.
  750. }
  751. Node *node = sync->get_root_node();
  752. ERR_CONTINUE(!node);
  753. uint32_t net_id = sync->get_net_id();
  754. if (!_verify_synchronizer(p_peer, sync, net_id)) {
  755. // The path based sync is not yet confirmed, skipping.
  756. continue;
  757. }
  758. int size;
  759. Vector<Variant> vars;
  760. Vector<const Variant *> varp;
  761. const List<NodePath> props = sync->get_replication_config_ptr()->get_sync_properties();
  762. Error err = MultiplayerSynchronizer::get_state(props, node, vars, varp);
  763. ERR_CONTINUE_MSG(err != OK, "Unable to retrieve sync state.");
  764. err = MultiplayerAPI::encode_and_compress_variants(varp.ptrw(), varp.size(), nullptr, size);
  765. ERR_CONTINUE_MSG(err != OK, "Unable to encode sync state.");
  766. // TODO Handle single state above MTU.
  767. ERR_CONTINUE_MSG(size > sync_mtu, vformat("Node states bigger than MTU will not be sent (%d > %d): %s", size, sync_mtu, node->get_path()));
  768. if (ofs + 4 + 4 + size > sync_mtu) {
  769. // Send what we got, and reset write.
  770. _send_raw(packet_cache.ptr(), ofs, p_peer, false);
  771. ofs = 3;
  772. }
  773. if (size) {
  774. ofs += encode_uint32(sync->get_net_id(), &ptr[ofs]);
  775. ofs += encode_uint32(size, &ptr[ofs]);
  776. MultiplayerAPI::encode_and_compress_variants(varp.ptrw(), varp.size(), &ptr[ofs], size);
  777. ofs += size;
  778. }
  779. #ifdef DEBUG_ENABLED
  780. _profile_node_data("sync_out", oid, size);
  781. #endif
  782. }
  783. if (ofs > 3) {
  784. // Got some left over to send.
  785. _send_raw(packet_cache.ptr(), ofs, p_peer, false);
  786. }
  787. }
  788. Error SceneReplicationInterface::on_sync_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len) {
  789. ERR_FAIL_COND_V_MSG(p_buffer_len < 11, ERR_INVALID_DATA, "Invalid sync packet received");
  790. bool is_delta = (p_buffer[0] & (1 << SceneMultiplayer::CMD_FLAG_0_SHIFT)) != 0;
  791. if (is_delta) {
  792. return on_delta_receive(p_from, p_buffer, p_buffer_len);
  793. }
  794. uint16_t time = decode_uint16(&p_buffer[1]);
  795. int ofs = 3;
  796. while (ofs + 8 < p_buffer_len) {
  797. uint32_t net_id = decode_uint32(&p_buffer[ofs]);
  798. ofs += 4;
  799. uint32_t size = decode_uint32(&p_buffer[ofs]);
  800. ofs += 4;
  801. ERR_FAIL_COND_V(size > uint32_t(p_buffer_len - ofs), ERR_INVALID_DATA);
  802. MultiplayerSynchronizer *sync = _find_synchronizer(p_from, net_id);
  803. if (!sync) {
  804. // Not received yet.
  805. ofs += size;
  806. continue;
  807. }
  808. Node *node = sync->get_root_node();
  809. if (sync->get_multiplayer_authority() != p_from || !node) {
  810. // Not valid for me.
  811. ofs += size;
  812. ERR_CONTINUE_MSG(true, "Ignoring sync data from non-authority or for missing node.");
  813. }
  814. if (!sync->update_inbound_sync_time(time)) {
  815. // State is too old.
  816. ofs += size;
  817. continue;
  818. }
  819. const List<NodePath> props = sync->get_replication_config_ptr()->get_sync_properties();
  820. Vector<Variant> vars;
  821. vars.resize(props.size());
  822. int consumed;
  823. Error err = MultiplayerAPI::decode_and_decompress_variants(vars, &p_buffer[ofs], size, consumed);
  824. ERR_FAIL_COND_V(err, err);
  825. err = MultiplayerSynchronizer::set_state(props, node, vars);
  826. ERR_FAIL_COND_V(err, err);
  827. ofs += size;
  828. sync->emit_signal(SNAME("synchronized"));
  829. #ifdef DEBUG_ENABLED
  830. _profile_node_data("sync_in", sync->get_instance_id(), size);
  831. #endif
  832. }
  833. return OK;
  834. }
  835. void SceneReplicationInterface::set_max_sync_packet_size(int p_size) {
  836. ERR_FAIL_COND_MSG(p_size < 128, "Sync maximum packet size must be at least 128 bytes.");
  837. sync_mtu = p_size;
  838. }
  839. int SceneReplicationInterface::get_max_sync_packet_size() const {
  840. return sync_mtu;
  841. }
  842. void SceneReplicationInterface::set_max_delta_packet_size(int p_size) {
  843. ERR_FAIL_COND_MSG(p_size < 128, "Sync maximum packet size must be at least 128 bytes.");
  844. delta_mtu = p_size;
  845. }
  846. int SceneReplicationInterface::get_max_delta_packet_size() const {
  847. return delta_mtu;
  848. }