networked_multiplayer_enet.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*************************************************************************/
  2. /* networked_multiplayer_enet.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef NETWORKED_MULTIPLAYER_ENET_H
  30. #define NETWORKED_MULTIPLAYER_ENET_H
  31. #include "io/networked_multiplayer_peer.h"
  32. #include "enet/enet.h"
  33. #include "io/compression.h"
  34. class NetworkedMultiplayerENet : public NetworkedMultiplayerPeer {
  35. OBJ_TYPE(NetworkedMultiplayerENet,NetworkedMultiplayerPeer)
  36. public:
  37. enum CompressionMode {
  38. COMPRESS_NONE,
  39. COMPRESS_RANGE_CODER,
  40. COMPRESS_FASTLZ,
  41. COMPRESS_ZLIB
  42. };
  43. private:
  44. enum {
  45. SYSMSG_ADD_PEER,
  46. SYSMSG_REMOVE_PEER
  47. };
  48. enum {
  49. SYSCH_CONFIG,
  50. SYSCH_RELIABLE,
  51. SYSCH_UNRELIABLE,
  52. SYSCH_MAX
  53. };
  54. bool active;
  55. bool server;
  56. uint32_t unique_id;
  57. int target_peer;
  58. TransferMode transfer_mode;
  59. ENetEvent event;
  60. ENetPeer *peer;
  61. ENetHost *host;
  62. bool refuse_connections;
  63. ConnectionStatus connection_status;
  64. Map<int,ENetPeer*> peer_map;
  65. struct Packet {
  66. ENetPacket *packet;
  67. int from;
  68. };
  69. CompressionMode compression_mode;
  70. mutable List<Packet> incoming_packets;
  71. mutable Packet current_packet;
  72. uint32_t _gen_unique_id() const;
  73. void _pop_current_packet() const;
  74. Vector<uint8_t> src_compressor_mem;
  75. Vector<uint8_t> dst_compressor_mem;
  76. ENetCompressor enet_compressor;
  77. static size_t enet_compress(void * context, const ENetBuffer * inBuffers, size_t inBufferCount, size_t inLimit, enet_uint8 * outData, size_t outLimit);
  78. static size_t enet_decompress (void * context, const enet_uint8 * inData, size_t inLimit, enet_uint8 * outData, size_t outLimit);
  79. static void enet_compressor_destroy(void * context);
  80. void _setup_compressor();
  81. enet_uint32 bind_ip;
  82. protected:
  83. static void _bind_methods();
  84. public:
  85. virtual void set_transfer_mode(TransferMode p_mode);
  86. virtual void set_target_peer(int p_peer);
  87. virtual int get_packet_peer() const;
  88. Error create_server(int p_port, int p_max_peers=32, int p_in_bandwidth=0, int p_out_bandwidth=0);
  89. Error create_client(const IP_Address& p_ip, int p_port, int p_in_bandwidth=0, int p_out_bandwidth=0);
  90. void close_connection();
  91. virtual void poll();
  92. virtual bool is_server() const;
  93. virtual int get_available_packet_count() const;
  94. virtual Error get_packet(const uint8_t **r_buffer,int &r_buffer_size) const; ///< buffer is GONE after next get_packet
  95. virtual Error put_packet(const uint8_t *p_buffer,int p_buffer_size);
  96. virtual int get_max_packet_size() const;
  97. virtual ConnectionStatus get_connection_status() const;
  98. virtual void set_refuse_new_connections(bool p_enable);
  99. virtual bool is_refusing_new_connections() const;
  100. virtual int get_unique_id() const;
  101. void set_compression_mode(CompressionMode p_mode);
  102. CompressionMode get_compression_mode() const;
  103. NetworkedMultiplayerENet();
  104. ~NetworkedMultiplayerENet();
  105. void set_bind_ip(const IP_Address& p_ip);
  106. };
  107. VARIANT_ENUM_CAST(NetworkedMultiplayerENet::CompressionMode);
  108. #endif // NETWORKED_MULTIPLAYER_ENET_H