enet_connection.cpp 20 KB

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