scene_multiplayer.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*************************************************************************/
  2. /* scene_multiplayer.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 SCENE_MULTIPLAYER_H
  31. #define SCENE_MULTIPLAYER_H
  32. #include "scene/main/multiplayer_api.h"
  33. #include "scene_cache_interface.h"
  34. #include "scene_replication_interface.h"
  35. #include "scene_rpc_interface.h"
  36. class SceneMultiplayer : public MultiplayerAPI {
  37. GDCLASS(SceneMultiplayer, MultiplayerAPI);
  38. public:
  39. enum NetworkCommands {
  40. NETWORK_COMMAND_REMOTE_CALL = 0,
  41. NETWORK_COMMAND_SIMPLIFY_PATH,
  42. NETWORK_COMMAND_CONFIRM_PATH,
  43. NETWORK_COMMAND_RAW,
  44. NETWORK_COMMAND_SPAWN,
  45. NETWORK_COMMAND_DESPAWN,
  46. NETWORK_COMMAND_SYNC,
  47. };
  48. // For each command, the 4 MSB can contain custom flags, as defined by subsystems.
  49. enum {
  50. CMD_FLAG_0_SHIFT = 4,
  51. CMD_FLAG_1_SHIFT = 5,
  52. CMD_FLAG_2_SHIFT = 6,
  53. CMD_FLAG_3_SHIFT = 7,
  54. };
  55. // This is the mask that will be used to extract the command.
  56. enum {
  57. CMD_MASK = 7, // 0x7 -> 0b00001111
  58. };
  59. private:
  60. Ref<MultiplayerPeer> multiplayer_peer;
  61. HashSet<int> connected_peers;
  62. int remote_sender_id = 0;
  63. int remote_sender_override = 0;
  64. Vector<uint8_t> packet_cache;
  65. NodePath root_path;
  66. bool allow_object_decoding = false;
  67. Ref<SceneCacheInterface> cache;
  68. Ref<SceneReplicationInterface> replicator;
  69. Ref<SceneRPCInterface> rpc;
  70. protected:
  71. static void _bind_methods();
  72. void _process_packet(int p_from, const uint8_t *p_packet, int p_packet_len);
  73. void _process_raw(int p_from, const uint8_t *p_packet, int p_packet_len);
  74. void _add_peer(int p_id);
  75. void _del_peer(int p_id);
  76. void _connected_to_server();
  77. void _connection_failed();
  78. void _server_disconnected();
  79. public:
  80. virtual void set_multiplayer_peer(const Ref<MultiplayerPeer> &p_peer) override;
  81. virtual Ref<MultiplayerPeer> get_multiplayer_peer() override;
  82. virtual Error poll() override;
  83. virtual int get_unique_id() override;
  84. virtual Vector<int> get_peer_ids() override;
  85. virtual int get_remote_sender_id() override { return remote_sender_override ? remote_sender_override : remote_sender_id; }
  86. virtual Error rpcp(Object *p_obj, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount) override;
  87. virtual Error object_configuration_add(Object *p_obj, Variant p_config) override;
  88. virtual Error object_configuration_remove(Object *p_obj, Variant p_config) override;
  89. void clear();
  90. // Usually from object_configuration_add/remove
  91. void set_root_path(const NodePath &p_path);
  92. NodePath get_root_path() const;
  93. 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);
  94. String get_rpc_md5(const Object *p_obj);
  95. const HashSet<int> get_connected_peers() const { return connected_peers; }
  96. void set_remote_sender_override(int p_id) { remote_sender_override = p_id; }
  97. void set_refuse_new_connections(bool p_refuse);
  98. bool is_refusing_new_connections() const;
  99. void set_allow_object_decoding(bool p_enable);
  100. bool is_object_decoding_allowed() const;
  101. Ref<SceneCacheInterface> get_path_cache() { return cache; }
  102. #ifdef DEBUG_ENABLED
  103. void profile_bandwidth(const String &p_inout, int p_size);
  104. #endif
  105. SceneMultiplayer();
  106. ~SceneMultiplayer();
  107. };
  108. #endif // SCENE_MULTIPLAYER_H