enet_packet_peer.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**************************************************************************/
  2. /* enet_packet_peer.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. #ifndef ENET_PACKET_PEER_H
  31. #define ENET_PACKET_PEER_H
  32. #include "core/io/packet_peer.h"
  33. #include <enet/enet.h>
  34. class ENetPacketPeer : public PacketPeer {
  35. GDCLASS(ENetPacketPeer, PacketPeer);
  36. private:
  37. ENetPeer *peer = nullptr;
  38. List<ENetPacket *> packet_queue;
  39. ENetPacket *last_packet = nullptr;
  40. static void _bind_methods();
  41. Error _send(int p_channel, PackedByteArray p_packet, int p_flags);
  42. protected:
  43. friend class ENetConnection;
  44. // Internally used by ENetConnection during service, destroy, etc.
  45. void _on_disconnect();
  46. void _queue_packet(ENetPacket *p_packet);
  47. public:
  48. enum {
  49. PACKET_THROTTLE_SCALE = ENET_PEER_PACKET_THROTTLE_SCALE,
  50. PACKET_LOSS_SCALE = ENET_PEER_PACKET_LOSS_SCALE,
  51. };
  52. enum {
  53. FLAG_RELIABLE = ENET_PACKET_FLAG_RELIABLE,
  54. FLAG_UNSEQUENCED = ENET_PACKET_FLAG_UNSEQUENCED,
  55. FLAG_UNRELIABLE_FRAGMENT = ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT,
  56. FLAG_ALLOWED = ENET_PACKET_FLAG_RELIABLE | ENET_PACKET_FLAG_UNSEQUENCED | ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT,
  57. };
  58. enum PeerState {
  59. STATE_DISCONNECTED = ENET_PEER_STATE_DISCONNECTED,
  60. STATE_CONNECTING = ENET_PEER_STATE_CONNECTING,
  61. STATE_ACKNOWLEDGING_CONNECT = ENET_PEER_STATE_ACKNOWLEDGING_CONNECT,
  62. STATE_CONNECTION_PENDING = ENET_PEER_STATE_CONNECTION_PENDING,
  63. STATE_CONNECTION_SUCCEEDED = ENET_PEER_STATE_CONNECTION_SUCCEEDED,
  64. STATE_CONNECTED = ENET_PEER_STATE_CONNECTED,
  65. STATE_DISCONNECT_LATER = ENET_PEER_STATE_DISCONNECT_LATER,
  66. STATE_DISCONNECTING = ENET_PEER_STATE_DISCONNECTING,
  67. STATE_ACKNOWLEDGING_DISCONNECT = ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT,
  68. STATE_ZOMBIE = ENET_PEER_STATE_ZOMBIE,
  69. };
  70. enum PeerStatistic {
  71. PEER_PACKET_LOSS,
  72. PEER_PACKET_LOSS_VARIANCE,
  73. PEER_PACKET_LOSS_EPOCH,
  74. PEER_ROUND_TRIP_TIME,
  75. PEER_ROUND_TRIP_TIME_VARIANCE,
  76. PEER_LAST_ROUND_TRIP_TIME,
  77. PEER_LAST_ROUND_TRIP_TIME_VARIANCE,
  78. PEER_PACKET_THROTTLE,
  79. PEER_PACKET_THROTTLE_LIMIT,
  80. PEER_PACKET_THROTTLE_COUNTER,
  81. PEER_PACKET_THROTTLE_EPOCH,
  82. PEER_PACKET_THROTTLE_ACCELERATION,
  83. PEER_PACKET_THROTTLE_DECELERATION,
  84. PEER_PACKET_THROTTLE_INTERVAL,
  85. };
  86. int get_max_packet_size() const override;
  87. int get_available_packet_count() const override;
  88. Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; ///< buffer is GONE after next get_packet
  89. Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override;
  90. void peer_disconnect(int p_data = 0);
  91. void peer_disconnect_later(int p_data = 0);
  92. void peer_disconnect_now(int p_data = 0);
  93. void ping();
  94. void ping_interval(int p_interval);
  95. void reset();
  96. int send(uint8_t p_channel, ENetPacket *p_packet);
  97. void throttle_configure(int interval, int acceleration, int deceleration);
  98. void set_timeout(int p_timeout, int p_timeout_min, int p_timeout_max);
  99. double get_statistic(PeerStatistic p_stat);
  100. PeerState get_state() const;
  101. int get_channels() const;
  102. int get_packet_flags() const;
  103. // Extras
  104. IPAddress get_remote_address() const;
  105. int get_remote_port() const;
  106. // Used by ENetMultiplayer (TODO use meta? If only they where StringNames)
  107. bool is_active() const;
  108. ENetPacketPeer(ENetPeer *p_peer);
  109. ~ENetPacketPeer();
  110. };
  111. VARIANT_ENUM_CAST(ENetPacketPeer::PeerState);
  112. VARIANT_ENUM_CAST(ENetPacketPeer::PeerStatistic);
  113. #endif // ENET_PACKET_PEER_H