2
0

enet_connection.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*************************************************************************/
  2. /* enet_connection.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "enet_connection.h"
  31. #include "enet_packet_peer.h"
  32. #include "core/io/compression.h"
  33. #include "core/io/ip.h"
  34. void ENetConnection::broadcast(enet_uint8 p_channel, ENetPacket *p_packet) {
  35. ERR_FAIL_COND_MSG(!host, "The ENetConnection instance isn't currently active.");
  36. ERR_FAIL_COND_MSG(p_channel >= host->channelLimit, vformat("Unable to send packet on channel %d, max channels: %d", p_channel, (int)host->channelLimit));
  37. enet_host_broadcast(host, p_channel, p_packet);
  38. }
  39. Error ENetConnection::create_host_bound(const IPAddress &p_bind_address, int p_port, int p_max_peers, int p_max_channels, int p_in_bandwidth, int p_out_bandwidth) {
  40. ERR_FAIL_COND_V_MSG(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER, "Invalid bind IP.");
  41. ERR_FAIL_COND_V_MSG(p_port < 0 || p_port > 65535, ERR_INVALID_PARAMETER, "The local port number must be between 0 and 65535 (inclusive).");
  42. ENetAddress address;
  43. memset(&address, 0, sizeof(address));
  44. address.port = p_port;
  45. #ifdef GODOT_ENET
  46. if (p_bind_address.is_wildcard()) {
  47. address.wildcard = 1;
  48. } else {
  49. enet_address_set_ip(&address, p_bind_address.get_ipv6(), 16);
  50. }
  51. #else
  52. if (p_bind_address.is_wildcard()) {
  53. address.host = 0;
  54. } else {
  55. ERR_FAIL_COND_V(!p_bind_address.is_ipv4(), ERR_INVALID_PARAMETER);
  56. address.host = *(uint32_t *)p_bind_address.get_ipv4();
  57. }
  58. #endif
  59. return _create(&address, p_max_peers, p_max_channels, p_in_bandwidth, p_out_bandwidth);
  60. }
  61. Error ENetConnection::create_host(int p_max_peers, int p_max_channels, int p_in_bandwidth, int p_out_bandwidth) {
  62. return _create(nullptr, p_max_peers, p_max_channels, p_in_bandwidth, p_out_bandwidth);
  63. }
  64. void ENetConnection::destroy() {
  65. ERR_FAIL_COND_MSG(!host, "Host already destroyed");
  66. for (List<Ref<ENetPacketPeer>>::Element *E = peers.front(); E; E = E->next()) {
  67. E->get()->_on_disconnect();
  68. }
  69. peers.clear();
  70. enet_host_destroy(host);
  71. host = nullptr;
  72. }
  73. Ref<ENetPacketPeer> ENetConnection::connect_to_host(const String &p_address, int p_port, int p_channels, int p_data) {
  74. Ref<ENetPacketPeer> out;
  75. ERR_FAIL_COND_V_MSG(!host, out, "The ENetConnection instance isn't currently active.");
  76. ERR_FAIL_COND_V_MSG(peers.size(), out, "The ENetConnection is already connected to a peer.");
  77. ERR_FAIL_COND_V_MSG(p_port < 1 || p_port > 65535, out, "The remote port number must be between 1 and 65535 (inclusive).");
  78. IPAddress ip;
  79. if (p_address.is_valid_ip_address()) {
  80. ip = p_address;
  81. } else {
  82. #ifdef GODOT_ENET
  83. ip = IP::get_singleton()->resolve_hostname(p_address);
  84. #else
  85. ip = IP::get_singleton()->resolve_hostname(p_address, IP::TYPE_IPV4);
  86. #endif
  87. ERR_FAIL_COND_V_MSG(!ip.is_valid(), out, "Couldn't resolve the server IP address or domain name.");
  88. }
  89. ENetAddress address;
  90. #ifdef GODOT_ENET
  91. enet_address_set_ip(&address, ip.get_ipv6(), 16);
  92. #else
  93. ERR_FAIL_COND_V_MSG(!ip.is_ipv4(), out, "Connecting to an IPv6 server isn't supported when using vanilla ENet. Recompile Godot with the bundled ENet library.");
  94. address.host = *(uint32_t *)ip.get_ipv4();
  95. #endif
  96. address.port = p_port;
  97. // Initiate connection, allocating enough channels
  98. ENetPeer *peer = enet_host_connect(host, &address, p_channels > 0 ? p_channels : ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT, p_data);
  99. if (peer == nullptr) {
  100. return nullptr;
  101. }
  102. out = Ref<ENetPacketPeer>(memnew(ENetPacketPeer(peer)));
  103. peers.push_back(out);
  104. return out;
  105. }
  106. ENetConnection::EventType ENetConnection::service(int p_timeout, Event &r_event) {
  107. ERR_FAIL_COND_V_MSG(!host, EVENT_ERROR, "The ENetConnection instance isn't currently active.");
  108. ERR_FAIL_COND_V(r_event.peer.is_valid(), EVENT_ERROR);
  109. // Drop peers that have already been disconnected.
  110. // NOTE: Forcibly disconnected peers (i.e. peers disconnected via
  111. // enet_peer_disconnect*) do not trigger DISCONNECTED events.
  112. List<Ref<ENetPacketPeer>>::Element *E = peers.front();
  113. while (E) {
  114. if (!E->get()->is_active()) {
  115. peers.erase(E->get());
  116. }
  117. E = E->next();
  118. }
  119. ENetEvent event;
  120. int ret = enet_host_service(host, &event, p_timeout);
  121. if (ret < 0) {
  122. return EVENT_ERROR;
  123. } else if (ret == 0) {
  124. return EVENT_NONE;
  125. }
  126. switch (event.type) {
  127. case ENET_EVENT_TYPE_CONNECT: {
  128. if (event.peer->data == nullptr) {
  129. Ref<ENetPacketPeer> pp = memnew(ENetPacketPeer(event.peer));
  130. peers.push_back(pp);
  131. }
  132. r_event.peer = Ref<ENetPacketPeer>((ENetPacketPeer *)event.peer->data);
  133. r_event.data = event.data;
  134. return EVENT_CONNECT;
  135. } break;
  136. case ENET_EVENT_TYPE_DISCONNECT: {
  137. // A peer disconnected.
  138. if (event.peer->data != nullptr) {
  139. Ref<ENetPacketPeer> pp = Ref<ENetPacketPeer>((ENetPacketPeer *)event.peer->data);
  140. pp->_on_disconnect();
  141. peers.erase(pp);
  142. r_event.peer = pp;
  143. r_event.data = event.data;
  144. return EVENT_DISCONNECT;
  145. }
  146. return EVENT_ERROR;
  147. } break;
  148. case ENET_EVENT_TYPE_RECEIVE: {
  149. // Packet reveived.
  150. if (event.peer->data != nullptr) {
  151. Ref<ENetPacketPeer> pp = Ref<ENetPacketPeer>((ENetPacketPeer *)event.peer->data);
  152. r_event.peer = Ref<ENetPacketPeer>((ENetPacketPeer *)event.peer->data);
  153. r_event.channel_id = event.channelID;
  154. r_event.packet = event.packet;
  155. return EVENT_RECEIVE;
  156. }
  157. return EVENT_ERROR;
  158. } break;
  159. case ENET_EVENT_TYPE_NONE:
  160. return EVENT_NONE;
  161. default:
  162. return EVENT_NONE;
  163. }
  164. }
  165. void ENetConnection::flush() {
  166. ERR_FAIL_COND_MSG(!host, "The ENetConnection instance isn't currently active.");
  167. enet_host_flush(host);
  168. }
  169. void ENetConnection::bandwidth_limit(int p_in_bandwidth, int p_out_bandwidth) {
  170. ERR_FAIL_COND_MSG(!host, "The ENetConnection instance isn't currently active.");
  171. enet_host_bandwidth_limit(host, p_in_bandwidth, p_out_bandwidth);
  172. }
  173. void ENetConnection::channel_limit(int p_max_channels) {
  174. ERR_FAIL_COND_MSG(!host, "The ENetConnection instance isn't currently active.");
  175. enet_host_channel_limit(host, p_max_channels);
  176. }
  177. void ENetConnection::bandwidth_throttle() {
  178. ERR_FAIL_COND_MSG(!host, "The ENetConnection instance isn't currently active.");
  179. enet_host_bandwidth_throttle(host);
  180. }
  181. void ENetConnection::compress(CompressionMode p_mode) {
  182. ERR_FAIL_COND_MSG(!host, "The ENetConnection instance isn't currently active.");
  183. Compressor::setup(host, p_mode);
  184. }
  185. double ENetConnection::pop_statistic(HostStatistic p_stat) {
  186. ERR_FAIL_COND_V_MSG(!host, 0, "The ENetConnection instance isn't currently active.");
  187. uint32_t *ptr = nullptr;
  188. switch (p_stat) {
  189. case HOST_TOTAL_SENT_DATA:
  190. ptr = &(host->totalSentData);
  191. break;
  192. case HOST_TOTAL_SENT_PACKETS:
  193. ptr = &(host->totalSentPackets);
  194. break;
  195. case HOST_TOTAL_RECEIVED_DATA:
  196. ptr = &(host->totalReceivedData);
  197. break;
  198. case HOST_TOTAL_RECEIVED_PACKETS:
  199. ptr = &(host->totalReceivedPackets);
  200. break;
  201. }
  202. ERR_FAIL_COND_V_MSG(ptr == nullptr, 0, "Invalid statistic: " + itos(p_stat));
  203. uint32_t ret = *ptr;
  204. *ptr = 0;
  205. return ret;
  206. }
  207. int ENetConnection::get_max_channels() const {
  208. ERR_FAIL_COND_V_MSG(!host, 0, "The ENetConnection instance isn't currently active.");
  209. return host->channelLimit;
  210. }
  211. int ENetConnection::get_local_port() const {
  212. ERR_FAIL_COND_V_MSG(!host, 0, "The ENetConnection instance isn't currently active.");
  213. ERR_FAIL_COND_V_MSG(!(host->socket), 0, "The ENetConnection instance isn't currently bound");
  214. ENetAddress address;
  215. ERR_FAIL_COND_V_MSG(enet_socket_get_address(host->socket, &address), 0, "Unable to get socket address");
  216. return address.port;
  217. }
  218. void ENetConnection::get_peers(List<Ref<ENetPacketPeer>> &r_peers) {
  219. for (const Ref<ENetPacketPeer> &I : peers) {
  220. r_peers.push_back(I);
  221. }
  222. }
  223. Array ENetConnection::_get_peers() {
  224. ERR_FAIL_COND_V_MSG(!host, Array(), "The ENetConnection instance isn't currently active.");
  225. Array out;
  226. for (const Ref<ENetPacketPeer> &I : peers) {
  227. out.push_back(I);
  228. }
  229. return out;
  230. }
  231. Error ENetConnection::dtls_server_setup(Ref<CryptoKey> p_key, Ref<X509Certificate> p_cert) {
  232. #ifdef GODOT_ENET
  233. ERR_FAIL_COND_V_MSG(!host, ERR_UNCONFIGURED, "The ENetConnection instance isn't currently active.");
  234. return enet_host_dtls_server_setup(host, p_key.ptr(), p_cert.ptr()) ? FAILED : OK;
  235. #else
  236. ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "ENet DTLS support not available in this build.");
  237. #endif
  238. }
  239. void ENetConnection::refuse_new_connections(bool p_refuse) {
  240. #ifdef GODOT_ENET
  241. ERR_FAIL_COND_MSG(!host, "The ENetConnection instance isn't currently active.");
  242. enet_host_refuse_new_connections(host, p_refuse);
  243. #else
  244. ERR_FAIL_MSG("ENet DTLS support not available in this build.");
  245. #endif
  246. }
  247. Error ENetConnection::dtls_client_setup(Ref<X509Certificate> p_cert, const String &p_hostname, bool p_verify) {
  248. #ifdef GODOT_ENET
  249. ERR_FAIL_COND_V_MSG(!host, ERR_UNCONFIGURED, "The ENetConnection instance isn't currently active.");
  250. return enet_host_dtls_client_setup(host, p_cert.ptr(), p_verify, p_hostname.utf8().get_data()) ? FAILED : OK;
  251. #else
  252. ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "ENet DTLS support not available in this build.");
  253. #endif
  254. }
  255. Error ENetConnection::_create(ENetAddress *p_address, int p_max_peers, int p_max_channels, int p_in_bandwidth, int p_out_bandwidth) {
  256. ERR_FAIL_COND_V_MSG(host != nullptr, ERR_ALREADY_IN_USE, "The ENetConnection instance is already active.");
  257. ERR_FAIL_COND_V_MSG(p_max_peers < 1 || p_max_peers > 4095, ERR_INVALID_PARAMETER, "The number of clients must be set between 1 and 4095 (inclusive).");
  258. ERR_FAIL_COND_V_MSG(p_max_channels < 0 || p_max_channels > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT, ERR_INVALID_PARAMETER, "Invalid channel count. Must be between 0 and 255 (0 means maximum, i.e. 255)");
  259. ERR_FAIL_COND_V_MSG(p_in_bandwidth < 0, ERR_INVALID_PARAMETER, "The incoming bandwidth limit must be greater than or equal to 0 (0 disables the limit).");
  260. ERR_FAIL_COND_V_MSG(p_out_bandwidth < 0, ERR_INVALID_PARAMETER, "The outgoing bandwidth limit must be greater than or equal to 0 (0 disables the limit).");
  261. host = enet_host_create(p_address /* the address to bind the server host to */,
  262. p_max_peers /* allow up to p_max_peers connections */,
  263. p_max_channels /* allow up to p_max_channel to be used */,
  264. p_in_bandwidth /* limit incoming bandwidth if > 0 */,
  265. p_out_bandwidth /* limit outgoing bandwidth if > 0 */);
  266. ERR_FAIL_COND_V_MSG(!host, ERR_CANT_CREATE, "Couldn't create an ENet host.");
  267. return OK;
  268. }
  269. Array ENetConnection::_service(int p_timeout) {
  270. Array out;
  271. Event event;
  272. Ref<ENetPacketPeer> peer;
  273. EventType ret = service(p_timeout, event);
  274. out.push_back(ret);
  275. out.push_back(event.peer);
  276. out.push_back(event.data);
  277. out.push_back(event.channel_id);
  278. if (event.packet && event.peer.is_valid()) {
  279. event.peer->_queue_packet(event.packet);
  280. }
  281. return out;
  282. }
  283. void ENetConnection::_broadcast(int p_channel, PackedByteArray p_packet, int p_flags) {
  284. ERR_FAIL_COND_MSG(!host, "The ENetConnection instance isn't currently active.");
  285. ERR_FAIL_COND_MSG(p_channel < 0 || p_channel > (int)host->channelLimit, "Invalid channel");
  286. ERR_FAIL_COND_MSG(p_flags & ~ENetPacketPeer::FLAG_ALLOWED, "Invalid flags");
  287. ENetPacket *pkt = enet_packet_create(p_packet.ptr(), p_packet.size(), p_flags);
  288. broadcast(p_channel, pkt);
  289. }
  290. void ENetConnection::_bind_methods() {
  291. ClassDB::bind_method(D_METHOD("create_host_bound", "bind_address", "bind_port", "max_peers", "max_channels", "in_bandwidth", "out_bandwidth"), &ENetConnection::create_host_bound, DEFVAL(32), DEFVAL(0), DEFVAL(0), DEFVAL(0));
  292. ClassDB::bind_method(D_METHOD("create_host", "max_peers", "max_channels", "in_bandwidth", "out_bandwidth"), &ENetConnection::create_host, DEFVAL(32), DEFVAL(0), DEFVAL(0), DEFVAL(0));
  293. ClassDB::bind_method(D_METHOD("destroy"), &ENetConnection::destroy);
  294. ClassDB::bind_method(D_METHOD("connect_to_host", "address", "port", "channels", "data"), &ENetConnection::connect_to_host, DEFVAL(0), DEFVAL(0));
  295. ClassDB::bind_method(D_METHOD("service", "timeout"), &ENetConnection::_service, DEFVAL(0));
  296. ClassDB::bind_method(D_METHOD("flush"), &ENetConnection::flush);
  297. ClassDB::bind_method(D_METHOD("bandwidth_limit", "in_bandwidth", "out_bandwidth"), &ENetConnection::bandwidth_limit, DEFVAL(0), DEFVAL(0));
  298. ClassDB::bind_method(D_METHOD("channel_limit", "limit"), &ENetConnection::channel_limit);
  299. ClassDB::bind_method(D_METHOD("broadcast", "channel", "packet", "flags"), &ENetConnection::_broadcast);
  300. ClassDB::bind_method(D_METHOD("compress", "mode"), &ENetConnection::compress);
  301. ClassDB::bind_method(D_METHOD("dtls_server_setup", "key", "certificate"), &ENetConnection::dtls_server_setup);
  302. ClassDB::bind_method(D_METHOD("dtls_client_setup", "certificate", "hostname", "verify"), &ENetConnection::dtls_client_setup, DEFVAL(true));
  303. ClassDB::bind_method(D_METHOD("refuse_new_connections", "refuse"), &ENetConnection::refuse_new_connections);
  304. ClassDB::bind_method(D_METHOD("pop_statistic", "statistic"), &ENetConnection::pop_statistic);
  305. ClassDB::bind_method(D_METHOD("get_max_channels"), &ENetConnection::get_max_channels);
  306. ClassDB::bind_method(D_METHOD("get_local_port"), &ENetConnection::get_local_port);
  307. ClassDB::bind_method(D_METHOD("get_peers"), &ENetConnection::_get_peers);
  308. BIND_ENUM_CONSTANT(COMPRESS_NONE);
  309. BIND_ENUM_CONSTANT(COMPRESS_RANGE_CODER);
  310. BIND_ENUM_CONSTANT(COMPRESS_FASTLZ);
  311. BIND_ENUM_CONSTANT(COMPRESS_ZLIB);
  312. BIND_ENUM_CONSTANT(COMPRESS_ZSTD);
  313. BIND_ENUM_CONSTANT(EVENT_ERROR);
  314. BIND_ENUM_CONSTANT(EVENT_NONE);
  315. BIND_ENUM_CONSTANT(EVENT_CONNECT);
  316. BIND_ENUM_CONSTANT(EVENT_DISCONNECT);
  317. BIND_ENUM_CONSTANT(EVENT_RECEIVE);
  318. BIND_ENUM_CONSTANT(HOST_TOTAL_SENT_DATA);
  319. BIND_ENUM_CONSTANT(HOST_TOTAL_SENT_PACKETS);
  320. BIND_ENUM_CONSTANT(HOST_TOTAL_RECEIVED_DATA);
  321. BIND_ENUM_CONSTANT(HOST_TOTAL_RECEIVED_PACKETS);
  322. }
  323. ENetConnection::~ENetConnection() {
  324. if (host) {
  325. destroy();
  326. }
  327. }
  328. size_t ENetConnection::Compressor::enet_compress(void *context, const ENetBuffer *inBuffers, size_t inBufferCount, size_t inLimit, enet_uint8 *outData, size_t outLimit) {
  329. Compressor *compressor = (Compressor *)(context);
  330. if (size_t(compressor->src_mem.size()) < inLimit) {
  331. compressor->src_mem.resize(inLimit);
  332. }
  333. int total = inLimit;
  334. int ofs = 0;
  335. while (total) {
  336. for (size_t i = 0; i < inBufferCount; i++) {
  337. int to_copy = MIN(total, int(inBuffers[i].dataLength));
  338. memcpy(&compressor->src_mem.write[ofs], inBuffers[i].data, to_copy);
  339. ofs += to_copy;
  340. total -= to_copy;
  341. }
  342. }
  343. Compression::Mode mode;
  344. switch (compressor->mode) {
  345. case COMPRESS_FASTLZ: {
  346. mode = Compression::MODE_FASTLZ;
  347. } break;
  348. case COMPRESS_ZLIB: {
  349. mode = Compression::MODE_DEFLATE;
  350. } break;
  351. case COMPRESS_ZSTD: {
  352. mode = Compression::MODE_ZSTD;
  353. } break;
  354. default: {
  355. ERR_FAIL_V_MSG(0, vformat("Invalid ENet compression mode: %d", compressor->mode));
  356. }
  357. }
  358. int req_size = Compression::get_max_compressed_buffer_size(ofs, mode);
  359. if (compressor->dst_mem.size() < req_size) {
  360. compressor->dst_mem.resize(req_size);
  361. }
  362. int ret = Compression::compress(compressor->dst_mem.ptrw(), compressor->src_mem.ptr(), ofs, mode);
  363. if (ret < 0) {
  364. return 0;
  365. }
  366. if (ret > int(outLimit)) {
  367. return 0; // Do not bother
  368. }
  369. memcpy(outData, compressor->dst_mem.ptr(), ret);
  370. return ret;
  371. }
  372. size_t ENetConnection::Compressor::enet_decompress(void *context, const enet_uint8 *inData, size_t inLimit, enet_uint8 *outData, size_t outLimit) {
  373. Compressor *compressor = (Compressor *)(context);
  374. int ret = -1;
  375. switch (compressor->mode) {
  376. case COMPRESS_FASTLZ: {
  377. ret = Compression::decompress(outData, outLimit, inData, inLimit, Compression::MODE_FASTLZ);
  378. } break;
  379. case COMPRESS_ZLIB: {
  380. ret = Compression::decompress(outData, outLimit, inData, inLimit, Compression::MODE_DEFLATE);
  381. } break;
  382. case COMPRESS_ZSTD: {
  383. ret = Compression::decompress(outData, outLimit, inData, inLimit, Compression::MODE_ZSTD);
  384. } break;
  385. default: {
  386. }
  387. }
  388. if (ret < 0) {
  389. return 0;
  390. } else {
  391. return ret;
  392. }
  393. }
  394. void ENetConnection::Compressor::setup(ENetHost *p_host, CompressionMode p_mode) {
  395. ERR_FAIL_COND(!p_host);
  396. switch (p_mode) {
  397. case COMPRESS_NONE: {
  398. enet_host_compress(p_host, nullptr);
  399. } break;
  400. case COMPRESS_RANGE_CODER: {
  401. enet_host_compress_with_range_coder(p_host);
  402. } break;
  403. case COMPRESS_FASTLZ:
  404. case COMPRESS_ZLIB:
  405. case COMPRESS_ZSTD: {
  406. Compressor *compressor = memnew(Compressor(p_mode));
  407. enet_host_compress(p_host, &(compressor->enet_compressor));
  408. } break;
  409. }
  410. }
  411. ENetConnection::Compressor::Compressor(CompressionMode p_mode) {
  412. mode = p_mode;
  413. enet_compressor.context = this;
  414. enet_compressor.compress = enet_compress;
  415. enet_compressor.decompress = enet_decompress;
  416. enet_compressor.destroy = enet_compressor_destroy;
  417. }