scene_replication_interface.cpp 35 KB

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