networked_multiplayer_enet.cpp 19 KB

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