enet_connection.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #ifndef ENET_CONNECTION_H
  31. #define ENET_CONNECTION_H
  32. #include "enet_packet_peer.h"
  33. #include "core/crypto/crypto.h"
  34. #include "core/object/ref_counted.h"
  35. #include <enet/enet.h>
  36. template <typename T>
  37. class TypedArray;
  38. class ENetConnection : public RefCounted {
  39. GDCLASS(ENetConnection, RefCounted);
  40. public:
  41. enum CompressionMode {
  42. COMPRESS_NONE = 0,
  43. COMPRESS_RANGE_CODER,
  44. COMPRESS_FASTLZ,
  45. COMPRESS_ZLIB,
  46. COMPRESS_ZSTD,
  47. };
  48. enum HostStatistic {
  49. HOST_TOTAL_SENT_DATA,
  50. HOST_TOTAL_SENT_PACKETS,
  51. HOST_TOTAL_RECEIVED_DATA,
  52. HOST_TOTAL_RECEIVED_PACKETS,
  53. };
  54. enum EventType {
  55. EVENT_ERROR = -1,
  56. EVENT_NONE = 0,
  57. EVENT_CONNECT,
  58. EVENT_DISCONNECT,
  59. EVENT_RECEIVE,
  60. };
  61. struct Event {
  62. Ref<ENetPacketPeer> peer;
  63. enet_uint8 channel_id = 0;
  64. enet_uint32 data = 0;
  65. ENetPacket *packet = nullptr;
  66. };
  67. protected:
  68. static void _bind_methods();
  69. private:
  70. ENetHost *host = nullptr;
  71. List<Ref<ENetPacketPeer>> peers;
  72. EventType _parse_event(const ENetEvent &p_event, Event &r_event);
  73. Error _create(ENetAddress *p_address, int p_max_peers, int p_max_channels, int p_in_bandwidth, int p_out_bandwidth);
  74. Array _service(int p_timeout = 0);
  75. void _broadcast(int p_channel, PackedByteArray p_packet, int p_flags);
  76. TypedArray<ENetPacketPeer> _get_peers();
  77. class Compressor {
  78. private:
  79. CompressionMode mode = COMPRESS_NONE;
  80. Vector<uint8_t> src_mem;
  81. Vector<uint8_t> dst_mem;
  82. ENetCompressor enet_compressor;
  83. Compressor(CompressionMode mode);
  84. static size_t enet_compress(void *context, const ENetBuffer *inBuffers, size_t inBufferCount, size_t inLimit, enet_uint8 *outData, size_t outLimit);
  85. static size_t enet_decompress(void *context, const enet_uint8 *inData, size_t inLimit, enet_uint8 *outData, size_t outLimit);
  86. static void enet_compressor_destroy(void *context) {
  87. memdelete((Compressor *)context);
  88. }
  89. public:
  90. static void setup(ENetHost *p_host, CompressionMode p_mode);
  91. };
  92. public:
  93. void broadcast(enet_uint8 p_channel, ENetPacket *p_packet);
  94. void socket_send(const String &p_address, int p_port, const PackedByteArray &p_packet);
  95. 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);
  96. Error create_host(int p_max_peers = 32, int p_max_channels = 0, int p_in_bandwidth = 0, int p_out_bandwidth = 0);
  97. void destroy();
  98. Ref<ENetPacketPeer> connect_to_host(const String &p_address, int p_port, int p_channels, int p_data = 0);
  99. EventType service(int p_timeout, Event &r_event);
  100. int check_events(EventType &r_type, Event &r_event);
  101. void flush();
  102. void bandwidth_limit(int p_in_bandwidth = 0, int p_out_bandwidth = 0);
  103. void channel_limit(int p_max_channels);
  104. void bandwidth_throttle();
  105. void compress(CompressionMode p_mode);
  106. double pop_statistic(HostStatistic p_stat);
  107. int get_max_channels() const;
  108. // Extras
  109. void get_peers(List<Ref<ENetPacketPeer>> &r_peers);
  110. int get_local_port() const;
  111. // Godot additions
  112. Error dtls_server_setup(const Ref<TLSOptions> &p_options);
  113. Error dtls_client_setup(const String &p_hostname, const Ref<TLSOptions> &p_options);
  114. void refuse_new_connections(bool p_refuse);
  115. ENetConnection() {}
  116. ~ENetConnection();
  117. };
  118. VARIANT_ENUM_CAST(ENetConnection::CompressionMode);
  119. VARIANT_ENUM_CAST(ENetConnection::EventType);
  120. VARIANT_ENUM_CAST(ENetConnection::HostStatistic);
  121. #endif // ENET_CONNECTION_H