Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/debugger/remote_debugger_peer.cpp
10277 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(Ref<StreamPeerTCP> p_tcp) {
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
tcp_client = p_tcp;
84
if (tcp_client.is_valid()) { // Attaching to an already connected stream.
85
connected = true;
86
running = true;
87
thread.start(_thread_func, this);
88
} else {
89
tcp_client.instantiate();
90
}
91
}
92
93
RemoteDebuggerPeerTCP::~RemoteDebuggerPeerTCP() {
94
close();
95
}
96
97
void RemoteDebuggerPeerTCP::_write_out() {
98
while (tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED && tcp_client->wait(NetSocket::POLL_TYPE_OUT) == OK) {
99
uint8_t *buf = out_buf.ptrw();
100
if (out_left <= 0) {
101
mutex.lock();
102
List<Array>::Element *E = out_queue.front();
103
if (!E) {
104
mutex.unlock();
105
break;
106
}
107
Variant var = E->get();
108
out_queue.pop_front();
109
mutex.unlock();
110
int size = 0;
111
Error err = encode_variant(var, nullptr, size);
112
ERR_CONTINUE(err != OK || size > out_buf.size() - 4); // 4 bytes separator.
113
encode_uint32(size, buf);
114
encode_variant(var, buf + 4, size);
115
out_left = size + 4;
116
out_pos = 0;
117
}
118
int sent = 0;
119
tcp_client->put_partial_data(buf + out_pos, out_left, sent);
120
out_left -= sent;
121
out_pos += sent;
122
}
123
}
124
125
void RemoteDebuggerPeerTCP::_read_in() {
126
while (tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED && tcp_client->wait(NetSocket::POLL_TYPE_IN) == OK) {
127
uint8_t *buf = in_buf.ptrw();
128
if (in_left <= 0) {
129
if (in_queue.size() > max_queued_messages) {
130
break; // Too many messages already in queue.
131
}
132
if (tcp_client->get_available_bytes() < 4) {
133
break; // Need 4 more bytes.
134
}
135
uint32_t size = 0;
136
int read = 0;
137
Error err = tcp_client->get_partial_data((uint8_t *)&size, 4, read);
138
ERR_CONTINUE(read != 4 || err != OK || size > (uint32_t)in_buf.size());
139
in_left = size;
140
in_pos = 0;
141
}
142
int read = 0;
143
tcp_client->get_partial_data(buf + in_pos, in_left, read);
144
in_left -= read;
145
in_pos += read;
146
if (in_left == 0) {
147
Variant var;
148
Error err = decode_variant(var, buf, in_pos, &read);
149
ERR_CONTINUE(read != in_pos || err != OK);
150
ERR_CONTINUE_MSG(var.get_type() != Variant::ARRAY, "Malformed packet received, not an Array.");
151
MutexLock lock(mutex);
152
in_queue.push_back(var);
153
}
154
}
155
}
156
157
Error RemoteDebuggerPeerTCP::connect_to_host(const String &p_host, uint16_t p_port) {
158
IPAddress ip;
159
if (p_host.is_valid_ip_address()) {
160
ip = p_host;
161
} else {
162
ip = IP::get_singleton()->resolve_hostname(p_host);
163
}
164
165
int port = p_port;
166
167
const int tries = 6;
168
const int waits[tries] = { 1, 10, 100, 1000, 1000, 1000 };
169
170
Error err = tcp_client->connect_to_host(ip, port);
171
ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Remote Debugger: Unable to connect to host '%s:%d'.", p_host, port));
172
173
for (int i = 0; i < tries; i++) {
174
tcp_client->poll();
175
if (tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED) {
176
print_verbose("Remote Debugger: Connected!");
177
break;
178
} else {
179
const int ms = waits[i];
180
OS::get_singleton()->delay_usec(ms * 1000);
181
print_verbose("Remote Debugger: Connection failed with status: '" + String::num_int64(tcp_client->get_status()) + "', retrying in " + String::num_int64(ms) + " msec.");
182
}
183
}
184
185
if (tcp_client->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
186
ERR_PRINT(vformat("Remote Debugger: Unable to connect. Status: %s.", String::num_int64(tcp_client->get_status())));
187
return FAILED;
188
}
189
connected = true;
190
running = true;
191
thread.start(_thread_func, this);
192
return OK;
193
}
194
195
void RemoteDebuggerPeerTCP::_thread_func(void *p_ud) {
196
// Update in time for 144hz monitors
197
const uint64_t min_tick = 6900;
198
RemoteDebuggerPeerTCP *peer = static_cast<RemoteDebuggerPeerTCP *>(p_ud);
199
while (peer->running && peer->is_peer_connected()) {
200
uint64_t ticks_usec = OS::get_singleton()->get_ticks_usec();
201
peer->_poll();
202
if (!peer->is_peer_connected()) {
203
break;
204
}
205
ticks_usec = OS::get_singleton()->get_ticks_usec() - ticks_usec;
206
if (ticks_usec < min_tick) {
207
OS::get_singleton()->delay_usec(min_tick - ticks_usec);
208
}
209
}
210
}
211
212
void RemoteDebuggerPeerTCP::poll() {
213
// Nothing to do, polling is done in thread.
214
}
215
216
void RemoteDebuggerPeerTCP::_poll() {
217
tcp_client->poll();
218
if (connected) {
219
_write_out();
220
_read_in();
221
connected = tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED;
222
}
223
}
224
225
RemoteDebuggerPeer *RemoteDebuggerPeerTCP::create(const String &p_uri) {
226
ERR_FAIL_COND_V(!p_uri.begins_with("tcp://"), nullptr);
227
228
String debug_host = p_uri.replace("tcp://", "");
229
uint16_t debug_port = 6007;
230
231
if (debug_host.contains_char(':')) {
232
int sep_pos = debug_host.rfind_char(':');
233
debug_port = debug_host.substr(sep_pos + 1).to_int();
234
debug_host = debug_host.substr(0, sep_pos);
235
}
236
237
RemoteDebuggerPeerTCP *peer = memnew(RemoteDebuggerPeerTCP);
238
Error err = peer->connect_to_host(debug_host, debug_port);
239
if (err != OK) {
240
memdelete(peer);
241
return nullptr;
242
}
243
return peer;
244
}
245
246
RemoteDebuggerPeer::RemoteDebuggerPeer() {
247
max_queued_messages = (int)GLOBAL_GET("network/limits/debugger/max_queued_messages");
248
}
249
250