multiplayer_api.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*************************************************************************/
  2. /* multiplayer_api.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_API_H
  31. #define MULTIPLAYER_API_H
  32. #include "core/multiplayer/multiplayer.h"
  33. #include "core/multiplayer/multiplayer_peer.h"
  34. #include "core/object/ref_counted.h"
  35. class MultiplayerAPI;
  36. class MultiplayerReplicationInterface : public RefCounted {
  37. GDCLASS(MultiplayerReplicationInterface, RefCounted);
  38. public:
  39. virtual void on_peer_change(int p_id, bool p_connected) {}
  40. virtual void on_reset() {}
  41. virtual Error on_spawn_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len) { return ERR_UNAVAILABLE; }
  42. virtual Error on_despawn_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len) { return ERR_UNAVAILABLE; }
  43. virtual Error on_sync_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len) { return ERR_UNAVAILABLE; }
  44. virtual Error on_spawn(Object *p_obj, Variant p_config) { return ERR_UNAVAILABLE; }
  45. virtual Error on_despawn(Object *p_obj, Variant p_config) { return ERR_UNAVAILABLE; }
  46. virtual Error on_replication_start(Object *p_obj, Variant p_config) { return ERR_UNAVAILABLE; }
  47. virtual Error on_replication_stop(Object *p_obj, Variant p_config) { return ERR_UNAVAILABLE; }
  48. virtual void on_network_process() {}
  49. MultiplayerReplicationInterface() {}
  50. };
  51. class MultiplayerRPCInterface : public RefCounted {
  52. GDCLASS(MultiplayerRPCInterface, RefCounted);
  53. public:
  54. // Called by Node.rpc
  55. virtual void rpcp(Object *p_obj, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount) {}
  56. virtual void process_rpc(int p_from, const uint8_t *p_packet, int p_packet_len) {}
  57. virtual String get_rpc_md5(const Object *p_obj) const { return String(); }
  58. MultiplayerRPCInterface() {}
  59. };
  60. class MultiplayerCacheInterface : public RefCounted {
  61. GDCLASS(MultiplayerCacheInterface, RefCounted);
  62. public:
  63. virtual void clear() {}
  64. virtual void on_peer_change(int p_id, bool p_connected) {}
  65. virtual void process_simplify_path(int p_from, const uint8_t *p_packet, int p_packet_len) {}
  66. virtual void process_confirm_path(int p_from, const uint8_t *p_packet, int p_packet_len) {}
  67. // Returns true if all peers have cached path.
  68. virtual bool send_object_cache(Object *p_obj, NodePath p_path, int p_target, int &p_id) { return false; }
  69. virtual Object *get_cached_object(int p_from, uint32_t p_cache_id) { return nullptr; }
  70. virtual bool is_cache_confirmed(NodePath p_path, int p_peer) { return false; }
  71. MultiplayerCacheInterface() {}
  72. };
  73. class MultiplayerAPI : public RefCounted {
  74. GDCLASS(MultiplayerAPI, RefCounted);
  75. public:
  76. enum NetworkCommands {
  77. NETWORK_COMMAND_REMOTE_CALL = 0,
  78. NETWORK_COMMAND_SIMPLIFY_PATH,
  79. NETWORK_COMMAND_CONFIRM_PATH,
  80. NETWORK_COMMAND_RAW,
  81. NETWORK_COMMAND_SPAWN,
  82. NETWORK_COMMAND_DESPAWN,
  83. NETWORK_COMMAND_SYNC,
  84. };
  85. // For each command, the 4 MSB can contain custom flags, as defined by subsystems.
  86. enum {
  87. CMD_FLAG_0_SHIFT = 4,
  88. CMD_FLAG_1_SHIFT = 5,
  89. CMD_FLAG_2_SHIFT = 6,
  90. CMD_FLAG_3_SHIFT = 7,
  91. };
  92. // This is the mask that will be used to extract the command.
  93. enum {
  94. CMD_MASK = 7, // 0x7 -> 0b00001111
  95. };
  96. private:
  97. Ref<MultiplayerPeer> multiplayer_peer;
  98. HashSet<int> connected_peers;
  99. int remote_sender_id = 0;
  100. int remote_sender_override = 0;
  101. Vector<uint8_t> packet_cache;
  102. NodePath root_path;
  103. bool allow_object_decoding = false;
  104. Ref<MultiplayerCacheInterface> cache;
  105. Ref<MultiplayerReplicationInterface> replicator;
  106. Ref<MultiplayerRPCInterface> rpc;
  107. protected:
  108. static void _bind_methods();
  109. void _process_packet(int p_from, const uint8_t *p_packet, int p_packet_len);
  110. void _process_raw(int p_from, const uint8_t *p_packet, int p_packet_len);
  111. public:
  112. static MultiplayerReplicationInterface *(*create_default_replication_interface)(MultiplayerAPI *p_multiplayer);
  113. static MultiplayerRPCInterface *(*create_default_rpc_interface)(MultiplayerAPI *p_multiplayer);
  114. static MultiplayerCacheInterface *(*create_default_cache_interface)(MultiplayerAPI *p_multiplayer);
  115. static Error encode_and_compress_variant(const Variant &p_variant, uint8_t *p_buffer, int &r_len, bool p_allow_object_decoding);
  116. static Error decode_and_decompress_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len, bool p_allow_object_decoding);
  117. static Error encode_and_compress_variants(const Variant **p_variants, int p_count, uint8_t *p_buffer, int &r_len, bool *r_raw = nullptr, bool p_allow_object_decoding = false);
  118. static Error decode_and_decompress_variants(Vector<Variant> &r_variants, const uint8_t *p_buffer, int p_len, int &r_len, bool p_raw = false, bool p_allow_object_decoding = false);
  119. void poll();
  120. void clear();
  121. void set_root_path(const NodePath &p_path);
  122. NodePath get_root_path() const;
  123. void set_multiplayer_peer(const Ref<MultiplayerPeer> &p_peer);
  124. Ref<MultiplayerPeer> get_multiplayer_peer() const;
  125. Error send_bytes(Vector<uint8_t> p_data, int p_to = MultiplayerPeer::TARGET_PEER_BROADCAST, Multiplayer::TransferMode p_mode = Multiplayer::TRANSFER_MODE_RELIABLE, int p_channel = 0);
  126. // RPC API
  127. void rpcp(Object *p_obj, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount);
  128. String get_rpc_md5(const Object *p_obj) const;
  129. // Replication API
  130. Error spawn(Object *p_object, Variant p_config);
  131. Error despawn(Object *p_object, Variant p_config);
  132. Error replication_start(Object *p_object, Variant p_config);
  133. Error replication_stop(Object *p_object, Variant p_config);
  134. // Cache API
  135. bool send_object_cache(Object *p_obj, NodePath p_path, int p_target, int &p_id);
  136. Object *get_cached_object(int p_from, uint32_t p_cache_id);
  137. bool is_cache_confirmed(NodePath p_path, int p_peer);
  138. void _add_peer(int p_id);
  139. void _del_peer(int p_id);
  140. void _connected_to_server();
  141. void _connection_failed();
  142. void _server_disconnected();
  143. bool has_multiplayer_peer() const { return multiplayer_peer.is_valid(); }
  144. Vector<int> get_peer_ids() const;
  145. const HashSet<int> get_connected_peers() const { return connected_peers; }
  146. int get_remote_sender_id() const { return remote_sender_override ? remote_sender_override : remote_sender_id; }
  147. void set_remote_sender_override(int p_id) { remote_sender_override = p_id; }
  148. int get_unique_id() const;
  149. bool is_server() const;
  150. void set_refuse_new_connections(bool p_refuse);
  151. bool is_refusing_new_connections() const;
  152. void set_allow_object_decoding(bool p_enable);
  153. bool is_object_decoding_allowed() const;
  154. #ifdef DEBUG_ENABLED
  155. void profile_bandwidth(const String &p_inout, int p_size);
  156. #endif
  157. MultiplayerAPI();
  158. ~MultiplayerAPI();
  159. };
  160. #endif // MULTIPLAYER_API_H