websocket_multiplayer_peer.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*************************************************************************/
  2. /* websocket_multiplayer_peer.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 "websocket_multiplayer_peer.h"
  31. #include "core/os/os.h"
  32. WebSocketMultiplayerPeer::WebSocketMultiplayerPeer() {
  33. peer_config = Ref<WebSocketPeer>(WebSocketPeer::create());
  34. }
  35. WebSocketMultiplayerPeer::~WebSocketMultiplayerPeer() {
  36. _clear();
  37. }
  38. Ref<WebSocketPeer> WebSocketMultiplayerPeer::_create_peer() {
  39. Ref<WebSocketPeer> peer = Ref<WebSocketPeer>(WebSocketPeer::create());
  40. peer->set_supported_protocols(get_supported_protocols());
  41. peer->set_handshake_headers(get_handshake_headers());
  42. peer->set_inbound_buffer_size(get_inbound_buffer_size());
  43. peer->set_outbound_buffer_size(get_outbound_buffer_size());
  44. peer->set_max_queued_packets(get_max_queued_packets());
  45. return peer;
  46. }
  47. void WebSocketMultiplayerPeer::_clear() {
  48. connection_status = CONNECTION_DISCONNECTED;
  49. unique_id = 0;
  50. peers_map.clear();
  51. use_tls = false;
  52. tcp_server.unref();
  53. pending_peers.clear();
  54. tls_certificate.unref();
  55. tls_key.unref();
  56. if (current_packet.data != nullptr) {
  57. memfree(current_packet.data);
  58. }
  59. for (Packet &E : incoming_packets) {
  60. memfree(E.data);
  61. E.data = nullptr;
  62. }
  63. incoming_packets.clear();
  64. }
  65. void WebSocketMultiplayerPeer::_bind_methods() {
  66. ClassDB::bind_method(D_METHOD("create_client", "url", "verify_tls", "tls_certificate"), &WebSocketMultiplayerPeer::create_client, DEFVAL(true), DEFVAL(Ref<X509Certificate>()));
  67. ClassDB::bind_method(D_METHOD("create_server", "port", "bind_address", "tls_key", "tls_certificate"), &WebSocketMultiplayerPeer::create_server, DEFVAL("*"), DEFVAL(Ref<CryptoKey>()), DEFVAL(Ref<X509Certificate>()));
  68. ClassDB::bind_method(D_METHOD("close"), &WebSocketMultiplayerPeer::close);
  69. ClassDB::bind_method(D_METHOD("get_peer", "peer_id"), &WebSocketMultiplayerPeer::get_peer);
  70. ClassDB::bind_method(D_METHOD("get_peer_address", "id"), &WebSocketMultiplayerPeer::get_peer_address);
  71. ClassDB::bind_method(D_METHOD("get_peer_port", "id"), &WebSocketMultiplayerPeer::get_peer_port);
  72. ClassDB::bind_method(D_METHOD("disconnect_peer", "id", "code", "reason"), &WebSocketMultiplayerPeer::disconnect_peer, DEFVAL(1000), DEFVAL(""));
  73. ClassDB::bind_method(D_METHOD("get_supported_protocols"), &WebSocketMultiplayerPeer::get_supported_protocols);
  74. ClassDB::bind_method(D_METHOD("set_supported_protocols", "protocols"), &WebSocketMultiplayerPeer::set_supported_protocols);
  75. ClassDB::bind_method(D_METHOD("get_handshake_headers"), &WebSocketMultiplayerPeer::get_handshake_headers);
  76. ClassDB::bind_method(D_METHOD("set_handshake_headers", "protocols"), &WebSocketMultiplayerPeer::set_handshake_headers);
  77. ClassDB::bind_method(D_METHOD("get_inbound_buffer_size"), &WebSocketMultiplayerPeer::get_inbound_buffer_size);
  78. ClassDB::bind_method(D_METHOD("set_inbound_buffer_size", "buffer_size"), &WebSocketMultiplayerPeer::set_inbound_buffer_size);
  79. ClassDB::bind_method(D_METHOD("get_outbound_buffer_size"), &WebSocketMultiplayerPeer::get_outbound_buffer_size);
  80. ClassDB::bind_method(D_METHOD("set_outbound_buffer_size", "buffer_size"), &WebSocketMultiplayerPeer::set_outbound_buffer_size);
  81. ClassDB::bind_method(D_METHOD("get_handshake_timeout"), &WebSocketMultiplayerPeer::get_handshake_timeout);
  82. ClassDB::bind_method(D_METHOD("set_handshake_timeout", "timeout"), &WebSocketMultiplayerPeer::set_handshake_timeout);
  83. ClassDB::bind_method(D_METHOD("set_max_queued_packets", "max_queued_packets"), &WebSocketMultiplayerPeer::set_max_queued_packets);
  84. ClassDB::bind_method(D_METHOD("get_max_queued_packets"), &WebSocketMultiplayerPeer::get_max_queued_packets);
  85. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "supported_protocols"), "set_supported_protocols", "get_supported_protocols");
  86. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "handshake_headers"), "set_handshake_headers", "get_handshake_headers");
  87. ADD_PROPERTY(PropertyInfo(Variant::INT, "inbound_buffer_size"), "set_inbound_buffer_size", "get_inbound_buffer_size");
  88. ADD_PROPERTY(PropertyInfo(Variant::INT, "outbound_buffer_size"), "set_outbound_buffer_size", "get_outbound_buffer_size");
  89. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "handshake_timeout"), "set_handshake_timeout", "get_handshake_timeout");
  90. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_queued_packets"), "set_max_queued_packets", "get_max_queued_packets");
  91. }
  92. //
  93. // PacketPeer
  94. //
  95. int WebSocketMultiplayerPeer::get_available_packet_count() const {
  96. return incoming_packets.size();
  97. }
  98. Error WebSocketMultiplayerPeer::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
  99. ERR_FAIL_COND_V(get_connection_status() != CONNECTION_CONNECTED, ERR_UNCONFIGURED);
  100. r_buffer_size = 0;
  101. if (current_packet.data != nullptr) {
  102. memfree(current_packet.data);
  103. current_packet.data = nullptr;
  104. }
  105. ERR_FAIL_COND_V(incoming_packets.size() == 0, ERR_UNAVAILABLE);
  106. current_packet = incoming_packets.front()->get();
  107. incoming_packets.pop_front();
  108. *r_buffer = current_packet.data;
  109. r_buffer_size = current_packet.size;
  110. return OK;
  111. }
  112. Error WebSocketMultiplayerPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
  113. ERR_FAIL_COND_V(get_connection_status() != CONNECTION_CONNECTED, ERR_UNCONFIGURED);
  114. Vector<uint8_t> buffer = _make_pkt(SYS_NONE, get_unique_id(), target_peer, p_buffer, p_buffer_size);
  115. if (is_server()) {
  116. return _server_relay(1, target_peer, &(buffer.ptr()[0]), buffer.size());
  117. } else {
  118. return get_peer(1)->put_packet(&(buffer.ptr()[0]), buffer.size());
  119. }
  120. }
  121. //
  122. // MultiplayerPeer
  123. //
  124. void WebSocketMultiplayerPeer::set_target_peer(int p_target_peer) {
  125. target_peer = p_target_peer;
  126. }
  127. int WebSocketMultiplayerPeer::get_packet_peer() const {
  128. ERR_FAIL_COND_V(incoming_packets.size() == 0, 1);
  129. return incoming_packets.front()->get().source;
  130. }
  131. int WebSocketMultiplayerPeer::get_unique_id() const {
  132. return unique_id;
  133. }
  134. int WebSocketMultiplayerPeer::get_max_packet_size() const {
  135. return get_outbound_buffer_size() - PROTO_SIZE;
  136. }
  137. Error WebSocketMultiplayerPeer::create_server(int p_port, IPAddress p_bind_ip, Ref<CryptoKey> p_tls_key, Ref<X509Certificate> p_tls_certificate) {
  138. ERR_FAIL_COND_V(get_connection_status() != CONNECTION_DISCONNECTED, ERR_ALREADY_IN_USE);
  139. _clear();
  140. tcp_server.instantiate();
  141. Error err = tcp_server->listen(p_port, p_bind_ip);
  142. if (err != OK) {
  143. tcp_server.unref();
  144. return err;
  145. }
  146. unique_id = 1;
  147. connection_status = CONNECTION_CONNECTED;
  148. // TLS config
  149. tls_key = p_tls_key;
  150. tls_certificate = p_tls_certificate;
  151. if (tls_key.is_valid() && tls_certificate.is_valid()) {
  152. use_tls = true;
  153. }
  154. return OK;
  155. }
  156. Error WebSocketMultiplayerPeer::create_client(const String &p_url, bool p_verify_tls, Ref<X509Certificate> p_tls_certificate) {
  157. ERR_FAIL_COND_V(get_connection_status() != CONNECTION_DISCONNECTED, ERR_ALREADY_IN_USE);
  158. _clear();
  159. Ref<WebSocketPeer> peer = _create_peer();
  160. Error err = peer->connect_to_url(p_url, p_verify_tls, p_tls_certificate);
  161. if (err != OK) {
  162. return err;
  163. }
  164. PendingPeer pending;
  165. pending.time = OS::get_singleton()->get_ticks_msec();
  166. pending_peers[1] = pending;
  167. peers_map[1] = peer;
  168. connection_status = CONNECTION_CONNECTING;
  169. return OK;
  170. }
  171. bool WebSocketMultiplayerPeer::is_server() const {
  172. return tcp_server.is_valid();
  173. }
  174. void WebSocketMultiplayerPeer::_poll_client() {
  175. ERR_FAIL_COND(connection_status == CONNECTION_DISCONNECTED); // Bug.
  176. ERR_FAIL_COND(!peers_map.has(1) || peers_map[1].is_null()); // Bug.
  177. Ref<WebSocketPeer> peer = peers_map[1];
  178. peer->poll(); // Update state and fetch packets.
  179. WebSocketPeer::State ready_state = peer->get_ready_state();
  180. if (ready_state == WebSocketPeer::STATE_OPEN) {
  181. while (peer->get_available_packet_count()) {
  182. _process_multiplayer(peer, 1);
  183. }
  184. } else if (peer->get_ready_state() == WebSocketPeer::STATE_CLOSED) {
  185. if (connection_status == CONNECTION_CONNECTED) {
  186. emit_signal(SNAME("server_disconnected"));
  187. } else {
  188. emit_signal(SNAME("connection_failed"));
  189. }
  190. _clear();
  191. return;
  192. }
  193. if (connection_status == CONNECTION_CONNECTING) {
  194. // Still connecting
  195. ERR_FAIL_COND(!pending_peers.has(1)); // Bug.
  196. if (OS::get_singleton()->get_ticks_msec() - pending_peers[1].time > handshake_timeout) {
  197. print_verbose(vformat("WebSocket handshake timed out after %.3f seconds.", handshake_timeout * 0.001));
  198. emit_signal(SNAME("connection_failed"));
  199. _clear();
  200. return;
  201. }
  202. }
  203. }
  204. void WebSocketMultiplayerPeer::_poll_server() {
  205. ERR_FAIL_COND(connection_status != CONNECTION_CONNECTED); // Bug.
  206. ERR_FAIL_COND(tcp_server.is_null() || !tcp_server->is_listening()); // Bug.
  207. // Accept new connections.
  208. if (!is_refusing_new_connections() && tcp_server->is_connection_available()) {
  209. PendingPeer peer;
  210. peer.time = OS::get_singleton()->get_ticks_msec();
  211. peer.tcp = tcp_server->take_connection();
  212. peer.connection = peer.tcp;
  213. pending_peers[generate_unique_id()] = peer;
  214. }
  215. // Process pending peers.
  216. HashSet<int> to_remove;
  217. for (KeyValue<int, PendingPeer> &E : pending_peers) {
  218. PendingPeer &peer = E.value;
  219. int id = E.key;
  220. if (OS::get_singleton()->get_ticks_msec() - peer.time > handshake_timeout) {
  221. print_verbose(vformat("WebSocket handshake timed out after %.3f seconds.", handshake_timeout * 0.001));
  222. to_remove.insert(id);
  223. continue;
  224. }
  225. if (peer.ws.is_valid()) {
  226. peer.ws->poll();
  227. WebSocketPeer::State state = peer.ws->get_ready_state();
  228. if (state == WebSocketPeer::STATE_OPEN) {
  229. // Connected.
  230. to_remove.insert(id);
  231. if (is_refusing_new_connections()) {
  232. // The user does not want new connections, dropping it.
  233. continue;
  234. }
  235. peers_map[id] = peer.ws;
  236. _send_ack(peer.ws, id);
  237. emit_signal("peer_connected", id);
  238. continue;
  239. } else if (state == WebSocketPeer::STATE_CONNECTING) {
  240. continue; // Still connecting.
  241. }
  242. to_remove.insert(id); // Error.
  243. continue;
  244. }
  245. if (peer.tcp->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
  246. to_remove.insert(id); // Error.
  247. continue;
  248. }
  249. if (!use_tls) {
  250. peer.ws = _create_peer();
  251. peer.ws->accept_stream(peer.tcp);
  252. continue;
  253. } else {
  254. if (peer.connection == peer.tcp) {
  255. Ref<StreamPeerTLS> tls = Ref<StreamPeerTLS>(StreamPeerTLS::create());
  256. Error err = tls->accept_stream(peer.tcp, tls_key, tls_certificate);
  257. if (err != OK) {
  258. to_remove.insert(id);
  259. continue;
  260. }
  261. }
  262. Ref<StreamPeerTLS> tls = static_cast<Ref<StreamPeerTLS>>(peer.connection);
  263. tls->poll();
  264. if (tls->get_status() == StreamPeerTLS::STATUS_CONNECTED) {
  265. peer.ws = _create_peer();
  266. peer.ws->accept_stream(peer.connection);
  267. continue;
  268. } else if (tls->get_status() == StreamPeerTLS::STATUS_HANDSHAKING) {
  269. // Still connecting.
  270. continue;
  271. } else {
  272. // Error
  273. to_remove.insert(id);
  274. }
  275. }
  276. }
  277. // Remove disconnected pending peers.
  278. for (const int &pid : to_remove) {
  279. pending_peers.erase(pid);
  280. }
  281. to_remove.clear();
  282. // Process connected peers.
  283. for (KeyValue<int, Ref<WebSocketPeer>> &E : peers_map) {
  284. Ref<WebSocketPeer> ws = E.value;
  285. int id = E.key;
  286. ws->poll();
  287. if (ws->get_ready_state() != WebSocketPeer::STATE_OPEN) {
  288. to_remove.insert(id); // Disconnected.
  289. continue;
  290. }
  291. // Fetch packets
  292. int pkts = ws->get_available_packet_count();
  293. while (pkts && ws->get_ready_state() == WebSocketPeer::STATE_OPEN) {
  294. _process_multiplayer(ws, id);
  295. pkts--;
  296. }
  297. }
  298. // Remove disconnected peers.
  299. for (const int &pid : to_remove) {
  300. emit_signal(SNAME("peer_disconnected"), pid);
  301. peers_map.erase(pid);
  302. }
  303. }
  304. void WebSocketMultiplayerPeer::poll() {
  305. if (connection_status == CONNECTION_DISCONNECTED) {
  306. return;
  307. }
  308. if (is_server()) {
  309. _poll_server();
  310. } else {
  311. _poll_client();
  312. }
  313. }
  314. MultiplayerPeer::ConnectionStatus WebSocketMultiplayerPeer::get_connection_status() const {
  315. return connection_status;
  316. }
  317. Ref<WebSocketPeer> WebSocketMultiplayerPeer::get_peer(int p_id) const {
  318. ERR_FAIL_COND_V(!peers_map.has(p_id), Ref<WebSocketPeer>());
  319. return peers_map[p_id];
  320. }
  321. void WebSocketMultiplayerPeer::_send_sys(Ref<WebSocketPeer> p_peer, uint8_t p_type, int32_t p_peer_id) {
  322. ERR_FAIL_COND(!p_peer.is_valid());
  323. ERR_FAIL_COND(p_peer->get_ready_state() != WebSocketPeer::STATE_OPEN);
  324. Vector<uint8_t> message = _make_pkt(p_type, 1, 0, (uint8_t *)&p_peer_id, 4);
  325. p_peer->put_packet(&(message.ptr()[0]), message.size());
  326. }
  327. Vector<uint8_t> WebSocketMultiplayerPeer::_make_pkt(uint8_t p_type, int32_t p_from, int32_t p_to, const uint8_t *p_data, uint32_t p_data_size) {
  328. Vector<uint8_t> out;
  329. out.resize(PROTO_SIZE + p_data_size);
  330. uint8_t *w = out.ptrw();
  331. memcpy(&w[0], &p_type, 1);
  332. memcpy(&w[1], &p_from, 4);
  333. memcpy(&w[5], &p_to, 4);
  334. memcpy(&w[PROTO_SIZE], p_data, p_data_size);
  335. return out;
  336. }
  337. void WebSocketMultiplayerPeer::_send_ack(Ref<WebSocketPeer> p_peer, int32_t p_peer_id) {
  338. ERR_FAIL_COND(p_peer.is_null());
  339. // First of all, confirm the ID!
  340. _send_sys(p_peer, SYS_ID, p_peer_id);
  341. // Then send the server peer (which will trigger connection_succeded in client)
  342. _send_sys(p_peer, SYS_ADD, 1);
  343. for (const KeyValue<int, Ref<WebSocketPeer>> &E : peers_map) {
  344. ERR_CONTINUE(E.value.is_null());
  345. int32_t id = E.key;
  346. if (p_peer_id == id) {
  347. continue; // Skip the newly added peer (already confirmed)
  348. }
  349. // Send new peer to others
  350. _send_sys(E.value, SYS_ADD, p_peer_id);
  351. // Send others to new peer
  352. _send_sys(E.value, SYS_ADD, id);
  353. }
  354. }
  355. void WebSocketMultiplayerPeer::_send_del(int32_t p_peer_id) {
  356. for (const KeyValue<int, Ref<WebSocketPeer>> &E : peers_map) {
  357. int32_t id = E.key;
  358. if (p_peer_id != id) {
  359. _send_sys(E.value, SYS_DEL, p_peer_id);
  360. }
  361. }
  362. }
  363. void WebSocketMultiplayerPeer::_store_pkt(int32_t p_source, int32_t p_dest, const uint8_t *p_data, uint32_t p_data_size) {
  364. Packet packet;
  365. packet.data = (uint8_t *)memalloc(p_data_size);
  366. packet.size = p_data_size;
  367. packet.source = p_source;
  368. packet.destination = p_dest;
  369. memcpy(packet.data, &p_data[PROTO_SIZE], p_data_size);
  370. incoming_packets.push_back(packet);
  371. }
  372. Error WebSocketMultiplayerPeer::_server_relay(int32_t p_from, int32_t p_to, const uint8_t *p_buffer, uint32_t p_buffer_size) {
  373. if (p_to == 1) {
  374. return OK; // Will not send to self
  375. } else if (p_to == 0) {
  376. for (KeyValue<int, Ref<WebSocketPeer>> &E : peers_map) {
  377. if (E.key != p_from) {
  378. E.value->put_packet(p_buffer, p_buffer_size);
  379. }
  380. }
  381. return OK; // Sent to all but sender
  382. } else if (p_to < 0) {
  383. for (KeyValue<int, Ref<WebSocketPeer>> &E : peers_map) {
  384. if (E.key != p_from && E.key != -p_to) {
  385. E.value->put_packet(p_buffer, p_buffer_size);
  386. }
  387. }
  388. return OK; // Sent to all but sender and excluded
  389. } else {
  390. ERR_FAIL_COND_V(p_to == p_from, FAILED);
  391. Ref<WebSocketPeer> peer_to = get_peer(p_to);
  392. ERR_FAIL_COND_V(peer_to.is_null(), FAILED);
  393. return peer_to->put_packet(p_buffer, p_buffer_size); // Sending to specific peer
  394. }
  395. }
  396. void WebSocketMultiplayerPeer::_process_multiplayer(Ref<WebSocketPeer> p_peer, uint32_t p_peer_id) {
  397. ERR_FAIL_COND(!p_peer.is_valid());
  398. const uint8_t *in_buffer;
  399. int size = 0;
  400. int data_size = 0;
  401. Error err = p_peer->get_packet(&in_buffer, size);
  402. ERR_FAIL_COND(err != OK);
  403. ERR_FAIL_COND(size < PROTO_SIZE);
  404. data_size = size - PROTO_SIZE;
  405. uint8_t type = 0;
  406. uint32_t from = 0;
  407. int32_t to = 0;
  408. memcpy(&type, in_buffer, 1);
  409. memcpy(&from, &in_buffer[1], 4);
  410. memcpy(&to, &in_buffer[5], 4);
  411. if (is_server()) { // Server can resend
  412. ERR_FAIL_COND(type != SYS_NONE); // Only server sends sys messages
  413. ERR_FAIL_COND(from != p_peer_id); // Someone is cheating
  414. if (to == 1) {
  415. // This is for the server
  416. _store_pkt(from, to, in_buffer, data_size);
  417. } else if (to == 0) {
  418. // Broadcast, for us too
  419. _store_pkt(from, to, in_buffer, data_size);
  420. } else if (to < -1) {
  421. // All but one, for us if not excluded
  422. _store_pkt(from, to, in_buffer, data_size);
  423. }
  424. // Relay if needed (i.e. "to" includes a peer that is not the server)
  425. _server_relay(from, to, in_buffer, size);
  426. } else {
  427. if (type == SYS_NONE) {
  428. // Payload message
  429. _store_pkt(from, to, in_buffer, data_size);
  430. return;
  431. }
  432. // System message
  433. ERR_FAIL_COND(data_size < 4);
  434. int id = 0;
  435. memcpy(&id, &in_buffer[PROTO_SIZE], 4);
  436. switch (type) {
  437. case SYS_ADD: // Add peer
  438. if (id != 1) {
  439. peers_map[id] = Ref<WebSocketPeer>();
  440. } else {
  441. pending_peers.clear();
  442. connection_status = CONNECTION_CONNECTED;
  443. }
  444. emit_signal(SNAME("peer_connected"), id);
  445. if (id == 1) { // We just connected to the server
  446. emit_signal(SNAME("connection_succeeded"));
  447. }
  448. break;
  449. case SYS_DEL: // Remove peer
  450. emit_signal(SNAME("peer_disconnected"), id);
  451. peers_map.erase(id);
  452. break;
  453. case SYS_ID: // Hello, server assigned ID
  454. unique_id = id;
  455. break;
  456. default:
  457. ERR_FAIL_MSG("Invalid multiplayer message.");
  458. break;
  459. }
  460. }
  461. }
  462. void WebSocketMultiplayerPeer::set_supported_protocols(const Vector<String> &p_protocols) {
  463. peer_config->set_supported_protocols(p_protocols);
  464. }
  465. Vector<String> WebSocketMultiplayerPeer::get_supported_protocols() const {
  466. return peer_config->get_supported_protocols();
  467. }
  468. void WebSocketMultiplayerPeer::set_handshake_headers(const Vector<String> &p_headers) {
  469. peer_config->set_handshake_headers(p_headers);
  470. }
  471. Vector<String> WebSocketMultiplayerPeer::get_handshake_headers() const {
  472. return peer_config->get_handshake_headers();
  473. }
  474. void WebSocketMultiplayerPeer::set_outbound_buffer_size(int p_buffer_size) {
  475. peer_config->set_outbound_buffer_size(p_buffer_size);
  476. }
  477. int WebSocketMultiplayerPeer::get_outbound_buffer_size() const {
  478. return peer_config->get_outbound_buffer_size();
  479. }
  480. void WebSocketMultiplayerPeer::set_inbound_buffer_size(int p_buffer_size) {
  481. peer_config->set_inbound_buffer_size(p_buffer_size);
  482. }
  483. int WebSocketMultiplayerPeer::get_inbound_buffer_size() const {
  484. return peer_config->get_inbound_buffer_size();
  485. }
  486. void WebSocketMultiplayerPeer::set_max_queued_packets(int p_max_queued_packets) {
  487. peer_config->set_max_queued_packets(p_max_queued_packets);
  488. }
  489. int WebSocketMultiplayerPeer::get_max_queued_packets() const {
  490. return peer_config->get_max_queued_packets();
  491. }
  492. float WebSocketMultiplayerPeer::get_handshake_timeout() const {
  493. return handshake_timeout / 1000.0;
  494. }
  495. void WebSocketMultiplayerPeer::set_handshake_timeout(float p_timeout) {
  496. ERR_FAIL_COND(p_timeout <= 0.0);
  497. handshake_timeout = p_timeout * 1000;
  498. }
  499. IPAddress WebSocketMultiplayerPeer::get_peer_address(int p_peer_id) const {
  500. ERR_FAIL_COND_V(!peers_map.has(p_peer_id), IPAddress());
  501. return peers_map[p_peer_id]->get_connected_host();
  502. }
  503. int WebSocketMultiplayerPeer::get_peer_port(int p_peer_id) const {
  504. ERR_FAIL_COND_V(!peers_map.has(p_peer_id), 0);
  505. return peers_map[p_peer_id]->get_connected_port();
  506. }
  507. void WebSocketMultiplayerPeer::disconnect_peer(int p_peer_id, int p_code, String p_reason) {
  508. ERR_FAIL_COND(!peers_map.has(p_peer_id));
  509. peers_map[p_peer_id]->close(p_code, p_reason);
  510. }
  511. void WebSocketMultiplayerPeer::close() {
  512. _clear();
  513. }