networked_multiplayer_enet.cpp 19 KB

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