summaryrefslogtreecommitdiff
path: root/modules/enet/enet_packet_peer.cpp
blob: d7d2ec9ebe0fc99a674deb4c61e743a8a352691e (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*************************************************************************/
/*  enet_packet_peer.cpp                                                 */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#include "enet_packet_peer.h"

void ENetPacketPeer::peer_disconnect(int p_data) {
	ERR_FAIL_COND(!peer);
	enet_peer_disconnect(peer, p_data);
}

void ENetPacketPeer::peer_disconnect_later(int p_data) {
	ERR_FAIL_COND(!peer);
	enet_peer_disconnect_later(peer, p_data);
}

void ENetPacketPeer::peer_disconnect_now(int p_data) {
	ERR_FAIL_COND(!peer);
	enet_peer_disconnect_now(peer, p_data);
	_on_disconnect();
}

void ENetPacketPeer::ping() {
	ERR_FAIL_COND(!peer);
	enet_peer_ping(peer);
}

void ENetPacketPeer::ping_interval(int p_interval) {
	ERR_FAIL_COND(!peer);
	enet_peer_ping_interval(peer, p_interval);
}

int ENetPacketPeer::send(uint8_t p_channel, ENetPacket *p_packet) {
	ERR_FAIL_COND_V(peer == nullptr, -1);
	ERR_FAIL_COND_V(p_packet == nullptr, -1);
	ERR_FAIL_COND_V_MSG(p_channel >= peer->channelCount, -1, vformat("Unable to send packet on channel %d, max channels: %d", p_channel, (int)peer->channelCount));
	return enet_peer_send(peer, p_channel, p_packet);
}

void ENetPacketPeer::reset() {
	ERR_FAIL_COND_MSG(peer == nullptr, "Peer not connected");
	enet_peer_reset(peer);
	_on_disconnect();
}

void ENetPacketPeer::throttle_configure(int p_interval, int p_acceleration, int p_deceleration) {
	ERR_FAIL_COND_MSG(peer == nullptr, "Peer not connected");
	enet_peer_throttle_configure(peer, p_interval, p_acceleration, p_deceleration);
}

void ENetPacketPeer::set_timeout(int p_timeout, int p_timeout_min, int p_timeout_max) {
	ERR_FAIL_COND_MSG(peer == nullptr, "Peer not connected");
	ERR_FAIL_COND_MSG(p_timeout > p_timeout_min || p_timeout_min > p_timeout_max, "Timeout limit must be less than minimum timeout, which itself must be less then maximum timeout");
	enet_peer_timeout(peer, p_timeout, p_timeout_min, p_timeout_max);
}

int ENetPacketPeer::get_max_packet_size() const {
	return 1 << 24;
}

int ENetPacketPeer::get_available_packet_count() const {
	return packet_queue.size();
}

Error ENetPacketPeer::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
	ERR_FAIL_COND_V(!peer, ERR_UNCONFIGURED);
	ERR_FAIL_COND_V(!packet_queue.size(), ERR_UNAVAILABLE);
	if (last_packet) {
		enet_packet_destroy(last_packet);
		last_packet = nullptr;
	}
	last_packet = packet_queue.front()->get();
	packet_queue.pop_front();
	*r_buffer = (const uint8_t *)(last_packet->data);
	r_buffer_size = last_packet->dataLength;
	return OK;
}

Error ENetPacketPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
	ERR_FAIL_COND_V(!peer, ERR_UNCONFIGURED);
	ENetPacket *packet = enet_packet_create(p_buffer, p_buffer_size, ENET_PACKET_FLAG_RELIABLE);
	return send(0, packet) < 0 ? FAILED : OK;
}

IPAddress ENetPacketPeer::get_remote_address() const {
	ERR_FAIL_COND_V(!peer, IPAddress());
	IPAddress out;
#ifdef GODOT_ENET
	out.set_ipv6((uint8_t *)&(peer->address.host));
#else
	out.set_ipv4((uint8_t *)&(peer->address.host));
#endif
	return out;
}

