1 | /**************************************************************************/ |
2 | /* enet_packet_peer.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_packet_peer.h" |
32 | |
33 | void ENetPacketPeer::peer_disconnect(int p_data) { |
34 | ERR_FAIL_COND(!peer); |
35 | enet_peer_disconnect(peer, p_data); |
36 | } |
37 | |
38 | void ENetPacketPeer::peer_disconnect_later(int p_data) { |
39 | ERR_FAIL_COND(!peer); |
40 | enet_peer_disconnect_later(peer, p_data); |
41 | } |
42 | |
43 | void ENetPacketPeer::peer_disconnect_now(int p_data) { |
44 | ERR_FAIL_COND(!peer); |
45 | enet_peer_disconnect_now(peer, p_data); |
46 | _on_disconnect(); |
47 | } |
48 | |
49 | void ENetPacketPeer::ping() { |
50 | ERR_FAIL_COND(!peer); |
51 | enet_peer_ping(peer); |
52 | } |
53 | |
54 | void ENetPacketPeer::ping_interval(int p_interval) { |
55 | ERR_FAIL_COND(!peer); |
56 | enet_peer_ping_interval(peer, p_interval); |
57 | } |
58 | |
59 | int ENetPacketPeer::send(uint8_t p_channel, ENetPacket *p_packet) { |
60 | ERR_FAIL_COND_V(peer == nullptr, -1); |
61 | ERR_FAIL_COND_V(p_packet == nullptr, -1); |
62 | 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)); |
63 | return enet_peer_send(peer, p_channel, p_packet); |
64 | } |
65 | |
66 | void ENetPacketPeer::reset() { |
67 | ERR_FAIL_COND_MSG(peer == nullptr, "Peer not connected" ); |
68 | enet_peer_reset(peer); |
69 | _on_disconnect(); |
70 | } |
71 | |
72 | void ENetPacketPeer::throttle_configure(int p_interval, int p_acceleration, int p_deceleration) { |
73 | ERR_FAIL_COND_MSG(peer == nullptr, "Peer not connected" ); |
74 | enet_peer_throttle_configure(peer, p_interval, p_acceleration, p_deceleration); |
75 | } |
76 | |
77 | void ENetPacketPeer::set_timeout(int p_timeout, int p_timeout_min, int p_timeout_max) { |
78 | ERR_FAIL_COND_MSG(peer == nullptr, "Peer not connected" ); |
79 | 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 than maximum timeout" ); |
80 | enet_peer_timeout(peer, p_timeout, p_timeout_min, p_timeout_max); |
81 | } |
82 | |
83 | int ENetPacketPeer::get_max_packet_size() const { |
84 | return 1 << 24; |
85 | } |
86 | |
87 | int ENetPacketPeer::get_available_packet_count() const { |
88 | return packet_queue.size(); |
89 | } |
90 | |
91 | Error ENetPacketPeer::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { |
92 | ERR_FAIL_COND_V(!peer, ERR_UNCONFIGURED); |
93 | ERR_FAIL_COND_V(!packet_queue.size(), ERR_UNAVAILABLE); |
94 | if (last_packet) { |
95 | enet_packet_destroy(last_packet); |
96 | last_packet = nullptr; |
97 | } |
98 | last_packet = packet_queue.front()->get(); |
99 | packet_queue.pop_front(); |
100 | *r_buffer = (const uint8_t *)(last_packet->data); |
101 | r_buffer_size = last_packet->dataLength; |
102 | return OK; |
103 | } |
104 | |
105 | Error ENetPacketPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size) { |
106 | ERR_FAIL_COND_V(!peer, ERR_UNCONFIGURED); |
107 | ENetPacket *packet = enet_packet_create(p_buffer, p_buffer_size, ENET_PACKET_FLAG_RELIABLE); |
108 | return send(0, packet) < 0 ? FAILED : OK; |
109 | } |
110 | |
111 | IPAddress ENetPacketPeer::get_remote_address() const { |
112 | ERR_FAIL_COND_V(!peer, IPAddress()); |
113 | IPAddress out; |
114 | #ifdef GODOT_ENET |
115 | out.set_ipv6((uint8_t *)&(peer->address.host)); |
116 | #else |
117 | out.set_ipv4((uint8_t *)&(peer->address.host)); |
118 | #endif |
119 | return out; |
120 | } |
121 | |
122 | int ENetPacketPeer::get_remote_port() const { |
123 | ERR_FAIL_COND_V(!peer, 0); |
124 | return peer->address.port; |
125 | } |
126 | |
127 | bool ENetPacketPeer::is_active() const { |
128 | return peer != nullptr; |
129 | } |
130 | |
131 | double ENetPacketPeer::get_statistic(PeerStatistic p_stat) { |
132 | ERR_FAIL_COND_V(!peer, 0); |
133 | switch (p_stat) { |
134 | case PEER_PACKET_LOSS: |
135 | return peer->packetLoss; |
136 | case PEER_PACKET_LOSS_VARIANCE: |
137 | return peer->packetLossVariance; |
138 | case PEER_PACKET_LOSS_EPOCH: |
139 | return peer->packetLossEpoch; |
140 | case PEER_ROUND_TRIP_TIME: |
141 | return peer->roundTripTime; |
142 | case PEER_ROUND_TRIP_TIME_VARIANCE: |
143 | return peer->roundTripTimeVariance; |
144 | case PEER_LAST_ROUND_TRIP_TIME: |
145 | return peer->lastRoundTripTime; |
146 | case PEER_LAST_ROUND_TRIP_TIME_VARIANCE: |
147 | return peer->lastRoundTripTimeVariance; |
148 | case PEER_PACKET_THROTTLE: |
149 | return peer->packetThrottle; |
150 | case PEER_PACKET_THROTTLE_LIMIT: |
151 | return peer->packetThrottleLimit; |
152 | case PEER_PACKET_THROTTLE_COUNTER: |
153 | return peer->packetThrottleCounter; |
154 | case PEER_PACKET_THROTTLE_EPOCH: |
155 | return peer->packetThrottleEpoch; |
156 | case PEER_PACKET_THROTTLE_ACCELERATION: |
157 | return peer->packetThrottleAcceleration; |
158 | case PEER_PACKET_THROTTLE_DECELERATION: |
159 | return peer->packetThrottleDeceleration; |
160 | case PEER_PACKET_THROTTLE_INTERVAL: |
161 | return peer->packetThrottleInterval; |
162 | } |
163 | ERR_FAIL_V(0); |
164 | } |
165 | |
166 | ENetPacketPeer::PeerState ENetPacketPeer::get_state() const { |
167 | if (!is_active()) { |
168 | return STATE_DISCONNECTED; |
169 | } |
170 | return (PeerState)peer->state; |
171 | } |
172 | |
173 | int ENetPacketPeer::get_channels() const { |
174 | ERR_FAIL_COND_V_MSG(!peer, 0, "The ENetConnection instance isn't currently active." ); |
175 | return peer->channelCount; |
176 | } |
177 | |
178 | void ENetPacketPeer::_on_disconnect() { |
179 | if (peer) { |
180 | peer->data = nullptr; |
181 | } |
182 | peer = nullptr; |
183 | } |
184 | |
185 | void ENetPacketPeer::_queue_packet(ENetPacket *p_packet) { |
186 | ERR_FAIL_COND(!peer); |
187 | packet_queue.push_back(p_packet); |
188 | } |
189 | |
190 | Error ENetPacketPeer::_send(int p_channel, PackedByteArray p_packet, int p_flags) { |
191 | ERR_FAIL_COND_V_MSG(peer == nullptr, ERR_UNCONFIGURED, "Peer not connected" ); |
192 | ERR_FAIL_COND_V_MSG(p_channel < 0 || p_channel > (int)peer->channelCount, ERR_INVALID_PARAMETER, "Invalid channel" ); |
193 | ERR_FAIL_COND_V_MSG(p_flags & ~FLAG_ALLOWED, ERR_INVALID_PARAMETER, "Invalid flags" ); |
194 | ENetPacket *packet = enet_packet_create(p_packet.ptr(), p_packet.size(), p_flags); |
195 | return send(p_channel, packet) == 0 ? OK : FAILED; |
196 | } |
197 | |
198 | void ENetPacketPeer::_bind_methods() { |
199 | ClassDB::bind_method(D_METHOD("peer_disconnect" , "data" ), &ENetPacketPeer::peer_disconnect, DEFVAL(0)); |
200 | ClassDB::bind_method(D_METHOD("peer_disconnect_later" , "data" ), &ENetPacketPeer::peer_disconnect_later, DEFVAL(0)); |
201 | ClassDB::bind_method(D_METHOD("peer_disconnect_now" , "data" ), &ENetPacketPeer::peer_disconnect_now, DEFVAL(0)); |
202 | |
203 | ClassDB::bind_method(D_METHOD("ping" ), &ENetPacketPeer::ping); |
204 | ClassDB::bind_method(D_METHOD("ping_interval" , "ping_interval" ), &ENetPacketPeer::ping_interval); |
205 | ClassDB::bind_method(D_METHOD("reset" ), &ENetPacketPeer::reset); |
206 | ClassDB::bind_method(D_METHOD("send" , "channel" , "packet" , "flags" ), &ENetPacketPeer::_send); |
207 | ClassDB::bind_method(D_METHOD("throttle_configure" , "interval" , "acceleration" , "deceleration" ), &ENetPacketPeer::throttle_configure); |
208 | ClassDB::bind_method(D_METHOD("set_timeout" , "timeout" , "timeout_min" , "timeout_max" ), &ENetPacketPeer::set_timeout); |
209 | ClassDB::bind_method(D_METHOD("get_remote_address" ), &ENetPacketPeer::get_remote_address); |
210 | ClassDB::bind_method(D_METHOD("get_remote_port" ), &ENetPacketPeer::get_remote_port); |
211 | ClassDB::bind_method(D_METHOD("get_statistic" , "statistic" ), &ENetPacketPeer::get_statistic); |
212 | ClassDB::bind_method(D_METHOD("get_state" ), &ENetPacketPeer::get_state); |
213 | ClassDB::bind_method(D_METHOD("get_channels" ), &ENetPacketPeer::get_channels); |
214 | ClassDB::bind_method(D_METHOD("is_active" ), &ENetPacketPeer::is_active); |
215 | |
216 | BIND_ENUM_CONSTANT(STATE_DISCONNECTED); |
217 | BIND_ENUM_CONSTANT(STATE_CONNECTING); |
218 | BIND_ENUM_CONSTANT(STATE_ACKNOWLEDGING_CONNECT); |
219 | BIND_ENUM_CONSTANT(STATE_CONNECTION_PENDING); |
220 | BIND_ENUM_CONSTANT(STATE_CONNECTION_SUCCEEDED); |
221 | BIND_ENUM_CONSTANT(STATE_CONNECTED); |
222 | BIND_ENUM_CONSTANT(STATE_DISCONNECT_LATER); |
223 | BIND_ENUM_CONSTANT(STATE_DISCONNECTING); |
224 | BIND_ENUM_CONSTANT(STATE_ACKNOWLEDGING_DISCONNECT); |
225 | BIND_ENUM_CONSTANT(STATE_ZOMBIE); |
226 | |
227 | BIND_ENUM_CONSTANT(PEER_PACKET_LOSS); |
228 | BIND_ENUM_CONSTANT(PEER_PACKET_LOSS_VARIANCE); |
229 | BIND_ENUM_CONSTANT(PEER_PACKET_LOSS_EPOCH); |
230 | BIND_ENUM_CONSTANT(PEER_ROUND_TRIP_TIME); |
231 | BIND_ENUM_CONSTANT(PEER_ROUND_TRIP_TIME_VARIANCE); |
232 | BIND_ENUM_CONSTANT(PEER_LAST_ROUND_TRIP_TIME); |
233 | BIND_ENUM_CONSTANT(PEER_LAST_ROUND_TRIP_TIME_VARIANCE); |
234 | BIND_ENUM_CONSTANT(PEER_PACKET_THROTTLE); |
235 | BIND_ENUM_CONSTANT(PEER_PACKET_THROTTLE_LIMIT); |
236 | BIND_ENUM_CONSTANT(PEER_PACKET_THROTTLE_COUNTER); |
237 | BIND_ENUM_CONSTANT(PEER_PACKET_THROTTLE_EPOCH); |
238 | BIND_ENUM_CONSTANT(PEER_PACKET_THROTTLE_ACCELERATION); |
239 | BIND_ENUM_CONSTANT(PEER_PACKET_THROTTLE_DECELERATION); |
240 | BIND_ENUM_CONSTANT(PEER_PACKET_THROTTLE_INTERVAL); |
241 | |
242 | BIND_CONSTANT(PACKET_LOSS_SCALE); |
243 | BIND_CONSTANT(PACKET_THROTTLE_SCALE); |
244 | |
245 | BIND_CONSTANT(FLAG_RELIABLE); |
246 | BIND_CONSTANT(FLAG_UNSEQUENCED); |
247 | BIND_CONSTANT(FLAG_UNRELIABLE_FRAGMENT); |
248 | } |
249 | |
250 | ENetPacketPeer::ENetPacketPeer(ENetPeer *p_peer) { |
251 | peer = p_peer; |
252 | peer->data = this; |
253 | } |
254 | |
255 | ENetPacketPeer::~ENetPacketPeer() { |
256 | _on_disconnect(); |
257 | if (last_packet) { |
258 | enet_packet_destroy(last_packet); |
259 | last_packet = nullptr; |
260 | } |
261 | for (List<ENetPacket *>::Element *E = packet_queue.front(); E; E = E->next()) { |
262 | enet_packet_destroy(E->get()); |
263 | } |
264 | packet_queue.clear(); |
265 | } |
266 | |