enet_packet_peer.cpp 10 KB

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