int ENetPacketPeer::get_remote_port() const {
	ERR_FAIL_COND_V(!peer, 0);
	return peer->address.port;
}

bool ENetPacketPeer::is_active() const {
	return peer != nullptr;
}

double ENetPacketPeer::get_statistic(PeerStatistic p_stat) {
	ERR_FAIL_COND_V(!peer, 0);
	switch (p_stat) {
		case PEER_PACKET_LOSS:
			return peer->packetLoss;
		case PEER_PACKET_LOSS_VARIANCE:
			return peer->packetLossVariance;
		case PEER_PACKET_LOSS_EPOCH:
			return peer->packetLossEpoch;
		case PEER_ROUND_TRIP_TIME:
			return peer->roundTripTime;
		case PEER_ROUND_TRIP_TIME_VARIANCE:
			return peer->roundTripTimeVariance;
		case PEER_LAST_ROUND_TRIP_TIME:
			return peer->lastRoundTripTime;
		case PEER_LAST_ROUND_TRIP_TIME_VARIANCE:
			return peer->lastRoundTripTimeVariance;
		case PEER_PACKET_THROTTLE:
			return peer->packetThrottle;
		case PEER_PACKET_THROTTLE_LIMIT:
			return peer->packetThrottleLimit;
		case PEER_PACKET_THROTTLE_COUNTER:
			return peer->packetThrottleCounter;
		case PEER_PACKET_THROTTLE_EPOCH:
			return peer->packetThrottleEpoch;
		case PEER_PACKET_THROTTLE_ACCELERATION:
			return peer->packetThrottleAcceleration;
		case PEER_PACKET_THROTTLE_DECELERATION:
			return peer->packetThrottleDeceleration;
		case PEER_PACKET_THROTTLE_INTERVAL:
			return peer->packetThrottleInterval;
	}
	ERR_FAIL_V(0);
}

ENetPacketPeer::PeerState ENetPacketPeer::get_state() const {
	if (!is_active()) {
		return STATE_DISCONNECTED;
	}
	return (PeerState)peer->state;
}

int ENetPacketPeer::get_channels() const {
	ERR_FAIL_COND_V_MSG(!peer, 0, "The ENetConnection instance isn't currently active.");
	return peer->channelCount;
}

void ENetPacketPeer::_on_disconnect() {
	if (peer) {
		peer->data = nullptr;
	}
	peer = nullptr;
}

void ENetPacketPeer::_queue_packet(ENetPacket *p_packet) {
	ERR_FAIL_COND(!peer);
	packet_queue.push_back(p_packet);
}

Error ENetPacketPeer::_send(int p_channel, PackedByteArray p_packet, int p_flags) {
	ERR_FAIL_COND_V_MSG(peer == nullptr, ERR_UNCONFIGURED, "Peer not connected");
	ERR_FAIL_COND_V_MSG(p_channel < 0 || p_channel > (int)peer->channelCount, ERR_INVALID_PARAMETER, "Invalid channel");
	ERR_FAIL_COND_V_MSG(p_flags & ~FLAG_ALLOWED, ERR_INVALID_PARAMETER, "Invalid flags");
	ENetPacket *packet = enet_packet_create(p_packet.ptr(), p_packet.size(), p_flags);
	return send(p_channel, packet) == 0 ? OK : FAILED;
}

