Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/enet/enet_connection.cpp
10277 views
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
39
void ENetConnection::broadcast(enet_uint8 p_channel, ENetPacket *p_packet) {
40
ERR_FAIL_NULL_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
45
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) {
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
69
Error 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
73
void ENetConnection::destroy() {
74
ERR_FAIL_NULL_MSG(host, "Host already destroyed.");
75
for (const Ref<ENetPacketPeer> &peer : peers) {
76
peer->_on_disconnect();
77
}
78
peers.clear();
79
enet_host_destroy(host);
80
host = nullptr;
81
}
82
83
Ref<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_NULL_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.instantiate(peer);
117
peers.push_back(out);
118
return out;
119
}
120
121
ENetConnection::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
162
ENetConnection::EventType ENetConnection::service(int p_timeout, Event &r_event) {
163
ERR_FAIL_NULL_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
188
int ENetConnection::check_events(EventType &r_type, Event &r_event) {
189
ERR_FAIL_NULL_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
200
void ENetConnection::flush() {
201
ERR_FAIL_NULL_MSG(host, "The ENetConnection instance isn't currently active.");
202
enet_host_flush(host);
203
}
204
205
void ENetConnection::bandwidth_limit(int p_in_bandwidth, int p_out_bandwidth) {
206
ERR_FAIL_NULL_MSG(host, "The ENetConnection instance isn't currently active.");
207
enet_host_bandwidth_limit(host, p_in_bandwidth, p_out_bandwidth);
208
}
209
210
void ENetConnection::channel_limit(int p_max_channels) {
211
ERR_FAIL_NULL_MSG(host, "The ENetConnection instance isn't currently active.");
212
enet_host_channel_limit(host, p_max_channels);
213
}
214
215
void ENetConnection::bandwidth_throttle() {
216
ERR_FAIL_NULL_MSG(host, "The ENetConnection instance isn't currently active.");
217
enet_host_bandwidth_throttle(host);
218
}
219
220
void ENetConnection::compress(CompressionMode p_mode) {
221
ERR_FAIL_NULL_MSG(host, "The ENetConnection instance isn't currently active.");
222
Compressor::setup(host, p_mode);
223
}
224
225
double ENetConnection::pop_statistic(HostStatistic p_stat) {
226
ERR_FAIL_NULL_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_NULL_V_MSG(ptr, 0, "Invalid statistic: " + itos(p_stat) + ".");
243
uint32_t ret = *ptr;
244
*ptr = 0;
245
return ret;
246
}
247
248
int ENetConnection::get_max_channels() const {
249
ERR_FAIL_NULL_V_MSG(host, 0, "The ENetConnection instance isn't currently active.");
250
return host->channelLimit;
251
}
252
253
int ENetConnection::get_local_port() const {
254
ERR_FAIL_NULL_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
261
void ENetConnection::get_peers(List<Ref<ENetPacketPeer>> &r_peers) {
262
for (const Ref<ENetPacketPeer> &I : peers) {
263
r_peers.push_back(I);
264
}
265
}
266
267
TypedArray<ENetPacketPeer> ENetConnection::_get_peers() {
268
ERR_FAIL_NULL_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
276
Error ENetConnection::dtls_server_setup(const Ref<TLSOptions> &p_options) {
277
#ifdef GODOT_ENET
278
ERR_FAIL_NULL_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, 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
286
void ENetConnection::refuse_new_connections(bool p_refuse) {
287
#ifdef GODOT_ENET
288
ERR_FAIL_NULL_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
295
Error ENetConnection::dtls_client_setup(const String &p_hostname, const Ref<TLSOptions> &p_options) {
296
#ifdef GODOT_ENET
297
ERR_FAIL_NULL_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(), 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
305
Error 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_NULL_V_MSG(host, ERR_CANT_CREATE, "Couldn't create an ENet host.");
319
return OK;
320
}
321
322
Array ENetConnection::_service(int p_timeout) {
323
Event event;
324
Ref<ENetPacketPeer> peer;
325
EventType ret = service(p_timeout, event);
326
Array out = { ret, event.peer, event.data, event.channel_id };
327
if (event.packet && event.peer.is_valid()) {
328
event.peer->_queue_packet(event.packet);
329
}
330
return out;
331
}
332
333
void ENetConnection::_broadcast(int p_channel, PackedByteArray p_packet, int p_flags) {
334
ERR_FAIL_NULL_MSG(host, "The ENetConnection instance isn't currently active.");
335
ERR_FAIL_COND_MSG(p_channel < 0 || p_channel > (int)host->channelLimit, "Invalid channel");
336
ERR_FAIL_COND_MSG(p_flags & ~ENetPacketPeer::FLAG_ALLOWED, "Invalid flags");
337
ENetPacket *pkt = enet_packet_create(p_packet.ptr(), p_packet.size(), p_flags);
338
broadcast(p_channel, pkt);
339
}
340
341
void ENetConnection::socket_send(const String &p_address, int p_port, const PackedByteArray &p_packet) {
342
ERR_FAIL_NULL_MSG(host, "The ENetConnection instance isn't currently active.");
343
ERR_FAIL_COND_MSG(!(host->socket), "The ENetConnection instance isn't currently bound.");
344
ERR_FAIL_COND_MSG(p_port < 1 || p_port > 65535, "The remote port number must be between 1 and 65535 (inclusive).");
345
346
IPAddress ip;
347
if (p_address.is_valid_ip_address()) {
348
ip = p_address;
349
} else {
350
#ifdef GODOT_ENET
351
ip = IP::get_singleton()->resolve_hostname(p_address);
352
#else
353
ip = IP::get_singleton()->resolve_hostname(p_address, IP::TYPE_IPV4);
354
#endif
355
ERR_FAIL_COND_MSG(!ip.is_valid(), "Couldn't resolve the server IP address or domain name.");
356
}
357
358
ENetAddress address;
359
#ifdef GODOT_ENET
360
enet_address_set_ip(&address, ip.get_ipv6(), 16);
361
#else
362
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.");
363
address.host = *(uint32_t *)ip.get_ipv4();
364
#endif
365
address.port = p_port;
366
367
ENetBuffer enet_buffers[1];
368
enet_buffers[0].data = (void *)p_packet.ptr();
369
enet_buffers[0].dataLength = p_packet.size();
370
371
enet_socket_send(host->socket, &address, enet_buffers, 1);
372
}
373
374
void ENetConnection::_bind_methods() {
375
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));
376
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));
377
ClassDB::bind_method(D_METHOD("destroy"), &ENetConnection::destroy);
378
ClassDB::bind_method(D_METHOD("connect_to_host", "address", "port", "channels", "data"), &ENetConnection::connect_to_host, DEFVAL(0), DEFVAL(0));
379
ClassDB::bind_method(D_METHOD("service", "timeout"), &ENetConnection::_service, DEFVAL(0));
380
ClassDB::bind_method(D_METHOD("flush"), &ENetConnection::flush);
381
ClassDB::bind_method(D_METHOD("bandwidth_limit", "in_bandwidth", "out_bandwidth"), &ENetConnection::bandwidth_limit, DEFVAL(0), DEFVAL(0));
382
ClassDB::bind_method(D_METHOD("channel_limit", "limit"), &ENetConnection::channel_limit);
383
ClassDB::bind_method(D_METHOD("broadcast", "channel", "packet", "flags"), &ENetConnection::_broadcast);
384
ClassDB::bind_method(D_METHOD("compress", "mode"), &ENetConnection::compress);
385
ClassDB::bind_method(D_METHOD("dtls_server_setup", "server_options"), &ENetConnection::dtls_server_setup);
386
ClassDB::bind_method(D_METHOD("dtls_client_setup", "hostname", "client_options"), &ENetConnection::dtls_client_setup, DEFVAL(Ref<TLSOptions>()));
387
ClassDB::bind_method(D_METHOD("refuse_new_connections", "refuse"), &ENetConnection::refuse_new_connections);
388
ClassDB::bind_method(D_METHOD("pop_statistic", "statistic"), &ENetConnection::pop_statistic);
389
ClassDB::bind_method(D_METHOD("get_max_channels"), &ENetConnection::get_max_channels);
390
ClassDB::bind_method(D_METHOD("get_local_port"), &ENetConnection::get_local_port);
391
ClassDB::bind_method(D_METHOD("get_peers"), &ENetConnection::_get_peers);
392
ClassDB::bind_method(D_METHOD("socket_send", "destination_address", "destination_port", "packet"), &ENetConnection::socket_send);
393
394
BIND_ENUM_CONSTANT(COMPRESS_NONE);
395
BIND_ENUM_CONSTANT(COMPRESS_RANGE_CODER);
396
BIND_ENUM_CONSTANT(COMPRESS_FASTLZ);
397
BIND_ENUM_CONSTANT(COMPRESS_ZLIB);
398
BIND_ENUM_CONSTANT(COMPRESS_ZSTD);
399
400
BIND_ENUM_CONSTANT(EVENT_ERROR);
401
BIND_ENUM_CONSTANT(EVENT_NONE);
402
BIND_ENUM_CONSTANT(EVENT_CONNECT);
403
BIND_ENUM_CONSTANT(EVENT_DISCONNECT);
404
BIND_ENUM_CONSTANT(EVENT_RECEIVE);
405
406
BIND_ENUM_CONSTANT(HOST_TOTAL_SENT_DATA);
407
BIND_ENUM_CONSTANT(HOST_TOTAL_SENT_PACKETS);
408
BIND_ENUM_CONSTANT(HOST_TOTAL_RECEIVED_DATA);
409
BIND_ENUM_CONSTANT(HOST_TOTAL_RECEIVED_PACKETS);
410
}
411
412
ENetConnection::~ENetConnection() {
413
if (host) {
414
destroy();
415
}
416
}
417
418
size_t ENetConnection::Compressor::enet_compress(void *context, const ENetBuffer *inBuffers, size_t inBufferCount, size_t inLimit, enet_uint8 *outData, size_t outLimit) {
419
Compressor *compressor = (Compressor *)(context);
420
421
if (size_t(compressor->src_mem.size()) < inLimit) {
422
compressor->src_mem.resize(inLimit);
423
}
424
425
size_t total = inLimit;
426
size_t ofs = 0;
427
while (total) {
428
for (size_t i = 0; i < inBufferCount; i++) {
429
const size_t to_copy = MIN(total, inBuffers[i].dataLength);
430
memcpy(&compressor->src_mem.write[ofs], inBuffers[i].data, to_copy);
431
ofs += to_copy;
432
total -= to_copy;
433
}
434
}
435
436
Compression::Mode mode;
437
438
switch (compressor->mode) {
439
case COMPRESS_FASTLZ: {
440
mode = Compression::MODE_FASTLZ;
441
} break;
442
case COMPRESS_ZLIB: {
443
mode = Compression::MODE_DEFLATE;
444
} break;
445
case COMPRESS_ZSTD: {
446
mode = Compression::MODE_ZSTD;
447
} break;
448
default: {
449
ERR_FAIL_V_MSG(0, vformat("Invalid ENet compression mode: %d", compressor->mode));
450
}
451
}
452
453
const int64_t req_size = Compression::get_max_compressed_buffer_size(ofs, mode);
454
if (compressor->dst_mem.size() < req_size) {
455
compressor->dst_mem.resize(req_size);
456
}
457
const int64_t ret = Compression::compress(compressor->dst_mem.ptrw(), compressor->src_mem.ptr(), ofs, mode);
458
459
if (ret < 0) {
460
return 0;
461
}
462
463
const size_t ret_size = size_t(ret);
464
if (ret_size > outLimit) {
465
return 0; // Do not bother
466
}
467
468
memcpy(outData, compressor->dst_mem.ptr(), ret_size);
469
470
return ret;
471
}
472
473
size_t ENetConnection::Compressor::enet_decompress(void *context, const enet_uint8 *inData, size_t inLimit, enet_uint8 *outData, size_t outLimit) {
474
Compressor *compressor = (Compressor *)(context);
475
int64_t ret = -1;
476
switch (compressor->mode) {
477
case COMPRESS_FASTLZ: {
478
ret = Compression::decompress(outData, outLimit, inData, inLimit, Compression::MODE_FASTLZ);
479
} break;
480
case COMPRESS_ZLIB: {
481
ret = Compression::decompress(outData, outLimit, inData, inLimit, Compression::MODE_DEFLATE);
482
} break;
483
case COMPRESS_ZSTD: {
484
ret = Compression::decompress(outData, outLimit, inData, inLimit, Compression::MODE_ZSTD);
485
} break;
486
default: {
487
}
488
}
489
if (ret < 0) {
490
return 0;
491
} else {
492
return ret;
493
}
494
}
495
496
void ENetConnection::Compressor::setup(ENetHost *p_host, CompressionMode p_mode) {
497
ERR_FAIL_NULL(p_host);
498
switch (p_mode) {
499
case COMPRESS_NONE: {
500
enet_host_compress(p_host, nullptr);
501
} break;
502
case COMPRESS_RANGE_CODER: {
503
enet_host_compress_with_range_coder(p_host);
504
} break;
505
case COMPRESS_FASTLZ:
506
case COMPRESS_ZLIB:
507
case COMPRESS_ZSTD: {
508
Compressor *compressor = memnew(Compressor(p_mode));
509
enet_host_compress(p_host, &(compressor->enet_compressor));
510
} break;
511
}
512
}
513
514
ENetConnection::Compressor::Compressor(CompressionMode p_mode) {
515
mode = p_mode;
516
enet_compressor.context = this;
517
enet_compressor.compress = enet_compress;
518
enet_compressor.decompress = enet_decompress;
519
enet_compressor.destroy = enet_compressor_destroy;
520
}
521
522