networked_multiplayer_enet.cpp 20 KB

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