Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/debugger/remote_debugger_peer.cpp
11351 views
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
if (in_left <= 0) {
130
if (in_queue.size() > max_queued_messages) {
131
break; // Too many messages already in queue.
132
}
133
if (tcp_client->get_available_bytes() < 4) {
134
break; // Need 4 more bytes.
135
}
136
uint32_t size = 0;
137
int read = 0;
138
Error err = tcp_client->get_partial_data((uint8_t *)&size, 4, read);
139
ERR_CONTINUE(read != 4 || err != OK || size > (uint32_t)in_buf.size());
140
in_left = size;
141
in_pos = 0;
142
}
143
int read = 0;
144
tcp_client->get_partial_data(buf + in_pos, in_left, read);
145
in_left -= read;
146
in_pos += read;
147
if (in_left == 0) {
148
Variant var;
149
Error err = decode_variant(var, buf, in_pos, &read);
150
ERR_CONTINUE(read != in_pos || err != OK);
151
ERR_CONTINUE_MSG(var.get_type() != Variant::ARRAY, "Malformed packet received, not an Array.");
152
MutexLock lock(mutex);
153
in_queue.push_back(var);
154
}
155
}
156
}
157
158
Error RemoteDebuggerPeerTCP::_try_connect(Ref<StreamPeerSocket> tcp_client) {
159
const int tries = 6;
160
const int waits[tries] = { 1, 10, 100, 1000, 1000, 1000 };
161
162
for (int i = 0; i < tries; i++) {
163
tcp_client->poll();
164
if (tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED) {
165
print_verbose("Remote Debugger: Connected!");
166
break;
167
} else {
168
const int ms = waits[i];
169
OS::get_singleton()->delay_usec(ms * 1000);
170
print_verbose("Remote Debugger: Connection failed with status: '" + String::num_int64(tcp_client->get_status()) + "', retrying in " + String::num_int64(ms) + " msec.");
171
}
172
}
173
174
if (tcp_client->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
175
ERR_PRINT(vformat("Remote Debugger: Unable to connect. Status: %s.", String::num_int64(tcp_client->get_status())));
176
return FAILED;
177
}
178
return OK;
179
}
180
181
void RemoteDebuggerPeerTCP::_thread_func(void *p_ud) {
182
// Update in time for 144hz monitors
183
const uint64_t min_tick = 6900;
184
RemoteDebuggerPeerTCP *peer = static_cast<RemoteDebuggerPeerTCP *>(p_ud);
185
while (peer->running && peer->is_peer_connected()) {
186
uint64_t ticks_usec = OS::get_singleton()->get_ticks_usec();
187
peer->_poll();
188
if (!peer->is_peer_connected()) {
189
break;
190
}
191
ticks_usec = OS::get_singleton()->get_ticks_usec() - ticks_usec;
192
if (ticks_usec < min_tick) {
193
OS::get_singleton()->delay_usec(min_tick - ticks_usec);
194
}
195
}
196
}
197
198
void RemoteDebuggerPeerTCP::poll() {
199
// Nothing to do, polling is done in thread.
200
}
201
202
void RemoteDebuggerPeerTCP::_poll() {
203
tcp_client->poll();
204
if (connected) {
205
_write_out();
206
_read_in();
207
connected = tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED;
208
}
209
}
210
211
RemoteDebuggerPeer *RemoteDebuggerPeerTCP::create_tcp(const String &p_uri) {
212
ERR_FAIL_COND_V(!p_uri.begins_with("tcp://"), nullptr);
213
214
String debug_host = p_uri.replace("tcp://", "");
215
uint16_t debug_port = 6007;
216
217
if (debug_host.contains_char(':')) {
218
int sep_pos = debug_host.rfind_char(':');
219
debug_port = debug_host.substr(sep_pos + 1).to_int();
220
debug_host = debug_host.substr(0, sep_pos);
221
}
222
223
IPAddress ip;
224
if (debug_host.is_valid_ip_address()) {
225
ip = debug_host;
226
} else {
227
ip = IP::get_singleton()->resolve_hostname(debug_host);
228
}
229
230
Ref<StreamPeerTCP> stream;
231
stream.instantiate();
232
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));
233
ERR_FAIL_COND_V(_try_connect(stream), nullptr);
234
return memnew(RemoteDebuggerPeerTCP(stream));
235
}
236
237
RemoteDebuggerPeer *RemoteDebuggerPeerTCP::create_unix(const String &p_uri) {
238
ERR_FAIL_COND_V(!p_uri.begins_with("unix://"), nullptr);
239
240
String debug_path = p_uri.replace("unix://", "");
241
Ref<StreamPeerUDS> stream;
242
stream.instantiate();
243
Error err = stream->connect_to_host(debug_path);
244
ERR_FAIL_COND_V_MSG(err != OK && err != ERR_BUSY, nullptr, vformat("Remote Debugger: Unable to connect to socket path '%s'.", debug_path));
245
ERR_FAIL_COND_V(_try_connect(stream), nullptr);
246
return memnew(RemoteDebuggerPeerTCP(stream));
247
}
248
249
RemoteDebuggerPeer::RemoteDebuggerPeer() {
250
max_queued_messages = (int)GLOBAL_GET("network/limits/debugger/max_queued_messages");
251
}
252
253