1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
/*************************************************************************/
/* remote_debugger_peer.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "remote_debugger_peer.h"
#include "core/io/marshalls.h"
#include "core/os/os.h"
#include "core/project_settings.h"
bool RemoteDebuggerPeerTCP::is_peer_connected() {
return connected;
}
bool RemoteDebuggerPeerTCP::has_message() {
return in_queue.size() > 0;
}
Array RemoteDebuggerPeerTCP::get_message() {
MutexLock lock(mutex);
ERR_FAIL_COND_V(!has_message(), Array());
Array out = in_queue[0];
in_queue.pop_front();
return out;
}
Error RemoteDebuggerPeerTCP::put_message(const Array &p_arr) {
MutexLock lock(mutex);
if (out_queue.size() >= max_queued_messages)
return ERR_OUT_OF_MEMORY;
out_queue.push_back(p_arr);
return OK;
}
int RemoteDebuggerPeerTCP::get_max_message_size() const {
return 8 << 20; // 8 MiB
}
void RemoteDebuggerPeerTCP::close() {
if (thread) {
running = false;
Thread::wait_to_finish(thread);
memdelete(thread);
thread = NULL;
}
tcp_client->disconnect_from_host();
out_buf.resize(0);
in_buf.resize(0);
}
RemoteDebuggerPeerTCP::RemoteDebuggerPeerTCP(Ref<StreamPeerTCP> p_tcp) {
// This means remote debugger takes 16 MiB just because it exists...
in_buf.resize((8 << 20) + 4); // 8 MiB should be way more than enough (need 4 extra bytes for encoding packet size).
out_buf.resize(8 << 20); // 8 MiB should be way more than enough
tcp_client = p_tcp;
if (tcp_client.is_valid()) { // Attaching to an already connected stream.
connected = true;
#ifndef NO_THREADS
running = true;
thread = Thread::create(_thread_func, this);
#endif
} else {
tcp_client.instance();
}
}
RemoteDebuggerPeerTCP::~RemoteDebuggerPeerTCP() {
close();
}
void RemoteDebuggerPeerTCP::_write_out() {
while (tcp_client->poll(NetSocket::POLL_TYPE_OUT) == OK) {
uint8_t *buf = out_buf.ptrw();
if (out_left <= 0) {
if (out_queue.size() == 0)
break; // Nothing left to send
mutex.lock();
Variant var = out_queue[0];
out_queue.pop_front();
mutex.unlock();
int size = 0;
Error err = encode_variant(var, NULL, size);
ERR_CONTINUE(err != OK || size > out_buf.size() - 4); // 4 bytes separator.
encode_uint32(size, buf);
encode_variant(var, buf + 4, size);
out_left = size + 4;
out_pos = 0;
}
int sent = 0;
tcp_client->put_partial_data(buf + out_pos, out_left, sent);
out_left -= sent;
out_pos += sent;
}
}
void RemoteDebuggerPeerTCP::_read_in() {
while (tcp_client->poll(NetSocket::POLL_TYPE_IN) == OK) {
uint8_t *buf = in_buf.ptrw();
if (in_left <= 0) {
if (in_queue.size() > max_queued_messages) {
break; // Too many messages already in queue.
}
if (tcp_client->get_available_bytes() < 4) {
break; // Need 4 more bytes.
}
uint32_t size = 0;
int read = 0;
Error err = tcp_client->get_partial_data((uint8_t *)&size, 4, read);
ERR_CONTINUE(read != 4 || err != OK || size > (uint32_t)in_buf.size());
in_left = size;
in_pos = 0;
}
int read = 0;
tcp_client->get_partial_data(buf + in_pos, in_left, read);
in_left -= read;
in_pos += read;
if (in_left == 0) {
Variant var;
Error err = decode_variant(var, buf, in_pos, &read);
ERR_CONTINUE(read != in_pos || err != OK);
ERR_CONTINUE_MSG(var.get_type() != Variant::ARRAY, "Malformed packet received, not an Array.");
mutex.lock();
in_queue.push_back(var);
mutex.unlock();
}
}
}
Error RemoteDebuggerPeerTCP::connect_to_host(const String &p_host, uint16_t p_port) {
IP_Address ip;
if (p_host.is_valid_ip_address())
ip = p_host;
else
ip = IP::get_singleton()->resolve_hostname(p_host);
int port = p_port;
const int tries = 6;
int waits[tries] = { 1, 10, 100, 1000, 1000, 1000 };
tcp_client->connect_to_host(ip, port);
for (int i = 0; i < tries; i++) {
if (tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED) {
print_verbose("Remote Debugger: Connected!");
break;
} else {
const int ms = waits[i];
OS::get_singleton()->delay_usec(ms * 1000);
print_verbose("Remote Debugger: Connection failed with status: '" + String::num(tcp_client->get_status()) + "', retrying in " + String::num(ms) + " msec.");
};
};
if (tcp_client->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
ERR_PRINT("Remote Debugger: Unable to connect. Status: " + String::num(tcp_client->get_status()) + ".");
return FAILED;
};
connected = true;
#ifndef NO_THREADS
running = true;
thread = Thread::create(_thread_func, this);
#endif
return OK;
}
void RemoteDebuggerPeerTCP::_thread_func(void *p_ud) {
RemoteDebuggerPeerTCP *peer = (RemoteDebuggerPeerTCP *)p_ud;
while (peer->running && peer->is_peer_connected()) {
peer->_poll();
if (!peer->is_peer_connected())
break;
peer->tcp_client->poll(NetSocket::POLL_TYPE_IN_OUT, 1);
}
}
void RemoteDebuggerPeerTCP::poll() {
#ifdef NO_THREADS
_poll();
#endif
}
void RemoteDebuggerPeerTCP::_poll() {
if (connected) {
_write_out();
_read_in();
connected = tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED;
}
}
Ref<RemoteDebuggerPeer> RemoteDebuggerPeer::create_from_uri(const String p_uri) {
if (!p_uri.begins_with("tcp://"))
return Ref<RemoteDebuggerPeer>(); // Only TCP supported for now, more to come.
String debug_host = p_uri.replace("tcp://", "");
uint16_t debug_port = 6007;
if (debug_host.find(":") != -1) {
int sep_pos = debug_host.find_last(":");
debug_port = debug_host.substr(sep_pos + 1).to_int();
debug_host = debug_host.substr(0, sep_pos);
}
Ref<RemoteDebuggerPeerTCP> peer = Ref<RemoteDebuggerPeer>(memnew(RemoteDebuggerPeerTCP));
Error err = peer->connect_to_host(debug_host, debug_port);
if (err != OK)
return Ref<RemoteDebuggerPeer>();
return peer;
}
RemoteDebuggerPeer::RemoteDebuggerPeer() {
max_queued_messages = (int)GLOBAL_GET("network/limits/debugger/max_queued_messages");
}
|