multiplayer_debugger.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**************************************************************************/
  2. /* multiplayer_debugger.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. #pragma once
  31. #include "core/debugger/engine_profiler.h"
  32. class MultiplayerSynchronizer;
  33. class MultiplayerDebugger {
  34. public:
  35. struct RPCNodeInfo {
  36. ObjectID node;
  37. String node_path;
  38. int incoming_rpc = 0;
  39. int incoming_size = 0;
  40. int outgoing_rpc = 0;
  41. int outgoing_size = 0;
  42. };
  43. struct RPCFrame {
  44. Vector<RPCNodeInfo> infos;
  45. Array serialize();
  46. bool deserialize(const Array &p_arr);
  47. };
  48. struct SyncInfo {
  49. ObjectID synchronizer;
  50. ObjectID config;
  51. ObjectID root_node;
  52. int incoming_syncs = 0;
  53. int incoming_size = 0;
  54. int outgoing_syncs = 0;
  55. int outgoing_size = 0;
  56. void write_to_array(Array &r_arr) const;
  57. bool read_from_array(const Array &p_arr, int p_offset);
  58. SyncInfo() {}
  59. SyncInfo(MultiplayerSynchronizer *p_sync);
  60. };
  61. struct ReplicationFrame {
  62. HashMap<ObjectID, SyncInfo> infos;
  63. Array serialize();
  64. bool deserialize(const Array &p_arr);
  65. };
  66. private:
  67. class BandwidthProfiler : public EngineProfiler {
  68. GDSOFTCLASS(BandwidthProfiler, EngineProfiler);
  69. protected:
  70. struct BandwidthFrame {
  71. uint32_t timestamp;
  72. int packet_size;
  73. };
  74. int bandwidth_in_ptr = 0;
  75. Vector<BandwidthFrame> bandwidth_in;
  76. int bandwidth_out_ptr = 0;
  77. Vector<BandwidthFrame> bandwidth_out;
  78. uint64_t last_bandwidth_time = 0;
  79. int bandwidth_usage(const Vector<BandwidthFrame> &p_buffer, int p_pointer);
  80. public:
  81. void toggle(bool p_enable, const Array &p_opts) override;
  82. void add(const Array &p_data) override;
  83. void tick(double p_frame_time, double p_process_time, double p_physics_time, double p_physics_frame_time) override;
  84. };
  85. class RPCProfiler : public EngineProfiler {
  86. GDSOFTCLASS(RPCProfiler, EngineProfiler);
  87. private:
  88. HashMap<ObjectID, RPCNodeInfo> rpc_node_data;
  89. uint64_t last_profile_time = 0;
  90. void init_node(const ObjectID p_node);
  91. public:
  92. void toggle(bool p_enable, const Array &p_opts) override;
  93. void add(const Array &p_data) override;
  94. void tick(double p_frame_time, double p_process_time, double p_physics_time, double p_physics_frame_time) override;
  95. };
  96. class ReplicationProfiler : public EngineProfiler {
  97. GDSOFTCLASS(ReplicationProfiler, EngineProfiler);
  98. private:
  99. HashMap<ObjectID, SyncInfo> sync_data;
  100. uint64_t last_profile_time = 0;
  101. public:
  102. void toggle(bool p_enable, const Array &p_opts) override;
  103. void add(const Array &p_data) override;
  104. void tick(double p_frame_time, double p_process_time, double p_physics_time, double p_physics_frame_time) override;
  105. };
  106. static Error _capture(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured);
  107. public:
  108. static void initialize();
  109. static void deinitialize();
  110. };