multiplayer_api.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /*************************************************************************/
  2. /* multiplayer_api.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "core/io/multiplayer_api.h"
  31. #include "core/io/marshalls.h"
  32. #include "scene/main/node.h"
  33. void MultiplayerAPI::poll() {
  34. if (!network_peer.is_valid() || network_peer->get_connection_status() == NetworkedMultiplayerPeer::CONNECTION_DISCONNECTED)
  35. return;
  36. network_peer->poll();
  37. if (!network_peer.is_valid()) //it's possible that polling might have resulted in a disconnection, so check here
  38. return;
  39. while (network_peer->get_available_packet_count()) {
  40. int sender = network_peer->get_packet_peer();
  41. const uint8_t *packet;
  42. int len;
  43. Error err = network_peer->get_packet(&packet, len);
  44. if (err != OK) {
  45. ERR_PRINT("Error getting packet!");
  46. }
  47. rpc_sender_id = sender;
  48. _process_packet(sender, packet, len);
  49. rpc_sender_id = 0;
  50. if (!network_peer.is_valid()) {
  51. break; //it's also possible that a packet or RPC caused a disconnection, so also check here
  52. }
  53. }
  54. }
  55. void MultiplayerAPI::clear() {
  56. connected_peers.clear();
  57. path_get_cache.clear();
  58. path_send_cache.clear();
  59. last_send_cache_id = 1;
  60. }
  61. void MultiplayerAPI::set_root_node(Node *p_node) {
  62. root_node = p_node;
  63. }
  64. void MultiplayerAPI::set_network_peer(const Ref<NetworkedMultiplayerPeer> &p_peer) {
  65. if (network_peer.is_valid()) {
  66. network_peer->disconnect("peer_connected", this, "_add_peer");
  67. network_peer->disconnect("peer_disconnected", this, "_del_peer");
  68. network_peer->disconnect("connection_succeeded", this, "_connected_to_server");
  69. network_peer->disconnect("connection_failed", this, "_connection_failed");
  70. network_peer->disconnect("server_disconnected", this, "_server_disconnected");
  71. clear();
  72. }
  73. network_peer = p_peer;
  74. ERR_EXPLAIN("Supplied NetworkedNetworkPeer must be connecting or connected.");
  75. ERR_FAIL_COND(p_peer.is_valid() && p_peer->get_connection_status() == NetworkedMultiplayerPeer::CONNECTION_DISCONNECTED);
  76. if (network_peer.is_valid()) {
  77. network_peer->connect("peer_connected", this, "_add_peer");
  78. network_peer->connect("peer_disconnected", this, "_del_peer");
  79. network_peer->connect("connection_succeeded", this, "_connected_to_server");
  80. network_peer->connect("connection_failed", this, "_connection_failed");
  81. network_peer->connect("server_disconnected", this, "_server_disconnected");
  82. }
  83. }
  84. Ref<NetworkedMultiplayerPeer> MultiplayerAPI::get_network_peer() const {
  85. return network_peer;
  86. }
  87. void MultiplayerAPI::_process_packet(int p_from, const uint8_t *p_packet, int p_packet_len) {
  88. ERR_FAIL_COND(root_node == NULL);
  89. ERR_FAIL_COND(p_packet_len < 1);
  90. uint8_t packet_type = p_packet[0];
  91. switch (packet_type) {
  92. case NETWORK_COMMAND_SIMPLIFY_PATH: {
  93. _process_simplify_path(p_from, p_packet, p_packet_len);
  94. } break;
  95. case NETWORK_COMMAND_CONFIRM_PATH: {
  96. _process_confirm_path(p_from, p_packet, p_packet_len);
  97. } break;
  98. case NETWORK_COMMAND_REMOTE_CALL:
  99. case NETWORK_COMMAND_REMOTE_SET: {
  100. ERR_FAIL_COND(p_packet_len < 6);
  101. Node *node = _process_get_node(p_from, p_packet, p_packet_len);
  102. ERR_FAIL_COND(node == NULL);
  103. //detect cstring end
  104. int len_end = 5;
  105. for (; len_end < p_packet_len; len_end++) {
  106. if (p_packet[len_end] == 0) {
  107. break;
  108. }
  109. }
  110. ERR_FAIL_COND(len_end >= p_packet_len);
  111. StringName name = String::utf8((const char *)&p_packet[5]);
  112. if (packet_type == NETWORK_COMMAND_REMOTE_CALL) {
  113. _process_rpc(node, name, p_from, p_packet, p_packet_len, len_end + 1);
  114. } else {
  115. _process_rset(node, name, p_from, p_packet, p_packet_len, len_end + 1);
  116. }
  117. } break;
  118. case NETWORK_COMMAND_RAW: {
  119. _process_raw(p_from, p_packet, p_packet_len);
  120. } break;
  121. }
  122. }
  123. Node *MultiplayerAPI::_process_get_node(int p_from, const uint8_t *p_packet, int p_packet_len) {
  124. uint32_t target = decode_uint32(&p_packet[1]);
  125. Node *node = NULL;
  126. if (target & 0x80000000) {
  127. //use full path (not cached yet)
  128. int ofs = target & 0x7FFFFFFF;
  129. ERR_FAIL_COND_V(ofs >= p_packet_len, NULL);
  130. String paths;
  131. paths.parse_utf8((const char *)&p_packet[ofs], p_packet_len - ofs);
  132. NodePath np = paths;
  133. node = root_node->get_node(np);
  134. if (!node)
  135. ERR_PRINTS("Failed to get path from RPC: " + String(np));
  136. } else {
  137. //use cached path
  138. int id = target;
  139. Map<int, PathGetCache>::Element *E = path_get_cache.find(p_from);
  140. ERR_FAIL_COND_V(!E, NULL);
  141. Map<int, PathGetCache::NodeInfo>::Element *F = E->get().nodes.find(id);
  142. ERR_FAIL_COND_V(!F, NULL);
  143. PathGetCache::NodeInfo *ni = &F->get();
  144. //do proper caching later
  145. node = root_node->get_node(ni->path);
  146. if (!node)
  147. ERR_PRINTS("Failed to get cached path from RPC: " + String(ni->path));
  148. }
  149. return node;
  150. }
  151. void MultiplayerAPI::_process_rpc(Node *p_node, const StringName &p_name, int p_from, const uint8_t *p_packet, int p_packet_len, int p_offset) {
  152. if (!p_node->can_call_rpc(p_name, p_from))
  153. return;
  154. ERR_FAIL_COND(p_offset >= p_packet_len);
  155. int argc = p_packet[p_offset];
  156. Vector<Variant> args;
  157. Vector<const Variant *> argp;
  158. args.resize(argc);
  159. argp.resize(argc);
  160. p_offset++;
  161. for (int i = 0; i < argc; i++) {
  162. ERR_FAIL_COND(p_offset >= p_packet_len);
  163. int vlen;
  164. Error err = decode_variant(args[i], &p_packet[p_offset], p_packet_len - p_offset, &vlen);
  165. ERR_FAIL_COND(err != OK);
  166. //args[i]=p_packet[3+i];
  167. argp[i] = &args[i];
  168. p_offset += vlen;
  169. }
  170. Variant::CallError ce;
  171. p_node->call(p_name, (const Variant **)argp.ptr(), argc, ce);
  172. if (ce.error != Variant::CallError::CALL_OK) {
  173. String error = Variant::get_call_error_text(p_node, p_name, (const Variant **)argp.ptr(), argc, ce);
  174. error = "RPC - " + error;
  175. ERR_PRINTS(error);
  176. }
  177. }
  178. void MultiplayerAPI::_process_rset(Node *p_node, const StringName &p_name, int p_from, const uint8_t *p_packet, int p_packet_len, int p_offset) {
  179. if (!p_node->can_call_rset(p_name, p_from))
  180. return;
  181. ERR_FAIL_COND(p_offset >= p_packet_len);
  182. Variant value;
  183. decode_variant(value, &p_packet[p_offset], p_packet_len - p_offset);
  184. bool valid;
  185. p_node->set(p_name, value, &valid);
  186. if (!valid) {
  187. String error = "Error setting remote property '" + String(p_name) + "', not found in object of type " + p_node->get_class();
  188. ERR_PRINTS(error);
  189. }
  190. }
  191. void MultiplayerAPI::_process_simplify_path(int p_from, const uint8_t *p_packet, int p_packet_len) {
  192. ERR_FAIL_COND(p_packet_len < 5);
  193. int id = decode_uint32(&p_packet[1]);
  194. String paths;
  195. paths.parse_utf8((const char *)&p_packet[5], p_packet_len - 5);
  196. NodePath path = paths;
  197. if (!path_get_cache.has(p_from)) {
  198. path_get_cache[p_from] = PathGetCache();
  199. }
  200. PathGetCache::NodeInfo ni;
  201. ni.path = path;
  202. ni.instance = 0;
  203. path_get_cache[p_from].nodes[id] = ni;
  204. //send ack
  205. //encode path
  206. CharString pname = String(path).utf8();
  207. int len = encode_cstring(pname.get_data(), NULL);
  208. Vector<uint8_t> packet;
  209. packet.resize(1 + len);
  210. packet[0] = NETWORK_COMMAND_CONFIRM_PATH;
  211. encode_cstring(pname.get_data(), &packet[1]);
  212. network_peer->set_transfer_mode(NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE);
  213. network_peer->set_target_peer(p_from);
  214. network_peer->put_packet(packet.ptr(), packet.size());
  215. }
  216. void MultiplayerAPI::_process_confirm_path(int p_from, const uint8_t *p_packet, int p_packet_len) {
  217. ERR_FAIL_COND(p_packet_len < 2);
  218. String paths;
  219. paths.parse_utf8((const char *)&p_packet[1], p_packet_len - 1);
  220. NodePath path = paths;
  221. PathSentCache *psc = path_send_cache.getptr(path);
  222. ERR_FAIL_COND(!psc);
  223. Map<int, bool>::Element *E = psc->confirmed_peers.find(p_from);
  224. ERR_FAIL_COND(!E);
  225. E->get() = true;
  226. }
  227. bool MultiplayerAPI::_send_confirm_path(NodePath p_path, PathSentCache *psc, int p_target) {
  228. bool has_all_peers = true;
  229. List<int> peers_to_add; //if one is missing, take note to add it
  230. for (Set<int>::Element *E = connected_peers.front(); E; E = E->next()) {
  231. if (p_target < 0 && E->get() == -p_target)
  232. continue; //continue, excluded
  233. if (p_target > 0 && E->get() != p_target)
  234. continue; //continue, not for this peer
  235. Map<int, bool>::Element *F = psc->confirmed_peers.find(E->get());
  236. if (!F || F->get() == false) {
  237. //path was not cached, or was cached but is unconfirmed
  238. if (!F) {
  239. //not cached at all, take note
  240. peers_to_add.push_back(E->get());
  241. }
  242. has_all_peers = false;
  243. }
  244. }
  245. //those that need to be added, send a message for this
  246. for (List<int>::Element *E = peers_to_add.front(); E; E = E->next()) {
  247. //encode function name
  248. CharString pname = String(p_path).utf8();
  249. int len = encode_cstring(pname.get_data(), NULL);
  250. Vector<uint8_t> packet;
  251. packet.resize(1 + 4 + len);
  252. packet[0] = NETWORK_COMMAND_SIMPLIFY_PATH;
  253. encode_uint32(psc->id, &packet[1]);
  254. encode_cstring(pname.get_data(), &packet[5]);
  255. network_peer->set_target_peer(E->get()); //to all of you
  256. network_peer->set_transfer_mode(NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE);
  257. network_peer->put_packet(packet.ptr(), packet.size());
  258. psc->confirmed_peers.insert(E->get(), false); //insert into confirmed, but as false since it was not confirmed
  259. }
  260. return has_all_peers;
  261. }
  262. void MultiplayerAPI::_send_rpc(Node *p_from, int p_to, bool p_unreliable, bool p_set, const StringName &p_name, const Variant **p_arg, int p_argcount) {
  263. if (network_peer.is_null()) {
  264. ERR_EXPLAIN("Attempt to remote call/set when networking is not active in SceneTree.");
  265. ERR_FAIL();
  266. }
  267. if (network_peer->get_connection_status() == NetworkedMultiplayerPeer::CONNECTION_CONNECTING) {
  268. ERR_EXPLAIN("Attempt to remote call/set when networking is not connected yet in SceneTree.");
  269. ERR_FAIL();
  270. }
  271. if (network_peer->get_connection_status() == NetworkedMultiplayerPeer::CONNECTION_DISCONNECTED) {
  272. ERR_EXPLAIN("Attempt to remote call/set when networking is disconnected.");
  273. ERR_FAIL();
  274. }
  275. if (p_argcount > 255) {
  276. ERR_EXPLAIN("Too many arguments >255.");
  277. ERR_FAIL();
  278. }
  279. if (p_to != 0 && !connected_peers.has(ABS(p_to))) {
  280. if (p_to == network_peer->get_unique_id()) {
  281. ERR_EXPLAIN("Attempt to remote call/set yourself! unique ID: " + itos(network_peer->get_unique_id()));
  282. } else {
  283. ERR_EXPLAIN("Attempt to remote call unexisting ID: " + itos(p_to));
  284. }
  285. ERR_FAIL();
  286. }
  287. NodePath from_path = (root_node->get_path()).rel_path_to(p_from->get_path());
  288. ERR_FAIL_COND(from_path.is_empty());
  289. //see if the path is cached
  290. PathSentCache *psc = path_send_cache.getptr(from_path);
  291. if (!psc) {
  292. //path is not cached, create
  293. path_send_cache[from_path] = PathSentCache();
  294. psc = path_send_cache.getptr(from_path);
  295. psc->id = last_send_cache_id++;
  296. }
  297. //create base packet, lots of hardcode because it must be tight
  298. int ofs = 0;
  299. #define MAKE_ROOM(m_amount) \
  300. if (packet_cache.size() < m_amount) packet_cache.resize(m_amount);
  301. //encode type
  302. MAKE_ROOM(1);
  303. packet_cache[0] = p_set ? NETWORK_COMMAND_REMOTE_SET : NETWORK_COMMAND_REMOTE_CALL;
  304. ofs += 1;
  305. //encode ID
  306. MAKE_ROOM(ofs + 4);
  307. encode_uint32(psc->id, &(packet_cache[ofs]));
  308. ofs += 4;
  309. //encode function name
  310. CharString name = String(p_name).utf8();
  311. int len = encode_cstring(name.get_data(), NULL);
  312. MAKE_ROOM(ofs + len);
  313. encode_cstring(name.get_data(), &(packet_cache[ofs]));
  314. ofs += len;
  315. if (p_set) {
  316. //set argument
  317. Error err = encode_variant(*p_arg[0], NULL, len);
  318. ERR_FAIL_COND(err != OK);
  319. MAKE_ROOM(ofs + len);
  320. encode_variant(*p_arg[0], &(packet_cache[ofs]), len);
  321. ofs += len;
  322. } else {
  323. //call arguments
  324. MAKE_ROOM(ofs + 1);
  325. packet_cache[ofs] = p_argcount;
  326. ofs += 1;
  327. for (int i = 0; i < p_argcount; i++) {
  328. Error err = encode_variant(*p_arg[i], NULL, len);
  329. ERR_FAIL_COND(err != OK);
  330. MAKE_ROOM(ofs + len);
  331. encode_variant(*p_arg[i], &(packet_cache[ofs]), len);
  332. ofs += len;
  333. }
  334. }
  335. //see if all peers have cached path (is so, call can be fast)
  336. bool has_all_peers = _send_confirm_path(from_path, psc, p_to);
  337. //take chance and set transfer mode, since all send methods will use it
  338. network_peer->set_transfer_mode(p_unreliable ? NetworkedMultiplayerPeer::TRANSFER_MODE_UNRELIABLE : NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE);
  339. if (has_all_peers) {
  340. //they all have verified paths, so send fast
  341. network_peer->set_target_peer(p_to); //to all of you
  342. network_peer->put_packet(packet_cache.ptr(), ofs); //a message with love
  343. } else {
  344. //not all verified path, so send one by one
  345. //apend path at the end, since we will need it for some packets
  346. CharString pname = String(from_path).utf8();
  347. int path_len = encode_cstring(pname.get_data(), NULL);
  348. MAKE_ROOM(ofs + path_len);
  349. encode_cstring(pname.get_data(), &(packet_cache[ofs]));
  350. for (Set<int>::Element *E = connected_peers.front(); E; E = E->next()) {
  351. if (p_to < 0 && E->get() == -p_to)
  352. continue; //continue, excluded
  353. if (p_to > 0 && E->get() != p_to)
  354. continue; //continue, not for this peer
  355. Map<int, bool>::Element *F = psc->confirmed_peers.find(E->get());
  356. ERR_CONTINUE(!F); //should never happen
  357. network_peer->set_target_peer(E->get()); //to this one specifically
  358. if (F->get() == true) {
  359. //this one confirmed path, so use id
  360. encode_uint32(psc->id, &(packet_cache[1]));
  361. network_peer->put_packet(packet_cache.ptr(), ofs);
  362. } else {
  363. //this one did not confirm path yet, so use entire path (sorry!)
  364. encode_uint32(0x80000000 | ofs, &(packet_cache[1])); //offset to path and flag
  365. network_peer->put_packet(packet_cache.ptr(), ofs + path_len);
  366. }
  367. }
  368. }
  369. }
  370. void MultiplayerAPI::_add_peer(int p_id) {
  371. connected_peers.insert(p_id);
  372. path_get_cache.insert(p_id, PathGetCache());
  373. emit_signal("network_peer_connected", p_id);
  374. }
  375. void MultiplayerAPI::_del_peer(int p_id) {
  376. connected_peers.erase(p_id);
  377. path_get_cache.erase(p_id); //I no longer need your cache, sorry
  378. emit_signal("network_peer_disconnected", p_id);
  379. }
  380. void MultiplayerAPI::_connected_to_server() {
  381. emit_signal("connected_to_server");
  382. }
  383. void MultiplayerAPI::_connection_failed() {
  384. emit_signal("connection_failed");
  385. }
  386. void MultiplayerAPI::_server_disconnected() {
  387. emit_signal("server_disconnected");
  388. }
  389. bool _should_call_native(Node::RPCMode mode, bool is_master, bool &r_skip_rpc) {
  390. switch (mode) {
  391. case Node::RPC_MODE_DISABLED: {
  392. //do nothing
  393. } break;
  394. case Node::RPC_MODE_REMOTE: {
  395. //do nothing also, no need to call local
  396. } break;
  397. case Node::RPC_MODE_SYNC: {
  398. //call it, sync always results in call
  399. return true;
  400. } break;
  401. case Node::RPC_MODE_MASTER: {
  402. if (is_master)
  403. r_skip_rpc = true; //no other master so..
  404. return is_master;
  405. } break;
  406. case Node::RPC_MODE_SLAVE: {
  407. return !is_master;
  408. } break;
  409. }
  410. return false;
  411. }
  412. bool _should_call_script(ScriptInstance::RPCMode mode, bool is_master, bool &r_skip_rpc) {
  413. switch (mode) {
  414. case ScriptInstance::RPC_MODE_DISABLED: {
  415. //do nothing
  416. } break;
  417. case ScriptInstance::RPC_MODE_REMOTE: {
  418. //do nothing also, no need to call local
  419. } break;
  420. case ScriptInstance::RPC_MODE_SYNC: {
  421. //call it, sync always results in call
  422. return true;
  423. } break;
  424. case ScriptInstance::RPC_MODE_MASTER: {
  425. if (is_master)
  426. r_skip_rpc = true; //no other master so..
  427. return is_master;
  428. } break;
  429. case ScriptInstance::RPC_MODE_SLAVE: {
  430. return !is_master;
  431. } break;
  432. }
  433. return false;
  434. }
  435. void MultiplayerAPI::rpcp(Node *p_node, int p_peer_id, bool p_unreliable, const StringName &p_method, const Variant **p_arg, int p_argcount) {
  436. ERR_FAIL_COND(!p_node->is_inside_tree());
  437. ERR_FAIL_COND(!network_peer.is_valid());
  438. int node_id = network_peer->get_unique_id();
  439. bool skip_rpc = false;
  440. bool call_local_native = false;
  441. bool call_local_script = false;
  442. bool is_master = p_node->is_network_master();
  443. if (p_peer_id == 0 || p_peer_id == node_id || (p_peer_id < 0 && p_peer_id != -node_id)) {
  444. //check that send mode can use local call
  445. const Map<StringName, Node::RPCMode>::Element *E = p_node->get_node_rpc_mode(p_method);
  446. if (E) {
  447. call_local_native = _should_call_native(E->get(), is_master, skip_rpc);
  448. }
  449. if (call_local_native) {
  450. // done below
  451. } else if (p_node->get_script_instance()) {
  452. //attempt with script
  453. ScriptInstance::RPCMode rpc_mode = p_node->get_script_instance()->get_rpc_mode(p_method);
  454. call_local_script = _should_call_script(rpc_mode, is_master, skip_rpc);
  455. }
  456. }
  457. if (!skip_rpc) {
  458. _send_rpc(p_node, p_peer_id, p_unreliable, false, p_method, p_arg, p_argcount);
  459. }
  460. if (call_local_native) {
  461. Variant::CallError ce;
  462. p_node->call(p_method, p_arg, p_argcount, ce);
  463. if (ce.error != Variant::CallError::CALL_OK) {
  464. String error = Variant::get_call_error_text(p_node, p_method, p_arg, p_argcount, ce);
  465. error = "rpc() aborted in local call: - " + error;
  466. ERR_PRINTS(error);
  467. return;
  468. }
  469. }
  470. if (call_local_script) {
  471. Variant::CallError ce;
  472. ce.error = Variant::CallError::CALL_OK;
  473. p_node->get_script_instance()->call(p_method, p_arg, p_argcount, ce);
  474. if (ce.error != Variant::CallError::CALL_OK) {
  475. String error = Variant::get_call_error_text(p_node, p_method, p_arg, p_argcount, ce);
  476. error = "rpc() aborted in script local call: - " + error;
  477. ERR_PRINTS(error);
  478. return;
  479. }
  480. }
  481. }
  482. void MultiplayerAPI::rsetp(Node *p_node, int p_peer_id, bool p_unreliable, const StringName &p_property, const Variant &p_value) {
  483. ERR_FAIL_COND(!p_node->is_inside_tree());
  484. ERR_FAIL_COND(!network_peer.is_valid());
  485. int node_id = network_peer->get_unique_id();
  486. bool is_master = p_node->is_network_master();
  487. bool skip_rset = false;
  488. if (p_peer_id == 0 || p_peer_id == node_id || (p_peer_id < 0 && p_peer_id != -node_id)) {
  489. //check that send mode can use local call
  490. bool set_local = false;
  491. const Map<StringName, Node::RPCMode>::Element *E = p_node->get_node_rset_mode(p_property);
  492. if (E) {
  493. set_local = _should_call_native(E->get(), is_master, skip_rset);
  494. }
  495. if (set_local) {
  496. bool valid;
  497. p_node->set(p_property, p_value, &valid);
  498. if (!valid) {
  499. String error = "rset() aborted in local set, property not found: - " + String(p_property);
  500. ERR_PRINTS(error);
  501. return;
  502. }
  503. } else if (p_node->get_script_instance()) {
  504. //attempt with script
  505. ScriptInstance::RPCMode rpc_mode = p_node->get_script_instance()->get_rset_mode(p_property);
  506. set_local = _should_call_script(rpc_mode, is_master, skip_rset);
  507. if (set_local) {
  508. bool valid = p_node->get_script_instance()->set(p_property, p_value);
  509. if (!valid) {
  510. String error = "rset() aborted in local script set, property not found: - " + String(p_property);
  511. ERR_PRINTS(error);
  512. return;
  513. }
  514. }
  515. }
  516. }
  517. if (skip_rset)
  518. return;
  519. const Variant *vptr = &p_value;
  520. _send_rpc(p_node, p_peer_id, p_unreliable, true, p_property, &vptr, 1);
  521. }
  522. Error MultiplayerAPI::send_bytes(PoolVector<uint8_t> p_data, int p_to) {
  523. ERR_FAIL_COND_V(p_data.size() < 1, ERR_INVALID_DATA);
  524. ERR_FAIL_COND_V(!network_peer.is_valid(), ERR_UNCONFIGURED);
  525. ERR_FAIL_COND_V(network_peer->get_connection_status() != NetworkedMultiplayerPeer::CONNECTION_CONNECTED, ERR_UNCONFIGURED);
  526. MAKE_ROOM(p_data.size() + 1);
  527. PoolVector<uint8_t>::Read r = p_data.read();
  528. packet_cache[0] = NETWORK_COMMAND_RAW;
  529. memcpy(&packet_cache[1], &r[0], p_data.size());
  530. network_peer->set_target_peer(p_to);
  531. return network_peer->put_packet(packet_cache.ptr(), p_data.size() + 1);
  532. }
  533. void MultiplayerAPI::_process_raw(int p_from, const uint8_t *p_packet, int p_packet_len) {
  534. ERR_FAIL_COND(p_packet_len < 2);
  535. PoolVector<uint8_t> out;
  536. int len = p_packet_len - 1;
  537. out.resize(len);
  538. {
  539. PoolVector<uint8_t>::Write w = out.write();
  540. memcpy(&w[0], &p_packet[1], len);
  541. }
  542. emit_signal("network_peer_packet", p_from, out);
  543. }
  544. int MultiplayerAPI::get_network_unique_id() const {
  545. ERR_FAIL_COND_V(!network_peer.is_valid(), 0);
  546. return network_peer->get_unique_id();
  547. }
  548. bool MultiplayerAPI::is_network_server() const {
  549. ERR_FAIL_COND_V(!network_peer.is_valid(), false);
  550. return network_peer->is_server();
  551. }
  552. void MultiplayerAPI::set_refuse_new_network_connections(bool p_refuse) {
  553. ERR_FAIL_COND(!network_peer.is_valid());
  554. network_peer->set_refuse_new_connections(p_refuse);
  555. }
  556. bool MultiplayerAPI::is_refusing_new_network_connections() const {
  557. ERR_FAIL_COND_V(!network_peer.is_valid(), false);
  558. return network_peer->is_refusing_new_connections();
  559. }
  560. Vector<int> MultiplayerAPI::get_network_connected_peers() const {
  561. ERR_FAIL_COND_V(!network_peer.is_valid(), Vector<int>());
  562. Vector<int> ret;
  563. for (Set<int>::Element *E = connected_peers.front(); E; E = E->next()) {
  564. ret.push_back(E->get());
  565. }
  566. return ret;
  567. }
  568. void MultiplayerAPI::_bind_methods() {
  569. ClassDB::bind_method(D_METHOD("set_root_node", "node"), &MultiplayerAPI::set_root_node);
  570. ClassDB::bind_method(D_METHOD("send_bytes", "bytes", "id"), &MultiplayerAPI::send_bytes, DEFVAL(NetworkedMultiplayerPeer::TARGET_PEER_BROADCAST));
  571. ClassDB::bind_method(D_METHOD("has_network_peer"), &MultiplayerAPI::has_network_peer);
  572. ClassDB::bind_method(D_METHOD("get_network_peer"), &MultiplayerAPI::get_network_peer);
  573. ClassDB::bind_method(D_METHOD("get_network_unique_id"), &MultiplayerAPI::get_network_unique_id);
  574. ClassDB::bind_method(D_METHOD("is_network_server"), &MultiplayerAPI::is_network_server);
  575. ClassDB::bind_method(D_METHOD("get_rpc_sender_id"), &MultiplayerAPI::get_rpc_sender_id);
  576. ClassDB::bind_method(D_METHOD("_add_peer", "id"), &MultiplayerAPI::_add_peer);
  577. ClassDB::bind_method(D_METHOD("_del_peer", "id"), &MultiplayerAPI::_del_peer);
  578. ClassDB::bind_method(D_METHOD("set_network_peer", "peer"), &MultiplayerAPI::set_network_peer);
  579. ClassDB::bind_method(D_METHOD("poll"), &MultiplayerAPI::poll);
  580. ClassDB::bind_method(D_METHOD("clear"), &MultiplayerAPI::clear);
  581. ClassDB::bind_method(D_METHOD("_connected_to_server"), &MultiplayerAPI::_connected_to_server);
  582. ClassDB::bind_method(D_METHOD("_connection_failed"), &MultiplayerAPI::_connection_failed);
  583. ClassDB::bind_method(D_METHOD("_server_disconnected"), &MultiplayerAPI::_server_disconnected);
  584. ClassDB::bind_method(D_METHOD("get_network_connected_peers"), &MultiplayerAPI::get_network_connected_peers);
  585. ClassDB::bind_method(D_METHOD("set_refuse_new_network_connections", "refuse"), &MultiplayerAPI::set_refuse_new_network_connections);
  586. ClassDB::bind_method(D_METHOD("is_refusing_new_network_connections"), &MultiplayerAPI::is_refusing_new_network_connections);
  587. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "refuse_new_network_connections"), "set_refuse_new_network_connections", "is_refusing_new_network_connections");
  588. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "network_peer", PROPERTY_HINT_RESOURCE_TYPE, "NetworkedMultiplayerPeer", 0), "set_network_peer", "get_network_peer");
  589. ADD_SIGNAL(MethodInfo("network_peer_connected", PropertyInfo(Variant::INT, "id")));
  590. ADD_SIGNAL(MethodInfo("network_peer_disconnected", PropertyInfo(Variant::INT, "id")));
  591. ADD_SIGNAL(MethodInfo("network_peer_packet", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::POOL_BYTE_ARRAY, "packet")));
  592. ADD_SIGNAL(MethodInfo("connected_to_server"));
  593. ADD_SIGNAL(MethodInfo("connection_failed"));
  594. ADD_SIGNAL(MethodInfo("server_disconnected"));
  595. }
  596. MultiplayerAPI::MultiplayerAPI() {
  597. clear();
  598. }
  599. MultiplayerAPI::~MultiplayerAPI() {
  600. clear();
  601. }