multiplayer_api.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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/io/multiplayer_peer.h"
  33. #include "core/object/ref_counted.h"
  34. class MultiplayerAPI : public RefCounted {
  35. GDCLASS(MultiplayerAPI, RefCounted);
  36. public:
  37. enum RPCMode {
  38. RPC_MODE_DISABLED, // No rpc for this method, calls to this will be blocked (default)
  39. RPC_MODE_REMOTE, // Using rpc() on it will call method in all remote peers
  40. RPC_MODE_MASTER, // Using rpc() on it will call method on wherever the master is, be it local or remote
  41. RPC_MODE_PUPPET, // Using rpc() on it will call method for all puppets
  42. };
  43. struct RPCConfig {
  44. StringName name;
  45. RPCMode rpc_mode = RPC_MODE_DISABLED;
  46. bool sync = false;
  47. MultiplayerPeer::TransferMode transfer_mode = MultiplayerPeer::TRANSFER_MODE_RELIABLE;
  48. int channel = 0;
  49. bool operator==(RPCConfig const &p_other) const {
  50. return name == p_other.name;
  51. }
  52. };
  53. struct SortRPCConfig {
  54. StringName::AlphCompare compare;
  55. bool operator()(const RPCConfig &p_a, const RPCConfig &p_b) const {
  56. return compare(p_a.name, p_b.name);
  57. }
  58. };
  59. private:
  60. //path sent caches
  61. struct PathSentCache {
  62. Map<int, bool> confirmed_peers;
  63. int id;
  64. };
  65. //path get caches
  66. struct PathGetCache {
  67. struct NodeInfo {
  68. NodePath path;
  69. ObjectID instance;
  70. };
  71. Map<int, NodeInfo> nodes;
  72. };
  73. Ref<MultiplayerPeer> network_peer;
  74. int rpc_sender_id = 0;
  75. Set<int> connected_peers;
  76. HashMap<NodePath, PathSentCache> path_send_cache;
  77. Map<int, PathGetCache> path_get_cache;
  78. int last_send_cache_id;
  79. Vector<uint8_t> packet_cache;
  80. Node *root_node = nullptr;
  81. bool allow_object_decoding = false;
  82. protected:
  83. static void _bind_methods();
  84. void _process_packet(int p_from, const uint8_t *p_packet, int p_packet_len);
  85. void _process_simplify_path(int p_from, const uint8_t *p_packet, int p_packet_len);
  86. void _process_confirm_path(int p_from, const uint8_t *p_packet, int p_packet_len);
  87. Node *_process_get_node(int p_from, const uint8_t *p_packet, uint32_t p_node_target, int p_packet_len);
  88. void _process_rpc(Node *p_node, const uint16_t p_rpc_method_id, int p_from, const uint8_t *p_packet, int p_packet_len, int p_offset);
  89. void _process_raw(int p_from, const uint8_t *p_packet, int p_packet_len);
  90. void _send_rpc(Node *p_from, int p_to, uint16_t p_rpc_id, const RPCConfig &p_config, const StringName &p_name, const Variant **p_arg, int p_argcount);
  91. bool _send_confirm_path(Node *p_node, NodePath p_path, PathSentCache *psc, int p_target);
  92. Error _encode_and_compress_variant(const Variant &p_variant, uint8_t *p_buffer, int &r_len);
  93. Error _decode_and_decompress_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len);
  94. public:
  95. enum NetworkCommands {
  96. NETWORK_COMMAND_REMOTE_CALL = 0,
  97. NETWORK_COMMAND_SIMPLIFY_PATH,
  98. NETWORK_COMMAND_CONFIRM_PATH,
  99. NETWORK_COMMAND_RAW,
  100. };
  101. enum NetworkNodeIdCompression {
  102. NETWORK_NODE_ID_COMPRESSION_8 = 0,
  103. NETWORK_NODE_ID_COMPRESSION_16,
  104. NETWORK_NODE_ID_COMPRESSION_32,
  105. };
  106. enum NetworkNameIdCompression {
  107. NETWORK_NAME_ID_COMPRESSION_8 = 0,
  108. NETWORK_NAME_ID_COMPRESSION_16,
  109. };
  110. void poll();
  111. void clear();
  112. void set_root_node(Node *p_node);
  113. Node *get_root_node();
  114. void set_network_peer(const Ref<MultiplayerPeer> &p_peer);
  115. Ref<MultiplayerPeer> get_network_peer() const;
  116. Error send_bytes(Vector<uint8_t> p_data, int p_to = MultiplayerPeer::TARGET_PEER_BROADCAST, MultiplayerPeer::TransferMode p_mode = MultiplayerPeer::TRANSFER_MODE_RELIABLE);
  117. // Called by Node.rpc
  118. void rpcp(Node *p_node, int p_peer_id, bool p_unreliable, const StringName &p_method, const Variant **p_arg, int p_argcount);
  119. void _add_peer(int p_id);
  120. void _del_peer(int p_id);
  121. void _connected_to_server();
  122. void _connection_failed();
  123. void _server_disconnected();
  124. bool has_network_peer() const { return network_peer.is_valid(); }
  125. Vector<int> get_network_connected_peers() const;
  126. int get_rpc_sender_id() const { return rpc_sender_id; }
  127. int get_network_unique_id() const;
  128. bool is_network_server() const;
  129. void set_refuse_new_network_connections(bool p_refuse);
  130. bool is_refusing_new_network_connections() const;
  131. void set_allow_object_decoding(bool p_enable);
  132. bool is_object_decoding_allowed() const;
  133. MultiplayerAPI();
  134. ~MultiplayerAPI();
  135. };
  136. VARIANT_ENUM_CAST(MultiplayerAPI::RPCMode);
  137. #endif // MULTIPLAYER_API_H