scene_rpc_interface.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**************************************************************************/
  2. /* scene_rpc_interface.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 SCENE_RPC_INTERFACE_H
  31. #define SCENE_RPC_INTERFACE_H
  32. #include "core/object/ref_counted.h"
  33. #include "scene/main/multiplayer_api.h"
  34. class SceneMultiplayer;
  35. class SceneCacheInterface;
  36. class SceneReplicationInterface;
  37. class Node;
  38. class SceneRPCInterface : public RefCounted {
  39. GDCLASS(SceneRPCInterface, RefCounted);
  40. private:
  41. struct RPCConfig {
  42. StringName name;
  43. MultiplayerAPI::RPCMode rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  44. bool call_local = false;
  45. MultiplayerPeer::TransferMode transfer_mode = MultiplayerPeer::TRANSFER_MODE_RELIABLE;
  46. int channel = 0;
  47. bool operator==(RPCConfig const &p_other) const {
  48. return name == p_other.name;
  49. }
  50. };
  51. struct RPCConfigCache {
  52. HashMap<uint16_t, RPCConfig> configs;
  53. HashMap<StringName, uint16_t> ids;
  54. };
  55. struct SortRPCConfig {
  56. StringName::AlphCompare compare;
  57. bool operator()(const RPCConfig &p_a, const RPCConfig &p_b) const {
  58. return compare(p_a.name, p_b.name);
  59. }
  60. };
  61. enum NetworkNodeIdCompression {
  62. NETWORK_NODE_ID_COMPRESSION_8 = 0,
  63. NETWORK_NODE_ID_COMPRESSION_16,
  64. NETWORK_NODE_ID_COMPRESSION_32,
  65. };
  66. enum NetworkNameIdCompression {
  67. NETWORK_NAME_ID_COMPRESSION_8 = 0,
  68. NETWORK_NAME_ID_COMPRESSION_16,
  69. };
  70. SceneMultiplayer *multiplayer = nullptr;
  71. SceneCacheInterface *multiplayer_cache = nullptr;
  72. SceneReplicationInterface *multiplayer_replicator = nullptr;
  73. Vector<uint8_t> packet_cache;
  74. HashMap<ObjectID, RPCConfigCache> rpc_cache;
  75. #ifdef DEBUG_ENABLED
  76. _FORCE_INLINE_ void _profile_node_data(const String &p_what, ObjectID p_id, int p_size);
  77. #endif
  78. protected:
  79. 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);
  80. void _send_rpc(Node *p_from, int p_to, uint16_t p_rpc_id, const RPCConfig &p_config, const StringName &p_name, const Variant **p_arg, int p_argcount);
  81. Node *_process_get_node(int p_from, const uint8_t *p_packet, uint32_t p_node_target, int p_packet_len);
  82. void _parse_rpc_config(const Variant &p_config, bool p_for_node, RPCConfigCache &r_cache);
  83. const RPCConfigCache &_get_node_config(const Node *p_node);
  84. public:
  85. Error rpcp(Object *p_obj, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount);
  86. void process_rpc(int p_from, const uint8_t *p_packet, int p_packet_len);
  87. String get_rpc_md5(const Object *p_obj);
  88. SceneRPCInterface(SceneMultiplayer *p_multiplayer, SceneCacheInterface *p_cache, SceneReplicationInterface *p_replicator) {
  89. multiplayer = p_multiplayer;
  90. multiplayer_cache = p_cache;
  91. multiplayer_replicator = p_replicator;
  92. }
  93. };
  94. #endif // SCENE_RPC_INTERFACE_H