multiplayer_api.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. /*************************************************************************/
  2. /* multiplayer_api.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 "multiplayer_api.h"
  31. #include "core/debugger/engine_debugger.h"
  32. #include "core/io/marshalls.h"
  33. #include "scene/main/node.h"
  34. #include <stdint.h>
  35. #define NODE_ID_COMPRESSION_SHIFT 3
  36. #define NAME_ID_COMPRESSION_SHIFT 5
  37. #define BYTE_ONLY_OR_NO_ARGS_SHIFT 6
  38. #ifdef DEBUG_ENABLED
  39. #include "core/os/os.h"
  40. #endif
  41. String _get_rpc_md5(const Node *p_node) {
  42. String rpc_list;
  43. const Vector<MultiplayerAPI::RPCConfig> node_config = p_node->get_node_rpc_methods();
  44. for (int i = 0; i < node_config.size(); i++) {
  45. rpc_list += String(node_config[i].name);
  46. }
  47. if (p_node->get_script_instance()) {
  48. const Vector<MultiplayerAPI::RPCConfig> script_config = p_node->get_script_instance()->get_rpc_methods();
  49. for (int i = 0; i < script_config.size(); i++) {
  50. rpc_list += String(script_config[i].name);
  51. }
  52. }
  53. return rpc_list.md5_text();
  54. }
  55. const MultiplayerAPI::RPCConfig _get_rpc_config(const Node *p_node, const StringName &p_method, uint16_t &r_id) {
  56. const Vector<MultiplayerAPI::RPCConfig> node_config = p_node->get_node_rpc_methods();
  57. for (int i = 0; i < node_config.size(); i++) {
  58. if (node_config[i].name == p_method) {
  59. r_id = ((uint16_t)i) | (1 << 15);
  60. return node_config[i];
  61. }
  62. }
  63. if (p_node->get_script_instance()) {
  64. const Vector<MultiplayerAPI::RPCConfig> script_config = p_node->get_script_instance()->get_rpc_methods();
  65. for (int i = 0; i < script_config.size(); i++) {
  66. if (script_config[i].name == p_method) {
  67. r_id = (uint16_t)i;
  68. return script_config[i];
  69. }
  70. }
  71. }
  72. return MultiplayerAPI::RPCConfig();
  73. }
  74. const MultiplayerAPI::RPCConfig _get_rpc_config_by_id(Node *p_node, uint16_t p_id) {
  75. Vector<MultiplayerAPI::RPCConfig> config;
  76. uint16_t id = p_id;
  77. if (id & (1 << 15)) {
  78. id = id & ~(1 << 15);
  79. config = p_node->get_node_rpc_methods();
  80. } else if (p_node->get_script_instance()) {
  81. config = p_node->get_script_instance()->get_rpc_methods();
  82. }
  83. if (id < config.size()) {
  84. return config[id];
  85. }
  86. return MultiplayerAPI::RPCConfig();
  87. }
  88. _FORCE_INLINE_ bool _can_call_mode(Node *p_node, MultiplayerAPI::RPCMode mode, int p_remote_id) {
  89. switch (mode) {
  90. case MultiplayerAPI::RPC_MODE_DISABLED: {
  91. return false;
  92. } break;
  93. case MultiplayerAPI::RPC_MODE_REMOTE: {
  94. return true;
  95. } break;
  96. case MultiplayerAPI::RPC_MODE_MASTER: {
  97. return p_node->is_network_master();
  98. } break;
  99. case MultiplayerAPI::RPC_MODE_PUPPET: {
  100. return !p_node->is_network_master() && p_remote_id == p_node->get_network_master();
  101. } break;
  102. }
  103. return false;
  104. }
  105. void MultiplayerAPI::poll() {
  106. if (!network_peer.is_valid() || network_peer->get_connection_status() == MultiplayerPeer::CONNECTION_DISCONNECTED) {
  107. return;
  108. }
  109. network_peer->poll();
  110. if (!network_peer.is_valid()) { // It's possible that polling might have resulted in a disconnection, so check here.
  111. return;
  112. }
  113. while (network_peer->get_available_packet_count()) {
  114. int sender = network_peer->get_packet_peer();
  115. const uint8_t *packet;
  116. int len;
  117. Error err = network_peer->get_packet(&packet, len);
  118. if (err != OK) {
  119. ERR_PRINT("Error getting packet!");
  120. break; // Something is wrong!
  121. }
  122. rpc_sender_id = sender;
  123. _process_packet(sender, packet, len);
  124. rpc_sender_id = 0;
  125. if (!network_peer.is_valid()) {
  126. break; // It's also possible that a packet or RPC caused a disconnection, so also check here.
  127. }
  128. }
  129. }
  130. void MultiplayerAPI::clear() {
  131. connected_peers.clear();
  132. path_get_cache.clear();
  133. path_send_cache.clear();
  134. packet_cache.clear();
  135. last_send_cache_id = 1;
  136. }
  137. void MultiplayerAPI::set_root_node(Node *p_node) {
  138. root_node = p_node;
  139. }
  140. Node *MultiplayerAPI::get_root_node() {
  141. return root_node;
  142. }
  143. void MultiplayerAPI::set_network_peer(const Ref<MultiplayerPeer> &p_peer) {
  144. if (p_peer == network_peer) {
  145. return; // Nothing to do
  146. }
  147. ERR_FAIL_COND_MSG(p_peer.is_valid() && p_peer->get_connection_status() == MultiplayerPeer::CONNECTION_DISCONNECTED,
  148. "Supplied MultiplayerPeer must be connecting or connected.");
  149. if (network_peer.is_valid()) {
  150. network_peer->disconnect("peer_connected", callable_mp(this, &MultiplayerAPI::_add_peer));
  151. network_peer->disconnect("peer_disconnected", callable_mp(this, &MultiplayerAPI::_del_peer));
  152. network_peer->disconnect("connection_succeeded", callable_mp(this, &MultiplayerAPI::_connected_to_server));
  153. network_peer->disconnect("connection_failed", callable_mp(this, &MultiplayerAPI::_connection_failed));
  154. network_peer->disconnect("server_disconnected", callable_mp(this, &MultiplayerAPI::_server_disconnected));
  155. clear();
  156. }
  157. network_peer = p_peer;
  158. if (network_peer.is_valid()) {
  159. network_peer->connect("peer_connected", callable_mp(this, &MultiplayerAPI::_add_peer));
  160. network_peer->connect("peer_disconnected", callable_mp(this, &MultiplayerAPI::_del_peer));
  161. network_peer->connect("connection_succeeded", callable_mp(this, &MultiplayerAPI::_connected_to_server));
  162. network_peer->connect("connection_failed", callable_mp(this, &MultiplayerAPI::_connection_failed));
  163. network_peer->connect("server_disconnected", callable_mp(this, &MultiplayerAPI::_server_disconnected));
  164. }
  165. }
  166. Ref<MultiplayerPeer> MultiplayerAPI::get_network_peer() const {
  167. return network_peer;
  168. }
  169. #ifdef DEBUG_ENABLED
  170. void _profile_node_data(const String &p_what, ObjectID p_id) {
  171. if (EngineDebugger::is_profiling("multiplayer")) {
  172. Array values;
  173. values.push_back("node");
  174. values.push_back(p_id);
  175. values.push_back(p_what);
  176. EngineDebugger::profiler_add_frame_data("multiplayer", values);
  177. }
  178. }
  179. void _profile_bandwidth_data(const String &p_inout, int p_size) {
  180. if (EngineDebugger::is_profiling("multiplayer")) {
  181. Array values;
  182. values.push_back("bandwidth");
  183. values.push_back(p_inout);
  184. values.push_back(OS::get_singleton()->get_ticks_msec());
  185. values.push_back(p_size);
  186. EngineDebugger::profiler_add_frame_data("multiplayer", values);
  187. }
  188. }
  189. #endif
  190. // Returns the packet size stripping the node path added when the node is not yet cached.
  191. int get_packet_len(uint32_t p_node_target, int p_packet_len) {
  192. if (p_node_target & 0x80000000) {
  193. int ofs = p_node_target & 0x7FFFFFFF;
  194. return p_packet_len - (p_packet_len - ofs);
  195. } else {
  196. return p_packet_len;
  197. }
  198. }
  199. void MultiplayerAPI::_process_packet(int p_from, const uint8_t *p_packet, int p_packet_len) {
  200. ERR_FAIL_COND_MSG(root_node == nullptr, "Multiplayer root node was not initialized. If you are using custom multiplayer, remember to set the root node via MultiplayerAPI.set_root_node before using it.");
  201. ERR_FAIL_COND_MSG(p_packet_len < 1, "Invalid packet received. Size too small.");
  202. #ifdef DEBUG_ENABLED
  203. _profile_bandwidth_data("in", p_packet_len);
  204. #endif
  205. // Extract the `packet_type` from the LSB three bits:
  206. uint8_t packet_type = p_packet[0] & 7;
  207. switch (packet_type) {
  208. case NETWORK_COMMAND_SIMPLIFY_PATH: {
  209. _process_simplify_path(p_from, p_packet, p_packet_len);
  210. } break;
  211. case NETWORK_COMMAND_CONFIRM_PATH: {
  212. _process_confirm_path(p_from, p_packet, p_packet_len);
  213. } break;
  214. case NETWORK_COMMAND_REMOTE_CALL: {
  215. // Extract packet meta
  216. int packet_min_size = 1;
  217. int name_id_offset = 1;
  218. ERR_FAIL_COND_MSG(p_packet_len < packet_min_size, "Invalid packet received. Size too small.");
  219. // Compute the meta size, which depends on the compression level.
  220. int node_id_compression = (p_packet[0] & 24) >> NODE_ID_COMPRESSION_SHIFT;
  221. int name_id_compression = (p_packet[0] & 32) >> NAME_ID_COMPRESSION_SHIFT;
  222. switch (node_id_compression) {
  223. case NETWORK_NODE_ID_COMPRESSION_8:
  224. packet_min_size += 1;
  225. name_id_offset += 1;
  226. break;
  227. case NETWORK_NODE_ID_COMPRESSION_16:
  228. packet_min_size += 2;
  229. name_id_offset += 2;
  230. break;
  231. case NETWORK_NODE_ID_COMPRESSION_32:
  232. packet_min_size += 4;
  233. name_id_offset += 4;
  234. break;
  235. default:
  236. ERR_FAIL_MSG("Was not possible to extract the node id compression mode.");
  237. }
  238. switch (name_id_compression) {
  239. case NETWORK_NAME_ID_COMPRESSION_8:
  240. packet_min_size += 1;
  241. break;
  242. case NETWORK_NAME_ID_COMPRESSION_16:
  243. packet_min_size += 2;
  244. break;
  245. default:
  246. ERR_FAIL_MSG("Was not possible to extract the name id compression mode.");
  247. }
  248. ERR_FAIL_COND_MSG(p_packet_len < packet_min_size, "Invalid packet received. Size too small.");
  249. uint32_t node_target = 0;
  250. switch (node_id_compression) {
  251. case NETWORK_NODE_ID_COMPRESSION_8:
  252. node_target = p_packet[1];
  253. break;
  254. case NETWORK_NODE_ID_COMPRESSION_16:
  255. node_target = decode_uint16(p_packet + 1);
  256. break;
  257. case NETWORK_NODE_ID_COMPRESSION_32:
  258. node_target = decode_uint32(p_packet + 1);
  259. break;
  260. default:
  261. // Unreachable, checked before.
  262. CRASH_NOW();
  263. }
  264. Node *node = _process_get_node(p_from, p_packet, node_target, p_packet_len);
  265. ERR_FAIL_COND_MSG(node == nullptr, "Invalid packet received. Requested node was not found.");
  266. uint16_t name_id = 0;
  267. switch (name_id_compression) {
  268. case NETWORK_NAME_ID_COMPRESSION_8:
  269. name_id = p_packet[name_id_offset];
  270. break;
  271. case NETWORK_NAME_ID_COMPRESSION_16:
  272. name_id = decode_uint16(p_packet + name_id_offset);
  273. break;
  274. default:
  275. // Unreachable, checked before.
  276. CRASH_NOW();
  277. }
  278. const int packet_len = get_packet_len(node_target, p_packet_len);
  279. _process_rpc(node, name_id, p_from, p_packet, packet_len, packet_min_size);
  280. } break;
  281. case NETWORK_COMMAND_RAW: {
  282. _process_raw(p_from, p_packet, p_packet_len);
  283. } break;
  284. }
  285. }
  286. Node *MultiplayerAPI::_process_get_node(int p_from, const uint8_t *p_packet, uint32_t p_node_target, int p_packet_len) {
  287. Node *node = nullptr;
  288. if (p_node_target & 0x80000000) {
  289. // Use full path (not cached yet).
  290. int ofs = p_node_target & 0x7FFFFFFF;
  291. ERR_FAIL_COND_V_MSG(ofs >= p_packet_len, nullptr, "Invalid packet received. Size smaller than declared.");
  292. String paths;
  293. paths.parse_utf8((const char *)&p_packet[ofs], p_packet_len - ofs);
  294. NodePath np = paths;
  295. node = root_node->get_node(np);
  296. if (!node) {
  297. ERR_PRINT("Failed to get path from RPC: " + String(np) + ".");
  298. }
  299. } else {
  300. // Use cached path.
  301. int id = p_node_target;
  302. Map<int, PathGetCache>::Element *E = path_get_cache.find(p_from);
  303. ERR_FAIL_COND_V_MSG(!E, nullptr, "Invalid packet received. Requests invalid peer cache.");
  304. Map<int, PathGetCache::NodeInfo>::Element *F = E->get().nodes.find(id);
  305. ERR_FAIL_COND_V_MSG(!F, nullptr, "Invalid packet received. Unabled to find requested cached node.");
  306. PathGetCache::NodeInfo *ni = &F->get();
  307. // Do proper caching later.
  308. node = root_node->get_node(ni->path);
  309. if (!node) {
  310. ERR_PRINT("Failed to get cached path from RPC: " + String(ni->path) + ".");
  311. }
  312. }
  313. return node;
  314. }
  315. void MultiplayerAPI::_process_rpc(Node *p_node, const uint16_t p_rpc_method_id, int p_from, const uint8_t *p_packet, int p_packet_len, int p_offset) {
  316. ERR_FAIL_COND_MSG(p_offset > p_packet_len, "Invalid packet received. Size too small.");
  317. // Check that remote can call the RPC on this node.
  318. const RPCConfig config = _get_rpc_config_by_id(p_node, p_rpc_method_id);
  319. ERR_FAIL_COND(config.name == StringName());
  320. bool can_call = _can_call_mode(p_node, config.rpc_mode, p_from);
  321. ERR_FAIL_COND_MSG(!can_call, "RPC '" + String(config.name) + "' is not allowed on node " + p_node->get_path() + " from: " + itos(p_from) + ". Mode is " + itos((int)config.rpc_mode) + ", master is " + itos(p_node->get_network_master()) + ".");
  322. int argc = 0;
  323. bool byte_only = false;
  324. const bool byte_only_or_no_args = ((p_packet[0] & 64) >> BYTE_ONLY_OR_NO_ARGS_SHIFT) == 1;
  325. if (byte_only_or_no_args) {
  326. if (p_offset < p_packet_len) {
  327. // This packet contains only bytes.
  328. argc = 1;
  329. byte_only = true;
  330. } else {
  331. // This rpc calls a method without parameters.
  332. }
  333. } else {
  334. // Normal variant, takes the argument count from the packet.
  335. ERR_FAIL_COND_MSG(p_offset >= p_packet_len, "Invalid packet received. Size too small.");
  336. argc = p_packet[p_offset];
  337. p_offset += 1;
  338. }
  339. Vector<Variant> args;
  340. Vector<const Variant *> argp;
  341. args.resize(argc);
  342. argp.resize(argc);
  343. #ifdef DEBUG_ENABLED
  344. _profile_node_data("in_rpc", p_node->get_instance_id());
  345. #endif
  346. if (byte_only) {
  347. Vector<uint8_t> pure_data;
  348. const int len = p_packet_len - p_offset;
  349. pure_data.resize(len);
  350. memcpy(pure_data.ptrw(), &p_packet[p_offset], len);
  351. args.write[0] = pure_data;
  352. argp.write[0] = &args[0];
  353. p_offset += len;
  354. } else {
  355. for (int i = 0; i < argc; i++) {
  356. ERR_FAIL_COND_MSG(p_offset >= p_packet_len, "Invalid packet received. Size too small.");
  357. int vlen;
  358. Error err = _decode_and_decompress_variant(args.write[i], &p_packet[p_offset], p_packet_len - p_offset, &vlen);
  359. ERR_FAIL_COND_MSG(err != OK, "Invalid packet received. Unable to decode RPC argument.");
  360. argp.write[i] = &args[i];
  361. p_offset += vlen;
  362. }
  363. }
  364. Callable::CallError ce;
  365. p_node->call(config.name, (const Variant **)argp.ptr(), argc, ce);
  366. if (ce.error != Callable::CallError::CALL_OK) {
  367. String error = Variant::get_call_error_text(p_node, config.name, (const Variant **)argp.ptr(), argc, ce);
  368. error = "RPC - " + error;
  369. ERR_PRINT(error);
  370. }
  371. }
  372. void MultiplayerAPI::_process_simplify_path(int p_from, const uint8_t *p_packet, int p_packet_len) {
  373. ERR_FAIL_COND_MSG(p_packet_len < 38, "Invalid packet received. Size too small.");
  374. int ofs = 1;
  375. String methods_md5;
  376. methods_md5.parse_utf8((const char *)(p_packet + ofs), 32);
  377. ofs += 33;
  378. int id = decode_uint32(&p_packet[ofs]);
  379. ofs += 4;
  380. String paths;
  381. paths.parse_utf8((const char *)(p_packet + ofs), p_packet_len - ofs);
  382. NodePath path = paths;
  383. if (!path_get_cache.has(p_from)) {
  384. path_get_cache[p_from] = PathGetCache();
  385. }
  386. Node *node = root_node->get_node(path);
  387. ERR_FAIL_COND(node == nullptr);
  388. const bool valid_rpc_checksum = _get_rpc_md5(node) == methods_md5;
  389. if (valid_rpc_checksum == false) {
  390. ERR_PRINT("The rpc node checksum failed. Make sure to have the same methods on both nodes. Node path: " + path);
  391. }
  392. PathGetCache::NodeInfo ni;
  393. ni.path = path;
  394. path_get_cache[p_from].nodes[id] = ni;
  395. // Encode path to send ack.
  396. CharString pname = String(path).utf8();
  397. int len = encode_cstring(pname.get_data(), nullptr);
  398. Vector<uint8_t> packet;
  399. packet.resize(1 + 1 + len);
  400. packet.write[0] = NETWORK_COMMAND_CONFIRM_PATH;
  401. packet.write[1] = valid_rpc_checksum;
  402. encode_cstring(pname.get_data(), &packet.write[2]);
  403. network_peer->set_transfer_channel(0);
  404. network_peer->set_transfer_mode(MultiplayerPeer::TRANSFER_MODE_RELIABLE);
  405. network_peer->set_target_peer(p_from);
  406. network_peer->put_packet(packet.ptr(), packet.size());
  407. }
  408. void MultiplayerAPI::_process_confirm_path(int p_from, const uint8_t *p_packet, int p_packet_len) {
  409. ERR_FAIL_COND_MSG(p_packet_len < 3, "Invalid packet received. Size too small.");
  410. const bool valid_rpc_checksum = p_packet[1];
  411. String paths;
  412. paths.parse_utf8((const char *)&p_packet[2], p_packet_len - 2);
  413. NodePath path = paths;
  414. if (valid_rpc_checksum == false) {
  415. ERR_PRINT("The rpc node checksum failed. Make sure to have the same methods on both nodes. Node path: " + path);
  416. }
  417. PathSentCache *psc = path_send_cache.getptr(path);
  418. ERR_FAIL_COND_MSG(!psc, "Invalid packet received. Tries to confirm a path which was not found in cache.");
  419. Map<int, bool>::Element *E = psc->confirmed_peers.find(p_from);
  420. ERR_FAIL_COND_MSG(!E, "Invalid packet received. Source peer was not found in cache for the given path.");
  421. E->get() = true;
  422. }
  423. bool MultiplayerAPI::_send_confirm_path(Node *p_node, NodePath p_path, PathSentCache *psc, int p_target) {
  424. bool has_all_peers = true;
  425. List<int> peers_to_add; // If one is missing, take note to add it.
  426. for (Set<int>::Element *E = connected_peers.front(); E; E = E->next()) {
  427. if (p_target < 0 && E->get() == -p_target) {
  428. continue; // Continue, excluded.
  429. }
  430. if (p_target > 0 && E->get() != p_target) {
  431. continue; // Continue, not for this peer.
  432. }
  433. Map<int, bool>::Element *F = psc->confirmed_peers.find(E->get());
  434. if (!F || !F->get()) {
  435. // Path was not cached, or was cached but is unconfirmed.
  436. if (!F) {
  437. // Not cached at all, take note.
  438. peers_to_add.push_back(E->get());
  439. }
  440. has_all_peers = false;
  441. }
  442. }
  443. if (peers_to_add.size() > 0) {
  444. // Those that need to be added, send a message for this.
  445. // Encode function name.
  446. const CharString path = String(p_path).utf8();
  447. const int path_len = encode_cstring(path.get_data(), nullptr);
  448. // Extract MD5 from rpc methods list.
  449. const String methods_md5 = _get_rpc_md5(p_node);
  450. const int methods_md5_len = 33; // 32 + 1 for the `0` that is added by the encoder.
  451. Vector<uint8_t> packet;
  452. packet.resize(1 + 4 + path_len + methods_md5_len);
  453. int ofs = 0;
  454. packet.write[ofs] = NETWORK_COMMAND_SIMPLIFY_PATH;
  455. ofs += 1;
  456. ofs += encode_cstring(methods_md5.utf8().get_data(), &packet.write[ofs]);
  457. ofs += encode_uint32(psc->id, &packet.write[ofs]);
  458. ofs += encode_cstring(path.get_data(), &packet.write[ofs]);
  459. for (int &E : peers_to_add) {
  460. network_peer->set_target_peer(E); // To all of you.
  461. network_peer->set_transfer_channel(0);
  462. network_peer->set_transfer_mode(MultiplayerPeer::TRANSFER_MODE_RELIABLE);
  463. network_peer->put_packet(packet.ptr(), packet.size());
  464. psc->confirmed_peers.insert(E, false); // Insert into confirmed, but as false since it was not confirmed.
  465. }
  466. }
  467. return has_all_peers;
  468. }
  469. // The variant is compressed and encoded; The first byte contains all the meta
  470. // information and the format is:
  471. // - The first LSB 5 bits are used for the variant type.
  472. // - The next two bits are used to store the encoding mode.
  473. // - The most significant is used to store the boolean value.
  474. #define VARIANT_META_TYPE_MASK 0x1F
  475. #define VARIANT_META_EMODE_MASK 0x60
  476. #define VARIANT_META_BOOL_MASK 0x80
  477. #define ENCODE_8 0 << 5
  478. #define ENCODE_16 1 << 5
  479. #define ENCODE_32 2 << 5
  480. #define ENCODE_64 3 << 5
  481. Error MultiplayerAPI::_encode_and_compress_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len) {
  482. // Unreachable because `VARIANT_MAX` == 27 and `ENCODE_VARIANT_MASK` == 31
  483. CRASH_COND(p_variant.get_type() > VARIANT_META_TYPE_MASK);
  484. uint8_t *buf = r_buffer;
  485. r_len = 0;
  486. uint8_t encode_mode = 0;
  487. switch (p_variant.get_type()) {
  488. case Variant::BOOL: {
  489. if (buf) {
  490. // We still have 1 free bit in the meta, so let's use it.
  491. buf[0] = (p_variant.operator bool()) ? (1 << 7) : 0;
  492. buf[0] |= encode_mode | p_variant.get_type();
  493. }
  494. r_len += 1;
  495. } break;
  496. case Variant::INT: {
  497. if (buf) {
  498. // Reserve the first byte for the meta.
  499. buf += 1;
  500. }
  501. r_len += 1;
  502. int64_t val = p_variant;
  503. if (val <= (int64_t)INT8_MAX && val >= (int64_t)INT8_MIN) {
  504. // Use 8 bit
  505. encode_mode = ENCODE_8;
  506. if (buf) {
  507. buf[0] = val;
  508. }
  509. r_len += 1;
  510. } else if (val <= (int64_t)INT16_MAX && val >= (int64_t)INT16_MIN) {
  511. // Use 16 bit
  512. encode_mode = ENCODE_16;
  513. if (buf) {
  514. encode_uint16(val, buf);
  515. }
  516. r_len += 2;
  517. } else if (val <= (int64_t)INT32_MAX && val >= (int64_t)INT32_MIN) {
  518. // Use 32 bit
  519. encode_mode = ENCODE_32;
  520. if (buf) {
  521. encode_uint32(val, buf);
  522. }
  523. r_len += 4;
  524. } else {
  525. // Use 64 bit
  526. encode_mode = ENCODE_64;
  527. if (buf) {
  528. encode_uint64(val, buf);
  529. }
  530. r_len += 8;
  531. }
  532. // Store the meta
  533. if (buf) {
  534. buf -= 1;
  535. buf[0] = encode_mode | p_variant.get_type();
  536. }
  537. } break;
  538. default:
  539. // Any other case is not yet compressed.
  540. Error err = encode_variant(p_variant, r_buffer, r_len, allow_object_decoding);
  541. if (err != OK) {
  542. return err;
  543. }
  544. if (r_buffer) {
  545. // The first byte is not used by the marshalling, so store the type
  546. // so we know how to decompress and decode this variant.
  547. r_buffer[0] = p_variant.get_type();
  548. }
  549. }
  550. return OK;
  551. }
  552. Error MultiplayerAPI::_decode_and_decompress_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len) {
  553. const uint8_t *buf = p_buffer;
  554. int len = p_len;
  555. ERR_FAIL_COND_V(len < 1, ERR_INVALID_DATA);
  556. uint8_t type = buf[0] & VARIANT_META_TYPE_MASK;
  557. uint8_t encode_mode = buf[0] & VARIANT_META_EMODE_MASK;
  558. ERR_FAIL_COND_V(type >= Variant::VARIANT_MAX, ERR_INVALID_DATA);
  559. switch (type) {
  560. case Variant::BOOL: {
  561. bool val = (buf[0] & VARIANT_META_BOOL_MASK) > 0;
  562. r_variant = val;
  563. if (r_len) {
  564. *r_len = 1;
  565. }
  566. } break;
  567. case Variant::INT: {
  568. buf += 1;
  569. len -= 1;
  570. if (r_len) {
  571. *r_len = 1;
  572. }
  573. if (encode_mode == ENCODE_8) {
  574. // 8 bits.
  575. ERR_FAIL_COND_V(len < 1, ERR_INVALID_DATA);
  576. int8_t val = buf[0];
  577. r_variant = val;
  578. if (r_len) {
  579. (*r_len) += 1;
  580. }
  581. } else if (encode_mode == ENCODE_16) {
  582. // 16 bits.
  583. ERR_FAIL_COND_V(len < 2, ERR_INVALID_DATA);
  584. int16_t val = decode_uint16(buf);
  585. r_variant = val;
  586. if (r_len) {
  587. (*r_len) += 2;
  588. }
  589. } else if (encode_mode == ENCODE_32) {
  590. // 32 bits.
  591. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  592. int32_t val = decode_uint32(buf);
  593. r_variant = val;
  594. if (r_len) {
  595. (*r_len) += 4;
  596. }
  597. } else {
  598. // 64 bits.
  599. ERR_FAIL_COND_V(len < 8, ERR_INVALID_DATA);
  600. int64_t val = decode_uint64(buf);
  601. r_variant = val;
  602. if (r_len) {
  603. (*r_len) += 8;
  604. }
  605. }
  606. } break;
  607. default:
  608. Error err = decode_variant(r_variant, p_buffer, p_len, r_len, allow_object_decoding);
  609. if (err != OK) {
  610. return err;
  611. }
  612. }
  613. return OK;
  614. }
  615. void MultiplayerAPI::_send_rpc(Node *p_from, int p_to, uint16_t p_rpc_id, const RPCConfig &p_config, const StringName &p_name, const Variant **p_arg, int p_argcount) {
  616. ERR_FAIL_COND_MSG(network_peer.is_null(), "Attempt to remote call/set when networking is not active in SceneTree.");
  617. ERR_FAIL_COND_MSG(network_peer->get_connection_status() == MultiplayerPeer::CONNECTION_CONNECTING, "Attempt to remote call/set when networking is not connected yet in SceneTree.");
  618. ERR_FAIL_COND_MSG(network_peer->get_connection_status() == MultiplayerPeer::CONNECTION_DISCONNECTED, "Attempt to remote call/set when networking is disconnected.");
  619. ERR_FAIL_COND_MSG(p_argcount > 255, "Too many arguments >255.");
  620. if (p_to != 0 && !connected_peers.has(ABS(p_to))) {
  621. ERR_FAIL_COND_MSG(p_to == network_peer->get_unique_id(), "Attempt to remote call/set yourself! unique ID: " + itos(network_peer->get_unique_id()) + ".");
  622. ERR_FAIL_MSG("Attempt to remote call unexisting ID: " + itos(p_to) + ".");
  623. }
  624. NodePath from_path = (root_node->get_path()).rel_path_to(p_from->get_path());
  625. ERR_FAIL_COND_MSG(from_path.is_empty(), "Unable to send RPC. Relative path is empty. THIS IS LIKELY A BUG IN THE ENGINE!");
  626. // See if the path is cached.
  627. PathSentCache *psc = path_send_cache.getptr(from_path);
  628. if (!psc) {
  629. // Path is not cached, create.
  630. path_send_cache[from_path] = PathSentCache();
  631. psc = path_send_cache.getptr(from_path);
  632. psc->id = last_send_cache_id++;
  633. }
  634. // See if all peers have cached path (if so, call can be fast).
  635. const bool has_all_peers = _send_confirm_path(p_from, from_path, psc, p_to);
  636. // Create base packet, lots of hardcode because it must be tight.
  637. int ofs = 0;
  638. #define MAKE_ROOM(m_amount) \
  639. if (packet_cache.size() < m_amount) \
  640. packet_cache.resize(m_amount);
  641. // Encode meta.
  642. // The meta is composed by a single byte that contains (starting from the least significant bit):
  643. // - `NetworkCommands` in the first three bits.
  644. // - `NetworkNodeIdCompression` in the next 2 bits.
  645. // - `NetworkNameIdCompression` in the next 1 bit.
  646. // - `byte_only_or_no_args` in the next 1 bit.
  647. // - So we still have the last bit free!
  648. uint8_t command_type = NETWORK_COMMAND_REMOTE_CALL;
  649. uint8_t node_id_compression = UINT8_MAX;
  650. uint8_t name_id_compression = UINT8_MAX;
  651. bool byte_only_or_no_args = false;
  652. MAKE_ROOM(1);
  653. // The meta is composed along the way, so just set 0 for now.
  654. packet_cache.write[0] = 0;
  655. ofs += 1;
  656. // Encode Node ID.
  657. if (has_all_peers) {
  658. // Compress the node ID only if all the target peers already know it.
  659. if (psc->id >= 0 && psc->id <= 255) {
  660. // We can encode the id in 1 byte
  661. node_id_compression = NETWORK_NODE_ID_COMPRESSION_8;
  662. MAKE_ROOM(ofs + 1);
  663. packet_cache.write[ofs] = static_cast<uint8_t>(psc->id);
  664. ofs += 1;
  665. } else if (psc->id >= 0 && psc->id <= 65535) {
  666. // We can encode the id in 2 bytes
  667. node_id_compression = NETWORK_NODE_ID_COMPRESSION_16;
  668. MAKE_ROOM(ofs + 2);
  669. encode_uint16(static_cast<uint16_t>(psc->id), &(packet_cache.write[ofs]));
  670. ofs += 2;
  671. } else {
  672. // Too big, let's use 4 bytes.
  673. node_id_compression = NETWORK_NODE_ID_COMPRESSION_32;
  674. MAKE_ROOM(ofs + 4);
  675. encode_uint32(psc->id, &(packet_cache.write[ofs]));
  676. ofs += 4;
  677. }
  678. } else {
  679. // The targets don't know the node yet, so we need to use 32 bits int.
  680. node_id_compression = NETWORK_NODE_ID_COMPRESSION_32;
  681. MAKE_ROOM(ofs + 4);
  682. encode_uint32(psc->id, &(packet_cache.write[ofs]));
  683. ofs += 4;
  684. }
  685. // Encode method ID
  686. if (p_rpc_id <= UINT8_MAX) {
  687. // The ID fits in 1 byte
  688. name_id_compression = NETWORK_NAME_ID_COMPRESSION_8;
  689. MAKE_ROOM(ofs + 1);
  690. packet_cache.write[ofs] = static_cast<uint8_t>(p_rpc_id);
  691. ofs += 1;
  692. } else {
  693. // The ID is larger, let's use 2 bytes
  694. name_id_compression = NETWORK_NAME_ID_COMPRESSION_16;
  695. MAKE_ROOM(ofs + 2);
  696. encode_uint16(p_rpc_id, &(packet_cache.write[ofs]));
  697. ofs += 2;
  698. }
  699. if (p_argcount == 0) {
  700. byte_only_or_no_args = true;
  701. } else if (p_argcount == 1 && p_arg[0]->get_type() == Variant::PACKED_BYTE_ARRAY) {
  702. byte_only_or_no_args = true;
  703. // Special optimization when only the byte vector is sent.
  704. const Vector<uint8_t> data = *p_arg[0];
  705. MAKE_ROOM(ofs + data.size());
  706. memcpy(&(packet_cache.write[ofs]), data.ptr(), sizeof(uint8_t) * data.size());
  707. ofs += data.size();
  708. } else {
  709. // Arguments
  710. MAKE_ROOM(ofs + 1);
  711. packet_cache.write[ofs] = p_argcount;
  712. ofs += 1;
  713. for (int i = 0; i < p_argcount; i++) {
  714. int len(0);
  715. Error err = _encode_and_compress_variant(*p_arg[i], nullptr, len);
  716. ERR_FAIL_COND_MSG(err != OK, "Unable to encode RPC argument. THIS IS LIKELY A BUG IN THE ENGINE!");
  717. MAKE_ROOM(ofs + len);
  718. _encode_and_compress_variant(*p_arg[i], &(packet_cache.write[ofs]), len);
  719. ofs += len;
  720. }
  721. }
  722. ERR_FAIL_COND(command_type > 7);
  723. ERR_FAIL_COND(node_id_compression > 3);
  724. ERR_FAIL_COND(name_id_compression > 1);
  725. // We can now set the meta
  726. packet_cache.write[0] = command_type + (node_id_compression << NODE_ID_COMPRESSION_SHIFT) + (name_id_compression << NAME_ID_COMPRESSION_SHIFT) + ((byte_only_or_no_args ? 1 : 0) << BYTE_ONLY_OR_NO_ARGS_SHIFT);
  727. #ifdef DEBUG_ENABLED
  728. _profile_bandwidth_data("out", ofs);
  729. #endif
  730. // Take chance and set transfer mode, since all send methods will use it.
  731. network_peer->set_transfer_channel(p_config.channel);
  732. network_peer->set_transfer_mode(p_config.transfer_mode);
  733. if (has_all_peers) {
  734. // They all have verified paths, so send fast.
  735. network_peer->set_target_peer(p_to); // To all of you.
  736. network_peer->put_packet(packet_cache.ptr(), ofs); // A message with love.
  737. } else {
  738. // Unreachable because the node ID is never compressed if the peers doesn't know it.
  739. CRASH_COND(node_id_compression != NETWORK_NODE_ID_COMPRESSION_32);
  740. // Not all verified path, so send one by one.
  741. // Append path at the end, since we will need it for some packets.
  742. CharString pname = String(from_path).utf8();
  743. int path_len = encode_cstring(pname.get_data(), nullptr);
  744. MAKE_ROOM(ofs + path_len);
  745. encode_cstring(pname.get_data(), &(packet_cache.write[ofs]));
  746. for (Set<int>::Element *E = connected_peers.front(); E; E = E->next()) {
  747. if (p_to < 0 && E->get() == -p_to) {
  748. continue; // Continue, excluded.
  749. }
  750. if (p_to > 0 && E->get() != p_to) {
  751. continue; // Continue, not for this peer.
  752. }
  753. Map<int, bool>::Element *F = psc->confirmed_peers.find(E->get());
  754. ERR_CONTINUE(!F); // Should never happen.
  755. network_peer->set_target_peer(E->get()); // To this one specifically.
  756. if (F->get()) {
  757. // This one confirmed path, so use id.
  758. encode_uint32(psc->id, &(packet_cache.write[1]));
  759. network_peer->put_packet(packet_cache.ptr(), ofs);
  760. } else {
  761. // This one did not confirm path yet, so use entire path (sorry!).
  762. encode_uint32(0x80000000 | ofs, &(packet_cache.write[1])); // Offset to path and flag.
  763. network_peer->put_packet(packet_cache.ptr(), ofs + path_len);
  764. }
  765. }
  766. }
  767. }
  768. void MultiplayerAPI::_add_peer(int p_id) {
  769. connected_peers.insert(p_id);
  770. path_get_cache.insert(p_id, PathGetCache());
  771. emit_signal(SNAME("network_peer_connected"), p_id);
  772. }
  773. void MultiplayerAPI::_del_peer(int p_id) {
  774. connected_peers.erase(p_id);
  775. // Cleanup get cache.
  776. path_get_cache.erase(p_id);
  777. // Cleanup sent cache.
  778. // Some refactoring is needed to make this faster and do paths GC.
  779. List<NodePath> keys;
  780. path_send_cache.get_key_list(&keys);
  781. for (const NodePath &E : keys) {
  782. PathSentCache *psc = path_send_cache.getptr(E);
  783. psc->confirmed_peers.erase(p_id);
  784. }
  785. emit_signal(SNAME("network_peer_disconnected"), p_id);
  786. }
  787. void MultiplayerAPI::_connected_to_server() {
  788. emit_signal(SNAME("connected_to_server"));
  789. }
  790. void MultiplayerAPI::_connection_failed() {
  791. emit_signal(SNAME("connection_failed"));
  792. }
  793. void MultiplayerAPI::_server_disconnected() {
  794. emit_signal(SNAME("server_disconnected"));
  795. }
  796. void MultiplayerAPI::rpcp(Node *p_node, int p_peer_id, bool p_unreliable, const StringName &p_method, const Variant **p_arg, int p_argcount) {
  797. ERR_FAIL_COND_MSG(!network_peer.is_valid(), "Trying to call an RPC while no network peer is active.");
  798. ERR_FAIL_COND_MSG(!p_node->is_inside_tree(), "Trying to call an RPC on a node which is not inside SceneTree.");
  799. ERR_FAIL_COND_MSG(network_peer->get_connection_status() != MultiplayerPeer::CONNECTION_CONNECTED, "Trying to call an RPC via a network peer which is not connected.");
  800. int node_id = network_peer->get_unique_id();
  801. bool call_local_native = false;
  802. bool call_local_script = false;
  803. uint16_t rpc_id = UINT16_MAX;
  804. const RPCConfig config = _get_rpc_config(p_node, p_method, rpc_id);
  805. ERR_FAIL_COND_MSG(config.name == StringName(),
  806. vformat("Unable to get the RPC configuration for the function \"%s\" at path: \"%s\". This happens when the method is not marked for RPCs.", p_method, p_node->get_path()));
  807. if (p_peer_id == 0 || p_peer_id == node_id || (p_peer_id < 0 && p_peer_id != -node_id)) {
  808. if (rpc_id & (1 << 15)) {
  809. call_local_native = config.sync;
  810. } else {
  811. call_local_script = config.sync;
  812. }
  813. }
  814. if (p_peer_id != node_id) {
  815. #ifdef DEBUG_ENABLED
  816. _profile_node_data("out_rpc", p_node->get_instance_id());
  817. #endif
  818. _send_rpc(p_node, p_peer_id, rpc_id, config, p_method, p_arg, p_argcount);
  819. }
  820. if (call_local_native) {
  821. int temp_id = rpc_sender_id;
  822. rpc_sender_id = get_network_unique_id();
  823. Callable::CallError ce;
  824. p_node->call(p_method, p_arg, p_argcount, ce);
  825. rpc_sender_id = temp_id;
  826. if (ce.error != Callable::CallError::CALL_OK) {
  827. String error = Variant::get_call_error_text(p_node, p_method, p_arg, p_argcount, ce);
  828. error = "rpc() aborted in local call: - " + error + ".";
  829. ERR_PRINT(error);
  830. return;
  831. }
  832. }
  833. if (call_local_script) {
  834. int temp_id = rpc_sender_id;
  835. rpc_sender_id = get_network_unique_id();
  836. Callable::CallError ce;
  837. ce.error = Callable::CallError::CALL_OK;
  838. p_node->get_script_instance()->call(p_method, p_arg, p_argcount, ce);
  839. rpc_sender_id = temp_id;
  840. if (ce.error != Callable::CallError::CALL_OK) {
  841. String error = Variant::get_call_error_text(p_node, p_method, p_arg, p_argcount, ce);
  842. error = "rpc() aborted in script local call: - " + error + ".";
  843. ERR_PRINT(error);
  844. return;
  845. }
  846. }
  847. ERR_FAIL_COND_MSG(p_peer_id == node_id && !config.sync, "RPC '" + p_method + "' on yourself is not allowed by selected mode.");
  848. }
  849. Error MultiplayerAPI::send_bytes(Vector<uint8_t> p_data, int p_to, MultiplayerPeer::TransferMode p_mode, int p_channel) {
  850. ERR_FAIL_COND_V_MSG(p_data.size() < 1, ERR_INVALID_DATA, "Trying to send an empty raw packet.");
  851. ERR_FAIL_COND_V_MSG(!network_peer.is_valid(), ERR_UNCONFIGURED, "Trying to send a raw packet while no network peer is active.");
  852. ERR_FAIL_COND_V_MSG(network_peer->get_connection_status() != MultiplayerPeer::CONNECTION_CONNECTED, ERR_UNCONFIGURED, "Trying to send a raw packet via a network peer which is not connected.");
  853. MAKE_ROOM(p_data.size() + 1);
  854. const uint8_t *r = p_data.ptr();
  855. packet_cache.write[0] = NETWORK_COMMAND_RAW;
  856. memcpy(&packet_cache.write[1], &r[0], p_data.size());
  857. network_peer->set_target_peer(p_to);
  858. network_peer->set_transfer_channel(p_channel);
  859. network_peer->set_transfer_mode(p_mode);
  860. return network_peer->put_packet(packet_cache.ptr(), p_data.size() + 1);
  861. }
  862. void MultiplayerAPI::_process_raw(int p_from, const uint8_t *p_packet, int p_packet_len) {
  863. ERR_FAIL_COND_MSG(p_packet_len < 2, "Invalid packet received. Size too small.");
  864. Vector<uint8_t> out;
  865. int len = p_packet_len - 1;
  866. out.resize(len);
  867. {
  868. uint8_t *w = out.ptrw();
  869. memcpy(&w[0], &p_packet[1], len);
  870. }
  871. emit_signal(SNAME("network_peer_packet"), p_from, out);
  872. }
  873. int MultiplayerAPI::get_network_unique_id() const {
  874. ERR_FAIL_COND_V_MSG(!network_peer.is_valid(), 0, "No network peer is assigned. Unable to get unique network ID.");
  875. return network_peer->get_unique_id();
  876. }
  877. bool MultiplayerAPI::is_network_server() const {
  878. return network_peer.is_valid() && network_peer->is_server();
  879. }
  880. void MultiplayerAPI::set_refuse_new_network_connections(bool p_refuse) {
  881. ERR_FAIL_COND_MSG(!network_peer.is_valid(), "No network peer is assigned. Unable to set 'refuse_new_connections'.");
  882. network_peer->set_refuse_new_connections(p_refuse);
  883. }
  884. bool MultiplayerAPI::is_refusing_new_network_connections() const {
  885. ERR_FAIL_COND_V_MSG(!network_peer.is_valid(), false, "No network peer is assigned. Unable to get 'refuse_new_connections'.");
  886. return network_peer->is_refusing_new_connections();
  887. }
  888. Vector<int> MultiplayerAPI::get_network_connected_peers() const {
  889. ERR_FAIL_COND_V_MSG(!network_peer.is_valid(), Vector<int>(), "No network peer is assigned. Assume no peers are connected.");
  890. Vector<int> ret;
  891. for (Set<int>::Element *E = connected_peers.front(); E; E = E->next()) {
  892. ret.push_back(E->get());
  893. }
  894. return ret;
  895. }
  896. void MultiplayerAPI::set_allow_object_decoding(bool p_enable) {
  897. allow_object_decoding = p_enable;
  898. }
  899. bool MultiplayerAPI::is_object_decoding_allowed() const {
  900. return allow_object_decoding;
  901. }
  902. void MultiplayerAPI::_bind_methods() {
  903. ClassDB::bind_method(D_METHOD("set_root_node", "node"), &MultiplayerAPI::set_root_node);
  904. ClassDB::bind_method(D_METHOD("get_root_node"), &MultiplayerAPI::get_root_node);
  905. ClassDB::bind_method(D_METHOD("send_bytes", "bytes", "id", "mode", "channel"), &MultiplayerAPI::send_bytes, DEFVAL(MultiplayerPeer::TARGET_PEER_BROADCAST), DEFVAL(MultiplayerPeer::TRANSFER_MODE_RELIABLE), DEFVAL(0));
  906. ClassDB::bind_method(D_METHOD("has_network_peer"), &MultiplayerAPI::has_network_peer);
  907. ClassDB::bind_method(D_METHOD("get_network_peer"), &MultiplayerAPI::get_network_peer);
  908. ClassDB::bind_method(D_METHOD("get_network_unique_id"), &MultiplayerAPI::get_network_unique_id);
  909. ClassDB::bind_method(D_METHOD("is_network_server"), &MultiplayerAPI::is_network_server);
  910. ClassDB::bind_method(D_METHOD("get_rpc_sender_id"), &MultiplayerAPI::get_rpc_sender_id);
  911. ClassDB::bind_method(D_METHOD("set_network_peer", "peer"), &MultiplayerAPI::set_network_peer);
  912. ClassDB::bind_method(D_METHOD("poll"), &MultiplayerAPI::poll);
  913. ClassDB::bind_method(D_METHOD("clear"), &MultiplayerAPI::clear);
  914. ClassDB::bind_method(D_METHOD("get_network_connected_peers"), &MultiplayerAPI::get_network_connected_peers);
  915. ClassDB::bind_method(D_METHOD("set_refuse_new_network_connections", "refuse"), &MultiplayerAPI::set_refuse_new_network_connections);
  916. ClassDB::bind_method(D_METHOD("is_refusing_new_network_connections"), &MultiplayerAPI::is_refusing_new_network_connections);
  917. ClassDB::bind_method(D_METHOD("set_allow_object_decoding", "enable"), &MultiplayerAPI::set_allow_object_decoding);
  918. ClassDB::bind_method(D_METHOD("is_object_decoding_allowed"), &MultiplayerAPI::is_object_decoding_allowed);
  919. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_object_decoding"), "set_allow_object_decoding", "is_object_decoding_allowed");
  920. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "refuse_new_network_connections"), "set_refuse_new_network_connections", "is_refusing_new_network_connections");
  921. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "network_peer", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerPeer", PROPERTY_USAGE_NONE), "set_network_peer", "get_network_peer");
  922. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "root_node", PROPERTY_HINT_RESOURCE_TYPE, "Node", PROPERTY_USAGE_NONE), "set_root_node", "get_root_node");
  923. ADD_PROPERTY_DEFAULT("refuse_new_network_connections", false);
  924. ADD_SIGNAL(MethodInfo("network_peer_connected", PropertyInfo(Variant::INT, "id")));
  925. ADD_SIGNAL(MethodInfo("network_peer_disconnected", PropertyInfo(Variant::INT, "id")));
  926. ADD_SIGNAL(MethodInfo("network_peer_packet", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::PACKED_BYTE_ARRAY, "packet")));
  927. ADD_SIGNAL(MethodInfo("connected_to_server"));
  928. ADD_SIGNAL(MethodInfo("connection_failed"));
  929. ADD_SIGNAL(MethodInfo("server_disconnected"));
  930. BIND_ENUM_CONSTANT(RPC_MODE_DISABLED);
  931. BIND_ENUM_CONSTANT(RPC_MODE_REMOTE);
  932. BIND_ENUM_CONSTANT(RPC_MODE_MASTER);
  933. BIND_ENUM_CONSTANT(RPC_MODE_PUPPET);
  934. }
  935. MultiplayerAPI::MultiplayerAPI() {
  936. clear();
  937. }
  938. MultiplayerAPI::~MultiplayerAPI() {
  939. clear();
  940. }