scene_replication_interface.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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. #include "scene/scene_string_names.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;
  43. values.push_back(p_what);
  44. values.push_back(p_id);
  45. values.push_back(p_size);
  46. EngineDebugger::profiler_add_frame_data("multiplayer:replication", values);
  47. }
  48. }
  49. #endif
  50. SceneReplicationInterface::TrackedNode &SceneReplicationInterface::_track(const ObjectID &p_id) {
  51. if (!tracked_nodes.has(p_id)) {
  52. tracked_nodes[p_id] = TrackedNode(p_id);
  53. Node *node = get_id_as<Node>(p_id);
  54. node->connect(SceneStringNames::get_singleton()->tree_exited, callable_mp(this, &SceneReplicationInterface::_untrack).bind(p_id), Node::CONNECT_ONE_SHOT);
  55. }
  56. return tracked_nodes[p_id];
  57. }
  58. void SceneReplicationInterface::_untrack(const ObjectID &p_id) {
  59. if (!tracked_nodes.has(p_id)) {
  60. return;
  61. }
  62. uint32_t net_id = tracked_nodes[p_id].net_id;
  63. uint32_t peer = tracked_nodes[p_id].remote_peer;
  64. tracked_nodes.erase(p_id);
  65. // If it was spawned by a remote, remove it from the received nodes.
  66. if (peer && peers_info.has(peer)) {
  67. peers_info[peer].recv_nodes.erase(net_id);
  68. }
  69. // If we spawned or synced it, we need to remove it from any peer it was sent to.
  70. if (net_id || peer == 0) {
  71. for (KeyValue<int, PeerInfo> &E : peers_info) {
  72. E.value.spawn_nodes.erase(p_id);
  73. }
  74. }
  75. }
  76. void SceneReplicationInterface::_free_remotes(const PeerInfo &p_info) {
  77. for (const KeyValue<uint32_t, ObjectID> &E : p_info.recv_nodes) {
  78. Node *node = tracked_nodes.has(E.value) ? get_id_as<Node>(E.value) : nullptr;
  79. ERR_CONTINUE(!node);
  80. node->queue_free();
  81. }
  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(SceneStringNames::get_singleton()->ready, callable_mp(this, &SceneReplicationInterface::_node_ready))) {
  124. node->disconnect(SceneStringNames::get_singleton()->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_COND_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(SceneStringNames::get_singleton()->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 (multiplayer->has_multiplayer_peer() && spawner->is_multiplayer_authority()) {
  169. if (tobj.net_id == 0) {
  170. tobj.net_id = ++last_net_id;
  171. }
  172. _update_spawn_visibility(0, oid);
  173. }
  174. }
  175. spawn_queue.clear();
  176. }
  177. Error SceneReplicationInterface::on_despawn(Object *p_obj, Variant p_config) {
  178. Node *node = Object::cast_to<Node>(p_obj);
  179. ERR_FAIL_COND_V(!node || p_config.get_type() != Variant::OBJECT, ERR_INVALID_PARAMETER);
  180. MultiplayerSpawner *spawner = Object::cast_to<MultiplayerSpawner>(p_config.get_validated_object());
  181. ERR_FAIL_COND_V(!p_obj || !spawner, ERR_INVALID_PARAMETER);
  182. // Forcibly despawn to all peers that knowns me.
  183. int len = 0;
  184. Error err = _make_despawn_packet(node, len);
  185. ERR_FAIL_COND_V(err != OK, ERR_BUG);
  186. const ObjectID oid = p_obj->get_instance_id();
  187. for (const KeyValue<int, PeerInfo> &E : peers_info) {
  188. if (!E.value.spawn_nodes.has(oid)) {
  189. continue;
  190. }
  191. _send_raw(packet_cache.ptr(), len, E.key, true);
  192. }
  193. // Also remove spawner tracking from the replication state.
  194. ERR_FAIL_COND_V(!tracked_nodes.has(oid), ERR_INVALID_PARAMETER);
  195. TrackedNode &tobj = _track(oid);
  196. ERR_FAIL_COND_V(tobj.spawner != spawner->get_instance_id(), ERR_INVALID_PARAMETER);
  197. tobj.spawner = ObjectID();
  198. spawned_nodes.erase(oid);
  199. for (KeyValue<int, PeerInfo> &E : peers_info) {
  200. E.value.spawn_nodes.erase(oid);
  201. }
  202. return OK;
  203. }
  204. Error SceneReplicationInterface::on_replication_start(Object *p_obj, Variant p_config) {
  205. Node *node = Object::cast_to<Node>(p_obj);
  206. ERR_FAIL_COND_V(!node || p_config.get_type() != Variant::OBJECT, ERR_INVALID_PARAMETER);
  207. MultiplayerSynchronizer *sync = Object::cast_to<MultiplayerSynchronizer>(p_config.get_validated_object());
  208. ERR_FAIL_COND_V(!sync, ERR_INVALID_PARAMETER);
  209. // Add to synchronizer list.
  210. TrackedNode &tobj = _track(p_obj->get_instance_id());
  211. const ObjectID sid = sync->get_instance_id();
  212. tobj.synchronizers.insert(sid);
  213. sync_nodes.insert(sid);
  214. // Update visibility.
  215. sync->connect("visibility_changed", callable_mp(this, &SceneReplicationInterface::_visibility_changed).bind(sync->get_instance_id()));
  216. _update_sync_visibility(0, sync);
  217. if (pending_spawn == p_obj->get_instance_id() && sync->get_multiplayer_authority() == pending_spawn_remote) {
  218. // Try to apply synchronizer Net ID
  219. 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()));
  220. ERR_FAIL_COND_V(!peers_info.has(pending_spawn_remote), ERR_INVALID_DATA);
  221. uint32_t net_id = pending_sync_net_ids[0];
  222. pending_sync_net_ids.pop_front();
  223. peers_info[pending_spawn_remote].recv_sync_ids[net_id] = sync->get_instance_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().is_null(), ERR_UNCONFIGURED);
  227. int consumed = 0;
  228. const List<NodePath> props = sync->get_replication_config()->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_COND_V(!sync, ERR_INVALID_PARAMETER);
  248. sync->disconnect("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_COND(!sync); // Bug.
  268. Node *node = sync->get_root_node();
  269. ERR_FAIL_COND(!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_COND_V(!p_sync, ERR_BUG);
  316. if (!multiplayer->has_multiplayer_peer() || !p_sync->is_multiplayer_authority() || 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_COND_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_COND_V(!node || !spawner || !spawner->is_multiplayer_authority(), 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 (!sync->is_multiplayer_authority()) {
  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 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->get_path_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. ERR_FAIL_COND_V(!multiplayer->has_multiplayer_peer(), ERR_UNCONFIGURED);
  426. Ref<MultiplayerPeer> peer = multiplayer->get_multiplayer_peer();
  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. const TrackedNode *tnode = tracked_nodes.getptr(oid);
  435. ERR_FAIL_COND_V(!tnode, ERR_INVALID_PARAMETER);
  436. uint32_t nid = tnode->net_id;
  437. ERR_FAIL_COND_V(!nid, ERR_UNCONFIGURED);
  438. // Prepare custom arg and scene_id
  439. uint8_t scene_id = p_spawner->find_spawnable_scene_index_from_object(oid);
  440. bool is_custom = scene_id == MultiplayerSpawner::INVALID_ID;
  441. Variant spawn_arg = p_spawner->get_spawn_argument(oid);
  442. int spawn_arg_size = 0;
  443. if (is_custom) {
  444. Error err = MultiplayerAPI::encode_and_compress_variant(spawn_arg, nullptr, spawn_arg_size, false);
  445. ERR_FAIL_COND_V(err, err);
  446. }
  447. // Prepare spawn state.
  448. List<NodePath> state_props;
  449. List<uint32_t> sync_ids;
  450. const HashSet<ObjectID> synchronizers = tnode->synchronizers;
  451. for (const ObjectID &sid : synchronizers) {
  452. MultiplayerSynchronizer *sync = get_id_as<MultiplayerSynchronizer>(sid);
  453. if (!sync->is_multiplayer_authority()) {
  454. continue;
  455. }
  456. ERR_CONTINUE(!sync);
  457. ERR_FAIL_COND_V(sync->get_replication_config().is_null(), ERR_BUG);
  458. for (const NodePath &prop : sync->get_replication_config()->get_spawn_properties()) {
  459. state_props.push_back(prop);
  460. }
  461. // Ensure the synchronizer has an ID.
  462. if (sync->get_net_id() == 0) {
  463. sync->set_net_id(++last_net_id);
  464. }
  465. sync_ids.push_back(sync->get_net_id());
  466. }
  467. int state_size = 0;
  468. Vector<Variant> state_vars;
  469. Vector<const Variant *> state_varp;
  470. if (state_props.size()) {
  471. Error err = MultiplayerSynchronizer::get_state(state_props, p_node, state_vars, state_varp);
  472. ERR_FAIL_COND_V_MSG(err != OK, err, "Unable to retrieve spawn state.");
  473. err = MultiplayerAPI::encode_and_compress_variants(state_varp.ptrw(), state_varp.size(), nullptr, state_size);
  474. ERR_FAIL_COND_V_MSG(err != OK, err, "Unable to encode spawn state.");
  475. }
  476. // Encode scene ID, path ID, net ID, node name.
  477. int path_id = multiplayer->get_path_cache()->make_object_cache(p_spawner);
  478. CharString cname = p_node->get_name().operator String().utf8();
  479. int nlen = encode_cstring(cname.get_data(), nullptr);
  480. MAKE_ROOM(1 + 1 + 4 + 4 + 4 + 4 * sync_ids.size() + 4 + nlen + (is_custom ? 4 + spawn_arg_size : 0) + state_size);
  481. uint8_t *ptr = packet_cache.ptrw();
  482. ptr[0] = (uint8_t)SceneMultiplayer::NETWORK_COMMAND_SPAWN;
  483. ptr[1] = scene_id;
  484. int ofs = 2;
  485. ofs += encode_uint32(path_id, &ptr[ofs]);
  486. ofs += encode_uint32(nid, &ptr[ofs]);
  487. ofs += encode_uint32(sync_ids.size(), &ptr[ofs]);
  488. ofs += encode_uint32(nlen, &ptr[ofs]);
  489. for (uint32_t snid : sync_ids) {
  490. ofs += encode_uint32(snid, &ptr[ofs]);
  491. }
  492. ofs += encode_cstring(cname.get_data(), &ptr[ofs]);
  493. // Write args
  494. if (is_custom) {
  495. ofs += encode_uint32(spawn_arg_size, &ptr[ofs]);
  496. Error err = MultiplayerAPI::encode_and_compress_variant(spawn_arg, &ptr[ofs], spawn_arg_size, false);
  497. ERR_FAIL_COND_V(err, err);
  498. ofs += spawn_arg_size;
  499. }
  500. // Write state.
  501. if (state_size) {
  502. Error err = MultiplayerAPI::encode_and_compress_variants(state_varp.ptrw(), state_varp.size(), &ptr[ofs], state_size);
  503. ERR_FAIL_COND_V(err, err);
  504. ofs += state_size;
  505. }
  506. r_len = ofs;
  507. return OK;
  508. }
  509. Error SceneReplicationInterface::_make_despawn_packet(Node *p_node, int &r_len) {
  510. const ObjectID oid = p_node->get_instance_id();
  511. const TrackedNode *tnode = tracked_nodes.getptr(oid);
  512. ERR_FAIL_COND_V(!tnode, ERR_INVALID_PARAMETER);
  513. MAKE_ROOM(5);
  514. uint8_t *ptr = packet_cache.ptrw();
  515. ptr[0] = (uint8_t)SceneMultiplayer::NETWORK_COMMAND_DESPAWN;
  516. int ofs = 1;
  517. uint32_t nid = tnode->net_id;
  518. ofs += encode_uint32(nid, &ptr[ofs]);
  519. r_len = ofs;
  520. return OK;
  521. }
  522. Error SceneReplicationInterface::on_spawn_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len) {
  523. ERR_FAIL_COND_V_MSG(p_buffer_len < 18, ERR_INVALID_DATA, "Invalid spawn packet received");
  524. int ofs = 1; // The spawn/despawn command.
  525. uint8_t scene_id = p_buffer[ofs];
  526. ofs += 1;
  527. uint32_t node_target = decode_uint32(&p_buffer[ofs]);
  528. ofs += 4;
  529. MultiplayerSpawner *spawner = Object::cast_to<MultiplayerSpawner>(multiplayer->get_path_cache()->get_cached_object(p_from, node_target));
  530. ERR_FAIL_COND_V(!spawner, ERR_DOES_NOT_EXIST);
  531. ERR_FAIL_COND_V(p_from != spawner->get_multiplayer_authority(), ERR_UNAUTHORIZED);
  532. uint32_t net_id = decode_uint32(&p_buffer[ofs]);
  533. ofs += 4;
  534. uint32_t sync_len = decode_uint32(&p_buffer[ofs]);
  535. ofs += 4;
  536. uint32_t name_len = decode_uint32(&p_buffer[ofs]);
  537. ofs += 4;
  538. 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)));
  539. List<uint32_t> sync_ids;
  540. for (uint32_t i = 0; i < sync_len; i++) {
  541. sync_ids.push_back(decode_uint32(&p_buffer[ofs]));
  542. ofs += 4;
  543. }
  544. ERR_FAIL_COND_V_MSG(name_len < 1, ERR_INVALID_DATA, "Zero spawn name size.");
  545. // We need to make sure no trickery happens here, but we want to allow autogenerated ("@") node names.
  546. const String name = String::utf8((const char *)&p_buffer[ofs], name_len);
  547. 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));
  548. ofs += name_len;
  549. // Check that we can spawn.
  550. Node *parent = spawner->get_node_or_null(spawner->get_spawn_path());
  551. ERR_FAIL_COND_V(!parent, ERR_UNCONFIGURED);
  552. ERR_FAIL_COND_V(parent->has_node(name), ERR_INVALID_DATA);
  553. Node *node = nullptr;
  554. if (scene_id == MultiplayerSpawner::INVALID_ID) {
  555. // Custom spawn.
  556. ERR_FAIL_COND_V(p_buffer_len - ofs < 4, ERR_INVALID_DATA);
  557. uint32_t arg_size = decode_uint32(&p_buffer[ofs]);
  558. ofs += 4;
  559. ERR_FAIL_COND_V(arg_size > uint32_t(p_buffer_len - ofs), ERR_INVALID_DATA);
  560. Variant v;
  561. Error err = MultiplayerAPI::decode_and_decompress_variant(v, &p_buffer[ofs], arg_size, nullptr, false);
  562. ERR_FAIL_COND_V(err != OK, err);
  563. ofs += arg_size;
  564. node = spawner->instantiate_custom(v);
  565. } else {
  566. // Scene based spawn.
  567. node = spawner->instantiate_scene(scene_id);
  568. }
  569. ERR_FAIL_COND_V(!node, ERR_UNAUTHORIZED);
  570. node->set_name(name);
  571. // Add and track remote
  572. ERR_FAIL_COND_V(!peers_info.has(p_from), ERR_UNAVAILABLE);
  573. ERR_FAIL_COND_V(peers_info[p_from].recv_nodes.has(net_id), ERR_ALREADY_IN_USE);
  574. ObjectID oid = node->get_instance_id();
  575. TrackedNode &tobj = _track(oid);
  576. tobj.spawner = spawner->get_instance_id();
  577. tobj.net_id = net_id;
  578. tobj.remote_peer = p_from;
  579. peers_info[p_from].recv_nodes[net_id] = oid;
  580. // The initial state will be applied during the sync config (i.e. before _ready).
  581. pending_spawn = node->get_instance_id();
  582. pending_spawn_remote = p_from;
  583. pending_buffer_size = p_buffer_len - ofs;
  584. pending_buffer = pending_buffer_size > 0 ? &p_buffer[ofs] : nullptr;
  585. pending_sync_net_ids = sync_ids;
  586. parent->add_child(node);
  587. spawner->emit_signal(SNAME("spawned"), node);
  588. pending_spawn = ObjectID();
  589. pending_spawn_remote = 0;
  590. pending_buffer = nullptr;
  591. pending_buffer_size = 0;
  592. if (pending_sync_net_ids.size()) {
  593. pending_sync_net_ids.clear();
  594. ERR_FAIL_V(ERR_INVALID_DATA); // Should have been consumed.
  595. }
  596. return OK;
  597. }
  598. Error SceneReplicationInterface::on_despawn_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len) {
  599. ERR_FAIL_COND_V_MSG(p_buffer_len < 5, ERR_INVALID_DATA, "Invalid spawn packet received");
  600. int ofs = 1; // The spawn/despawn command.
  601. uint32_t net_id = decode_uint32(&p_buffer[ofs]);
  602. ofs += 4;
  603. // Untrack remote
  604. ERR_FAIL_COND_V(!peers_info.has(p_from), ERR_UNAUTHORIZED);
  605. PeerInfo &pinfo = peers_info[p_from];
  606. ERR_FAIL_COND_V(!pinfo.recv_nodes.has(net_id), ERR_UNAUTHORIZED);
  607. Node *node = get_id_as<Node>(pinfo.recv_nodes[net_id]);
  608. ERR_FAIL_COND_V(!node, ERR_BUG);
  609. pinfo.recv_nodes.erase(net_id);
  610. const ObjectID oid = node->get_instance_id();
  611. ERR_FAIL_COND_V(!tracked_nodes.has(oid), ERR_BUG);
  612. MultiplayerSpawner *spawner = get_id_as<MultiplayerSpawner>(tracked_nodes[oid].spawner);
  613. ERR_FAIL_COND_V(!spawner, ERR_DOES_NOT_EXIST);
  614. ERR_FAIL_COND_V(p_from != spawner->get_multiplayer_authority(), ERR_UNAUTHORIZED);
  615. if (node->get_parent() != nullptr) {
  616. node->get_parent()->remove_child(node);
  617. }
  618. node->queue_free();
  619. spawner->emit_signal(SNAME("despawned"), node);
  620. return OK;
  621. }
  622. bool SceneReplicationInterface::_verify_synchronizer(int p_peer, MultiplayerSynchronizer *p_sync, uint32_t &r_net_id) {
  623. r_net_id = p_sync->get_net_id();
  624. if (r_net_id == 0 || (r_net_id & 0x80000000)) {
  625. int path_id = 0;
  626. bool verified = multiplayer->get_path_cache()->send_object_cache(p_sync, p_peer, path_id);
  627. ERR_FAIL_COND_V_MSG(path_id < 0, false, "This should never happen!");
  628. if (r_net_id == 0) {
  629. // First time path based ID.
  630. r_net_id = path_id | 0x80000000;
  631. p_sync->set_net_id(r_net_id | 0x80000000);
  632. }
  633. return verified;
  634. }
  635. return true;
  636. }
  637. MultiplayerSynchronizer *SceneReplicationInterface::_find_synchronizer(int p_peer, uint32_t p_net_id) {
  638. MultiplayerSynchronizer *sync = nullptr;
  639. if (p_net_id & 0x80000000) {
  640. sync = Object::cast_to<MultiplayerSynchronizer>(multiplayer->get_path_cache()->get_cached_object(p_peer, p_net_id & 0x7FFFFFFF));
  641. } else if (peers_info[p_peer].recv_sync_ids.has(p_net_id)) {
  642. const ObjectID &sid = peers_info[p_peer].recv_sync_ids[p_net_id];
  643. sync = get_id_as<MultiplayerSynchronizer>(sid);
  644. }
  645. return sync;
  646. }
  647. 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) {
  648. MAKE_ROOM(/* header */ 1 + /* element */ 4 + 8 + 4 + delta_mtu);
  649. uint8_t *ptr = packet_cache.ptrw();
  650. ptr[0] = SceneMultiplayer::NETWORK_COMMAND_SYNC | (1 << SceneMultiplayer::CMD_FLAG_0_SHIFT);
  651. int ofs = 1;
  652. for (const ObjectID &oid : p_synchronizers) {
  653. MultiplayerSynchronizer *sync = get_id_as<MultiplayerSynchronizer>(oid);
  654. ERR_CONTINUE(!sync || !sync->get_replication_config().is_valid() || !sync->is_multiplayer_authority());
  655. uint32_t net_id;
  656. if (!_verify_synchronizer(p_peer, sync, net_id)) {
  657. continue;
  658. }
  659. uint64_t last_usec = p_last_watch_usecs.has(oid) ? p_last_watch_usecs[oid] : 0;
  660. uint64_t indexes;
  661. List<Variant> delta = sync->get_delta_state(p_usec, last_usec, indexes);
  662. if (!delta.size()) {
  663. continue; // Nothing to update.
  664. }
  665. Vector<const Variant *> varp;
  666. varp.resize(delta.size());
  667. const Variant **vptr = varp.ptrw();
  668. int i = 0;
  669. for (const Variant &v : delta) {
  670. vptr[i] = &v;
  671. i++;
  672. }
  673. int size;
  674. Error err = MultiplayerAPI::encode_and_compress_variants(vptr, varp.size(), nullptr, size);
  675. ERR_CONTINUE_MSG(err != OK, "Unable to encode delta state.");
  676. 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()));
  677. if (ofs + 4 + 8 + 4 + size > delta_mtu) {
  678. // Send what we got, and reset write.
  679. _send_raw(packet_cache.ptr(), ofs, p_peer, true);
  680. ofs = 1;
  681. }
  682. if (size) {
  683. ofs += encode_uint32(sync->get_net_id(), &ptr[ofs]);
  684. ofs += encode_uint64(indexes, &ptr[ofs]);
  685. ofs += encode_uint32(size, &ptr[ofs]);
  686. MultiplayerAPI::encode_and_compress_variants(vptr, varp.size(), &ptr[ofs], size);
  687. ofs += size;
  688. }
  689. #ifdef DEBUG_ENABLED
  690. _profile_node_data("delta_out", oid, size);
  691. #endif
  692. peers_info[p_peer].last_watch_usecs[oid] = p_usec;
  693. }
  694. if (ofs > 1) {
  695. // Got some left over to send.
  696. _send_raw(packet_cache.ptr(), ofs, p_peer, true);
  697. }
  698. }
  699. Error SceneReplicationInterface::on_delta_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len) {
  700. int ofs = 1;
  701. while (ofs + 4 + 8 + 4 < p_buffer_len) {
  702. uint32_t net_id = decode_uint32(&p_buffer[ofs]);
  703. ofs += 4;
  704. uint64_t indexes = decode_uint64(&p_buffer[ofs]);
  705. ofs += 8;
  706. uint32_t size = decode_uint32(&p_buffer[ofs]);
  707. ofs += 4;
  708. ERR_FAIL_COND_V(size > uint32_t(p_buffer_len - ofs), ERR_INVALID_DATA);
  709. MultiplayerSynchronizer *sync = _find_synchronizer(p_from, net_id);
  710. Node *node = sync ? sync->get_root_node() : nullptr;
  711. if (!sync || sync->get_multiplayer_authority() != p_from || !node) {
  712. ofs += size;
  713. ERR_CONTINUE_MSG(true, "Ignoring delta for non-authority or invalid synchronizer.");
  714. }
  715. List<NodePath> props = sync->get_delta_properties(indexes);
  716. ERR_FAIL_COND_V(props.size() == 0, ERR_INVALID_DATA);
  717. Vector<Variant> vars;
  718. vars.resize(props.size());
  719. int consumed = 0;
  720. Error err = MultiplayerAPI::decode_and_decompress_variants(vars, p_buffer + ofs, size, consumed);
  721. ERR_FAIL_COND_V(err != OK, err);
  722. ERR_FAIL_COND_V(uint32_t(consumed) != size, ERR_INVALID_DATA);
  723. err = MultiplayerSynchronizer::set_state(props, node, vars);
  724. ERR_FAIL_COND_V(err != OK, err);
  725. ofs += size;
  726. sync->emit_signal(SNAME("delta_synchronized"));
  727. #ifdef DEBUG_ENABLED
  728. _profile_node_data("delta_in", sync->get_instance_id(), size);
  729. #endif
  730. }
  731. return OK;
  732. }
  733. void SceneReplicationInterface::_send_sync(int p_peer, const HashSet<ObjectID> p_synchronizers, uint16_t p_sync_net_time, uint64_t p_usec) {
  734. MAKE_ROOM(/* header */ 3 + /* element */ 4 + 4 + sync_mtu);
  735. uint8_t *ptr = packet_cache.ptrw();
  736. ptr[0] = SceneMultiplayer::NETWORK_COMMAND_SYNC;
  737. int ofs = 1;
  738. ofs += encode_uint16(p_sync_net_time, &ptr[1]);
  739. // Can only send updates for already notified nodes.
  740. // This is a lazy implementation, we could optimize much more here with by grouping by replication config.
  741. for (const ObjectID &oid : p_synchronizers) {
  742. MultiplayerSynchronizer *sync = get_id_as<MultiplayerSynchronizer>(oid);
  743. ERR_CONTINUE(!sync || !sync->get_replication_config().is_valid() || !sync->is_multiplayer_authority());
  744. if (!sync->update_outbound_sync_time(p_usec)) {
  745. continue; // nothing to sync.
  746. }
  747. Node *node = sync->get_root_node();
  748. ERR_CONTINUE(!node);
  749. uint32_t net_id = sync->get_net_id();
  750. if (!_verify_synchronizer(p_peer, sync, net_id)) {
  751. // The path based sync is not yet confirmed, skipping.
  752. continue;
  753. }
  754. int size;
  755. Vector<Variant> vars;
  756. Vector<const Variant *> varp;
  757. const List<NodePath> props = sync->get_replication_config()->get_sync_properties();
  758. Error err = MultiplayerSynchronizer::get_state(props, node, vars, varp);
  759. ERR_CONTINUE_MSG(err != OK, "Unable to retrieve sync state.");
  760. err = MultiplayerAPI::encode_and_compress_variants(varp.ptrw(), varp.size(), nullptr, size);
  761. ERR_CONTINUE_MSG(err != OK, "Unable to encode sync state.");
  762. // TODO Handle single state above MTU.
  763. 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()));
  764. if (ofs + 4 + 4 + size > sync_mtu) {
  765. // Send what we got, and reset write.
  766. _send_raw(packet_cache.ptr(), ofs, p_peer, false);
  767. ofs = 3;
  768. }
  769. if (size) {
  770. ofs += encode_uint32(sync->get_net_id(), &ptr[ofs]);
  771. ofs += encode_uint32(size, &ptr[ofs]);
  772. MultiplayerAPI::encode_and_compress_variants(varp.ptrw(), varp.size(), &ptr[ofs], size);
  773. ofs += size;
  774. }
  775. #ifdef DEBUG_ENABLED
  776. _profile_node_data("sync_out", oid, size);
  777. #endif
  778. }
  779. if (ofs > 3) {
  780. // Got some left over to send.
  781. _send_raw(packet_cache.ptr(), ofs, p_peer, false);
  782. }
  783. }
  784. Error SceneReplicationInterface::on_sync_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len) {
  785. ERR_FAIL_COND_V_MSG(p_buffer_len < 11, ERR_INVALID_DATA, "Invalid sync packet received");
  786. bool is_delta = (p_buffer[0] & (1 << SceneMultiplayer::CMD_FLAG_0_SHIFT)) != 0;
  787. if (is_delta) {
  788. return on_delta_receive(p_from, p_buffer, p_buffer_len);
  789. }
  790. uint16_t time = decode_uint16(&p_buffer[1]);
  791. int ofs = 3;
  792. while (ofs + 8 < p_buffer_len) {
  793. uint32_t net_id = decode_uint32(&p_buffer[ofs]);
  794. ofs += 4;
  795. uint32_t size = decode_uint32(&p_buffer[ofs]);
  796. ofs += 4;
  797. ERR_FAIL_COND_V(size > uint32_t(p_buffer_len - ofs), ERR_INVALID_DATA);
  798. MultiplayerSynchronizer *sync = _find_synchronizer(p_from, net_id);
  799. if (!sync) {
  800. // Not received yet.
  801. ofs += size;
  802. continue;
  803. }
  804. Node *node = sync->get_root_node();
  805. if (sync->get_multiplayer_authority() != p_from || !node) {
  806. // Not valid for me.
  807. ofs += size;
  808. ERR_CONTINUE_MSG(true, "Ignoring sync data from non-authority or for missing node.");
  809. }
  810. if (!sync->update_inbound_sync_time(time)) {
  811. // State is too old.
  812. ofs += size;
  813. continue;
  814. }
  815. const List<NodePath> props = sync->get_replication_config()->get_sync_properties();
  816. Vector<Variant> vars;
  817. vars.resize(props.size());
  818. int consumed;
  819. Error err = MultiplayerAPI::decode_and_decompress_variants(vars, &p_buffer[ofs], size, consumed);
  820. ERR_FAIL_COND_V(err, err);
  821. err = MultiplayerSynchronizer::set_state(props, node, vars);
  822. ERR_FAIL_COND_V(err, err);
  823. ofs += size;
  824. sync->emit_signal(SNAME("synchronized"));
  825. #ifdef DEBUG_ENABLED
  826. _profile_node_data("sync_in", sync->get_instance_id(), size);
  827. #endif
  828. }
  829. return OK;
  830. }
  831. void SceneReplicationInterface::set_max_sync_packet_size(int p_size) {
  832. ERR_FAIL_COND_MSG(p_size < 128, "Sync maximum packet size must be at least 128 bytes.");
  833. sync_mtu = p_size;
  834. }
  835. int SceneReplicationInterface::get_max_sync_packet_size() const {
  836. return sync_mtu;
  837. }
  838. void SceneReplicationInterface::set_max_delta_packet_size(int p_size) {
  839. ERR_FAIL_COND_MSG(p_size < 128, "Sync maximum packet size must be at least 128 bytes.");
  840. delta_mtu = p_size;
  841. }
  842. int SceneReplicationInterface::get_max_delta_packet_size() const {
  843. return delta_mtu;
  844. }