2023-01-05 20:25:55 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* remote_debugger_peer.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2020-02-27 10:30:20 +08:00
|
|
|
|
|
|
|
#include "remote_debugger_peer.h"
|
|
|
|
|
2020-11-08 06:33:38 +08:00
|
|
|
#include "core/config/project_settings.h"
|
2020-02-27 10:30:20 +08:00
|
|
|
#include "core/io/marshalls.h"
|
|
|
|
#include "core/os/os.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);
|
2020-05-14 22:41:43 +08:00
|
|
|
if (out_queue.size() >= max_queued_messages) {
|
2020-02-27 10:30:20 +08:00
|
|
|
return ERR_OUT_OF_MEMORY;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2020-02-27 10:30:20 +08:00
|
|
|
|
|
|
|
out_queue.push_back(p_arr);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RemoteDebuggerPeerTCP::get_max_message_size() const {
|
|
|
|
return 8 << 20; // 8 MiB
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteDebuggerPeerTCP::close() {
|
2021-01-19 20:29:41 +08:00
|
|
|
running = false;
|
|
|
|
thread.wait_to_finish();
|
2020-02-27 10:30:20 +08:00
|
|
|
tcp_client->disconnect_from_host();
|
2022-02-02 02:19:13 +08:00
|
|
|
out_buf.clear();
|
|
|
|
in_buf.clear();
|
2020-02-27 10:30:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
running = true;
|
2021-01-19 20:29:41 +08:00
|
|
|
thread.start(_thread_func, this);
|
2020-02-27 10:30:20 +08:00
|
|
|
} else {
|
2021-06-18 06:03:09 +08:00
|
|
|
tcp_client.instantiate();
|
2020-02-27 10:30:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoteDebuggerPeerTCP::~RemoteDebuggerPeerTCP() {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteDebuggerPeerTCP::_write_out() {
|
2022-03-27 21:30:17 +08:00
|
|
|
while (tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED && tcp_client->wait(NetSocket::POLL_TYPE_OUT) == OK) {
|
2020-02-27 10:30:20 +08:00
|
|
|
uint8_t *buf = out_buf.ptrw();
|
|
|
|
if (out_left <= 0) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (out_queue.size() == 0) {
|
2020-02-27 10:30:20 +08:00
|
|
|
break; // Nothing left to send
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2020-02-27 10:30:20 +08:00
|
|
|
mutex.lock();
|
|
|
|
Variant var = out_queue[0];
|
|
|
|
out_queue.pop_front();
|
|
|
|
mutex.unlock();
|
|
|
|
int size = 0;
|
2020-04-02 07:20:12 +08:00
|
|
|
Error err = encode_variant(var, nullptr, size);
|
2020-02-27 10:30:20 +08:00
|
|
|
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() {
|
2022-03-27 21:30:17 +08:00
|
|
|
while (tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED && tcp_client->wait(NetSocket::POLL_TYPE_IN) == OK) {
|
2020-02-27 10:30:20 +08:00
|
|
|
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) {
|
2021-05-06 08:48:18 +08:00
|
|
|
IPAddress ip;
|
2020-05-14 22:41:43 +08:00
|
|
|
if (p_host.is_valid_ip_address()) {
|
2020-02-27 10:30:20 +08:00
|
|
|
ip = p_host;
|
2020-05-14 22:41:43 +08:00
|
|
|
} else {
|
2020-02-27 10:30:20 +08:00
|
|
|
ip = IP::get_singleton()->resolve_hostname(p_host);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2020-02-27 10:30:20 +08:00
|
|
|
|
|
|
|
int port = p_port;
|
|
|
|
|
|
|
|
const int tries = 6;
|
2022-04-05 18:40:26 +08:00
|
|
|
const int waits[tries] = { 1, 10, 100, 1000, 1000, 1000 };
|
2020-02-27 10:30:20 +08:00
|
|
|
|
|
|
|
tcp_client->connect_to_host(ip, port);
|
|
|
|
|
|
|
|
for (int i = 0; i < tries; i++) {
|
2022-03-27 21:30:17 +08:00
|
|
|
tcp_client->poll();
|
2020-02-27 10:30:20 +08:00
|
|
|
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.");
|
2020-05-19 21:46:49 +08:00
|
|
|
}
|
|
|
|
}
|
2020-02-27 10:30:20 +08:00
|
|
|
|
|
|
|
if (tcp_client->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
|
|
|
|
ERR_PRINT("Remote Debugger: Unable to connect. Status: " + String::num(tcp_client->get_status()) + ".");
|
|
|
|
return FAILED;
|
2020-05-19 21:46:49 +08:00
|
|
|
}
|
2020-02-27 10:30:20 +08:00
|
|
|
connected = true;
|
|
|
|
running = true;
|
2021-01-19 20:29:41 +08:00
|
|
|
thread.start(_thread_func, this);
|
2020-02-27 10:30:20 +08:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteDebuggerPeerTCP::_thread_func(void *p_ud) {
|
2022-02-02 02:53:32 +08:00
|
|
|
// Update in time for 144hz monitors
|
|
|
|
const uint64_t min_tick = 6900;
|
2022-04-05 18:40:26 +08:00
|
|
|
RemoteDebuggerPeerTCP *peer = static_cast<RemoteDebuggerPeerTCP *>(p_ud);
|
2020-02-27 10:30:20 +08:00
|
|
|
while (peer->running && peer->is_peer_connected()) {
|
2021-03-17 04:05:06 +08:00
|
|
|
uint64_t ticks_usec = OS::get_singleton()->get_ticks_usec();
|
2020-02-27 10:30:20 +08:00
|
|
|
peer->_poll();
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!peer->is_peer_connected()) {
|
2020-02-27 10:30:20 +08:00
|
|
|
break;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2021-03-17 04:05:06 +08:00
|
|
|
ticks_usec = OS::get_singleton()->get_ticks_usec() - ticks_usec;
|
|
|
|
if (ticks_usec < min_tick) {
|
|
|
|
OS::get_singleton()->delay_usec(min_tick - ticks_usec);
|
|
|
|
}
|
2020-02-27 10:30:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteDebuggerPeerTCP::poll() {
|
2022-10-03 16:57:36 +08:00
|
|
|
// Nothing to do, polling is done in thread.
|
2020-02-27 10:30:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteDebuggerPeerTCP::_poll() {
|
2022-03-27 21:30:17 +08:00
|
|
|
tcp_client->poll();
|
2020-02-27 10:30:20 +08:00
|
|
|
if (connected) {
|
|
|
|
_write_out();
|
|
|
|
_read_in();
|
|
|
|
connected = tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-11 18:23:21 +08:00
|
|
|
RemoteDebuggerPeer *RemoteDebuggerPeerTCP::create(const String &p_uri) {
|
|
|
|
ERR_FAIL_COND_V(!p_uri.begins_with("tcp://"), nullptr);
|
2020-02-27 10:30:20 +08:00
|
|
|
|
|
|
|
String debug_host = p_uri.replace("tcp://", "");
|
|
|
|
uint16_t debug_port = 6007;
|
|
|
|
|
2022-02-04 00:03:38 +08:00
|
|
|
if (debug_host.contains(":")) {
|
2020-07-03 21:26:22 +08:00
|
|
|
int sep_pos = debug_host.rfind(":");
|
2020-02-27 10:30:20 +08:00
|
|
|
debug_port = debug_host.substr(sep_pos + 1).to_int();
|
|
|
|
debug_host = debug_host.substr(0, sep_pos);
|
|
|
|
}
|
2020-03-11 18:23:21 +08:00
|
|
|
|
|
|
|
RemoteDebuggerPeerTCP *peer = memnew(RemoteDebuggerPeerTCP);
|
2020-02-27 10:30:20 +08:00
|
|
|
Error err = peer->connect_to_host(debug_host, debug_port);
|
2020-03-11 18:23:21 +08:00
|
|
|
if (err != OK) {
|
|
|
|
memdelete(peer);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-02-27 10:30:20 +08:00
|
|
|
return peer;
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoteDebuggerPeer::RemoteDebuggerPeer() {
|
|
|
|
max_queued_messages = (int)GLOBAL_GET("network/limits/debugger/max_queued_messages");
|
|
|
|
}
|