multiplayer_api.cpp 22 KB

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