networked_multiplayer_enet.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/io/compression.h"
  33. #include "core/io/networked_multiplayer_peer.h"
  34. #include <enet/enet.h>
  35. class NetworkedMultiplayerENet : public NetworkedMultiplayerPeer {
  36. GDCLASS(NetworkedMultiplayerENet, NetworkedMultiplayerPeer)
  37. public:
  38. enum CompressionMode {
  39. COMPRESS_NONE,
  40. COMPRESS_RANGE_CODER,
  41. COMPRESS_FASTLZ,
  42. COMPRESS_ZLIB,
  43. COMPRESS_ZSTD
  44. };
  45. private:
  46. enum {
  47. SYSMSG_ADD_PEER,
  48. SYSMSG_REMOVE_PEER
  49. };
  50. enum {
  51. SYSCH_CONFIG,
  52. SYSCH_RELIABLE,
  53. SYSCH_UNRELIABLE,
  54. SYSCH_MAX
  55. };
  56. bool active;
  57. bool server;
  58. uint32_t unique_id;
  59. int target_peer;
  60. TransferMode transfer_mode;
  61. int transfer_channel;
  62. int channel_count;
  63. bool always_ordered;
  64. ENetEvent event;
  65. ENetPeer *peer;
  66. ENetHost *host;
  67. bool refuse_connections;
  68. ConnectionStatus connection_status;
  69. Map<int, ENetPeer *> peer_map;
  70. struct Packet {
  71. ENetPacket *packet;
  72. int from;
  73. int channel;
  74. };
  75. CompressionMode compression_mode;
  76. List<Packet> incoming_packets;
  77. Packet current_packet;
  78. uint32_t _gen_unique_id() const;
  79. void _pop_current_packet();
  80. Vector<uint8_t> src_compressor_mem;
  81. Vector<uint8_t> dst_compressor_mem;
  82. ENetCompressor enet_compressor;
  83. static size_t enet_compress(void *context, const ENetBuffer *inBuffers, size_t inBufferCount, size_t inLimit, enet_uint8 *outData, size_t outLimit);
  84. static size_t enet_decompress(void *context, const enet_uint8 *inData, size_t inLimit, enet_uint8 *outData, size_t outLimit);
  85. static void enet_compressor_destroy(void *context);
  86. void _setup_compressor();
  87. IP_Address bind_ip;
  88. protected:
  89. static void _bind_methods();
  90. public:
  91. virtual void set_transfer_mode(TransferMode p_mode);
  92. virtual TransferMode get_transfer_mode() const;
  93. virtual void set_target_peer(int p_peer);
  94. virtual int get_packet_peer() const;
  95. virtual IP_Address get_peer_address(int p_peer_id) const;
  96. virtual int get_peer_port(int p_peer_id) const;
  97. Error create_server(int p_port, int p_max_clients = 32, int p_in_bandwidth = 0, int p_out_bandwidth = 0);
  98. 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);
  99. void close_connection(uint32_t wait_usec = 100);
  100. void disconnect_peer(int p_peer, bool now = false);
  101. virtual void poll();
  102. virtual bool is_server() const;
  103. virtual int get_available_packet_count() const;
  104. virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size); ///< buffer is GONE after next get_packet
  105. virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size);
  106. virtual int get_max_packet_size() const;
  107. virtual ConnectionStatus get_connection_status() const;
  108. virtual void set_refuse_new_connections(bool p_enable);
  109. virtual bool is_refusing_new_connections() const;
  110. virtual int get_unique_id() const;
  111. void set_compression_mode(CompressionMode p_mode);
  112. CompressionMode get_compression_mode() const;
  113. int get_packet_channel() const;
  114. int get_last_packet_channel() const;
  115. void set_transfer_channel(int p_channel);
  116. int get_transfer_channel() const;
  117. void set_channel_count(int p_channel);
  118. int get_channel_count() const;
  119. void set_always_ordered(bool p_ordered);
  120. bool is_always_ordered() const;
  121. NetworkedMultiplayerENet();
  122. ~NetworkedMultiplayerENet();
  123. void set_bind_ip(const IP_Address &p_ip);
  124. };
  125. VARIANT_ENUM_CAST(NetworkedMultiplayerENet::CompressionMode);
  126. #endif // NETWORKED_MULTIPLAYER_ENET_H