multiplayer_peer.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*************************************************************************/
  2. /* 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_PEER_H
  31. #define NETWORKED_MULTIPLAYER_PEER_H
  32. #include "core/io/packet_peer.h"
  33. #include "core/multiplayer/multiplayer.h"
  34. #include "core/object/gdvirtual.gen.inc"
  35. #include "core/object/script_language.h"
  36. #include "core/variant/native_ptr.h"
  37. class MultiplayerPeer : public PacketPeer {
  38. GDCLASS(MultiplayerPeer, PacketPeer);
  39. protected:
  40. static void _bind_methods();
  41. private:
  42. int transfer_channel = 0;
  43. Multiplayer::TransferMode transfer_mode = Multiplayer::TRANSFER_MODE_RELIABLE;
  44. bool refuse_connections = false;
  45. public:
  46. enum {
  47. TARGET_PEER_BROADCAST = 0,
  48. TARGET_PEER_SERVER = 1
  49. };
  50. enum ConnectionStatus {
  51. CONNECTION_DISCONNECTED,
  52. CONNECTION_CONNECTING,
  53. CONNECTION_CONNECTED,
  54. };
  55. virtual void set_transfer_channel(int p_channel);
  56. virtual int get_transfer_channel() const;
  57. virtual void set_transfer_mode(Multiplayer::TransferMode p_mode);
  58. virtual Multiplayer::TransferMode get_transfer_mode() const;
  59. virtual void set_refuse_new_connections(bool p_enable);
  60. virtual bool is_refusing_new_connections() const;
  61. virtual void set_target_peer(int p_peer_id) = 0;
  62. virtual int get_packet_peer() const = 0;
  63. virtual bool is_server() const = 0;
  64. virtual void poll() = 0;
  65. virtual int get_unique_id() const = 0;
  66. virtual ConnectionStatus get_connection_status() const = 0;
  67. uint32_t generate_unique_id() const;
  68. MultiplayerPeer() {}
  69. };
  70. VARIANT_ENUM_CAST(MultiplayerPeer::ConnectionStatus);
  71. class MultiplayerPeerExtension : public MultiplayerPeer {
  72. GDCLASS(MultiplayerPeerExtension, MultiplayerPeer);
  73. protected:
  74. static void _bind_methods();
  75. public:
  76. /* PacketPeer */
  77. virtual int get_available_packet_count() const override;
  78. virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; ///< buffer is GONE after next get_packet
  79. virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override;
  80. virtual int get_max_packet_size() const override;
  81. /* MultiplayerPeer */
  82. virtual void set_transfer_channel(int p_channel) override;
  83. virtual int get_transfer_channel() const override;
  84. virtual void set_transfer_mode(Multiplayer::TransferMode p_mode) override;
  85. virtual Multiplayer::TransferMode get_transfer_mode() const override;
  86. virtual void set_target_peer(int p_peer_id) override;
  87. virtual int get_packet_peer() const override;
  88. virtual bool is_server() const override;
  89. virtual void poll() override;
  90. virtual int get_unique_id() const override;
  91. virtual void set_refuse_new_connections(bool p_enable) override;
  92. virtual bool is_refusing_new_connections() const override;
  93. virtual ConnectionStatus get_connection_status() const override;
  94. /* PacketPeer GDExtension */
  95. GDVIRTUAL0RC(int, _get_available_packet_count);
  96. GDVIRTUAL2R(int, _get_packet, GDNativeConstPtr<const uint8_t *>, GDNativePtr<int>);
  97. GDVIRTUAL2R(int, _put_packet, GDNativeConstPtr<const uint8_t>, int);
  98. GDVIRTUAL0RC(int, _get_max_packet_size);
  99. /* MultiplayerPeer GDExtension */
  100. GDVIRTUAL1(_set_transfer_channel, int);
  101. GDVIRTUAL0RC(int, _get_transfer_channel);
  102. GDVIRTUAL1(_set_transfer_mode, int);
  103. GDVIRTUAL0RC(int, _get_transfer_mode);
  104. GDVIRTUAL1(_set_target_peer, int);
  105. GDVIRTUAL0RC(int, _get_packet_peer);
  106. GDVIRTUAL0RC(bool, _is_server);
  107. GDVIRTUAL0R(int, _poll);
  108. GDVIRTUAL0RC(int, _get_unique_id);
  109. GDVIRTUAL1(_set_refuse_new_connections, bool);
  110. GDVIRTUAL0RC(bool, _is_refusing_new_connections);
  111. GDVIRTUAL0RC(int, _get_connection_status);
  112. };
  113. #endif // NETWORKED_MULTIPLAYER_PEER_H