networked_multiplayer_enet.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*************************************************************************/
  2. /* networked_multiplayer_enet.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 NETWORKED_MULTIPLAYER_ENET_H
  31. #define NETWORKED_MULTIPLAYER_ENET_H
  32. #include "core/crypto/crypto.h"
  33. #include "core/io/compression.h"
  34. #include "core/io/networked_multiplayer_peer.h"
  35. #include <enet/enet.h>
  36. class NetworkedMultiplayerENet : public NetworkedMultiplayerPeer {
  37. GDCLASS(NetworkedMultiplayerENet, NetworkedMultiplayerPeer);
  38. public:
  39. enum CompressionMode {
  40. COMPRESS_NONE,
  41. COMPRESS_RANGE_CODER,
  42. COMPRESS_FASTLZ,
  43. COMPRESS_ZLIB,
  44. COMPRESS_ZSTD
  45. };
  46. private:
  47. enum {
  48. SYSMSG_ADD_PEER,
  49. SYSMSG_REMOVE_PEER
  50. };
  51. enum {
  52. SYSCH_CONFIG,
  53. SYSCH_RELIABLE,
  54. SYSCH_UNRELIABLE,
  55. SYSCH_MAX
  56. };
  57. bool active;
  58. bool server;
  59. uint32_t unique_id;
  60. int target_peer;
  61. TransferMode transfer_mode;
  62. int transfer_channel;
  63. int channel_count;
  64. bool always_ordered;
  65. ENetEvent event;
  66. ENetPeer *peer;
  67. ENetHost *host;
  68. bool refuse_connections;
  69. bool server_relay;
  70. ConnectionStatus connection_status;
  71. Map<int, ENetPeer *> peer_map;
  72. struct Packet {
  73. ENetPacket *packet;
  74. int from;
  75. int channel;
  76. };
  77. CompressionMode compression_mode;
  78. List<Packet> incoming_packets;
  79. Packet current_packet;
  80. uint32_t _gen_unique_id() const;
  81. void _pop_current_packet();
  82. Vector<uint8_t> src_compressor_mem;
  83. Vector<uint8_t> dst_compressor_mem;
  84. ENetCompressor enet_compressor;
  85. static size_t enet_compress(void *context, const ENetBuffer *inBuffers, size_t inBufferCount, size_t inLimit, enet_uint8 *outData, size_t outLimit);
  86. static size_t enet_decompress(void *context, const enet_uint8 *inData, size_t inLimit, enet_uint8 *outData, size_t outLimit);
  87. static void enet_compressor_destroy(void *context);
  88. void _setup_compressor();
  89. IP_Address bind_ip;
  90. bool dtls_enabled;
  91. Ref<CryptoKey> dtls_key;
  92. Ref<X509Certificate> dtls_cert;
  93. bool dtls_verify;
  94. String dtls_hostname;
  95. protected:
  96. static void _bind_methods();
  97. public:
  98. virtual void set_transfer_mode(TransferMode p_mode);
  99. virtual TransferMode get_transfer_mode() const;
  100. virtual void set_target_peer(int p_peer);
  101. virtual int get_packet_peer() const;
  102. virtual IP_Address get_peer_address(int p_peer_id) const;
  103. virtual int get_peer_port(int p_peer_id) const;
  104. void set_peer_timeout(int p_peer_id, int p_timeout_limit, int p_timeout_min, int p_timeout_max);
  105. Error create_server(int p_port, int p_max_clients = 32, int p_in_bandwidth = 0, int p_out_bandwidth = 0);
  106. Error create_client(const String &p_address, int p_port, int p_in_bandwidth = 0, int p_out_bandwidth = 0, int p_client_port = 0);
  107. void close_connection(uint32_t wait_usec = 100);
  108. void disconnect_peer(int p_peer, bool now = false);
  109. virtual void poll();
  110. virtual bool is_server() const;
  111. virtual int get_available_packet_count() const;
  112. virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size); ///< buffer is GONE after next get_packet
  113. virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size);
  114. virtual int get_max_packet_size() const;
  115. virtual ConnectionStatus get_connection_status() const;
  116. virtual void set_refuse_new_connections(bool p_enable);
  117. virtual bool is_refusing_new_connections() const;
  118. virtual int get_unique_id() const;
  119. void set_compression_mode(CompressionMode p_mode);
  120. CompressionMode get_compression_mode() const;
  121. int get_packet_channel() const;
  122. int get_last_packet_channel() const;
  123. void set_transfer_channel(int p_channel);
  124. int get_transfer_channel() const;
  125. void set_channel_count(int p_channel);
  126. int get_channel_count() const;
  127. void set_always_ordered(bool p_ordered);
  128. bool is_always_ordered() const;
  129. void set_server_relay_enabled(bool p_enabled);
  130. bool is_server_relay_enabled() const;
  131. NetworkedMultiplayerENet();
  132. ~NetworkedMultiplayerENet();
  133. void set_bind_ip(const IP_Address &p_ip);
  134. void set_dtls_enabled(bool p_enabled);
  135. bool is_dtls_enabled() const;
  136. void set_dtls_verify_enabled(bool p_enabled);
  137. bool is_dtls_verify_enabled() const;
  138. void set_dtls_key(Ref<CryptoKey> p_key);
  139. void set_dtls_certificate(Ref<X509Certificate> p_cert);
  140. void set_dtls_hostname(const String &p_hostname);
  141. String get_dtls_hostname() const;
  142. };
  143. VARIANT_ENUM_CAST(NetworkedMultiplayerENet::CompressionMode);
  144. #endif // NETWORKED_MULTIPLAYER_ENET_H