multiplayer_api.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #ifndef MULTIPLAYER_PROTOCOL_H
  2. #define MULTIPLAYER_PROTOCOL_H
  3. #include "core/io/networked_multiplayer_peer.h"
  4. #include "core/reference.h"
  5. class MultiplayerAPI : public Reference {
  6. GDCLASS(MultiplayerAPI, Reference);
  7. private:
  8. //path sent caches
  9. struct PathSentCache {
  10. Map<int, bool> confirmed_peers;
  11. int id;
  12. };
  13. //path get caches
  14. struct PathGetCache {
  15. struct NodeInfo {
  16. NodePath path;
  17. ObjectID instance;
  18. };
  19. Map<int, NodeInfo> nodes;
  20. };
  21. Ref<NetworkedMultiplayerPeer> network_peer;
  22. int rpc_sender_id;
  23. Set<int> connected_peers;
  24. HashMap<NodePath, PathSentCache> path_send_cache;
  25. Map<int, PathGetCache> path_get_cache;
  26. int last_send_cache_id;
  27. Vector<uint8_t> packet_cache;
  28. Node *root_node;
  29. protected:
  30. static void _bind_methods();
  31. void _process_packet(int p_from, const uint8_t *p_packet, int p_packet_len);
  32. void _process_simplify_path(int p_from, const uint8_t *p_packet, int p_packet_len);
  33. void _process_confirm_path(int p_from, const uint8_t *p_packet, int p_packet_len);
  34. Node *_process_get_node(int p_from, const uint8_t *p_packet, int p_packet_len);
  35. 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);
  36. 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);
  37. void _process_raw(int p_from, const uint8_t *p_packet, int p_packet_len);
  38. 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);
  39. bool _send_confirm_path(NodePath p_path, PathSentCache *psc, int p_from);
  40. public:
  41. enum NetworkCommands {
  42. NETWORK_COMMAND_REMOTE_CALL,
  43. NETWORK_COMMAND_REMOTE_SET,
  44. NETWORK_COMMAND_SIMPLIFY_PATH,
  45. NETWORK_COMMAND_CONFIRM_PATH,
  46. NETWORK_COMMAND_RAW,
  47. };
  48. void poll();
  49. void clear();
  50. void set_root_node(Node *p_node);
  51. void set_network_peer(const Ref<NetworkedMultiplayerPeer> &p_peer);
  52. Ref<NetworkedMultiplayerPeer> get_network_peer() const;
  53. Error send_bytes(PoolVector<uint8_t> p_data, int p_to = NetworkedMultiplayerPeer::TARGET_PEER_BROADCAST);
  54. // Called by Node.rpc
  55. void rpcp(Node *p_node, int p_peer_id, bool p_unreliable, const StringName &p_method, const Variant **p_arg, int p_argcount);
  56. // Called by Node.rset
  57. void rsetp(Node *p_node, int p_peer_id, bool p_unreliable, const StringName &p_property, const Variant &p_value);
  58. void _add_peer(int p_id);
  59. void _del_peer(int p_id);
  60. void _connected_to_server();
  61. void _connection_failed();
  62. void _server_disconnected();
  63. bool has_network_peer() const { return network_peer.is_valid(); }
  64. Vector<int> get_network_connected_peers() const;
  65. int get_rpc_sender_id() const { return rpc_sender_id; }
  66. int get_network_unique_id() const;
  67. bool is_network_server() const;
  68. void set_refuse_new_network_connections(bool p_refuse);
  69. bool is_refusing_new_network_connections() const;
  70. MultiplayerAPI();
  71. ~MultiplayerAPI();
  72. };
  73. #endif // MULTIPLAYER_PROTOCOL_H