multiplayer_api.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*************************************************************************/
  2. /* multiplayer_api.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. public:
  37. struct ProfilingInfo {
  38. ObjectID node;
  39. String node_path;
  40. int incoming_rpc;
  41. int incoming_rset;
  42. int outgoing_rpc;
  43. int outgoing_rset;
  44. };
  45. private:
  46. //path sent caches
  47. struct PathSentCache {
  48. Map<int, bool> confirmed_peers;
  49. int id;
  50. };
  51. //path get caches
  52. struct PathGetCache {
  53. struct NodeInfo {
  54. NodePath path;
  55. ObjectID instance;
  56. };
  57. Map<int, NodeInfo> nodes;
  58. };
  59. #ifdef DEBUG_ENABLED
  60. struct BandwidthFrame {
  61. uint32_t timestamp;
  62. int packet_size;
  63. };
  64. int bandwidth_incoming_pointer;
  65. Vector<BandwidthFrame> bandwidth_incoming_data;
  66. int bandwidth_outgoing_pointer;
  67. Vector<BandwidthFrame> bandwidth_outgoing_data;
  68. Map<ObjectID, ProfilingInfo> profiler_frame_data;
  69. bool profiling;
  70. void _init_node_profile(ObjectID p_node);
  71. int _get_bandwidth_usage(const Vector<BandwidthFrame> &p_buffer, int p_pointer);
  72. #endif
  73. Ref<NetworkedMultiplayerPeer> network_peer;
  74. int rpc_sender_id;
  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;
  81. bool allow_object_decoding;
  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, int p_packet_len);
  88. 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);
  89. 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);
  90. void _process_raw(int p_from, const uint8_t *p_packet, int p_packet_len);
  91. 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);
  92. bool _send_confirm_path(NodePath p_path, PathSentCache *psc, int p_target);
  93. public:
  94. enum NetworkCommands {
  95. NETWORK_COMMAND_REMOTE_CALL,
  96. NETWORK_COMMAND_REMOTE_SET,
  97. NETWORK_COMMAND_SIMPLIFY_PATH,
  98. NETWORK_COMMAND_CONFIRM_PATH,
  99. NETWORK_COMMAND_RAW,
  100. };
  101. enum RPCMode {
  102. RPC_MODE_DISABLED, // No rpc for this method, calls to this will be blocked (default)
  103. RPC_MODE_REMOTE, // Using rpc() on it will call method / set property in all remote peers
  104. RPC_MODE_MASTER, // Using rpc() on it will call method on wherever the master is, be it local or remote
  105. RPC_MODE_PUPPET, // Using rpc() on it will call method for all puppets
  106. RPC_MODE_SLAVE = RPC_MODE_PUPPET, // Deprecated, same as puppet
  107. RPC_MODE_REMOTESYNC, // Using rpc() on it will call method / set property in all remote peers and locally
  108. RPC_MODE_SYNC = RPC_MODE_REMOTESYNC, // Deprecated. Same as RPC_MODE_REMOTESYNC
  109. RPC_MODE_MASTERSYNC, // Using rpc() on it will call method / set property in the master peer and locally
  110. RPC_MODE_PUPPETSYNC, // Using rpc() on it will call method / set property in all puppets peers and locally
  111. };
  112. void poll();
  113. void clear();
  114. void set_root_node(Node *p_node);
  115. void set_network_peer(const Ref<NetworkedMultiplayerPeer> &p_peer);
  116. Ref<NetworkedMultiplayerPeer> get_network_peer() const;
  117. Error send_bytes(PoolVector<uint8_t> p_data, int p_to = NetworkedMultiplayerPeer::TARGET_PEER_BROADCAST, NetworkedMultiplayerPeer::TransferMode p_mode = NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE);
  118. // Called by Node.rpc
  119. void rpcp(Node *p_node, int p_peer_id, bool p_unreliable, const StringName &p_method, const Variant **p_arg, int p_argcount);
  120. // Called by Node.rset
  121. void rsetp(Node *p_node, int p_peer_id, bool p_unreliable, const StringName &p_property, const Variant &p_value);
  122. void _add_peer(int p_id);
  123. void _del_peer(int p_id);
  124. void _connected_to_server();
  125. void _connection_failed();
  126. void _server_disconnected();
  127. bool has_network_peer() const { return network_peer.is_valid(); }
  128. Vector<int> get_network_connected_peers() const;
  129. int get_rpc_sender_id() const { return rpc_sender_id; }
  130. int get_network_unique_id() const;
  131. bool is_network_server() const;
  132. void set_refuse_new_network_connections(bool p_refuse);
  133. bool is_refusing_new_network_connections() const;
  134. void set_allow_object_decoding(bool p_enable);
  135. bool is_object_decoding_allowed() const;
  136. void profiling_start();
  137. void profiling_end();
  138. int get_profiling_frame(ProfilingInfo *r_info);
  139. int get_incoming_bandwidth_usage();
  140. int get_outgoing_bandwidth_usage();
  141. MultiplayerAPI();
  142. ~MultiplayerAPI();
  143. };
  144. VARIANT_ENUM_CAST(MultiplayerAPI::RPCMode);
  145. #endif // MULTIPLAYER_PROTOCOL_H