enet_multiplayer_peer.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**************************************************************************/
  2. /* enet_multiplayer_peer.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 ENET_MULTIPLAYER_PEER_H
  31. #define ENET_MULTIPLAYER_PEER_H
  32. #include "enet_connection.h"
  33. #include "core/crypto/crypto.h"
  34. #include "scene/main/multiplayer_peer.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_RELIABLE = 0,
  45. SYSCH_UNRELIABLE = 1,
  46. SYSCH_MAX = 2
  47. };
  48. enum Mode {
  49. MODE_NONE,
  50. MODE_SERVER,
  51. MODE_CLIENT,
  52. MODE_MESH,
  53. };
  54. Mode active_mode = MODE_NONE;
  55. uint32_t unique_id = 0;
  56. int target_peer = 0;
  57. ConnectionStatus connection_status = CONNECTION_DISCONNECTED;
  58. HashMap<int, Ref<ENetConnection>> hosts;
  59. HashMap<int, Ref<ENetPacketPeer>> peers;
  60. struct Packet {
  61. ENetPacket *packet = nullptr;
  62. int from = 0;
  63. int channel = 0;
  64. TransferMode transfer_mode = TRANSFER_MODE_RELIABLE;
  65. };
  66. List<Packet> incoming_packets;
  67. Packet current_packet;
  68. void _store_packet(int32_t p_source, ENetConnection::Event &p_event);
  69. void _pop_current_packet();
  70. void _disconnect_inactive_peers();
  71. void _destroy_unused(ENetPacket *p_packet);
  72. _FORCE_INLINE_ bool _is_active() const { return active_mode != MODE_NONE; }
  73. IPAddress bind_ip;
  74. protected:
  75. static void _bind_methods();
  76. public:
  77. virtual void set_target_peer(int p_peer) override;
  78. virtual int get_packet_peer() const override;
  79. virtual TransferMode get_packet_mode() const override;
  80. virtual int get_packet_channel() const override;
  81. virtual void poll() override;
  82. virtual void close() override;
  83. virtual void disconnect_peer(int p_peer, bool p_force = false) override;
  84. virtual bool is_server() const override;
  85. virtual bool is_server_relay_supported() const override;
  86. // Overridden so we can instrument the DTLSServer when needed.
  87. virtual void set_refuse_new_connections(bool p_enabled) override;
  88. virtual ConnectionStatus get_connection_status() const override;
  89. virtual int get_unique_id() const override;
  90. virtual int get_max_packet_size() const override;
  91. virtual int get_available_packet_count() const override;
  92. virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override;
  93. virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override;
  94. 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);
  95. 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);
  96. Error create_mesh(int p_id);
  97. Error add_mesh_peer(int p_id, Ref<ENetConnection> p_host);
  98. void set_bind_ip(const IPAddress &p_ip);
  99. Ref<ENetConnection> get_host() const;
  100. Ref<ENetPacketPeer> get_peer(int p_id) const;
  101. ENetMultiplayerPeer();
  102. ~ENetMultiplayerPeer();
  103. };
  104. #endif // ENET_MULTIPLAYER_PEER_H