multiplayer_api.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*************************************************************************/
  2. /* multiplayer_api.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_PROTOCOL_H
  31. #define MULTIPLAYER_PROTOCOL_H
  32. #include "core/io/networked_multiplayer_peer.h"
  33. #include "core/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;
  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;
  58. bool allow_object_decoding;
  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, int p_packet_len);
  65. void _process_rpc(Node *p_node, const StringName &p_name, int p_from, const uint8_t *p_packet, int p_packet_len, int p_offset);
  66. void _process_rset(Node *p_node, const StringName &p_name, 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(NodePath p_path, PathSentCache *psc, int p_target);
  70. public:
  71. enum NetworkCommands {
  72. NETWORK_COMMAND_REMOTE_CALL,
  73. NETWORK_COMMAND_REMOTE_SET,
  74. NETWORK_COMMAND_SIMPLIFY_PATH,
  75. NETWORK_COMMAND_CONFIRM_PATH,
  76. NETWORK_COMMAND_RAW,
  77. };
  78. enum RPCMode {
  79. RPC_MODE_DISABLED, // No rpc for this method, calls to this will be blocked (default)
  80. RPC_MODE_REMOTE, // Using rpc() on it will call method / set property in all remote peers
  81. RPC_MODE_MASTER, // Using rpc() on it will call method on wherever the master is, be it local or remote
  82. RPC_MODE_PUPPET, // Using rpc() on it will call method for all puppets
  83. RPC_MODE_SLAVE = RPC_MODE_PUPPET, // Deprecated, same as puppet
  84. RPC_MODE_REMOTESYNC, // Using rpc() on it will call method / set property in all remote peers and locally
  85. RPC_MODE_SYNC = RPC_MODE_REMOTESYNC, // Deprecated. Same as RPC_MODE_REMOTESYNC
  86. RPC_MODE_MASTERSYNC, // Using rpc() on it will call method / set property in the master peer and locally
  87. RPC_MODE_PUPPETSYNC, // Using rpc() on it will call method / set property in all puppets peers and locally
  88. };
  89. void poll();
  90. void clear();
  91. void set_root_node(Node *p_node);
  92. void set_network_peer(const Ref<NetworkedMultiplayerPeer> &p_peer);
  93. Ref<NetworkedMultiplayerPeer> get_network_peer() const;
  94. Error send_bytes(PoolVector<uint8_t> p_data, int p_to = NetworkedMultiplayerPeer::TARGET_PEER_BROADCAST, NetworkedMultiplayerPeer::TransferMode p_mode = NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE);
  95. // Called by Node.rpc
  96. void rpcp(Node *p_node, int p_peer_id, bool p_unreliable, const StringName &p_method, const Variant **p_arg, int p_argcount);
  97. // Called by Node.rset
  98. void rsetp(Node *p_node, int p_peer_id, bool p_unreliable, const StringName &p_property, const Variant &p_value);
  99. void _add_peer(int p_id);
  100. void _del_peer(int p_id);
  101. void _connected_to_server();
  102. void _connection_failed();
  103. void _server_disconnected();
  104. bool has_network_peer() const { return network_peer.is_valid(); }
  105. Vector<int> get_network_connected_peers() const;
  106. int get_rpc_sender_id() const { return rpc_sender_id; }
  107. int get_network_unique_id() const;
  108. bool is_network_server() const;
  109. void set_refuse_new_network_connections(bool p_refuse);
  110. bool is_refusing_new_network_connections() const;
  111. void set_allow_object_decoding(bool p_enable);
  112. bool is_object_decoding_allowed() const;
  113. MultiplayerAPI();
  114. ~MultiplayerAPI();
  115. };
  116. VARIANT_ENUM_CAST(MultiplayerAPI::RPCMode);
  117. #endif // MULTIPLAYER_PROTOCOL_H