multiplayer_api.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /*************************************************************************/
  2. /* multiplayer_api.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 <stdint.h>
  34. #ifdef DEBUG_ENABLED
  35. #include "core/os/os.h"
  36. #endif
  37. MultiplayerReplicationInterface *(*MultiplayerAPI::create_default_replication_interface)(MultiplayerAPI *p_multiplayer) = nullptr;
  38. MultiplayerRPCInterface *(*MultiplayerAPI::create_default_rpc_interface)(MultiplayerAPI *p_multiplayer) = nullptr;
  39. MultiplayerCacheInterface *(*MultiplayerAPI::create_default_cache_interface)(MultiplayerAPI *p_multiplayer) = nullptr;
  40. #ifdef DEBUG_ENABLED
  41. void MultiplayerAPI::profile_bandwidth(const String &p_inout, int p_size) {
  42. if (EngineDebugger::is_profiling("multiplayer")) {
  43. Array values;
  44. values.push_back(p_inout);
  45. values.push_back(OS::get_singleton()->get_ticks_msec());
  46. values.push_back(p_size);
  47. EngineDebugger::profiler_add_frame_data("multiplayer", values);
  48. }
  49. }
  50. #endif
  51. void MultiplayerAPI::poll() {
  52. if (!multiplayer_peer.is_valid() || multiplayer_peer->get_connection_status() == MultiplayerPeer::CONNECTION_DISCONNECTED) {
  53. return;
  54. }
  55. multiplayer_peer->poll();
  56. if (!multiplayer_peer.is_valid()) { // It's possible that polling might have resulted in a disconnection, so check here.
  57. return;
  58. }
  59. while (multiplayer_peer->get_available_packet_count()) {
  60. int sender = multiplayer_peer->get_packet_peer();
  61. const uint8_t *packet;
  62. int len;
  63. Error err = multiplayer_peer->get_packet(&packet, len);
  64. if (err != OK) {
  65. ERR_PRINT("Error getting packet!");
  66. return; // Something is wrong!
  67. }
  68. remote_sender_id = sender;
  69. _process_packet(sender, packet, len);
  70. remote_sender_id = 0;
  71. if (!multiplayer_peer.is_valid()) {
  72. return; // It's also possible that a packet or RPC caused a disconnection, so also check here.
  73. }
  74. }
  75. replicator->on_network_process();
  76. }
  77. void MultiplayerAPI::clear() {
  78. connected_peers.clear();
  79. packet_cache.clear();
  80. cache->clear();
  81. }
  82. void MultiplayerAPI::set_root_path(const NodePath &p_path) {
  83. ERR_FAIL_COND_MSG(!p_path.is_absolute() && !p_path.is_empty(), "MultiplayerAPI root path must be absolute.");
  84. root_path = p_path;
  85. }
  86. NodePath MultiplayerAPI::get_root_path() const {
  87. return root_path;
  88. }
  89. void MultiplayerAPI::set_multiplayer_peer(const Ref<MultiplayerPeer> &p_peer) {
  90. if (p_peer == multiplayer_peer) {
  91. return; // Nothing to do
  92. }
  93. ERR_FAIL_COND_MSG(p_peer.is_valid() && p_peer->get_connection_status() == MultiplayerPeer::CONNECTION_DISCONNECTED,
  94. "Supplied MultiplayerPeer must be connecting or connected.");
  95. if (multiplayer_peer.is_valid()) {
  96. multiplayer_peer->disconnect("peer_connected", callable_mp(this, &MultiplayerAPI::_add_peer));
  97. multiplayer_peer->disconnect("peer_disconnected", callable_mp(this, &MultiplayerAPI::_del_peer));
  98. multiplayer_peer->disconnect("connection_succeeded", callable_mp(this, &MultiplayerAPI::_connected_to_server));
  99. multiplayer_peer->disconnect("connection_failed", callable_mp(this, &MultiplayerAPI::_connection_failed));
  100. multiplayer_peer->disconnect("server_disconnected", callable_mp(this, &MultiplayerAPI::_server_disconnected));
  101. clear();
  102. }
  103. multiplayer_peer = p_peer;
  104. if (multiplayer_peer.is_valid()) {
  105. multiplayer_peer->connect("peer_connected", callable_mp(this, &MultiplayerAPI::_add_peer));
  106. multiplayer_peer->connect("peer_disconnected", callable_mp(this, &MultiplayerAPI::_del_peer));
  107. multiplayer_peer->connect("connection_succeeded", callable_mp(this, &MultiplayerAPI::_connected_to_server));
  108. multiplayer_peer->connect("connection_failed", callable_mp(this, &MultiplayerAPI::_connection_failed));
  109. multiplayer_peer->connect("server_disconnected", callable_mp(this, &MultiplayerAPI::_server_disconnected));
  110. }
  111. replicator->on_reset();
  112. }
  113. Ref<MultiplayerPeer> MultiplayerAPI::get_multiplayer_peer() const {
  114. return multiplayer_peer;
  115. }
  116. void MultiplayerAPI::_process_packet(int p_from, const uint8_t *p_packet, int p_packet_len) {
  117. ERR_FAIL_COND_MSG(root_path.is_empty(), "Multiplayer root was not initialized. If you are using custom multiplayer, remember to set the root path via MultiplayerAPI.set_root_path before using it.");
  118. ERR_FAIL_COND_MSG(p_packet_len < 1, "Invalid packet received. Size too small.");
  119. #ifdef DEBUG_ENABLED
  120. profile_bandwidth("in", p_packet_len);
  121. #endif
  122. // Extract the `packet_type` from the LSB three bits:
  123. uint8_t packet_type = p_packet[0] & CMD_MASK;
  124. switch (packet_type) {
  125. case NETWORK_COMMAND_SIMPLIFY_PATH: {
  126. cache->process_simplify_path(p_from, p_packet, p_packet_len);
  127. } break;
  128. case NETWORK_COMMAND_CONFIRM_PATH: {
  129. cache->process_confirm_path(p_from, p_packet, p_packet_len);
  130. } break;
  131. case NETWORK_COMMAND_REMOTE_CALL: {
  132. rpc->process_rpc(p_from, p_packet, p_packet_len);
  133. } break;
  134. case NETWORK_COMMAND_RAW: {
  135. _process_raw(p_from, p_packet, p_packet_len);
  136. } break;
  137. case NETWORK_COMMAND_SPAWN: {
  138. replicator->on_spawn_receive(p_from, p_packet, p_packet_len);
  139. } break;
  140. case NETWORK_COMMAND_DESPAWN: {
  141. replicator->on_despawn_receive(p_from, p_packet, p_packet_len);
  142. } break;
  143. case NETWORK_COMMAND_SYNC: {
  144. replicator->on_sync_receive(p_from, p_packet, p_packet_len);
  145. } break;
  146. }
  147. }
  148. // The variant is compressed and encoded; The first byte contains all the meta
  149. // information and the format is:
  150. // - The first LSB 5 bits are used for the variant type.
  151. // - The next two bits are used to store the encoding mode.
  152. // - The most significant is used to store the boolean value.
  153. #define VARIANT_META_TYPE_MASK 0x1F
  154. #define VARIANT_META_EMODE_MASK 0x60
  155. #define VARIANT_META_BOOL_MASK 0x80
  156. #define ENCODE_8 0 << 5
  157. #define ENCODE_16 1 << 5
  158. #define ENCODE_32 2 << 5
  159. #define ENCODE_64 3 << 5
  160. Error MultiplayerAPI::encode_and_compress_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_allow_object_decoding) {
  161. // Unreachable because `VARIANT_MAX` == 27 and `ENCODE_VARIANT_MASK` == 31
  162. CRASH_COND(p_variant.get_type() > VARIANT_META_TYPE_MASK);
  163. uint8_t *buf = r_buffer;
  164. r_len = 0;
  165. uint8_t encode_mode = 0;
  166. switch (p_variant.get_type()) {
  167. case Variant::BOOL: {
  168. if (buf) {
  169. // We still have 1 free bit in the meta, so let's use it.
  170. buf[0] = (p_variant.operator bool()) ? (1 << 7) : 0;
  171. buf[0] |= encode_mode | p_variant.get_type();
  172. }
  173. r_len += 1;
  174. } break;
  175. case Variant::INT: {
  176. if (buf) {
  177. // Reserve the first byte for the meta.
  178. buf += 1;
  179. }
  180. r_len += 1;
  181. int64_t val = p_variant;
  182. if (val <= (int64_t)INT8_MAX && val >= (int64_t)INT8_MIN) {
  183. // Use 8 bit
  184. encode_mode = ENCODE_8;
  185. if (buf) {
  186. buf[0] = val;
  187. }
  188. r_len += 1;
  189. } else if (val <= (int64_t)INT16_MAX && val >= (int64_t)INT16_MIN) {
  190. // Use 16 bit
  191. encode_mode = ENCODE_16;
  192. if (buf) {
  193. encode_uint16(val, buf);
  194. }
  195. r_len += 2;
  196. } else if (val <= (int64_t)INT32_MAX && val >= (int64_t)INT32_MIN) {
  197. // Use 32 bit
  198. encode_mode = ENCODE_32;
  199. if (buf) {
  200. encode_uint32(val, buf);
  201. }
  202. r_len += 4;
  203. } else {
  204. // Use 64 bit
  205. encode_mode = ENCODE_64;
  206. if (buf) {
  207. encode_uint64(val, buf);
  208. }
  209. r_len += 8;
  210. }
  211. // Store the meta
  212. if (buf) {
  213. buf -= 1;
  214. buf[0] = encode_mode | p_variant.get_type();
  215. }
  216. } break;
  217. default:
  218. // Any other case is not yet compressed.
  219. Error err = encode_variant(p_variant, r_buffer, r_len, p_allow_object_decoding);
  220. if (err != OK) {
  221. return err;
  222. }
  223. if (r_buffer) {
  224. // The first byte is not used by the marshalling, so store the type
  225. // so we know how to decompress and decode this variant.
  226. r_buffer[0] = p_variant.get_type();
  227. }
  228. }
  229. return OK;
  230. }
  231. Error MultiplayerAPI::decode_and_decompress_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len, bool p_allow_object_decoding) {
  232. const uint8_t *buf = p_buffer;
  233. int len = p_len;
  234. ERR_FAIL_COND_V(len < 1, ERR_INVALID_DATA);
  235. uint8_t type = buf[0] & VARIANT_META_TYPE_MASK;
  236. uint8_t encode_mode = buf[0] & VARIANT_META_EMODE_MASK;
  237. ERR_FAIL_COND_V(type >= Variant::VARIANT_MAX, ERR_INVALID_DATA);
  238. switch (type) {
  239. case Variant::BOOL: {
  240. bool val = (buf[0] & VARIANT_META_BOOL_MASK) > 0;
  241. r_variant = val;
  242. if (r_len) {
  243. *r_len = 1;
  244. }
  245. } break;
  246. case Variant::INT: {
  247. buf += 1;
  248. len -= 1;
  249. if (r_len) {
  250. *r_len = 1;
  251. }
  252. if (encode_mode == ENCODE_8) {
  253. // 8 bits.
  254. ERR_FAIL_COND_V(len < 1, ERR_INVALID_DATA);
  255. int8_t val = buf[0];
  256. r_variant = val;
  257. if (r_len) {
  258. (*r_len) += 1;
  259. }
  260. } else if (encode_mode == ENCODE_16) {
  261. // 16 bits.
  262. ERR_FAIL_COND_V(len < 2, ERR_INVALID_DATA);
  263. int16_t val = decode_uint16(buf);
  264. r_variant = val;
  265. if (r_len) {
  266. (*r_len) += 2;
  267. }
  268. } else if (encode_mode == ENCODE_32) {
  269. // 32 bits.
  270. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
  271. int32_t val = decode_uint32(buf);
  272. r_variant = val;
  273. if (r_len) {
  274. (*r_len) += 4;
  275. }
  276. } else {
  277. // 64 bits.
  278. ERR_FAIL_COND_V(len < 8, ERR_INVALID_DATA);
  279. int64_t val = decode_uint64(buf);
  280. r_variant = val;
  281. if (r_len) {
  282. (*r_len) += 8;
  283. }
  284. }
  285. } break;
  286. default:
  287. Error err = decode_variant(r_variant, p_buffer, p_len, r_len, p_allow_object_decoding);
  288. if (err != OK) {
  289. return err;
  290. }
  291. }
  292. return OK;
  293. }
  294. Error MultiplayerAPI::encode_and_compress_variants(const Variant **p_variants, int p_count, uint8_t *p_buffer, int &r_len, bool *r_raw, bool p_allow_object_decoding) {
  295. r_len = 0;
  296. int size = 0;
  297. if (p_count == 0) {
  298. if (r_raw) {
  299. *r_raw = true;
  300. }
  301. return OK;
  302. }
  303. // Try raw encoding optimization.
  304. if (r_raw && p_count == 1) {
  305. *r_raw = false;
  306. const Variant &v = *(p_variants[0]);
  307. if (v.get_type() == Variant::PACKED_BYTE_ARRAY) {
  308. *r_raw = true;
  309. const PackedByteArray pba = v;
  310. if (p_buffer) {
  311. memcpy(p_buffer, pba.ptr(), pba.size());
  312. }
  313. r_len += pba.size();
  314. } else {
  315. encode_and_compress_variant(v, p_buffer, size, p_allow_object_decoding);
  316. r_len += size;
  317. }
  318. return OK;
  319. }
  320. // Regular encoding.
  321. for (int i = 0; i < p_count; i++) {
  322. const Variant &v = *(p_variants[i]);
  323. encode_and_compress_variant(v, p_buffer ? p_buffer + r_len : nullptr, size, p_allow_object_decoding);
  324. r_len += size;
  325. }
  326. return OK;
  327. }
  328. Error MultiplayerAPI::decode_and_decompress_variants(Vector<Variant> &r_variants, const uint8_t *p_buffer, int p_len, int &r_len, bool p_raw, bool p_allow_object_decoding) {
  329. r_len = 0;
  330. int argc = r_variants.size();
  331. if (argc == 0 && p_raw) {
  332. return OK;
  333. }
  334. ERR_FAIL_COND_V(p_raw && argc != 1, ERR_INVALID_DATA);
  335. if (p_raw) {
  336. r_len = p_len;
  337. PackedByteArray pba;
  338. pba.resize(p_len);
  339. memcpy(pba.ptrw(), p_buffer, p_len);
  340. r_variants.write[0] = pba;
  341. return OK;
  342. }
  343. Vector<Variant> args;
  344. Vector<const Variant *> argp;
  345. args.resize(argc);
  346. for (int i = 0; i < argc; i++) {
  347. ERR_FAIL_COND_V_MSG(r_len >= p_len, ERR_INVALID_DATA, "Invalid packet received. Size too small.");
  348. int vlen;
  349. Error err = MultiplayerAPI::decode_and_decompress_variant(r_variants.write[i], &p_buffer[r_len], p_len - r_len, &vlen, p_allow_object_decoding);
  350. ERR_FAIL_COND_V_MSG(err != OK, err, "Invalid packet received. Unable to decode state variable.");
  351. r_len += vlen;
  352. }
  353. return OK;
  354. }
  355. void MultiplayerAPI::_add_peer(int p_id) {
  356. connected_peers.insert(p_id);
  357. cache->on_peer_change(p_id, true);
  358. replicator->on_peer_change(p_id, true);
  359. emit_signal(SNAME("peer_connected"), p_id);
  360. }
  361. void MultiplayerAPI::_del_peer(int p_id) {
  362. replicator->on_peer_change(p_id, false);
  363. cache->on_peer_change(p_id, false);
  364. connected_peers.erase(p_id);
  365. emit_signal(SNAME("peer_disconnected"), p_id);
  366. }
  367. void MultiplayerAPI::_connected_to_server() {
  368. emit_signal(SNAME("connected_to_server"));
  369. }
  370. void MultiplayerAPI::_connection_failed() {
  371. emit_signal(SNAME("connection_failed"));
  372. }
  373. void MultiplayerAPI::_server_disconnected() {
  374. replicator->on_reset();
  375. emit_signal(SNAME("server_disconnected"));
  376. }
  377. Error MultiplayerAPI::send_bytes(Vector<uint8_t> p_data, int p_to, Multiplayer::TransferMode p_mode, int p_channel) {
  378. ERR_FAIL_COND_V_MSG(p_data.size() < 1, ERR_INVALID_DATA, "Trying to send an empty raw packet.");
  379. ERR_FAIL_COND_V_MSG(!multiplayer_peer.is_valid(), ERR_UNCONFIGURED, "Trying to send a raw packet while no multiplayer peer is active.");
  380. ERR_FAIL_COND_V_MSG(multiplayer_peer->get_connection_status() != MultiplayerPeer::CONNECTION_CONNECTED, ERR_UNCONFIGURED, "Trying to send a raw packet via a multiplayer peer which is not connected.");
  381. if (packet_cache.size() < p_data.size() + 1) {
  382. packet_cache.resize(p_data.size() + 1);
  383. }
  384. const uint8_t *r = p_data.ptr();
  385. packet_cache.write[0] = NETWORK_COMMAND_RAW;
  386. memcpy(&packet_cache.write[1], &r[0], p_data.size());
  387. multiplayer_peer->set_target_peer(p_to);
  388. multiplayer_peer->set_transfer_channel(p_channel);
  389. multiplayer_peer->set_transfer_mode(p_mode);
  390. return multiplayer_peer->put_packet(packet_cache.ptr(), p_data.size() + 1);
  391. }
  392. void MultiplayerAPI::_process_raw(int p_from, const uint8_t *p_packet, int p_packet_len) {
  393. ERR_FAIL_COND_MSG(p_packet_len < 2, "Invalid packet received. Size too small.");
  394. Vector<uint8_t> out;
  395. int len = p_packet_len - 1;
  396. out.resize(len);
  397. {
  398. uint8_t *w = out.ptrw();
  399. memcpy(&w[0], &p_packet[1], len);
  400. }
  401. emit_signal(SNAME("peer_packet"), p_from, out);
  402. }
  403. bool MultiplayerAPI::is_cache_confirmed(NodePath p_path, int p_peer) {
  404. return cache->is_cache_confirmed(p_path, p_peer);
  405. }
  406. bool MultiplayerAPI::send_object_cache(Object *p_obj, NodePath p_path, int p_peer_id, int &r_id) {
  407. return cache->send_object_cache(p_obj, p_path, p_peer_id, r_id);
  408. }
  409. Object *MultiplayerAPI::get_cached_object(int p_from, uint32_t p_cache_id) {
  410. return cache->get_cached_object(p_from, p_cache_id);
  411. }
  412. int MultiplayerAPI::get_unique_id() const {
  413. ERR_FAIL_COND_V_MSG(!multiplayer_peer.is_valid(), 0, "No multiplayer peer is assigned. Unable to get unique ID.");
  414. return multiplayer_peer->get_unique_id();
  415. }
  416. bool MultiplayerAPI::is_server() const {
  417. return multiplayer_peer.is_valid() && multiplayer_peer->is_server();
  418. }
  419. void MultiplayerAPI::set_refuse_new_connections(bool p_refuse) {
  420. ERR_FAIL_COND_MSG(!multiplayer_peer.is_valid(), "No multiplayer peer is assigned. Unable to set 'refuse_new_connections'.");
  421. multiplayer_peer->set_refuse_new_connections(p_refuse);
  422. }
  423. bool MultiplayerAPI::is_refusing_new_connections() const {
  424. ERR_FAIL_COND_V_MSG(!multiplayer_peer.is_valid(), false, "No multiplayer peer is assigned. Unable to get 'refuse_new_connections'.");
  425. return multiplayer_peer->is_refusing_new_connections();
  426. }
  427. Vector<int> MultiplayerAPI::get_peer_ids() const {
  428. ERR_FAIL_COND_V_MSG(!multiplayer_peer.is_valid(), Vector<int>(), "No multiplayer peer is assigned. Assume no peers are connected.");
  429. Vector<int> ret;
  430. for (const int &E : connected_peers) {
  431. ret.push_back(E);
  432. }
  433. return ret;
  434. }
  435. void MultiplayerAPI::set_allow_object_decoding(bool p_enable) {
  436. allow_object_decoding = p_enable;
  437. }
  438. bool MultiplayerAPI::is_object_decoding_allowed() const {
  439. return allow_object_decoding;
  440. }
  441. String MultiplayerAPI::get_rpc_md5(const Object *p_obj) const {
  442. return rpc->get_rpc_md5(p_obj);
  443. }
  444. void MultiplayerAPI::rpcp(Object *p_obj, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount) {
  445. rpc->rpcp(p_obj, p_peer_id, p_method, p_arg, p_argcount);
  446. }
  447. Error MultiplayerAPI::spawn(Object *p_object, Variant p_config) {
  448. return replicator->on_spawn(p_object, p_config);
  449. }
  450. Error MultiplayerAPI::despawn(Object *p_object, Variant p_config) {
  451. return replicator->on_despawn(p_object, p_config);
  452. }
  453. Error MultiplayerAPI::replication_start(Object *p_object, Variant p_config) {
  454. return replicator->on_replication_start(p_object, p_config);
  455. }
  456. Error MultiplayerAPI::replication_stop(Object *p_object, Variant p_config) {
  457. return replicator->on_replication_stop(p_object, p_config);
  458. }
  459. void MultiplayerAPI::_bind_methods() {
  460. ClassDB::bind_method(D_METHOD("set_root_path", "path"), &MultiplayerAPI::set_root_path);
  461. ClassDB::bind_method(D_METHOD("get_root_path"), &MultiplayerAPI::get_root_path);
  462. ClassDB::bind_method(D_METHOD("send_bytes", "bytes", "id", "mode", "channel"), &MultiplayerAPI::send_bytes, DEFVAL(MultiplayerPeer::TARGET_PEER_BROADCAST), DEFVAL(Multiplayer::TRANSFER_MODE_RELIABLE), DEFVAL(0));
  463. ClassDB::bind_method(D_METHOD("has_multiplayer_peer"), &MultiplayerAPI::has_multiplayer_peer);
  464. ClassDB::bind_method(D_METHOD("get_multiplayer_peer"), &MultiplayerAPI::get_multiplayer_peer);
  465. ClassDB::bind_method(D_METHOD("set_multiplayer_peer", "peer"), &MultiplayerAPI::set_multiplayer_peer);
  466. ClassDB::bind_method(D_METHOD("get_unique_id"), &MultiplayerAPI::get_unique_id);
  467. ClassDB::bind_method(D_METHOD("is_server"), &MultiplayerAPI::is_server);
  468. ClassDB::bind_method(D_METHOD("get_remote_sender_id"), &MultiplayerAPI::get_remote_sender_id);
  469. ClassDB::bind_method(D_METHOD("poll"), &MultiplayerAPI::poll);
  470. ClassDB::bind_method(D_METHOD("clear"), &MultiplayerAPI::clear);
  471. ClassDB::bind_method(D_METHOD("get_peers"), &MultiplayerAPI::get_peer_ids);
  472. ClassDB::bind_method(D_METHOD("set_refuse_new_connections", "refuse"), &MultiplayerAPI::set_refuse_new_connections);
  473. ClassDB::bind_method(D_METHOD("is_refusing_new_connections"), &MultiplayerAPI::is_refusing_new_connections);
  474. ClassDB::bind_method(D_METHOD("set_allow_object_decoding", "enable"), &MultiplayerAPI::set_allow_object_decoding);
  475. ClassDB::bind_method(D_METHOD("is_object_decoding_allowed"), &MultiplayerAPI::is_object_decoding_allowed);
  476. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_object_decoding"), "set_allow_object_decoding", "is_object_decoding_allowed");
  477. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "refuse_new_connections"), "set_refuse_new_connections", "is_refusing_new_connections");
  478. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multiplayer_peer", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerPeer", PROPERTY_USAGE_NONE), "set_multiplayer_peer", "get_multiplayer_peer");
  479. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "root_path"), "set_root_path", "get_root_path");
  480. ADD_PROPERTY_DEFAULT("refuse_new_connections", false);
  481. ADD_SIGNAL(MethodInfo("peer_connected", PropertyInfo(Variant::INT, "id")));
  482. ADD_SIGNAL(MethodInfo("peer_disconnected", PropertyInfo(Variant::INT, "id")));
  483. ADD_SIGNAL(MethodInfo("peer_packet", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::PACKED_BYTE_ARRAY, "packet")));
  484. ADD_SIGNAL(MethodInfo("connected_to_server"));
  485. ADD_SIGNAL(MethodInfo("connection_failed"));
  486. ADD_SIGNAL(MethodInfo("server_disconnected"));
  487. }
  488. MultiplayerAPI::MultiplayerAPI() {
  489. if (create_default_replication_interface) {
  490. replicator = Ref<MultiplayerReplicationInterface>(create_default_replication_interface(this));
  491. } else {
  492. replicator.instantiate();
  493. }
  494. if (create_default_rpc_interface) {
  495. rpc = Ref<MultiplayerRPCInterface>(create_default_rpc_interface(this));
  496. } else {
  497. rpc.instantiate();
  498. }
  499. if (create_default_cache_interface) {
  500. cache = Ref<MultiplayerCacheInterface>(create_default_cache_interface(this));
  501. } else {
  502. cache.instantiate();
  503. }
  504. }
  505. MultiplayerAPI::~MultiplayerAPI() {
  506. clear();
  507. }