networked_multiplayer_enet.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "io/compression.h"
  33. #include "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. ENetEvent event;
  62. ENetPeer *peer;
  63. ENetHost *host;
  64. bool refuse_connections;
  65. ConnectionStatus connection_status;
  66. Map<int, ENetPeer *> peer_map;
  67. struct Packet {
  68. ENetPacket *packet;
  69. int from;
  70. };
  71. CompressionMode compression_mode;
  72. mutable List<Packet> incoming_packets;
  73. mutable Packet current_packet;
  74. uint32_t _gen_unique_id() const;
  75. void _pop_current_packet() const;
  76. Vector<uint8_t> src_compressor_mem;
  77. Vector<uint8_t> dst_compressor_mem;
  78. ENetCompressor enet_compressor;
  79. static size_t enet_compress(void *context, const ENetBuffer *inBuffers, size_t inBufferCount, size_t inLimit, enet_uint8 *outData, size_t outLimit);
  80. static size_t enet_decompress(void *context, const enet_uint8 *inData, size_t inLimit, enet_uint8 *outData, size_t outLimit);
  81. static void enet_compressor_destroy(void *context);
  82. void _setup_compressor();
  83. IP_Address bind_ip;
  84. protected:
  85. static void _bind_methods();
  86. public:
  87. virtual void set_transfer_mode(TransferMode p_mode);
  88. virtual void set_target_peer(int p_peer);
  89. virtual int get_packet_peer() const;
  90. Error create_server(int p_port, int p_max_clients = 32, int p_in_bandwidth = 0, int p_out_bandwidth = 0);
  91. Error create_client(const IP_Address &p_ip, int p_port, int p_in_bandwidth = 0, int p_out_bandwidth = 0);
  92. void close_connection();
  93. virtual void poll();
  94. virtual bool is_server() const;
  95. virtual int get_available_packet_count() const;
  96. virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) const; ///< buffer is GONE after next get_packet
  97. virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size);
  98. virtual int get_max_packet_size() const;
  99. virtual ConnectionStatus get_connection_status() const;
  100. virtual void set_refuse_new_connections(bool p_enable);
  101. virtual bool is_refusing_new_connections() const;
  102. virtual int get_unique_id() const;
  103. void set_compression_mode(CompressionMode p_mode);
  104. CompressionMode get_compression_mode() const;
  105. NetworkedMultiplayerENet();
  106. ~NetworkedMultiplayerENet();
  107. void set_bind_ip(const IP_Address &p_ip);
  108. };
  109. VARIANT_ENUM_CAST(NetworkedMultiplayerENet::CompressionMode);
  110. #endif // NETWORKED_MULTIPLAYER_ENET_H