| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "enet_packet_peer.h" |
| 34 |
|
| 35 |
#include "core/crypto/crypto.h" |
| 36 |
#include "core/object/ref_counted.h" |
| 37 |
|
| 38 |
#include <enet/enet.h> |
| 39 |
|
| 40 |
template <typename T> |
| 41 |
class TypedArray; |
| 42 |
|
| 43 |
class ENetConnection : public RefCounted { |
| 44 |
GDCLASS(ENetConnection, RefCounted); |
| 45 |
|
| 46 |
public: |
| 47 |
enum CompressionMode { |
| 48 |
COMPRESS_NONE = 0, |
| 49 |
COMPRESS_RANGE_CODER, |
| 50 |
COMPRESS_FASTLZ, |
| 51 |
COMPRESS_ZLIB, |
| 52 |
COMPRESS_ZSTD, |
| 53 |
}; |
| 54 |
|
| 55 |
enum HostStatistic { |
| 56 |
HOST_TOTAL_SENT_DATA, |
| 57 |
HOST_TOTAL_SENT_PACKETS, |
| 58 |
HOST_TOTAL_RECEIVED_DATA, |
| 59 |
HOST_TOTAL_RECEIVED_PACKETS, |
| 60 |
}; |
| 61 |
|
| 62 |
enum EventType { |
| 63 |
EVENT_ERROR = -1, |
| 64 |
EVENT_NONE = 0, |
| 65 |
EVENT_CONNECT, |
| 66 |
EVENT_DISCONNECT, |
| 67 |
EVENT_RECEIVE, |
| 68 |
}; |
| 69 |
|
| 70 |
struct Event { |
| 71 |
Ref<ENetPacketPeer> peer; |
| 72 |
enet_uint8 channel_id = 0; |
| 73 |
enet_uint32 data = 0; |
| 74 |
ENetPacket *packet = nullptr; |
| 75 |
}; |
| 76 |
|
| 77 |
protected: |
| 78 |
static void _bind_methods(); |
| 79 |
|
| 80 |
private: |
| 81 |
ENetHost *host = nullptr; |
| 82 |
List<Ref<ENetPacketPeer>> peers; |
| 83 |
|
| 84 |
EventType _parse_event(const ENetEvent &p_event, Event &r_event); |
| 85 |
Error _create(ENetAddress *p_address, int p_max_peers, int p_max_channels, int p_in_bandwidth, int p_out_bandwidth); |
| 86 |
Array _service(int p_timeout = 0); |
| 87 |
void _broadcast(int p_channel, PackedByteArray p_packet, int p_flags); |
| 88 |
TypedArray<ENetPacketPeer> _get_peers(); |
| 89 |
|
| 90 |
class Compressor { |
| 91 |
private: |
| 92 |
CompressionMode mode = COMPRESS_NONE; |
| 93 |
Vector<uint8_t> src_mem; |
| 94 |
Vector<uint8_t> dst_mem; |
| 95 |
ENetCompressor enet_compressor; |
| 96 |
|
| 97 |
Compressor(CompressionMode mode); |
| 98 |
|
| 99 |
static size_t enet_compress(void *context, const ENetBuffer *inBuffers, size_t inBufferCount, size_t inLimit, enet_uint8 *outData, size_t outLimit); |
| 100 |
static size_t enet_decompress(void *context, const enet_uint8 *inData, size_t inLimit, enet_uint8 *outData, size_t outLimit); |
| 101 |
static void enet_compressor_destroy(void *context) { |
| 102 |
memdelete((Compressor *)context); |
| 103 |
} |
| 104 |
|
| 105 |
public: |
| 106 |
static void setup(ENetHost *p_host, CompressionMode p_mode); |
| 107 |
}; |
| 108 |
|
| 109 |
public: |
| 110 |
void broadcast(enet_uint8 p_channel, ENetPacket *p_packet); |
| 111 |
void socket_send(const String &p_address, int p_port, const PackedByteArray &p_packet); |
| 112 |
Error create_host_bound(const IPAddress &p_bind_address = IPAddress("*"), int p_port = 0, int p_max_peers = 32, int p_max_channels = 0, int p_in_bandwidth = 0, int p_out_bandwidth = 0); |
| 113 |
Error create_host(int p_max_peers = 32, int p_max_channels = 0, int p_in_bandwidth = 0, int p_out_bandwidth = 0); |
| 114 |
void destroy(); |
| 115 |
Ref<ENetPacketPeer> connect_to_host(const String &p_address, int p_port, int p_channels, int p_data = 0); |
| 116 |
EventType service(int p_timeout, Event &r_event); |
| 117 |
int check_events(EventType &r_type, Event &r_event); |
| 118 |
void flush(); |
| 119 |
void bandwidth_limit(int p_in_bandwidth = 0, int p_out_bandwidth = 0); |
| 120 |
void channel_limit(int p_max_channels); |
| 121 |
void bandwidth_throttle(); |
| 122 |
void compress(CompressionMode p_mode); |
| 123 |
double pop_statistic(HostStatistic p_stat); |
| 124 |
int get_max_channels() const; |
| 125 |
|
| 126 |
|
| 127 |
void get_peers(List<Ref<ENetPacketPeer>> &r_peers); |
| 128 |
int get_local_port() const; |
| 129 |
|
| 130 |
|
| 131 |
Error dtls_server_setup(const Ref<TLSOptions> &p_options); |
| 132 |
Error dtls_client_setup(const String &p_hostname, const Ref<TLSOptions> &p_options); |
| 133 |
void refuse_new_connections(bool p_refuse); |
| 134 |
|
| 135 |
ENetConnection() {} |
| 136 |
~ENetConnection(); |
| 137 |
}; |
| 138 |
|
| 139 |
VARIANT_ENUM_CAST(ENetConnection::CompressionMode); |
| 140 |
VARIANT_ENUM_CAST(ENetConnection::EventType); |
| 141 |
VARIANT_ENUM_CAST(ENetConnection::HostStatistic); |