2
0

enet_multiplayer_peer.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*************************************************************************/
  2. /* enet_multiplayer_peer.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/multiplayer/multiplayer_peer.h"
  34. #include "enet_connection.h"
  35. #include <enet/enet.h>
  36. class ENetMultiplayerPeer : public MultiplayerPeer {
  37. GDCLASS(ENetMultiplayerPeer, MultiplayerPeer);
  38. private:
  39. enum {
  40. SYSMSG_ADD_PEER,
  41. SYSMSG_REMOVE_PEER
  42. };
  43. enum {
  44. SYSCH_CONFIG = 0,
  45. SYSCH_RELIABLE = 1,
  46. SYSCH_UNRELIABLE = 2,
  47. SYSCH_MAX = 3
  48. };
  49. enum Mode {
  50. MODE_NONE,
  51. MODE_SERVER,
  52. MODE_CLIENT,
  53. MODE_MESH,
  54. };
  55. Mode active_mode = MODE_NONE;
  56. uint32_t unique_id = 0;
  57. int target_peer = 0;
  58. bool server_relay = true;
  59. ConnectionStatus connection_status = CONNECTION_DISCONNECTED;
  60. Map<int, Ref<ENetConnection>> hosts;
  61. Map<int, Ref<ENetPacketPeer>> peers;
  62. struct Packet {
  63. ENetPacket *packet = nullptr;
  64. int from = 0;
  65. int channel = 0;
  66. };
  67. List<Packet> incoming_packets;
  68. Packet current_packet;
  69. void _pop_current_packet();
  70. bool _poll_server();
  71. bool _poll_client();
  72. bool _poll_mesh();
  73. void _relay(int p_from, int p_to, enet_uint8 p_channel, ENetPacket *p_packet);
  74. void _notify_peers(int p_id, bool p_connected);
  75. void _destroy_unused(ENetPacket *p_packet);
  76. _FORCE_INLINE_ bool _is_active() const { return active_mode != MODE_NONE; }
  77. IPAddress bind_ip;
  78. protected:
  79. static void _bind_methods();
  80. public:
  81. virtual void set_target_peer(int p_peer) override;
  82. virtual int get_packet_peer() const override;
  83. virtual void poll() override;
  84. virtual bool is_server() const override;
  85. // Overriden so we can instrument the DTLSServer when needed.
  86. virtual void set_refuse_new_connections(bool p_enabled) override;
  87. virtual ConnectionStatus get_connection_status() const override;
  88. virtual int get_unique_id() const override;
  89. virtual int get_max_packet_size() const override;
  90. virtual int get_available_packet_count() const override;
  91. virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override;
  92. virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override;
  93. Error create_server(int p_port, int p_max_clients = 32, int p_max_channels = 0, int p_in_bandwidth = 0, int p_out_bandwidth = 0);
  94. Error create_client(const String &p_address, int p_port, int p_channel_count = 0, int p_in_bandwidth = 0, int p_out_bandwidth = 0, int p_local_port = 0);
  95. Error create_mesh(int p_id);
  96. Error add_mesh_peer(int p_id, Ref<ENetConnection> p_host);
  97. void close_connection(uint32_t wait_usec = 100);
  98. void disconnect_peer(int p_peer, bool now = false);
  99. void set_bind_ip(const IPAddress &p_ip);
  100. void set_server_relay_enabled(bool p_enabled);
  101. bool is_server_relay_enabled() const;
  102. Ref<ENetConnection> get_host() const;
  103. Ref<ENetPacketPeer> get_peer(int p_id) const;
  104. ENetMultiplayerPeer();
  105. ~ENetMultiplayerPeer();
  106. };
  107. #endif // NETWORKED_MULTIPLAYER_ENET_H