scene_rpc_interface.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 Node;
  36. class SceneRPCInterface : public RefCounted {
  37. GDCLASS(SceneRPCInterface, RefCounted);
  38. private:
  39. struct RPCConfig {
  40. StringName name;
  41. MultiplayerAPI::RPCMode rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  42. bool call_local = false;
  43. MultiplayerPeer::TransferMode transfer_mode = MultiplayerPeer::TRANSFER_MODE_RELIABLE;
  44. int channel = 0;
  45. bool operator==(RPCConfig const &p_other) const {
  46. return name == p_other.name;
  47. }
  48. };
  49. struct RPCConfigCache {
  50. HashMap<uint16_t, RPCConfig> configs;
  51. HashMap<StringName, uint16_t> ids;
  52. };
  53. struct SortRPCConfig {
  54. StringName::AlphCompare compare;
  55. bool operator()(const RPCConfig &p_a, const RPCConfig &p_b) const {
  56. return compare(p_a.name, p_b.name);
  57. }
  58. };
  59. enum NetworkNodeIdCompression {
  60. NETWORK_NODE_ID_COMPRESSION_8 = 0,
  61. NETWORK_NODE_ID_COMPRESSION_16,
  62. NETWORK_NODE_ID_COMPRESSION_32,
  63. };
  64. enum NetworkNameIdCompression {
  65. NETWORK_NAME_ID_COMPRESSION_8 = 0,
  66. NETWORK_NAME_ID_COMPRESSION_16,
  67. };
  68. SceneMultiplayer *multiplayer = nullptr;
  69. Vector<uint8_t> packet_cache;
  70. HashMap<ObjectID, RPCConfigCache> rpc_cache;
  71. #ifdef DEBUG_ENABLED
  72. _FORCE_INLINE_ void _profile_node_data(const String &p_what, ObjectID p_id, int p_size);
  73. #endif
  74. protected:
  75. 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);
  76. 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);
  77. Node *_process_get_node(int p_from, const uint8_t *p_packet, uint32_t p_node_target, int p_packet_len);
  78. void _parse_rpc_config(const Variant &p_config, bool p_for_node, RPCConfigCache &r_cache);
  79. const RPCConfigCache &_get_node_config(const Node *p_node);
  80. public:
  81. Error rpcp(Object *p_obj, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount);
  82. void process_rpc(int p_from, const uint8_t *p_packet, int p_packet_len);
  83. String get_rpc_md5(const Object *p_obj);
  84. SceneRPCInterface(SceneMultiplayer *p_multiplayer) { multiplayer = p_multiplayer; }
  85. };
  86. #endif // SCENE_RPC_INTERFACE_H