networked_multiplayer_enet.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /*************************************************************************/
  2. /* networked_multiplayer_enet.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 "networked_multiplayer_enet.h"
  31. #include "io/marshalls.h"
  32. #include "os/os.h"
  33. void NetworkedMultiplayerENet::set_transfer_mode(TransferMode p_mode) {
  34. transfer_mode = p_mode;
  35. }
  36. NetworkedMultiplayerPeer::TransferMode NetworkedMultiplayerENet::get_transfer_mode() const {
  37. return transfer_mode;
  38. }
  39. void NetworkedMultiplayerENet::set_target_peer(int p_peer) {
  40. target_peer = p_peer;
  41. }
  42. int NetworkedMultiplayerENet::get_packet_peer() const {
  43. ERR_FAIL_COND_V(!active, 1);
  44. ERR_FAIL_COND_V(incoming_packets.size() == 0, 1);
  45. return incoming_packets.front()->get().from;
  46. }
  47. Error NetworkedMultiplayerENet::create_server(int p_port, int p_max_clients, int p_in_bandwidth, int p_out_bandwidth) {
  48. ERR_FAIL_COND_V(active, ERR_ALREADY_IN_USE);
  49. ENetAddress address;
  50. #ifdef GODOT_ENET
  51. if (bind_ip.is_wildcard()) {
  52. address.wildcard = 1;
  53. } else {
  54. enet_address_set_ip(&address, bind_ip.get_ipv6(), 16);
  55. }
  56. #else
  57. if (bind_ip.is_wildcard()) {
  58. address.host = 0;
  59. } else {
  60. ERR_FAIL_COND_V(!bind_ip.is_ipv4(), ERR_INVALID_PARAMETER);
  61. address.host = *(uint32_t *)bind_ip.get_ipv4();
  62. }
  63. #endif
  64. address.port = p_port;
  65. host = enet_host_create(&address /* the address to bind the server host to */,
  66. p_max_clients /* allow up to 32 clients and/or outgoing connections */,
  67. SYSCH_MAX /* allow up to SYSCH_MAX channels to be used */,
  68. p_in_bandwidth /* assume any amount of incoming bandwidth */,
  69. p_out_bandwidth /* assume any amount of outgoing bandwidth */);
  70. ERR_FAIL_COND_V(!host, ERR_CANT_CREATE);
  71. _setup_compressor();
  72. active = true;
  73. server = true;
  74. refuse_connections = false;
  75. unique_id = 1;
  76. connection_status = CONNECTION_CONNECTED;
  77. return OK;
  78. }
  79. Error NetworkedMultiplayerENet::create_client(const IP_Address &p_ip, int p_port, int p_in_bandwidth, int p_out_bandwidth) {
  80. ERR_FAIL_COND_V(active, ERR_ALREADY_IN_USE);
  81. host = enet_host_create(NULL /* create a client host */,
  82. 1 /* only allow 1 outgoing connection */,
  83. SYSCH_MAX /* allow up to SYSCH_MAX channels to be used */,
  84. p_in_bandwidth /* 56K modem with 56 Kbps downstream bandwidth */,
  85. p_out_bandwidth /* 56K modem with 14 Kbps upstream bandwidth */);
  86. ERR_FAIL_COND_V(!host, ERR_CANT_CREATE);
  87. _setup_compressor();
  88. ENetAddress address;
  89. #ifdef GODOT_ENET
  90. enet_address_set_ip(&address, p_ip.get_ipv6(), 16);
  91. #else
  92. ERR_FAIL_COND_V(!p_ip.is_ipv4(), ERR_INVALID_PARAMETER);
  93. address.host = *(uint32_t *)p_ip.get_ipv4();
  94. #endif
  95. address.port = p_port;
  96. unique_id = _gen_unique_id();
  97. /* Initiate the connection, allocating the enough channels */
  98. ENetPeer *peer = enet_host_connect(host, &address, SYSCH_MAX, unique_id);
  99. if (peer == NULL) {
  100. enet_host_destroy(host);
  101. ERR_FAIL_COND_V(!peer, ERR_CANT_CREATE);
  102. }
  103. // Technically safe to ignore the peer or anything else.
  104. connection_status = CONNECTION_CONNECTING;
  105. active = true;
  106. server = false;
  107. refuse_connections = false;
  108. return OK;
  109. }
  110. void NetworkedMultiplayerENet::poll() {
  111. ERR_FAIL_COND(!active);
  112. _pop_current_packet();
  113. ENetEvent event;
  114. /* Wait up to 1000 milliseconds for an event. */
  115. while (true) {
  116. if (!host || !active) // Might have been disconnected while emitting a notification
  117. return;
  118. int ret = enet_host_service(host, &event, 1);
  119. if (ret < 0) {
  120. // Error, do something?
  121. break;
  122. } else if (ret == 0) {
  123. break;
  124. }
  125. switch (event.type) {
  126. case ENET_EVENT_TYPE_CONNECT: {
  127. /* Store any relevant client information here. */
  128. if (server && refuse_connections) {
  129. enet_peer_reset(event.peer);
  130. break;
  131. }
  132. int *new_id = memnew(int);
  133. *new_id = event.data;
  134. if (*new_id == 0) { // Data zero is sent by server (enet won't let you configure this). Server is always 1
  135. *new_id = 1;
  136. }
  137. event.peer->data = new_id;
  138. peer_map[*new_id] = event.peer;
  139. connection_status = CONNECTION_CONNECTED; // If connecting, this means it connected to something!
  140. emit_signal("peer_connected", *new_id);
  141. if (server) {
  142. // Someone connected, notify all the peers available
  143. for (Map<int, ENetPeer *>::Element *E = peer_map.front(); E; E = E->next()) {
  144. if (E->key() == *new_id)
  145. continue;
  146. // Send existing peers to new peer
  147. ENetPacket *packet = enet_packet_create(NULL, 8, ENET_PACKET_FLAG_RELIABLE);
  148. encode_uint32(SYSMSG_ADD_PEER, &packet->data[0]);
  149. encode_uint32(E->key(), &packet->data[4]);
  150. enet_peer_send(event.peer, SYSCH_CONFIG, packet);
  151. // Send the new peer to existing peers
  152. packet = enet_packet_create(NULL, 8, ENET_PACKET_FLAG_RELIABLE);
  153. encode_uint32(SYSMSG_ADD_PEER, &packet->data[0]);
  154. encode_uint32(*new_id, &packet->data[4]);
  155. enet_peer_send(E->get(), SYSCH_CONFIG, packet);
  156. }
  157. } else {
  158. emit_signal("connection_succeeded");
  159. }
  160. } break;
  161. case ENET_EVENT_TYPE_DISCONNECT: {
  162. /* Reset the peer's client information. */
  163. int *id = (int *)event.peer->data;
  164. if (!id) {
  165. if (!server) {
  166. emit_signal("connection_failed");
  167. }
  168. } else {
  169. if (server) {
  170. // Someone disconnected, notify everyone else
  171. for (Map<int, ENetPeer *>::Element *E = peer_map.front(); E; E = E->next()) {
  172. if (E->key() == *id)
  173. continue;
  174. ENetPacket *packet = enet_packet_create(NULL, 8, ENET_PACKET_FLAG_RELIABLE);
  175. encode_uint32(SYSMSG_REMOVE_PEER, &packet->data[0]);
  176. encode_uint32(*id, &packet->data[4]);
  177. enet_peer_send(E->get(), SYSCH_CONFIG, packet);
  178. }
  179. } else if (!server) {
  180. emit_signal("server_disconnected");
  181. close_connection();
  182. return;
  183. }
  184. emit_signal("peer_disconnected", *id);
  185. peer_map.erase(*id);
  186. memdelete(id);
  187. }
  188. } break;
  189. case ENET_EVENT_TYPE_RECEIVE: {
  190. if (event.channelID == SYSCH_CONFIG) {
  191. // Some config message
  192. ERR_CONTINUE(event.packet->dataLength < 8);
  193. // Only server can send config messages
  194. ERR_CONTINUE(server);
  195. int msg = decode_uint32(&event.packet->data[0]);
  196. int id = decode_uint32(&event.packet->data[4]);
  197. switch (msg) {
  198. case SYSMSG_ADD_PEER: {
  199. peer_map[id] = NULL;
  200. emit_signal("peer_connected", id);
  201. } break;
  202. case SYSMSG_REMOVE_PEER: {
  203. peer_map.erase(id);
  204. emit_signal("peer_disconnected", id);
  205. } break;
  206. }
  207. enet_packet_destroy(event.packet);
  208. } else if (event.channelID < SYSCH_MAX) {
  209. Packet packet;
  210. packet.packet = event.packet;
  211. uint32_t *id = (uint32_t *)event.peer->data;
  212. ERR_CONTINUE(event.packet->dataLength < 12)
  213. uint32_t source = decode_uint32(&event.packet->data[0]);
  214. int target = decode_uint32(&event.packet->data[4]);
  215. uint32_t flags = decode_uint32(&event.packet->data[8]);
  216. packet.from = source;
  217. if (server) {
  218. // Someone is cheating and trying to fake the source!
  219. ERR_CONTINUE(source != *id);
  220. packet.from = *id;
  221. if (target == 0) {
  222. // Re-send to everyone but sender :|
  223. incoming_packets.push_back(packet);
  224. // And make copies for sending
  225. for (Map<int, ENetPeer *>::Element *E = peer_map.front(); E; E = E->next()) {
  226. if (uint32_t(E->key()) == source) // Do not resend to self
  227. continue;
  228. ENetPacket *packet2 = enet_packet_create(packet.packet->data, packet.packet->dataLength, flags);
  229. enet_peer_send(E->get(), event.channelID, packet2);
  230. }
  231. } else if (target < 0) {
  232. // To all but one
  233. // And make copies for sending
  234. for (Map<int, ENetPeer *>::Element *E = peer_map.front(); E; E = E->next()) {
  235. if (uint32_t(E->key()) == source || E->key() == -target) // Do not resend to self, also do not send to excluded
  236. continue;
  237. ENetPacket *packet2 = enet_packet_create(packet.packet->data, packet.packet->dataLength, flags);
  238. enet_peer_send(E->get(), event.channelID, packet2);
  239. }
  240. if (-target != 1) {
  241. // Server is not excluded
  242. incoming_packets.push_back(packet);
  243. } else {
  244. // Server is excluded, erase packet
  245. enet_packet_destroy(packet.packet);
  246. }
  247. } else if (target == 1) {
  248. // To myself and only myself
  249. incoming_packets.push_back(packet);
  250. } else {
  251. // To someone else, specifically
  252. ERR_CONTINUE(!peer_map.has(target));
  253. enet_peer_send(peer_map[target], event.channelID, packet.packet);
  254. }
  255. } else {
  256. incoming_packets.push_back(packet);
  257. }
  258. // Destroy packet later..
  259. } else {
  260. ERR_CONTINUE(true);
  261. }
  262. } break;
  263. case ENET_EVENT_TYPE_NONE: {
  264. // Do nothing
  265. } break;
  266. }
  267. }
  268. }
  269. bool NetworkedMultiplayerENet::is_server() const {
  270. ERR_FAIL_COND_V(!active, false);
  271. return server;
  272. }
  273. void NetworkedMultiplayerENet::close_connection() {
  274. if (!active)
  275. return;
  276. _pop_current_packet();
  277. bool peers_disconnected = false;
  278. for (Map<int, ENetPeer *>::Element *E = peer_map.front(); E; E = E->next()) {
  279. if (E->get()) {
  280. enet_peer_disconnect_now(E->get(), unique_id);
  281. peers_disconnected = true;
  282. }
  283. }
  284. if (peers_disconnected) {
  285. enet_host_flush(host);
  286. OS::get_singleton()->delay_usec(100); // Wait 100ms for disconnection packets to send
  287. }
  288. enet_host_destroy(host);
  289. active = false;
  290. incoming_packets.clear();
  291. unique_id = 1; // Server is 1
  292. connection_status = CONNECTION_DISCONNECTED;
  293. }
  294. void NetworkedMultiplayerENet::disconnect_peer(int p_peer, bool now) {
  295. ERR_FAIL_COND(!active);
  296. ERR_FAIL_COND(!is_server());
  297. ERR_FAIL_COND(!peer_map.has(p_peer))
  298. if (now) {
  299. enet_peer_disconnect_now(peer_map[p_peer], 0);
  300. // enet_peer_disconnect_now doesn't generate ENET_EVENT_TYPE_DISCONNECT,
  301. // notify everyone else, send disconnect signal & remove from peer_map like in poll()
  302. for (Map<int, ENetPeer *>::Element *E = peer_map.front(); E; E = E->next()) {
  303. if (E->key() == p_peer)
  304. continue;
  305. ENetPacket *packet = enet_packet_create(NULL, 8, ENET_PACKET_FLAG_RELIABLE);
  306. encode_uint32(SYSMSG_REMOVE_PEER, &packet->data[0]);
  307. encode_uint32(p_peer, &packet->data[4]);
  308. enet_peer_send(E->get(), SYSCH_CONFIG, packet);
  309. }
  310. emit_signal("peer_disconnected", p_peer);
  311. peer_map.erase(p_peer);
  312. } else {
  313. enet_peer_disconnect_later(peer_map[p_peer], 0);
  314. }
  315. }
  316. int NetworkedMultiplayerENet::get_available_packet_count() const {
  317. return incoming_packets.size();
  318. }
  319. Error NetworkedMultiplayerENet::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
  320. ERR_FAIL_COND_V(incoming_packets.size() == 0, ERR_UNAVAILABLE);
  321. _pop_current_packet();
  322. current_packet = incoming_packets.front()->get();
  323. incoming_packets.pop_front();
  324. *r_buffer = (const uint8_t *)(&current_packet.packet->data[12]);
  325. r_buffer_size = current_packet.packet->dataLength - 12;
  326. return OK;
  327. }
  328. Error NetworkedMultiplayerENet::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
  329. ERR_FAIL_COND_V(!active, ERR_UNCONFIGURED);
  330. ERR_FAIL_COND_V(connection_status != CONNECTION_CONNECTED, ERR_UNCONFIGURED);
  331. int packet_flags = 0;
  332. int channel = SYSCH_RELIABLE;
  333. switch (transfer_mode) {
  334. case TRANSFER_MODE_UNRELIABLE: {
  335. packet_flags = ENET_PACKET_FLAG_UNSEQUENCED;
  336. channel = SYSCH_UNRELIABLE;
  337. } break;
  338. case TRANSFER_MODE_UNRELIABLE_ORDERED: {
  339. packet_flags = 0;
  340. channel = SYSCH_UNRELIABLE;
  341. } break;
  342. case TRANSFER_MODE_RELIABLE: {
  343. packet_flags = ENET_PACKET_FLAG_RELIABLE;
  344. channel = SYSCH_RELIABLE;
  345. } break;
  346. }
  347. Map<int, ENetPeer *>::Element *E = NULL;
  348. if (target_peer != 0) {
  349. E = peer_map.find(ABS(target_peer));
  350. if (!E) {
  351. ERR_EXPLAIN("Invalid Target Peer: " + itos(target_peer));
  352. ERR_FAIL_V(ERR_INVALID_PARAMETER);
  353. }
  354. }
  355. ENetPacket *packet = enet_packet_create(NULL, p_buffer_size + 12, packet_flags);
  356. encode_uint32(unique_id, &packet->data[0]); // Source ID
  357. encode_uint32(target_peer, &packet->data[4]); // Dest ID
  358. encode_uint32(packet_flags, &packet->data[8]); // Dest ID
  359. copymem(&packet->data[12], p_buffer, p_buffer_size);
  360. if (server) {
  361. if (target_peer == 0) {
  362. enet_host_broadcast(host, channel, packet);
  363. } else if (target_peer < 0) {
  364. // Send to all but one
  365. // and make copies for sending
  366. int exclude = -target_peer;
  367. for (Map<int, ENetPeer *>::Element *F = peer_map.front(); F; F = F->next()) {
  368. if (F->key() == exclude) // Exclude packet
  369. continue;
  370. ENetPacket *packet2 = enet_packet_create(packet->data, packet->dataLength, packet_flags);
  371. enet_peer_send(F->get(), channel, packet2);
  372. }
  373. enet_packet_destroy(packet); // Original packet no longer needed
  374. } else {
  375. enet_peer_send(E->get(), channel, packet);
  376. }
  377. } else {
  378. ERR_FAIL_COND_V(!peer_map.has(1), ERR_BUG);
  379. enet_peer_send(peer_map[1], channel, packet); // Send to server for broadcast..
  380. }
  381. enet_host_flush(host);
  382. return OK;
  383. }
  384. int NetworkedMultiplayerENet::get_max_packet_size() const {
  385. return 1 << 24; // Anything is good
  386. }
  387. void NetworkedMultiplayerENet::_pop_current_packet() {
  388. if (current_packet.packet) {
  389. enet_packet_destroy(current_packet.packet);
  390. current_packet.packet = NULL;
  391. current_packet.from = 0;
  392. }
  393. }
  394. NetworkedMultiplayerPeer::ConnectionStatus NetworkedMultiplayerENet::get_connection_status() const {
  395. return connection_status;
  396. }
  397. uint32_t NetworkedMultiplayerENet::_gen_unique_id() const {
  398. uint32_t hash = 0;
  399. while (hash == 0 || hash == 1) {
  400. hash = hash_djb2_one_32(
  401. (uint32_t)OS::get_singleton()->get_ticks_usec());
  402. hash = hash_djb2_one_32(
  403. (uint32_t)OS::get_singleton()->get_unix_time(), hash);
  404. hash = hash_djb2_one_32(
  405. (uint32_t)OS::get_singleton()->get_user_data_dir().hash64(), hash);
  406. hash = hash_djb2_one_32(
  407. (uint32_t)((uint64_t)this), hash); // Rely on ASLR heap
  408. hash = hash_djb2_one_32(
  409. (uint32_t)((uint64_t)&hash), hash); // Rely on ASLR stack
  410. hash = hash & 0x7FFFFFFF; // Make it compatible with unsigned, since negative ID is used for exclusion
  411. }
  412. return hash;
  413. }
  414. int NetworkedMultiplayerENet::get_unique_id() const {
  415. ERR_FAIL_COND_V(!active, 0);
  416. return unique_id;
  417. }
  418. void NetworkedMultiplayerENet::set_refuse_new_connections(bool p_enable) {
  419. refuse_connections = p_enable;
  420. }
  421. bool NetworkedMultiplayerENet::is_refusing_new_connections() const {
  422. return refuse_connections;
  423. }
  424. void NetworkedMultiplayerENet::set_compression_mode(CompressionMode p_mode) {
  425. compression_mode = p_mode;
  426. }
  427. NetworkedMultiplayerENet::CompressionMode NetworkedMultiplayerENet::get_compression_mode() const {
  428. return compression_mode;
  429. }
  430. size_t NetworkedMultiplayerENet::enet_compress(void *context, const ENetBuffer *inBuffers, size_t inBufferCount, size_t inLimit, enet_uint8 *outData, size_t outLimit) {
  431. NetworkedMultiplayerENet *enet = (NetworkedMultiplayerENet *)(context);
  432. if (size_t(enet->src_compressor_mem.size()) < inLimit) {
  433. enet->src_compressor_mem.resize(inLimit);
  434. }
  435. int total = inLimit;
  436. int ofs = 0;
  437. while (total) {
  438. for (size_t i = 0; i < inBufferCount; i++) {
  439. int to_copy = MIN(total, int(inBuffers[i].dataLength));
  440. copymem(&enet->src_compressor_mem[ofs], inBuffers[i].data, to_copy);
  441. ofs += to_copy;
  442. total -= to_copy;
  443. }
  444. }
  445. Compression::Mode mode;
  446. switch (enet->compression_mode) {
  447. case COMPRESS_FASTLZ: {
  448. mode = Compression::MODE_FASTLZ;
  449. } break;
  450. case COMPRESS_ZLIB: {
  451. mode = Compression::MODE_DEFLATE;
  452. } break;
  453. case COMPRESS_ZSTD: {
  454. mode = Compression::MODE_ZSTD;
  455. } break;
  456. default: { ERR_FAIL_V(0); }
  457. }
  458. int req_size = Compression::get_max_compressed_buffer_size(ofs, mode);
  459. if (enet->dst_compressor_mem.size() < req_size) {
  460. enet->dst_compressor_mem.resize(req_size);
  461. }
  462. int ret = Compression::compress(enet->dst_compressor_mem.ptrw(), enet->src_compressor_mem.ptr(), ofs, mode);
  463. if (ret < 0)
  464. return 0;
  465. if (ret > int(outLimit))
  466. return 0; // Do not bother
  467. copymem(outData, enet->dst_compressor_mem.ptr(), ret);
  468. return ret;
  469. }
  470. size_t NetworkedMultiplayerENet::enet_decompress(void *context, const enet_uint8 *inData, size_t inLimit, enet_uint8 *outData, size_t outLimit) {
  471. NetworkedMultiplayerENet *enet = (NetworkedMultiplayerENet *)(context);
  472. int ret = -1;
  473. switch (enet->compression_mode) {
  474. case COMPRESS_FASTLZ: {
  475. ret = Compression::decompress(outData, outLimit, inData, inLimit, Compression::MODE_FASTLZ);
  476. } break;
  477. case COMPRESS_ZLIB: {
  478. ret = Compression::decompress(outData, outLimit, inData, inLimit, Compression::MODE_DEFLATE);
  479. } break;
  480. case COMPRESS_ZSTD: {
  481. ret = Compression::decompress(outData, outLimit, inData, inLimit, Compression::MODE_ZSTD);
  482. } break;
  483. default: {}
  484. }
  485. if (ret < 0) {
  486. return 0;
  487. } else {
  488. return ret;
  489. }
  490. }
  491. void NetworkedMultiplayerENet::_setup_compressor() {
  492. switch (compression_mode) {
  493. case COMPRESS_NONE: {
  494. enet_host_compress(host, NULL);
  495. } break;
  496. case COMPRESS_RANGE_CODER: {
  497. enet_host_compress_with_range_coder(host);
  498. } break;
  499. case COMPRESS_FASTLZ:
  500. case COMPRESS_ZLIB:
  501. case COMPRESS_ZSTD: {
  502. enet_host_compress(host, &enet_compressor);
  503. } break;
  504. }
  505. }
  506. void NetworkedMultiplayerENet::enet_compressor_destroy(void *context) {
  507. //do none
  508. }
  509. void NetworkedMultiplayerENet::_bind_methods() {
  510. ClassDB::bind_method(D_METHOD("create_server", "port", "max_clients", "in_bandwidth", "out_bandwidth"), &NetworkedMultiplayerENet::create_server, DEFVAL(32), DEFVAL(0), DEFVAL(0));
  511. ClassDB::bind_method(D_METHOD("create_client", "ip", "port", "in_bandwidth", "out_bandwidth"), &NetworkedMultiplayerENet::create_client, DEFVAL(0), DEFVAL(0));
  512. ClassDB::bind_method(D_METHOD("close_connection"), &NetworkedMultiplayerENet::close_connection);
  513. ClassDB::bind_method(D_METHOD("disconnect_peer", "id", "now"), &NetworkedMultiplayerENet::disconnect_peer, DEFVAL(false));
  514. ClassDB::bind_method(D_METHOD("set_compression_mode", "mode"), &NetworkedMultiplayerENet::set_compression_mode);
  515. ClassDB::bind_method(D_METHOD("get_compression_mode"), &NetworkedMultiplayerENet::get_compression_mode);
  516. ClassDB::bind_method(D_METHOD("set_bind_ip", "ip"), &NetworkedMultiplayerENet::set_bind_ip);
  517. ADD_PROPERTY(PropertyInfo(Variant::INT, "compression_mode", PROPERTY_HINT_ENUM, "None,Range Coder,FastLZ,ZLib,ZStd"), "set_compression_mode", "get_compression_mode");
  518. BIND_ENUM_CONSTANT(COMPRESS_NONE);
  519. BIND_ENUM_CONSTANT(COMPRESS_RANGE_CODER);
  520. BIND_ENUM_CONSTANT(COMPRESS_FASTLZ);
  521. BIND_ENUM_CONSTANT(COMPRESS_ZLIB);
  522. BIND_ENUM_CONSTANT(COMPRESS_ZSTD);
  523. }
  524. NetworkedMultiplayerENet::NetworkedMultiplayerENet() {
  525. active = false;
  526. server = false;
  527. refuse_connections = false;
  528. unique_id = 0;
  529. target_peer = 0;
  530. current_packet.packet = NULL;
  531. transfer_mode = TRANSFER_MODE_RELIABLE;
  532. connection_status = CONNECTION_DISCONNECTED;
  533. compression_mode = COMPRESS_NONE;
  534. enet_compressor.context = this;
  535. enet_compressor.compress = enet_compress;
  536. enet_compressor.decompress = enet_decompress;
  537. enet_compressor.destroy = enet_compressor_destroy;
  538. bind_ip = IP_Address("*");
  539. }
  540. NetworkedMultiplayerENet::~NetworkedMultiplayerENet() {
  541. close_connection();
  542. }
  543. // Sets IP for ENet to bind when using create_server
  544. // if no IP is set, then ENet bind to ENET_HOST_ANY
  545. void NetworkedMultiplayerENet::set_bind_ip(const IP_Address &p_ip) {
  546. ERR_FAIL_COND(!p_ip.is_valid() && !p_ip.is_wildcard());
  547. bind_ip = p_ip;
  548. }