scene_multiplayer.h 7.9 KB

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