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