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