multiplayer_peer.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**************************************************************************/
  2. /* 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 MULTIPLAYER_PEER_H
  31. #define MULTIPLAYER_PEER_H
  32. #include "core/io/packet_peer.h"
  33. #include "core/extension/ext_wrappers.gen.inc"
  34. #include "core/object/gdvirtual.gen.inc"
  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 bool is_server_relay_supported() const;
  67. virtual void set_target_peer(int p_peer_id) = 0;
  68. virtual int get_packet_peer() const = 0;
  69. virtual TransferMode get_packet_mode() const = 0;
  70. virtual int get_packet_channel() const = 0;
  71. virtual void disconnect_peer(int p_peer, bool p_force = false) = 0;
  72. virtual bool is_server() const = 0;
  73. virtual void poll() = 0;
  74. virtual void close() = 0;
  75. virtual int get_unique_id() const = 0;
  76. virtual ConnectionStatus get_connection_status() const = 0;
  77. uint32_t generate_unique_id() const;
  78. MultiplayerPeer() {}
  79. };
  80. VARIANT_ENUM_CAST(MultiplayerPeer::ConnectionStatus);
  81. VARIANT_ENUM_CAST(MultiplayerPeer::TransferMode);
  82. class MultiplayerPeerExtension : public MultiplayerPeer {
  83. GDCLASS(MultiplayerPeerExtension, MultiplayerPeer);
  84. protected:
  85. static void _bind_methods();
  86. PackedByteArray script_buffer;
  87. public:
  88. /* PacketPeer extension */
  89. virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; ///< buffer is GONE after next get_packet
  90. GDVIRTUAL2R(Error, _get_packet, GDExtensionConstPtr<const uint8_t *>, GDExtensionPtr<int>);
  91. GDVIRTUAL0R(PackedByteArray, _get_packet_script); // For GDScript.
  92. virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override;
  93. GDVIRTUAL2R(Error, _put_packet, GDExtensionConstPtr<const uint8_t>, int);
  94. GDVIRTUAL1R(Error, _put_packet_script, PackedByteArray); // For GDScript.
  95. EXBIND0RC(int, get_available_packet_count);
  96. EXBIND0RC(int, get_max_packet_size);
  97. /* MultiplayerPeer extension */
  98. virtual void set_refuse_new_connections(bool p_enable) override;
  99. GDVIRTUAL1(_set_refuse_new_connections, bool); // Optional.
  100. virtual bool is_refusing_new_connections() const override;
  101. GDVIRTUAL0RC(bool, _is_refusing_new_connections); // Optional.
  102. virtual bool is_server_relay_supported() const override;
  103. GDVIRTUAL0RC(bool, _is_server_relay_supported); // Optional.
  104. EXBIND1(set_transfer_channel, int);
  105. EXBIND0RC(int, get_transfer_channel);
  106. EXBIND1(set_transfer_mode, TransferMode);
  107. EXBIND0RC(TransferMode, get_transfer_mode);
  108. EXBIND1(set_target_peer, int);
  109. EXBIND0RC(int, get_packet_peer);
  110. EXBIND0RC(TransferMode, get_packet_mode);
  111. EXBIND0RC(int, get_packet_channel);
  112. EXBIND0RC(bool, is_server);
  113. EXBIND0(poll);
  114. EXBIND0(close);
  115. EXBIND2(disconnect_peer, int, bool);
  116. EXBIND0RC(int, get_unique_id);
  117. EXBIND0RC(ConnectionStatus, get_connection_status);
  118. };
  119. #endif // MULTIPLAYER_PEER_H