multiplayer_peer.h 5.7 KB

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