scene_multiplayer.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**************************************************************************/
  2. /* scene_multiplayer.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. #pragma once
  31. #include "scene_cache_interface.h"
  32. #include "scene_replication_interface.h"
  33. #include "scene_rpc_interface.h"
  34. #include "scene/main/multiplayer_api.h"
  35. class OfflineMultiplayerPeer : public MultiplayerPeer {
  36. GDCLASS(OfflineMultiplayerPeer, MultiplayerPeer);
  37. public:
  38. virtual int get_available_packet_count() const override { return 0; }
  39. virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override {
  40. *r_buffer = nullptr;
  41. r_buffer_size = 0;
  42. return OK;
  43. }
  44. virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override { return OK; }
  45. virtual int get_max_packet_size() const override { return 0; }
  46. virtual void set_target_peer(int p_peer_id) override {}
  47. virtual int get_packet_peer() const override { return 0; }
  48. virtual TransferMode get_packet_mode() const override { return TRANSFER_MODE_RELIABLE; }
  49. virtual int get_packet_channel() const override { return 0; }
  50. virtual void disconnect_peer(int p_peer, bool p_force = false) override {}
  51. virtual bool is_server() const override { return true; }
  52. virtual void poll() override {}
  53. virtual void close() override {}
  54. virtual int get_unique_id() const override { return TARGET_PEER_SERVER; }
  55. virtual ConnectionStatus get_connection_status() const override { return CONNECTION_CONNECTED; }
  56. };
  57. class SceneMultiplayer : public MultiplayerAPI {
  58. GDCLASS(SceneMultiplayer, MultiplayerAPI);
  59. public:
  60. enum NetworkCommands {
  61. NETWORK_COMMAND_REMOTE_CALL = 0,
  62. NETWORK_COMMAND_SIMPLIFY_PATH,
  63. NETWORK_COMMAND_CONFIRM_PATH,
  64. NETWORK_COMMAND_RAW,
  65. NETWORK_COMMAND_SPAWN,
  66. NETWORK_COMMAND_DESPAWN,
  67. NETWORK_COMMAND_SYNC,
  68. NETWORK_COMMAND_SYS,
  69. };
  70. enum SysCommands {
  71. SYS_COMMAND_AUTH,
  72. SYS_COMMAND_ADD_PEER,
  73. SYS_COMMAND_DEL_PEER,
  74. SYS_COMMAND_RELAY,
  75. };
  76. enum {
  77. SYS_CMD_SIZE = 6, // Command + sys command + peer_id (+ optional payload).
  78. };
  79. // For each command, the 4 MSB can contain custom flags, as defined by subsystems.
  80. enum {
  81. CMD_FLAG_0_SHIFT = 4,
  82. CMD_FLAG_1_SHIFT = 5,
  83. CMD_FLAG_2_SHIFT = 6,
  84. CMD_FLAG_3_SHIFT = 7,
  85. };
  86. // This is the mask that will be used to extract the command.
  87. enum {
  88. CMD_MASK = 7, // 0x7 -> 0b00000111
  89. };
  90. private:
  91. struct PendingPeer {
  92. bool local = false;
  93. bool remote = false;
  94. uint64_t time = 0;
  95. };
  96. Ref<MultiplayerPeer> multiplayer_peer;
  97. MultiplayerPeer::ConnectionStatus last_connection_status = MultiplayerPeer::CONNECTION_DISCONNECTED;
  98. HashMap<int, PendingPeer> pending_peers; // true if locally finalized.
  99. Callable auth_callback;
  100. uint64_t auth_timeout = 3000;
  101. HashSet<int> connected_peers;
  102. int remote_sender_id = 0;
  103. int remote_sender_override = 0;
  104. Vector<uint8_t> packet_cache;
  105. NodePath root_path;
  106. bool allow_object_decoding = false;
  107. bool server_relay = true;
  108. Ref<StreamPeerBuffer> relay_buffer;
  109. Ref<SceneCacheInterface> cache;
  110. Ref<SceneReplicationInterface> replicator;
  111. Ref<SceneRPCInterface> rpc;
  112. #ifdef DEBUG_ENABLED
  113. _FORCE_INLINE_ void _profile_bandwidth(const String &p_what, int p_value);
  114. _FORCE_INLINE_ Error _send(const uint8_t *p_packet, int p_packet_len); // Also profiles.
  115. #else
  116. _FORCE_INLINE_ Error _send(const uint8_t *p_packet, int p_packet_len) {
  117. return multiplayer_peer->put_packet(p_packet, p_packet_len);
  118. }
  119. #endif
  120. protected:
  121. static void _bind_methods();
  122. void _process_packet(int p_from, const uint8_t *p_packet, int p_packet_len);
  123. void _process_raw(int p_from, const uint8_t *p_packet, int p_packet_len);
  124. void _process_sys(int p_from, const uint8_t *p_packet, int p_packet_len, MultiplayerPeer::TransferMode p_mode, int p_channel);
  125. void _add_peer(int p_id);
  126. void _admit_peer(int p_id);
  127. void _del_peer(int p_id);
  128. void _update_status();
  129. public:
  130. virtual void set_multiplayer_peer(const Ref<MultiplayerPeer> &p_peer) override;
  131. virtual Ref<MultiplayerPeer> get_multiplayer_peer() override;
  132. virtual Error poll() override;
  133. virtual int get_unique_id() override;
  134. virtual Vector<int> get_peer_ids() override;
  135. virtual int get_remote_sender_id() override { return remote_sender_override ? remote_sender_override : remote_sender_id; }
  136. virtual Error rpcp(Object *p_obj, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount) override;
  137. virtual Error object_configuration_add(Object *p_obj, Variant p_config) override;
  138. virtual Error object_configuration_remove(Object *p_obj, Variant p_config) override;
  139. void clear();
  140. // Usually from object_configuration_add/remove
  141. void set_root_path(const NodePath &p_path);
  142. NodePath get_root_path() const;
  143. void disconnect_peer(int p_id);
  144. Error send_auth(int p_to, Vector<uint8_t> p_bytes);
  145. Error complete_auth(int p_peer);
  146. void set_auth_callback(Callable p_callback);
  147. Callable get_auth_callback() const;
  148. void set_auth_timeout(double p_timeout);
  149. double get_auth_timeout() const;
  150. Vector<int> get_authenticating_peer_ids();
  151. Error send_command(int p_to, const uint8_t *p_packet, int p_packet_len); // Used internally to relay packets when needed.
  152. Error send_bytes(Vector<uint8_t> p_data, int p_to = MultiplayerPeer::TARGET_PEER_BROADCAST, MultiplayerPeer::TransferMode p_mode = MultiplayerPeer::TRANSFER_MODE_RELIABLE, int p_channel = 0);
  153. String get_rpc_md5(const Object *p_obj);
  154. const HashSet<int> get_connected_peers() const { return connected_peers; }
  155. void set_remote_sender_override(int p_id) { remote_sender_override = p_id; }
  156. void set_refuse_new_connections(bool p_refuse);
  157. bool is_refusing_new_connections() const;
  158. void set_allow_object_decoding(bool p_enable);
  159. bool is_object_decoding_allowed() const;
  160. void set_server_relay_enabled(bool p_enabled);
  161. bool is_server_relay_enabled() const;
  162. void set_max_sync_packet_size(int p_size);
  163. int get_max_sync_packet_size() const;
  164. void set_max_delta_packet_size(int p_size);
  165. int get_max_delta_packet_size() const;
  166. SceneMultiplayer();
  167. ~SceneMultiplayer();
  168. };