networked_multiplayer_enet.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. /* Copyright (c) 2014-2017 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. void NetworkedMultiplayerENet::set_target_peer(int p_peer) {
  37. target_peer = p_peer;
  38. }
  39. int NetworkedMultiplayerENet::get_packet_peer() const {
  40. ERR_FAIL_COND_V(!active, 1);
  41. ERR_FAIL_COND_V(incoming_packets.size() == 0, 1);
  42. return incoming_packets.front()->get().from;
  43. }
  44. Error NetworkedMultiplayerENet::create_server(int p_port, int p_max_clients, int p_in_bandwidth, int p_out_bandwidth) {
  45. ERR_FAIL_COND_V(active, ERR_ALREADY_IN_USE);
  46. ENetAddress address;
  47. #ifdef GODOT_ENET
  48. if (bind_ip.is_wildcard()) {
  49. address.wildcard = 1;
  50. } else {
  51. enet_address_set_ip(&address, bind_ip.get_ipv6(), 16);
  52. }
  53. #else
  54. if (bind_ip.is_wildcard()) {
  55. address.host = 0;
  56. } else {
  57. ERR_FAIL_COND_V(!bind_ip.is_ipv4(), ERR_INVALID_PARAMETER);
  58. address.host = *(uint32_t *)bind_ip.get_ipv4();
  59. }
  60. #endif
  61. address.port = p_port;
  62. host = enet_host_create(&address /* the address to bind the server host to */,
  63. p_max_clients /* allow up to 32 clients and/or outgoing connections */,
  64. SYSCH_MAX /* allow up to SYSCH_MAX channels to be used */,
  65. p_in_bandwidth /* assume any amount of incoming bandwidth */,
  66. p_out_bandwidth /* assume any amount of outgoing bandwidth */);
  67. ERR_FAIL_COND_V(!host, ERR_CANT_CREATE);
  68. _setup_compressor();
  69. active = true;
  70. server = true;
  71. refuse_connections = false;
  72. unique_id = 1;
  73. connection_status = CONNECTION_CONNECTED;
  74. return OK;
  75. }
  76. Error NetworkedMultiplayerENet::create_client(const IP_Address &p_ip, int p_port, int p_in_bandwidth, int p_out_bandwidth) {
  77. ERR_FAIL_COND_V(active, ERR_ALREADY_IN_USE);
  78. host = enet_host_create(NULL /* create a client host */,
  79. 1 /* only allow 1 outgoing connection */,
  80. SYSCH_MAX /* allow up to SYSCH_MAX channels to be used */,
  81. p_in_bandwidth /* 56K modem with 56 Kbps downstream bandwidth */,
  82. p_out_bandwidth /* 56K modem with 14 Kbps upstream bandwidth */);
  83. ERR_FAIL_COND_V(!host, ERR_CANT_CREATE);
  84. _setup_compressor();
  85. ENetAddress address;
  86. #ifdef GODOT_ENET
  87. enet_address_set_ip(&address, p_ip.get_ipv6(), 16);
  88. #else
  89. ERR_FAIL_COND_V(!p_ip.is_ipv4(), ERR_INVALID_PARAMETER);
  90. address.host = *(uint32_t *)p_ip.get_ipv4();
  91. #endif
  92. address.port = p_port;
  93. //enet_address_set_host (& address, "localhost");
  94. //address.port = p_port;
  95. unique_id = _gen_unique_id();
  96. /* Initiate the connection, allocating the enough channels */
  97. ENetPeer *peer = enet_host_connect(host, &address, SYSCH_MAX, unique_id);
  98. if (peer == NULL) {
  99. enet_host_destroy(host);
  100. ERR_FAIL_COND_V(!peer, ERR_CANT_CREATE);
  101. }
  102. //technically safe to ignore the peer or anything else.
  103. connection_status = CONNECTION_CONNECTING;
  104. active = true;
  105. server = false;
  106. refuse_connections = false;
  107. return OK;
  108. }
  109. void NetworkedMultiplayerENet::poll() {
  110. ERR_FAIL_COND(!active);
  111. _pop_current_packet();
  112. ENetEvent event;
  113. /* Wait up to 1000 milliseconds for an event. */
  114. while (true) {
  115. if (!host || !active) //might have been disconnected while emitting a notification
  116. return;
  117. int ret = enet_host_service(host, &event, 1);
  118. if (ret < 0) {
  119. //error, do something?
  120. break;
  121. } else if (ret == 0) {
  122. break;
  123. }
  124. switch (event.type) {
  125. case ENET_EVENT_TYPE_CONNECT: {
  126. /* Store any relevant client information here. */
  127. if (server && refuse_connections) {
  128. enet_peer_reset(event.peer);
  129. break;
  130. }
  131. int *new_id = memnew(int);
  132. *new_id = event.data;
  133. if (*new_id == 0) { //data zero is sent by server (enet won't let you configure this). Server is always 1
  134. *new_id = 1;
  135. }
  136. event.peer->data = new_id;
  137. peer_map[*new_id] = event.peer;
  138. connection_status = CONNECTION_CONNECTED; //if connecting, this means it connected t something!
  139. emit_signal("peer_connected", *new_id);
  140. if (server) {
  141. //someone connected, let it know of all the peers available
  142. for (Map<int, ENetPeer *>::Element *E = peer_map.front(); E; E = E->next()) {
  143. if (E->key() == *new_id)
  144. continue;
  145. //send existing peers to new peer
  146. ENetPacket *packet = enet_packet_create(NULL, 8, ENET_PACKET_FLAG_RELIABLE);
  147. encode_uint32(SYSMSG_ADD_PEER, &packet->data[0]);
  148. encode_uint32(E->key(), &packet->data[4]);
  149. enet_peer_send(event.peer, SYSCH_CONFIG, packet);
  150. //send the new peer to existing peers
  151. packet = enet_packet_create(NULL, 8, ENET_PACKET_FLAG_RELIABLE);
  152. encode_uint32(SYSMSG_ADD_PEER, &packet->data[0]);
  153. encode_uint32(*new_id, &packet->data[4]);
  154. enet_peer_send(E->get(), SYSCH_CONFIG, packet);
  155. }
  156. } else {
  157. emit_signal("connection_succeeded");
  158. }
  159. } break;
  160. case ENET_EVENT_TYPE_DISCONNECT: {
  161. /* Reset the peer's client information. */
  162. int *id = (int *)event.peer->data;
  163. if (!id) {
  164. if (!server) {
  165. emit_signal("connection_failed");
  166. }
  167. } else {
  168. if (server) {
  169. //someone disconnected, let it know to everyone else
  170. for (Map<int, ENetPeer *>::Element *E = peer_map.front(); E; E = E->next()) {
  171. if (E->key() == *id)
  172. continue;
  173. //send the new peer to existing peers
  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 the 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. int NetworkedMultiplayerENet::get_available_packet_count() const {
  295. return incoming_packets.size();
  296. }
  297. Error NetworkedMultiplayerENet::get_packet(const uint8_t **r_buffer, int &r_buffer_size) const {
  298. ERR_FAIL_COND_V(incoming_packets.size() == 0, ERR_UNAVAILABLE);
  299. _pop_current_packet();
  300. current_packet = incoming_packets.front()->get();
  301. incoming_packets.pop_front();
  302. *r_buffer = (const uint8_t *)(&current_packet.packet->data[12]);
  303. r_buffer_size = current_packet.packet->dataLength - 12;
  304. return OK;
  305. }
  306. Error NetworkedMultiplayerENet::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
  307. ERR_FAIL_COND_V(!active, ERR_UNCONFIGURED);
  308. ERR_FAIL_COND_V(connection_status != CONNECTION_CONNECTED, ERR_UNCONFIGURED);
  309. int packet_flags = 0;
  310. int channel = SYSCH_RELIABLE;
  311. switch (transfer_mode) {
  312. case TRANSFER_MODE_UNRELIABLE: {
  313. packet_flags = ENET_PACKET_FLAG_UNSEQUENCED;
  314. channel = SYSCH_UNRELIABLE;
  315. } break;
  316. case TRANSFER_MODE_UNRELIABLE_ORDERED: {
  317. packet_flags = 0;
  318. channel = SYSCH_UNRELIABLE;
  319. } break;
  320. case TRANSFER_MODE_RELIABLE: {
  321. packet_flags = ENET_PACKET_FLAG_RELIABLE;
  322. channel = SYSCH_RELIABLE;
  323. } break;
  324. }
  325. Map<int, ENetPeer *>::Element *E = NULL;
  326. if (target_peer != 0) {
  327. E = peer_map.find(ABS(target_peer));
  328. if (!E) {
  329. ERR_EXPLAIN("Invalid Target Peer: " + itos(target_peer));
  330. ERR_FAIL_V(ERR_INVALID_PARAMETER);
  331. }
  332. }
  333. ENetPacket *packet = enet_packet_create(NULL, p_buffer_size + 12, packet_flags);
  334. encode_uint32(unique_id, &packet->data[0]); //source ID
  335. encode_uint32(target_peer, &packet->data[4]); //dest ID
  336. encode_uint32(packet_flags, &packet->data[8]); //dest ID
  337. copymem(&packet->data[12], p_buffer, p_buffer_size);
  338. if (server) {
  339. if (target_peer == 0) {
  340. enet_host_broadcast(host, channel, packet);
  341. } else if (target_peer < 0) {
  342. //send to all but one
  343. //and make copies for sending
  344. int exclude = -target_peer;
  345. for (Map<int, ENetPeer *>::Element *F = peer_map.front(); F; F = F->next()) {
  346. if (F->key() == exclude) // exclude packet
  347. continue;
  348. ENetPacket *packet2 = enet_packet_create(packet->data, packet->dataLength, packet_flags);
  349. enet_peer_send(F->get(), channel, packet2);
  350. }
  351. enet_packet_destroy(packet); //original packet no longer needed
  352. } else {
  353. enet_peer_send(E->get(), channel, packet);
  354. }
  355. } else {
  356. ERR_FAIL_COND_V(!peer_map.has(1), ERR_BUG);
  357. enet_peer_send(peer_map[1], channel, packet); //send to server for broadcast..
  358. }
  359. enet_host_flush(host);
  360. return OK;
  361. }
  362. int NetworkedMultiplayerENet::get_max_packet_size() const {
  363. return 1 << 24; //anything is good
  364. }
  365. void NetworkedMultiplayerENet::_pop_current_packet() const {
  366. if (current_packet.packet) {
  367. enet_packet_destroy(current_packet.packet);
  368. current_packet.packet = NULL;
  369. current_packet.from = 0;
  370. }
  371. }
  372. NetworkedMultiplayerPeer::ConnectionStatus NetworkedMultiplayerENet::get_connection_status() const {
  373. return connection_status;
  374. }
  375. uint32_t NetworkedMultiplayerENet::_gen_unique_id() const {
  376. uint32_t hash = 0;
  377. while (hash == 0 || hash == 1) {
  378. hash = hash_djb2_one_32(
  379. (uint32_t)OS::get_singleton()->get_ticks_usec());
  380. hash = hash_djb2_one_32(
  381. (uint32_t)OS::get_singleton()->get_unix_time(), hash);
  382. hash = hash_djb2_one_32(
  383. (uint32_t)OS::get_singleton()->get_data_dir().hash64(), hash);
  384. /*
  385. hash = hash_djb2_one_32(
  386. (uint32_t)OS::get_singleton()->get_unique_id().hash64(), hash );
  387. */
  388. hash = hash_djb2_one_32(
  389. (uint32_t)((uint64_t)this), hash); //rely on aslr heap
  390. hash = hash_djb2_one_32(
  391. (uint32_t)((uint64_t)&hash), hash); //rely on aslr stack
  392. hash = hash & 0x7FFFFFFF; // make it compatible with unsigned, since negatie id is used for exclusion
  393. }
  394. return hash;
  395. }
  396. int NetworkedMultiplayerENet::get_unique_id() const {
  397. ERR_FAIL_COND_V(!active, 0);
  398. return unique_id;
  399. }
  400. void NetworkedMultiplayerENet::set_refuse_new_connections(bool p_enable) {
  401. refuse_connections = p_enable;
  402. }
  403. bool NetworkedMultiplayerENet::is_refusing_new_connections() const {
  404. return refuse_connections;
  405. }
  406. void NetworkedMultiplayerENet::set_compression_mode(CompressionMode p_mode) {
  407. compression_mode = p_mode;
  408. }
  409. NetworkedMultiplayerENet::CompressionMode NetworkedMultiplayerENet::get_compression_mode() const {
  410. return compression_mode;
  411. }
  412. size_t NetworkedMultiplayerENet::enet_compress(void *context, const ENetBuffer *inBuffers, size_t inBufferCount, size_t inLimit, enet_uint8 *outData, size_t outLimit) {
  413. NetworkedMultiplayerENet *enet = (NetworkedMultiplayerENet *)(context);
  414. if (size_t(enet->src_compressor_mem.size()) < inLimit) {
  415. enet->src_compressor_mem.resize(inLimit);
  416. }
  417. int total = inLimit;
  418. int ofs = 0;
  419. while (total) {
  420. for (size_t i = 0; i < inBufferCount; i++) {
  421. int to_copy = MIN(total, int(inBuffers[i].dataLength));
  422. copymem(&enet->src_compressor_mem[ofs], inBuffers[i].data, to_copy);
  423. ofs += to_copy;
  424. total -= to_copy;
  425. }
  426. }
  427. Compression::Mode mode;
  428. switch (enet->compression_mode) {
  429. case COMPRESS_FASTLZ: {
  430. mode = Compression::MODE_FASTLZ;
  431. } break;
  432. case COMPRESS_ZLIB: {
  433. mode = Compression::MODE_DEFLATE;
  434. } break;
  435. case COMPRESS_ZSTD: {
  436. mode = Compression::MODE_ZSTD;
  437. } break;
  438. default: { ERR_FAIL_V(0); }
  439. }
  440. int req_size = Compression::get_max_compressed_buffer_size(ofs, mode);
  441. if (enet->dst_compressor_mem.size() < req_size) {
  442. enet->dst_compressor_mem.resize(req_size);
  443. }
  444. int ret = Compression::compress(enet->dst_compressor_mem.ptr(), enet->src_compressor_mem.ptr(), ofs, mode);
  445. if (ret < 0)
  446. return 0;
  447. if (ret > int(outLimit))
  448. return 0; //do not bother
  449. copymem(outData, enet->dst_compressor_mem.ptr(), ret);
  450. return ret;
  451. }
  452. size_t NetworkedMultiplayerENet::enet_decompress(void *context, const enet_uint8 *inData, size_t inLimit, enet_uint8 *outData, size_t outLimit) {
  453. NetworkedMultiplayerENet *enet = (NetworkedMultiplayerENet *)(context);
  454. int ret = -1;
  455. switch (enet->compression_mode) {
  456. case COMPRESS_FASTLZ: {
  457. ret = Compression::decompress(outData, outLimit, inData, inLimit, Compression::MODE_FASTLZ);
  458. } break;
  459. case COMPRESS_ZLIB: {
  460. ret = Compression::decompress(outData, outLimit, inData, inLimit, Compression::MODE_DEFLATE);
  461. } break;
  462. case COMPRESS_ZSTD: {
  463. ret = Compression::decompress(outData, outLimit, inData, inLimit, Compression::MODE_ZSTD);
  464. } break;
  465. default: {}
  466. }
  467. if (ret < 0) {
  468. return 0;
  469. } else {
  470. return ret;
  471. }
  472. }
  473. void NetworkedMultiplayerENet::_setup_compressor() {
  474. switch (compression_mode) {
  475. case COMPRESS_NONE: {
  476. enet_host_compress(host, NULL);
  477. } break;
  478. case COMPRESS_RANGE_CODER: {
  479. enet_host_compress_with_range_coder(host);
  480. } break;
  481. case COMPRESS_FASTLZ:
  482. case COMPRESS_ZLIB:
  483. case COMPRESS_ZSTD: {
  484. enet_host_compress(host, &enet_compressor);
  485. } break;
  486. }
  487. }
  488. void NetworkedMultiplayerENet::enet_compressor_destroy(void *context) {
  489. //do none
  490. }
  491. void NetworkedMultiplayerENet::_bind_methods() {
  492. ClassDB::bind_method(D_METHOD("create_server", "port", "max_clients", "in_bandwidth", "out_bandwidth"), &NetworkedMultiplayerENet::create_server, DEFVAL(32), DEFVAL(0), DEFVAL(0));
  493. ClassDB::bind_method(D_METHOD("create_client", "ip", "port", "in_bandwidth", "out_bandwidth"), &NetworkedMultiplayerENet::create_client, DEFVAL(0), DEFVAL(0));
  494. ClassDB::bind_method(D_METHOD("close_connection"), &NetworkedMultiplayerENet::close_connection);
  495. ClassDB::bind_method(D_METHOD("set_compression_mode", "mode"), &NetworkedMultiplayerENet::set_compression_mode);
  496. ClassDB::bind_method(D_METHOD("get_compression_mode"), &NetworkedMultiplayerENet::get_compression_mode);
  497. ClassDB::bind_method(D_METHOD("set_bind_ip", "ip"), &NetworkedMultiplayerENet::set_bind_ip);
  498. BIND_CONSTANT(COMPRESS_NONE);
  499. BIND_CONSTANT(COMPRESS_RANGE_CODER);
  500. BIND_CONSTANT(COMPRESS_FASTLZ);
  501. BIND_CONSTANT(COMPRESS_ZLIB);
  502. BIND_CONSTANT(COMPRESS_ZSTD);
  503. }
  504. NetworkedMultiplayerENet::NetworkedMultiplayerENet() {
  505. active = false;
  506. server = false;
  507. refuse_connections = false;
  508. unique_id = 0;
  509. target_peer = 0;
  510. current_packet.packet = NULL;
  511. transfer_mode = TRANSFER_MODE_RELIABLE;
  512. connection_status = CONNECTION_DISCONNECTED;
  513. compression_mode = COMPRESS_NONE;
  514. enet_compressor.context = this;
  515. enet_compressor.compress = enet_compress;
  516. enet_compressor.decompress = enet_decompress;
  517. enet_compressor.destroy = enet_compressor_destroy;
  518. bind_ip = IP_Address("*");
  519. }
  520. NetworkedMultiplayerENet::~NetworkedMultiplayerENet() {
  521. close_connection();
  522. }
  523. // sets IP for ENet to bind when using create_server
  524. // if no IP is set, then ENet bind to ENET_HOST_ANY
  525. void NetworkedMultiplayerENet::set_bind_ip(const IP_Address &p_ip) {
  526. ERR_FAIL_COND(!p_ip.is_valid() && !p_ip.is_wildcard());
  527. bind_ip = p_ip;
  528. }