networked_multiplayer_enet.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*************************************************************************/
  2. /* networked_multiplayer_enet.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "networked_multiplayer_enet.h"
  30. #include "io/marshalls.h"
  31. #include "os/os.h"
  32. void NetworkedMultiplayerENet::set_transfer_mode(TransferMode p_mode) {
  33. transfer_mode = p_mode;
  34. }
  35. void NetworkedMultiplayerENet::set_target_peer(int p_peer) {
  36. target_peer = p_peer;
  37. }
  38. int NetworkedMultiplayerENet::get_packet_peer() const {
  39. ERR_FAIL_COND_V(!active, 1);
  40. ERR_FAIL_COND_V(incoming_packets.size() == 0, 1);
  41. return incoming_packets.front()->get().from;
  42. }
  43. Error NetworkedMultiplayerENet::create_server(int p_port, int p_max_clients, int p_in_bandwidth, int p_out_bandwidth) {
  44. ERR_FAIL_COND_V(active, ERR_ALREADY_IN_USE);
  45. ENetAddress address;
  46. #ifdef GODOT_ENET
  47. if (bind_ip.is_wildcard()) {
  48. address.wildcard = 1;
  49. } else {
  50. enet_address_set_ip(&address, bind_ip.get_ipv6(), 16);
  51. }
  52. #else
  53. if (bind_ip.is_wildcard()) {
  54. address.host = 0;
  55. } else {
  56. ERR_FAIL_COND_V(!bind_ip.is_ipv4(), ERR_INVALID_PARAMETER);
  57. address.host = *(uint32_t *)bind_ip.get_ipv4();
  58. }
  59. #endif
  60. address.port = p_port;
  61. host = enet_host_create(&address /* the address to bind the server host to */,
  62. p_max_clients /* allow up to 32 clients and/or outgoing connections */,
  63. SYSCH_MAX /* allow up to SYSCH_MAX channels to be used */,
  64. p_in_bandwidth /* assume any amount of incoming bandwidth */,
  65. p_out_bandwidth /* assume any amount of outgoing bandwidth */);
  66. ERR_FAIL_COND_V(!host, ERR_CANT_CREATE);
  67. _setup_compressor();
  68. active = true;
  69. server = true;
  70. refuse_connections = false;
  71. unique_id = 1;
  72. connection_status = CONNECTION_CONNECTED;
  73. return OK;
  74. }
  75. Error NetworkedMultiplayerENet::create_client(const IP_Address &p_ip, int p_port, int p_in_bandwidth, int p_out_bandwidth) {
  76. ERR_FAIL_COND_V(active, ERR_ALREADY_IN_USE);
  77. host = enet_host_create(NULL /* create a client host */,
  78. 1 /* only allow 1 outgoing connection */,
  79. SYSCH_MAX /* allow up to SYSCH_MAX channels to be used */,
  80. p_in_bandwidth /* 56K modem with 56 Kbps downstream bandwidth */,
  81. p_out_bandwidth /* 56K modem with 14 Kbps upstream bandwidth */);
  82. ERR_FAIL_COND_V(!host, ERR_CANT_CREATE);
  83. _setup_compressor();
  84. ENetAddress address;
  85. #ifdef GODOT_ENET
  86. enet_address_set_ip(&address, p_ip.get_ipv6(), 16);
  87. #else
  88. ERR_FAIL_COND_V(!p_ip.is_ipv4(), ERR_INVALID_PARAMETER);
  89. address.host = *(uint32_t *)p_ip.get_ipv4();
  90. #endif
  91. address.port = p_port;
  92. //enet_address_set_host (& address, "localhost");
  93. //address.port = p_port;
  94. unique_id = _gen_unique_id();
  95. /* Initiate the connection, allocating the enough channels */
  96. ENetPeer *peer = enet_host_connect(host, &address, SYSCH_MAX, unique_id);
  97. if (peer == NULL) {
  98. enet_host_destroy(host);
  99. ERR_FAIL_COND_V(!peer, ERR_CANT_CREATE);
  100. }
  101. //technically safe to ignore the peer or anything else.
  102. connection_status = CONNECTION_CONNECTING;
  103. active = true;
  104. server = false;
  105. refuse_connections = false;
  106. return OK;
  107. }
  108. void NetworkedMultiplayerENet::poll() {
  109. ERR_FAIL_COND(!active);
  110. _pop_current_packet();
  111. ENetEvent event;
  112. /* Wait up to 1000 milliseconds for an event. */
  113. while (true) {
  114. if (!host || !active) //might have been disconnected while emitting a notification
  115. return;
  116. int ret = enet_host_service(host, &event, 1);
  117. if (ret < 0) {
  118. //error, do something?
  119. break;
  120. } else if (ret == 0) {
  121. break;
  122. }
  123. switch (event.type) {
  124. case ENET_EVENT_TYPE_CONNECT: {
  125. /* Store any relevant client information here. */
  126. if (server && refuse_connections) {
  127. enet_peer_reset(event.peer);
  128. break;
  129. }
  130. int *new_id = memnew(int);
  131. *new_id = event.data;
  132. if (*new_id == 0) { //data zero is sent by server (enet won't let you configure this). Server is always 1
  133. *new_id = 1;
  134. }
  135. event.peer->data = new_id;
  136. peer_map[*new_id] = event.peer;
  137. connection_status = CONNECTION_CONNECTED; //if connecting, this means it connected t something!
  138. emit_signal("peer_connected", *new_id);
  139. if (server) {
  140. //someone connected, let it know of all the peers available
  141. for (Map<int, ENetPeer *>::Element *E = peer_map.front(); E; E = E->next()) {
  142. if (E->key() == *new_id)
  143. continue;
  144. //send existing peers to new peer
  145. ENetPacket *packet = enet_packet_create(NULL, 8, ENET_PACKET_FLAG_RELIABLE);
  146. encode_uint32(SYSMSG_ADD_PEER, &packet->data[0]);
  147. encode_uint32(E->key(), &packet->data[4]);
  148. enet_peer_send(event.peer, SYSCH_CONFIG, packet);
  149. //send the new peer to existing peers
  150. packet = enet_packet_create(NULL, 8, ENET_PACKET_FLAG_RELIABLE);
  151. encode_uint32(SYSMSG_ADD_PEER, &packet->data[0]);
  152. encode_uint32(*new_id, &packet->data[4]);
  153. enet_peer_send(E->get(), SYSCH_CONFIG, packet);
  154. }
  155. } else {
  156. emit_signal("connection_succeeded");
  157. }
  158. } break;
  159. case ENET_EVENT_TYPE_DISCONNECT: {
  160. /* Reset the peer's client information. */
  161. int *id = (int *)event.peer->data;
  162. if (!id) {
  163. if (!server) {
  164. emit_signal("connection_failed");
  165. }
  166. } else {
  167. if (server) {
  168. //someone disconnected, let it know to everyone else
  169. for (Map<int, ENetPeer *>::Element *E = peer_map.front(); E; E = E->next()) {
  170. if (E->key() == *id)
  171. continue;
  172. //send the new peer to existing peers
  173. ENetPacket *packet = enet_packet_create(NULL, 8, ENET_PACKET_FLAG_RELIABLE);
  174. encode_uint32(SYSMSG_REMOVE_PEER, &packet->data[0]);
  175. encode_uint32(*id, &packet->data[4]);
  176. enet_peer_send(E->get(), SYSCH_CONFIG, packet);
  177. }
  178. } else if (!server) {
  179. emit_signal("server_disconnected");
  180. close_connection();
  181. return;
  182. }
  183. emit_signal("peer_disconnected", *id);
  184. peer_map.erase(*id);
  185. memdelete(id);
  186. }
  187. } break;
  188. case ENET_EVENT_TYPE_RECEIVE: {
  189. if (event.channelID == SYSCH_CONFIG) {
  190. //some config message
  191. ERR_CONTINUE(event.packet->dataLength < 8);
  192. // Only server can send config messages
  193. ERR_CONTINUE(server);
  194. int msg = decode_uint32(&event.packet->data[0]);
  195. int id = decode_uint32(&event.packet->data[4]);
  196. switch (msg) {
  197. case SYSMSG_ADD_PEER: {
  198. peer_map[id] = NULL;
  199. emit_signal("peer_connected", id);
  200. } break;
  201. case SYSMSG_REMOVE_PEER: {
  202. peer_map.erase(id);
  203. emit_signal("peer_disconnected", id);
  204. } break;
  205. }
  206. enet_packet_destroy(event.packet);
  207. } else if (event.channelID < SYSCH_MAX) {
  208. Packet packet;
  209. packet.packet = event.packet;
  210. uint32_t *id = (uint32_t *)event.peer->data;
  211. ERR_CONTINUE(event.packet->dataLength < 12)
  212. uint32_t source = decode_uint32(&event.packet->data[0]);
  213. int target = decode_uint32(&event.packet->data[4]);
  214. uint32_t flags = decode_uint32(&event.packet->data[8]);
  215. packet.from = source;
  216. if (server) {
  217. // Someone is cheating and trying to fake the source!
  218. ERR_CONTINUE(source != *id);
  219. packet.from = *id;
  220. if (target == 0) {
  221. //re-send the everyone but sender :|
  222. incoming_packets.push_back(packet);
  223. //and make copies for sending
  224. for (Map<int, ENetPeer *>::Element *E = peer_map.front(); E; E = E->next()) {
  225. if (uint32_t(E->key()) == source) //do not resend to self
  226. continue;
  227. ENetPacket *packet2 = enet_packet_create(packet.packet->data, packet.packet->dataLength, flags);
  228. enet_peer_send(E->get(), event.channelID, packet2);
  229. }
  230. } else if (target < 0) {
  231. //to all but one
  232. //and make copies for sending
  233. for (Map<int, ENetPeer *>::Element *E = peer_map.front(); E; E = E->next()) {
  234. if (uint32_t(E->key()) == source || E->key() == -target) //do not resend to self, also do not send to excluded
  235. continue;
  236. ENetPacket *packet2 = enet_packet_create(packet.packet->data, packet.packet->dataLength, flags);
  237. enet_peer_send(E->get(), event.channelID, packet2);
  238. }
  239. if (-target != 1) {
  240. //server is not excluded
  241. incoming_packets.push_back(packet);
  242. } else {
  243. //server is excluded, erase packet
  244. enet_packet_destroy(packet.packet);
  245. }
  246. } else if (target == 1) {
  247. //to myself and only myself
  248. incoming_packets.push_back(packet);
  249. } else {
  250. //to someone else, specifically
  251. ERR_CONTINUE(!peer_map.has(target));
  252. enet_peer_send(peer_map[target], event.channelID, packet.packet);
  253. }
  254. } else {
  255. incoming_packets.push_back(packet);
  256. }
  257. //destroy packet later..
  258. } else {
  259. ERR_CONTINUE(true);
  260. }
  261. } break;
  262. case ENET_EVENT_TYPE_NONE: {
  263. //do nothing
  264. } break;
  265. }
  266. }
  267. }
  268. bool NetworkedMultiplayerENet::is_server() const {
  269. ERR_FAIL_COND_V(!active, false);
  270. return server;
  271. }
  272. void NetworkedMultiplayerENet::close_connection() {
  273. if (!active)
  274. return;
  275. _pop_current_packet();
  276. bool peers_disconnected = false;
  277. for (Map<int, ENetPeer *>::Element *E = peer_map.front(); E; E = E->next()) {
  278. if (E->get()) {
  279. enet_peer_disconnect_now(E->get(), unique_id);
  280. peers_disconnected = true;
  281. }
  282. }
  283. if (peers_disconnected) {
  284. enet_host_flush(host);
  285. OS::get_singleton()->delay_usec(100); //wait 100ms for disconnection packets to send
  286. }
  287. enet_host_destroy(host);
  288. active = false;
  289. incoming_packets.clear();
  290. unique_id = 1; //server is 1
  291. connection_status = CONNECTION_DISCONNECTED;
  292. }
  293. int NetworkedMultiplayerENet::get_available_packet_count() const {
  294. return incoming_packets.size();
  295. }
  296. Error NetworkedMultiplayerENet::get_packet(const uint8_t **r_buffer, int &r_buffer_size) const {
  297. ERR_FAIL_COND_V(incoming_packets.size() == 0, ERR_UNAVAILABLE);
  298. _pop_current_packet();
  299. current_packet = incoming_packets.front()->get();
  300. incoming_packets.pop_front();
  301. *r_buffer = (const uint8_t *)(&current_packet.packet->data[12]);
  302. r_buffer_size = current_packet.packet->dataLength - 12;
  303. return OK;
  304. }
  305. Error NetworkedMultiplayerENet::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
  306. ERR_FAIL_COND_V(!active, ERR_UNCONFIGURED);
  307. ERR_FAIL_COND_V(connection_status != CONNECTION_CONNECTED, ERR_UNCONFIGURED);
  308. int packet_flags = 0;
  309. int channel = SYSCH_RELIABLE;
  310. switch (transfer_mode) {
  311. case TRANSFER_MODE_UNRELIABLE: {
  312. packet_flags = ENET_PACKET_FLAG_UNSEQUENCED;
  313. channel = SYSCH_UNRELIABLE;
  314. } break;
  315. case TRANSFER_MODE_UNRELIABLE_ORDERED: {
  316. packet_flags = 0;
  317. channel = SYSCH_UNRELIABLE;
  318. } break;
  319. case TRANSFER_MODE_RELIABLE: {
  320. packet_flags = ENET_PACKET_FLAG_RELIABLE;
  321. channel = SYSCH_RELIABLE;
  322. } break;
  323. }
  324. Map<int, ENetPeer *>::Element *E = NULL;
  325. if (target_peer != 0) {
  326. E = peer_map.find(ABS(target_peer));
  327. if (!E) {
  328. ERR_EXPLAIN("Invalid Target Peer: " + itos(target_peer));
  329. ERR_FAIL_V(ERR_INVALID_PARAMETER);
  330. }
  331. }
  332. ENetPacket *packet = enet_packet_create(NULL, p_buffer_size + 12, packet_flags);
  333. encode_uint32(unique_id, &packet->data[0]); //source ID
  334. encode_uint32(target_peer, &packet->data[4]); //dest ID
  335. encode_uint32(packet_flags, &packet->data[8]); //dest ID
  336. copymem(&packet->data[12], p_buffer, p_buffer_size);
  337. if (server) {
  338. if (target_peer == 0) {
  339. enet_host_broadcast(host, channel, packet);
  340. } else if (target_peer < 0) {
  341. //send to all but one
  342. //and make copies for sending
  343. int exclude = -target_peer;
  344. for (Map<int, ENetPeer *>::Element *F = peer_map.front(); F; F = F->next()) {
  345. if (F->key() == exclude) // exclude packet
  346. continue;
  347. ENetPacket *packet2 = enet_packet_create(packet->data, packet->dataLength, packet_flags);
  348. enet_peer_send(F->get(), channel, packet2);
  349. }
  350. enet_packet_destroy(packet); //original packet no longer needed
  351. } else {
  352. enet_peer_send(E->get(), channel, packet);
  353. }
  354. } else {
  355. ERR_FAIL_COND_V(!peer_map.has(1), ERR_BUG);
  356. enet_peer_send(peer_map[1], channel, packet); //send to server for broadcast..
  357. }
  358. enet_host_flush(host);
  359. return OK;
  360. }
  361. int NetworkedMultiplayerENet::get_max_packet_size() const {
  362. return 1 << 24; //anything is good
  363. }
  364. void NetworkedMultiplayerENet::_pop_current_packet() const {
  365. if (current_packet.packet) {
  366. enet_packet_destroy(current_packet.packet);
  367. current_packet.packet = NULL;
  368. current_packet.from = 0;
  369. }
  370. }
  371. NetworkedMultiplayerPeer::ConnectionStatus NetworkedMultiplayerENet::get_connection_status() const {
  372. return connection_status;
  373. }
  374. uint32_t NetworkedMultiplayerENet::_gen_unique_id() const {
  375. uint32_t hash = 0;
  376. while (hash == 0 || hash == 1) {
  377. hash = hash_djb2_one_32(
  378. (uint32_t)OS::get_singleton()->get_ticks_usec());
  379. hash = hash_djb2_one_32(
  380. (uint32_t)OS::get_singleton()->get_unix_time(), hash);
  381. hash = hash_djb2_one_32(
  382. (uint32_t)OS::get_singleton()->get_data_dir().hash64(), hash);
  383. /*
  384. hash = hash_djb2_one_32(
  385. (uint32_t)OS::get_singleton()->get_unique_ID().hash64(), hash );
  386. */
  387. hash = hash_djb2_one_32(
  388. (uint32_t)((uint64_t)this), hash); //rely on aslr heap
  389. hash = hash_djb2_one_32(
  390. (uint32_t)((uint64_t)&hash), hash); //rely on aslr stack
  391. hash = hash & 0x7FFFFFFF; // make it compatible with unsigned, since negatie id is used for exclusion
  392. }
  393. return hash;
  394. }
  395. int NetworkedMultiplayerENet::get_unique_id() const {
  396. ERR_FAIL_COND_V(!active, 0);
  397. return unique_id;
  398. }
  399. void NetworkedMultiplayerENet::set_refuse_new_connections(bool p_enable) {
  400. refuse_connections = p_enable;
  401. }
  402. bool NetworkedMultiplayerENet::is_refusing_new_connections() const {
  403. return refuse_connections;
  404. }
  405. void NetworkedMultiplayerENet::set_compression_mode(CompressionMode p_mode) {
  406. compression_mode = p_mode;
  407. }
  408. NetworkedMultiplayerENet::CompressionMode NetworkedMultiplayerENet::get_compression_mode() const {
  409. return compression_mode;
  410. }
  411. size_t NetworkedMultiplayerENet::enet_compress(void *context, const ENetBuffer *inBuffers, size_t inBufferCount, size_t inLimit, enet_uint8 *outData, size_t outLimit) {
  412. NetworkedMultiplayerENet *enet = (NetworkedMultiplayerENet *)(context);
  413. if (size_t(enet->src_compressor_mem.size()) < inLimit) {
  414. enet->src_compressor_mem.resize(inLimit);
  415. }
  416. int total = inLimit;
  417. int ofs = 0;
  418. while (total) {
  419. for (size_t i = 0; i < inBufferCount; i++) {
  420. int to_copy = MIN(total, int(inBuffers[i].dataLength));
  421. copymem(&enet->src_compressor_mem[ofs], inBuffers[i].data, to_copy);
  422. ofs += to_copy;
  423. total -= to_copy;
  424. }
  425. }
  426. Compression::Mode mode;
  427. switch (enet->compression_mode) {
  428. case COMPRESS_FASTLZ: {
  429. mode = Compression::MODE_FASTLZ;
  430. } break;
  431. case COMPRESS_ZLIB: {
  432. mode = Compression::MODE_DEFLATE;
  433. } break;
  434. default: { ERR_FAIL_V(0); }
  435. }
  436. int req_size = Compression::get_max_compressed_buffer_size(ofs, mode);
  437. if (enet->dst_compressor_mem.size() < req_size) {
  438. enet->dst_compressor_mem.resize(req_size);
  439. }
  440. int ret = Compression::compress(enet->dst_compressor_mem.ptr(), enet->src_compressor_mem.ptr(), ofs, mode);
  441. if (ret < 0)
  442. return 0;
  443. if (ret > int(outLimit))
  444. return 0; //do not bother
  445. copymem(outData, enet->dst_compressor_mem.ptr(), ret);
  446. return ret;
  447. }
  448. size_t NetworkedMultiplayerENet::enet_decompress(void *context, const enet_uint8 *inData, size_t inLimit, enet_uint8 *outData, size_t outLimit) {
  449. NetworkedMultiplayerENet *enet = (NetworkedMultiplayerENet *)(context);
  450. int ret = -1;
  451. switch (enet->compression_mode) {
  452. case COMPRESS_FASTLZ: {
  453. ret = Compression::decompress(outData, outLimit, inData, inLimit, Compression::MODE_FASTLZ);
  454. } break;
  455. case COMPRESS_ZLIB: {
  456. ret = Compression::decompress(outData, outLimit, inData, inLimit, Compression::MODE_DEFLATE);
  457. } break;
  458. default: {}
  459. }
  460. if (ret < 0) {
  461. return 0;
  462. } else {
  463. return ret;
  464. }
  465. }
  466. void NetworkedMultiplayerENet::_setup_compressor() {
  467. switch (compression_mode) {
  468. case COMPRESS_NONE: {
  469. enet_host_compress(host, NULL);
  470. } break;
  471. case COMPRESS_RANGE_CODER: {
  472. enet_host_compress_with_range_coder(host);
  473. } break;
  474. case COMPRESS_FASTLZ:
  475. case COMPRESS_ZLIB: {
  476. enet_host_compress(host, &enet_compressor);
  477. } break;
  478. }
  479. }
  480. void NetworkedMultiplayerENet::enet_compressor_destroy(void *context) {
  481. //do none
  482. }
  483. void NetworkedMultiplayerENet::_bind_methods() {
  484. ClassDB::bind_method(D_METHOD("create_server", "port", "max_clients", "in_bandwidth", "out_bandwidth"), &NetworkedMultiplayerENet::create_server, DEFVAL(32), DEFVAL(0), DEFVAL(0));
  485. ClassDB::bind_method(D_METHOD("create_client", "ip", "port", "in_bandwidth", "out_bandwidth"), &NetworkedMultiplayerENet::create_client, DEFVAL(0), DEFVAL(0));
  486. ClassDB::bind_method(D_METHOD("close_connection"), &NetworkedMultiplayerENet::close_connection);
  487. ClassDB::bind_method(D_METHOD("set_compression_mode", "mode"), &NetworkedMultiplayerENet::set_compression_mode);
  488. ClassDB::bind_method(D_METHOD("get_compression_mode"), &NetworkedMultiplayerENet::get_compression_mode);
  489. ClassDB::bind_method(D_METHOD("set_bind_ip", "ip"), &NetworkedMultiplayerENet::set_bind_ip);
  490. BIND_CONSTANT(COMPRESS_NONE);
  491. BIND_CONSTANT(COMPRESS_RANGE_CODER);
  492. BIND_CONSTANT(COMPRESS_FASTLZ);
  493. BIND_CONSTANT(COMPRESS_ZLIB);
  494. }
  495. NetworkedMultiplayerENet::NetworkedMultiplayerENet() {
  496. active = false;
  497. server = false;
  498. refuse_connections = false;
  499. unique_id = 0;
  500. target_peer = 0;
  501. current_packet.packet = NULL;
  502. transfer_mode = TRANSFER_MODE_RELIABLE;
  503. connection_status = CONNECTION_DISCONNECTED;
  504. compression_mode = COMPRESS_NONE;
  505. enet_compressor.context = this;
  506. enet_compressor.compress = enet_compress;
  507. enet_compressor.decompress = enet_decompress;
  508. enet_compressor.destroy = enet_compressor_destroy;
  509. bind_ip = IP_Address("*");
  510. }
  511. NetworkedMultiplayerENet::~NetworkedMultiplayerENet() {
  512. close_connection();
  513. }
  514. // sets IP for ENet to bind when using create_server
  515. // if no IP is set, then ENet bind to ENET_HOST_ANY
  516. void NetworkedMultiplayerENet::set_bind_ip(const IP_Address &p_ip) {
  517. ERR_FAIL_COND(!p_ip.is_valid() && !p_ip.is_wildcard());
  518. bind_ip = p_ip;
  519. }