Path: blob/master/core/debugger/remote_debugger_peer.cpp
10277 views
/**************************************************************************/1/* remote_debugger_peer.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "remote_debugger_peer.h"3132#include "core/config/project_settings.h"33#include "core/io/marshalls.h"34#include "core/os/os.h"3536bool RemoteDebuggerPeerTCP::is_peer_connected() {37return connected;38}3940bool RemoteDebuggerPeerTCP::has_message() {41return in_queue.size() > 0;42}4344Array RemoteDebuggerPeerTCP::get_message() {45MutexLock lock(mutex);46List<Array>::Element *E = in_queue.front();47ERR_FAIL_NULL_V_MSG(E, Array(), "No remote debugger messages in queue.");4849Array out = E->get();50in_queue.pop_front();51return out;52}5354Error RemoteDebuggerPeerTCP::put_message(const Array &p_arr) {55MutexLock lock(mutex);56if (out_queue.size() >= max_queued_messages) {57return ERR_OUT_OF_MEMORY;58}5960out_queue.push_back(p_arr);61return OK;62}6364int RemoteDebuggerPeerTCP::get_max_message_size() const {65return 8 << 20; // 8 MiB66}6768void RemoteDebuggerPeerTCP::close() {69running = false;70if (thread.is_started()) {71thread.wait_to_finish();72}73tcp_client->disconnect_from_host();74out_buf.clear();75in_buf.clear();76}7778RemoteDebuggerPeerTCP::RemoteDebuggerPeerTCP(Ref<StreamPeerTCP> p_tcp) {79// This means remote debugger takes 16 MiB just because it exists...80in_buf.resize((8 << 20) + 4); // 8 MiB should be way more than enough (need 4 extra bytes for encoding packet size).81out_buf.resize(8 << 20); // 8 MiB should be way more than enough82tcp_client = p_tcp;83if (tcp_client.is_valid()) { // Attaching to an already connected stream.84connected = true;85running = true;86thread.start(_thread_func, this);87} else {88tcp_client.instantiate();89}90}9192RemoteDebuggerPeerTCP::~RemoteDebuggerPeerTCP() {93close();94}9596void RemoteDebuggerPeerTCP::_write_out() {97while (tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED && tcp_client->wait(NetSocket::POLL_TYPE_OUT) == OK) {98uint8_t *buf = out_buf.ptrw();99if (out_left <= 0) {100mutex.lock();101List<Array>::Element *E = out_queue.front();102if (!E) {103mutex.unlock();104break;105}106Variant var = E->get();107out_queue.pop_front();108mutex.unlock();109int size = 0;110Error err = encode_variant(var, nullptr, size);111ERR_CONTINUE(err != OK || size > out_buf.size() - 4); // 4 bytes separator.112encode_uint32(size, buf);113encode_variant(var, buf + 4, size);114out_left = size + 4;115out_pos = 0;116}117int sent = 0;118tcp_client->put_partial_data(buf + out_pos, out_left, sent);119out_left -= sent;120out_pos += sent;121}122}123124void RemoteDebuggerPeerTCP::_read_in() {125while (tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED && tcp_client->wait(NetSocket::POLL_TYPE_IN) == OK) {126uint8_t *buf = in_buf.ptrw();127if (in_left <= 0) {128if (in_queue.size() > max_queued_messages) {129break; // Too many messages already in queue.130}131if (tcp_client->get_available_bytes() < 4) {132break; // Need 4 more bytes.133}134uint32_t size = 0;135int read = 0;136Error err = tcp_client->get_partial_data((uint8_t *)&size, 4, read);137ERR_CONTINUE(read != 4 || err != OK || size > (uint32_t)in_buf.size());138in_left = size;139in_pos = 0;140}141int read = 0;142tcp_client->get_partial_data(buf + in_pos, in_left, read);143in_left -= read;144in_pos += read;145if (in_left == 0) {146Variant var;147Error err = decode_variant(var, buf, in_pos, &read);148ERR_CONTINUE(read != in_pos || err != OK);149ERR_CONTINUE_MSG(var.get_type() != Variant::ARRAY, "Malformed packet received, not an Array.");150MutexLock lock(mutex);151in_queue.push_back(var);152}153}154}155156Error RemoteDebuggerPeerTCP::connect_to_host(const String &p_host, uint16_t p_port) {157IPAddress ip;158if (p_host.is_valid_ip_address()) {159ip = p_host;160} else {161ip = IP::get_singleton()->resolve_hostname(p_host);162}163164int port = p_port;165166const int tries = 6;167const int waits[tries] = { 1, 10, 100, 1000, 1000, 1000 };168169Error err = tcp_client->connect_to_host(ip, port);170ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Remote Debugger: Unable to connect to host '%s:%d'.", p_host, port));171172for (int i = 0; i < tries; i++) {173tcp_client->poll();174if (tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED) {175print_verbose("Remote Debugger: Connected!");176break;177} else {178const int ms = waits[i];179OS::get_singleton()->delay_usec(ms * 1000);180print_verbose("Remote Debugger: Connection failed with status: '" + String::num_int64(tcp_client->get_status()) + "', retrying in " + String::num_int64(ms) + " msec.");181}182}183184if (tcp_client->get_status() != StreamPeerTCP::STATUS_CONNECTED) {185ERR_PRINT(vformat("Remote Debugger: Unable to connect. Status: %s.", String::num_int64(tcp_client->get_status())));186return FAILED;187}188connected = true;189running = true;190thread.start(_thread_func, this);191return OK;192}193194void RemoteDebuggerPeerTCP::_thread_func(void *p_ud) {195// Update in time for 144hz monitors196const uint64_t min_tick = 6900;197RemoteDebuggerPeerTCP *peer = static_cast<RemoteDebuggerPeerTCP *>(p_ud);198while (peer->running && peer->is_peer_connected()) {199uint64_t ticks_usec = OS::get_singleton()->get_ticks_usec();200peer->_poll();201if (!peer->is_peer_connected()) {202break;203}204ticks_usec = OS::get_singleton()->get_ticks_usec() - ticks_usec;205if (ticks_usec < min_tick) {206OS::get_singleton()->delay_usec(min_tick - ticks_usec);207}208}209}210211void RemoteDebuggerPeerTCP::poll() {212// Nothing to do, polling is done in thread.213}214215void RemoteDebuggerPeerTCP::_poll() {216tcp_client->poll();217if (connected) {218_write_out();219_read_in();220connected = tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED;221}222}223224RemoteDebuggerPeer *RemoteDebuggerPeerTCP::create(const String &p_uri) {225ERR_FAIL_COND_V(!p_uri.begins_with("tcp://"), nullptr);226227String debug_host = p_uri.replace("tcp://", "");228uint16_t debug_port = 6007;229230if (debug_host.contains_char(':')) {231int sep_pos = debug_host.rfind_char(':');232debug_port = debug_host.substr(sep_pos + 1).to_int();233debug_host = debug_host.substr(0, sep_pos);234}235236RemoteDebuggerPeerTCP *peer = memnew(RemoteDebuggerPeerTCP);237Error err = peer->connect_to_host(debug_host, debug_port);238if (err != OK) {239memdelete(peer);240return nullptr;241}242return peer;243}244245RemoteDebuggerPeer::RemoteDebuggerPeer() {246max_queued_messages = (int)GLOBAL_GET("network/limits/debugger/max_queued_messages");247}248249250