multiplayer_api.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*************************************************************************/
  2. /* multiplayer_api.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 MultiplayerReplicator;
  36. class RPCManager;
  37. class MultiplayerAPI : public RefCounted {
  38. GDCLASS(MultiplayerAPI, RefCounted);
  39. public:
  40. enum NetworkCommands {
  41. NETWORK_COMMAND_REMOTE_CALL = 0,
  42. NETWORK_COMMAND_SIMPLIFY_PATH,
  43. NETWORK_COMMAND_CONFIRM_PATH,
  44. NETWORK_COMMAND_RAW,
  45. NETWORK_COMMAND_SPAWN,
  46. NETWORK_COMMAND_DESPAWN,
  47. NETWORK_COMMAND_SYNC,
  48. };
  49. // For each command, the 4 MSB can contain custom flags, as defined by subsystems.
  50. enum {
  51. CMD_FLAG_0_SHIFT = 4,
  52. CMD_FLAG_1_SHIFT = 5,
  53. CMD_FLAG_2_SHIFT = 6,
  54. CMD_FLAG_3_SHIFT = 7,
  55. };
  56. // This is the mask that will be used to extract the command.
  57. enum {
  58. CMD_MASK = 7, // 0x7 -> 0b00001111
  59. };
  60. private:
  61. //path sent caches
  62. struct PathSentCache {
  63. Map<int, bool> confirmed_peers;
  64. int id;
  65. };
  66. //path get caches
  67. struct PathGetCache {
  68. struct NodeInfo {
  69. NodePath path;
  70. ObjectID instance;
  71. };
  72. Map<int, NodeInfo> nodes;
  73. };
  74. Ref<MultiplayerPeer> network_peer;
  75. Set<int> connected_peers;
  76. int remote_sender_id = 0;
  77. int remote_sender_override = 0;
  78. HashMap<NodePath, PathSentCache> path_send_cache;
  79. Map<int, PathGetCache> path_get_cache;
  80. int last_send_cache_id;
  81. Vector<uint8_t> packet_cache;
  82. Node *root_node = nullptr;
  83. bool allow_object_decoding = false;
  84. MultiplayerReplicator *replicator = nullptr;
  85. RPCManager *rpc_manager = nullptr;
  86. protected:
  87. static void _bind_methods();
  88. bool _send_confirm_path(Node *p_node, NodePath p_path, PathSentCache *psc, int p_target);
  89. void _process_packet(int p_from, const uint8_t *p_packet, int p_packet_len);
  90. void _process_simplify_path(int p_from, const uint8_t *p_packet, int p_packet_len);
  91. void _process_confirm_path(int p_from, const uint8_t *p_packet, int p_packet_len);
  92. void _process_raw(int p_from, const uint8_t *p_packet, int p_packet_len);
  93. public:
  94. void poll();
  95. void clear();
  96. void set_root_node(Node *p_node);
  97. Node *get_root_node();
  98. void set_network_peer(const Ref<MultiplayerPeer> &p_peer);
  99. Ref<MultiplayerPeer> get_network_peer() const;
  100. 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);
  101. Error encode_and_compress_variant(const Variant &p_variant, uint8_t *p_buffer, int &r_len);
  102. Error decode_and_decompress_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len);
  103. // Called by Node.rpc
  104. void rpcp(Node *p_node, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount);
  105. // Called by Node._notification
  106. void scene_enter_exit_notify(const String &p_scene, Node *p_node, bool p_enter);
  107. // Called by replicator
  108. bool send_confirm_path(Node *p_node, NodePath p_path, int p_target, int &p_id);
  109. Node *get_cached_node(int p_from, uint32_t p_node_id);
  110. bool is_cache_confirmed(NodePath p_path, int p_peer);
  111. void _add_peer(int p_id);
  112. void _del_peer(int p_id);
  113. void _connected_to_server();
  114. void _connection_failed();
  115. void _server_disconnected();
  116. bool has_network_peer() const { return network_peer.is_valid(); }
  117. Vector<int> get_network_connected_peers() const;
  118. const Set<int> get_connected_peers() const { return connected_peers; }
  119. int get_remote_sender_id() const { return remote_sender_override ? remote_sender_override : remote_sender_id; }
  120. void set_remote_sender_override(int p_id) { remote_sender_override = p_id; }
  121. int get_network_unique_id() const;
  122. bool is_network_server() const;
  123. void set_refuse_new_network_connections(bool p_refuse);
  124. bool is_refusing_new_network_connections() const;
  125. void set_allow_object_decoding(bool p_enable);
  126. bool is_object_decoding_allowed() const;
  127. MultiplayerReplicator *get_replicator() const { return replicator; }
  128. RPCManager *get_rpc_manager() const { return rpc_manager; }
  129. #ifdef DEBUG_ENABLED
  130. void profile_bandwidth(const String &p_inout, int p_size);
  131. #endif
  132. MultiplayerAPI();
  133. ~MultiplayerAPI();
  134. };
  135. #endif // MULTIPLAYER_API_H