Debian Codegraph @

core/debugger/remote_debugger_peer.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* remote_debugger_peer.cpp */
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 #include "remote_debugger_peer.h"
32
33 #include "core/config/project_settings.h"
34 #include "core/io/marshalls.h"
35 #include "core/os/os.h"
36
37 bool RemoteDebuggerPeerTCP::is_peer_connected() {
38 return connected;
39 }
40
41 bool RemoteDebuggerPeerTCP::has_message() {
42 return in_queue.size() > 0;
43 }
44
45 Array RemoteDebuggerPeerTCP::get_message() {
46 MutexLock lock(mutex);
47 List<Array>::Element *E = in_queue.front();
48 ERR_FAIL_NULL_V_MSG(E, Array(), "No remote debugger messages in queue.");
49
50 Array out = E->get();
51 in_queue.pop_front();
52 return out;
53 }
54
55 Error RemoteDebuggerPeerTCP::put_message(const Array &p_arr) {
56 MutexLock lock(mutex);
57 if (out_queue.size() >= max_queued_messages) {
58 return ERR_OUT_OF_MEMORY;
59 }
60
61 out_queue.push_back(p_arr);
62 return OK;
63 }
64
65 int RemoteDebuggerPeerTCP::get_max_message_size() const {
66 return 8 << 20; // 8 MiB
67 }
68
69 void RemoteDebuggerPeerTCP::close() {
70 running = false;
71 if (thread.is_started()) {
72 thread.wait_to_finish();
73 }
74 tcp_client->disconnect_from_host();
75 out_buf.clear();
76 in_buf.clear();
77 }
78
79 RemoteDebuggerPeerTCP::RemoteDebuggerPeerTCP() {
80 // This means remote debugger takes 16 MiB just because it exists...
81 in_buf.resize((8 << 20) + 4); // 8 MiB should be way more than enough (need 4 extra bytes for encoding packet size).
82 out_buf.resize(8 << 20); // 8 MiB should be way more than enough
83 }
84
85 RemoteDebuggerPeerTCP::RemoteDebuggerPeerTCP(Ref<StreamPeerSocket> p_stream) :
86 RemoteDebuggerPeerTCP() {
87 DEV_ASSERT(p_stream.is_valid());
88 tcp_client = p_stream;
89 connected = true;
90 running = true;
91 thread.start(_thread_func, this);
92 }
93
94 RemoteDebuggerPeerTCP::~RemoteDebuggerPeerTCP() {
95 close();
96 }
97
98 void RemoteDebuggerPeerTCP::_write_out() {
99 while (tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED && tcp_client->wait(NetSocket::POLL_TYPE_OUT) == OK) {
100 uint8_t *buf = out_buf.ptrw();
101 if (out_left <= 0) {
102 mutex.lock();
103 List<Array>::Element *E = out_queue.front();
104 if (!E) {
105 mutex.unlock();
106 break;
107 }
108 Variant var = E->get();
109 out_queue.pop_front();
110 mutex.unlock();
111 int size = 0;
112 Error err = encode_variant(var, nullptr, size);
113 ERR_CONTINUE(err != OK || size > out_buf.size() - 4); // 4 bytes separator.
114 encode_uint32(size, buf);
115 encode_variant(var, buf + 4, size);
116 out_left = size + 4;
117 out_pos = 0;
118 }
119 int sent = 0;
120 tcp_client->put_partial_data(buf + out_pos, out_left, sent);
121 out_left -= sent;
122 out_pos += sent;
123 }
124 }
125
126 void RemoteDebuggerPeerTCP::_read_in() {
127 while (tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED && tcp_client->wait(NetSocket::POLL_TYPE_IN) == OK) {
128 uint8_t *buf = in_buf.ptrw();
129 Error err = OK;
130 if (in_left <= 0) {
131 if (in_queue.size() > max_queued_messages) {
132 break; // Too many messages already in queue.
133 }
134 if (tcp_client->get_available_bytes() < 4) {
135 break; // Need 4 more bytes.
136 }
137 uint32_t size = 0;
138 int read = 0;
139 err = tcp_client->get_partial_data((uint8_t *)&size, 4, read);
140 if (err != OK || read != 4) {
141 _disconnect_with_error("Remote debugger: Connection lost while reading packet size.");
142 return;
143 }
144 if (size > (uint32_t)in_buf.size()) {
145 _disconnect_with_error(vformat("Remote debugger: Packet too large (%s > %s bytes). Disconnecting.", String::num_uint64(size), String::num_int64(in_buf.size())));
146 return;
147 }
148 in_left = size;
149 in_pos = 0;
150 }
151 int read = 0;
152 err = tcp_client->get_partial_data(buf + in_pos, in_left, read);
153 if (err != OK || read == 0) {
154 _disconnect_with_error("Remote debugger: Connection lost while reading packet payload.");
155 return;
156 }
157 in_left -= read;
158 in_pos += read;
159 if (in_left == 0) {
160 Variant var;
161 err = decode_variant(var, buf, in_pos, &read);
162 ERR_CONTINUE(read != in_pos || err != OK);
163 ERR_CONTINUE_MSG(var.get_type() != Variant::ARRAY, "Malformed packet received, not an Array.");
164 MutexLock lock(mutex);
165 in_queue.push_back(var);
166 }
167 }
168 }
169
170 Error RemoteDebuggerPeerTCP::_try_connect(Ref<StreamPeerSocket> tcp_client) {
171 const int tries = 6;
172 const int waits[tries] = { 1, 10, 100, 1000, 1000, 1000 };
173
174 for (int i = 0; i < tries; i++) {
175 tcp_client->poll();
176 if (tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED) {
177 print_verbose("Remote Debugger: Connected!");
178 break;
179 } else {
180 const int ms = waits[i];
181 OS::get_singleton()->delay_usec(ms * 1000);
182 print_verbose("Remote Debugger: Connection failed with status: '" + String::num_int64(tcp_client->get_status()) + "', retrying in " + String::num_int64(ms) + " msec.");
183 }
184 }
185
186 if (tcp_client->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
187 ERR_PRINT(vformat("Remote Debugger: Unable to connect. Status: %s.", String::num_int64(tcp_client->get_status())));
188 return FAILED;
189 }
190 return OK;
191 }
192
193 void RemoteDebuggerPeerTCP::_thread_func(void *p_ud) {
194 // Update in time for 144hz monitors
195 const uint64_t min_tick = 6900;
196 RemoteDebuggerPeerTCP *peer = static_cast<RemoteDebuggerPeerTCP *>(p_ud);
197 while (peer->running && peer->is_peer_connected()) {
198 uint64_t ticks_usec = OS::get_singleton()->get_ticks_usec();
199 peer->_poll();
200 if (!peer->is_peer_connected()) {
201 break;
202 }
203 ticks_usec = OS::get_singleton()->get_ticks_usec() - ticks_usec;
204 if (ticks_usec < min_tick) {
205 OS::get_singleton()->delay_usec(min_tick - ticks_usec);
206 }
207 }
208 }
209
210 void RemoteDebuggerPeerTCP::poll() {
211 // Nothing to do, polling is done in thread.
212 }
213
214 void RemoteDebuggerPeerTCP::_poll() {
215 tcp_client->poll();
216 if (connected) {
217 _write_out();
218 _read_in();
219 connected = tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED;
220 }
221 }
222
223 RemoteDebuggerPeer *RemoteDebuggerPeerTCP::create_tcp(const String &p_uri) {
224 ERR_FAIL_COND_V(!p_uri.begins_with("tcp://"), nullptr);
225
226 String debug_host = p_uri.replace("tcp://", "");
227 uint16_t debug_port = 6007;
228
229 if (debug_host.contains_char(':')) {
230 int sep_pos = debug_host.rfind_char(':');
231 debug_port = debug_host.substr(sep_pos + 1).to_int();
232 debug_host = debug_host.substr(0, sep_pos);
233 }
234
235 IPAddress ip;
236 if (debug_host.is_valid_ip_address()) {
237 ip = debug_host;
238 } else {
239 ip = IP::get_singleton()->resolve_hostname(debug_host);
240 }
241
242 Ref<StreamPeerTCP> stream;
243 stream.instantiate();
244 ERR_FAIL_COND_V_MSG(stream->connect_to_host(ip, debug_port) != OK, nullptr, vformat("Remote Debugger: Unable to connect to host '%s:%d'.", debug_host, debug_port));
245 ERR_FAIL_COND_V(_try_connect(stream), nullptr);
246 return memnew(RemoteDebuggerPeerTCP(stream));
247 }
248
249 RemoteDebuggerPeer *RemoteDebuggerPeerTCP::create_unix(const String &p_uri) {
250 ERR_FAIL_COND_V(!p_uri.begins_with("unix://"), nullptr);
251
252 String debug_path = p_uri.replace("unix://", "");
253 Ref<StreamPeerUDS> stream;
254 stream.instantiate();
255 Error err = stream->connect_to_host(debug_path);
256 ERR_FAIL_COND_V_MSG(err != OK && err != ERR_BUSY, nullptr, vformat("Remote Debugger: Unable to connect to socket path '%s'.", debug_path));
257 ERR_FAIL_COND_V(_try_connect(stream), nullptr);
258 return memnew(RemoteDebuggerPeerTCP(stream));
259 }
260
261 RemoteDebuggerPeer::RemoteDebuggerPeer() {
262 max_queued_messages = (int)GLOBAL_GET("network/limits/debugger/max_queued_messages");
263 }
264
265 void RemoteDebuggerPeerTCP::_disconnect_with_error(const String &p_reason) {
266 ERR_PRINT(p_reason);
267 tcp_client->disconnect_from_host();
268 }