multiplayer_api.h 6.5 KB

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