multiplayer_api.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. protected:
  59. static void _bind_methods();
  60. void _process_packet(int p_from, const uint8_t *p_packet, int p_packet_len);
  61. void _process_simplify_path(int p_from, const uint8_t *p_packet, int p_packet_len);
  62. void _process_confirm_path(int p_from, const uint8_t *p_packet, int p_packet_len);
  63. Node *_process_get_node(int p_from, const uint8_t *p_packet, int p_packet_len);
  64. 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);
  65. 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);
  66. void _process_raw(int p_from, const uint8_t *p_packet, int p_packet_len);
  67. 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);
  68. bool _send_confirm_path(NodePath p_path, PathSentCache *psc, int p_from);
  69. public:
  70. enum NetworkCommands {
  71. NETWORK_COMMAND_REMOTE_CALL,
  72. NETWORK_COMMAND_REMOTE_SET,
  73. NETWORK_COMMAND_SIMPLIFY_PATH,
  74. NETWORK_COMMAND_CONFIRM_PATH,
  75. NETWORK_COMMAND_RAW,
  76. };
  77. enum RPCMode {
  78. RPC_MODE_DISABLED, // No rpc for this method, calls to this will be blocked (default)
  79. RPC_MODE_REMOTE, // Using rpc() on it will call method / set property in all remote peers
  80. RPC_MODE_MASTER, // Using rpc() on it will call method on wherever the master is, be it local or remote
  81. RPC_MODE_PUPPET, // Using rpc() on it will call method for all puppets
  82. RPC_MODE_SLAVE = RPC_MODE_PUPPET, // Deprecated, same as puppet
  83. RPC_MODE_REMOTESYNC, // Using rpc() on it will call method / set property in all remote peers and locally
  84. RPC_MODE_SYNC = RPC_MODE_REMOTESYNC, // Deprecated. Same as RPC_MODE_REMOTESYNC
  85. RPC_MODE_MASTERSYNC, // Using rpc() on it will call method / set property in the master peer and locally
  86. RPC_MODE_PUPPETSYNC, // Using rpc() on it will call method / set property in all puppets peers and locally
  87. };
  88. void poll();
  89. void clear();
  90. void set_root_node(Node *p_node);
  91. void set_network_peer(const Ref<NetworkedMultiplayerPeer> &p_peer);
  92. Ref<NetworkedMultiplayerPeer> get_network_peer() const;
  93. Error send_bytes(PoolVector<uint8_t> p_data, int p_to = NetworkedMultiplayerPeer::TARGET_PEER_BROADCAST, NetworkedMultiplayerPeer::TransferMode p_mode = NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE);
  94. // Called by Node.rpc
  95. void rpcp(Node *p_node, int p_peer_id, bool p_unreliable, const StringName &p_method, const Variant **p_arg, int p_argcount);
  96. // Called by Node.rset
  97. void rsetp(Node *p_node, int p_peer_id, bool p_unreliable, const StringName &p_property, const Variant &p_value);
  98. void _add_peer(int p_id);
  99. void _del_peer(int p_id);
  100. void _connected_to_server();
  101. void _connection_failed();
  102. void _server_disconnected();
  103. bool has_network_peer() const { return network_peer.is_valid(); }
  104. Vector<int> get_network_connected_peers() const;
  105. int get_rpc_sender_id() const { return rpc_sender_id; }
  106. int get_network_unique_id() const;
  107. bool is_network_server() const;
  108. void set_refuse_new_network_connections(bool p_refuse);
  109. bool is_refusing_new_network_connections() const;
  110. MultiplayerAPI();
  111. ~MultiplayerAPI();
  112. };
  113. VARIANT_ENUM_CAST(MultiplayerAPI::RPCMode);
  114. #endif // MULTIPLAYER_PROTOCOL_H