void ENetPacketPeer::_bind_methods() {
	ClassDB::bind_method(D_METHOD("peer_disconnect", "data"), &ENetPacketPeer::peer_disconnect, DEFVAL(0));
	ClassDB::bind_method(D_METHOD("peer_disconnect_later", "data"), &ENetPacketPeer::peer_disconnect_later, DEFVAL(0));
	ClassDB::bind_method(D_METHOD("peer_disconnect_now", "data"), &ENetPacketPeer::peer_disconnect_now, DEFVAL(0));

	ClassDB::bind_method(D_METHOD("ping"), &ENetPacketPeer::ping);
	ClassDB::bind_method(D_METHOD("ping_interval", "ping_interval"), &ENetPacketPeer::ping_interval);
	ClassDB::bind_method(D_METHOD("reset"), &ENetPacketPeer::reset);
	ClassDB::bind_method(D_METHOD("send", "channel", "packet", "flags"), &ENetPacketPeer::_send);
	ClassDB::bind_method(D_METHOD("throttle_configure", "interval", "acceleration", "deceleration"), &ENetPacketPeer::throttle_configure);
	ClassDB::bind_method(D_METHOD("set_timeout", "timeout", "timeout_min", "timeout_max"), &ENetPacketPeer::set_timeout);
	ClassDB::bind_method(D_METHOD("get_statistic", "statistic"), &ENetPacketPeer::get_statistic);
	ClassDB::bind_method(D_METHOD("get_state"), &ENetPacketPeer::get_state);
	ClassDB::bind_method(D_METHOD("get_channels"), &ENetPacketPeer::get_channels);
	ClassDB::bind_method(D_METHOD("is_active"), &ENetPacketPeer::is_active);

	BIND_ENUM_CONSTANT(STATE_DISCONNECTED);
	BIND_ENUM_CONSTANT(STATE_CONNECTING);
	BIND_ENUM_CONSTANT(STATE_ACKNOWLEDGING_CONNECT);
	BIND_ENUM_CONSTANT(STATE_CONNECTION_PENDING);
	BIND_ENUM_CONSTANT(STATE_CONNECTION_SUCCEEDED);
	BIND_ENUM_CONSTANT(STATE_CONNECTED);
	BIND_ENUM_CONSTANT(STATE_DISCONNECT_LATER);
	BIND_ENUM_CONSTANT(STATE_DISCONNECTING);
	BIND_ENUM_CONSTANT(STATE_ACKNOWLEDGING_DISCONNECT);
	BIND_ENUM_CONSTANT(STATE_ZOMBIE);

	BIND_ENUM_CONSTANT(PEER_PACKET_LOSS);
	BIND_ENUM_CONSTANT(PEER_PACKET_LOSS_VARIANCE);
	BIND_ENUM_CONSTANT(PEER_PACKET_LOSS_EPOCH);
	BIND_ENUM_CONSTANT(PEER_ROUND_TRIP_TIME);
	BIND_ENUM_CONSTANT(PEER_ROUND_TRIP_TIME_VARIANCE);
	BIND_ENUM_CONSTANT(PEER_LAST_ROUND_TRIP_TIME);
	BIND_ENUM_CONSTANT(PEER_LAST_ROUND_TRIP_TIME_VARIANCE);
	BIND_ENUM_CONSTANT(PEER_PACKET_THROTTLE);
	BIND_ENUM_CONSTANT(PEER_PACKET_THROTTLE_LIMIT);
	BIND_ENUM_CONSTANT(PEER_PACKET_THROTTLE_COUNTER);
	BIND_ENUM_CONSTANT(PEER_PACKET_THROTTLE_EPOCH);
	BIND_ENUM_CONSTANT(PEER_PACKET_THROTTLE_ACCELERATION);
	BIND_ENUM_CONSTANT(PEER_PACKET_THROTTLE_DECELERATION);
	BIND_ENUM_CONSTANT(PEER_PACKET_THROTTLE_INTERVAL);

	BIND_CONSTANT(PACKET_LOSS_SCALE);
	BIND_CONSTANT(PACKET_THROTTLE_SCALE);

	BIND_CONSTANT(FLAG_RELIABLE);
	BIND_CONSTANT(FLAG_UNSEQUENCED);
	BIND_CONSTANT(FLAG_UNRELIABLE_FRAGMENT);
}

ENetPacketPeer::ENetPacketPeer(ENetPeer *p_peer) {
	peer = p_peer;
	peer->data = this;
}

ENetPacketPeer::~ENetPacketPeer() {
	_on_disconnect();
	if (last_packet) {
		enet_packet_destroy(last_packet);
		last_packet = nullptr;
	}
	for (List<ENetPacket *>::Element *E = packet_queue.front(); E; E = E->next()) {
		enet_packet_destroy(E->get());
	}
	packet_queue.clear();
}