Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/enet/enet_connection.h
10277 views
1
/**************************************************************************/
2
/* enet_connection.h */
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
#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
// Extras
127
void get_peers(List<Ref<ENetPacketPeer>> &r_peers);
128
int get_local_port() const;
129
130
// Godot additions
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);
142
143