networked_multiplayer_enet.cpp 20 KB

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