Path: blob/master/editor/debugger/editor_debugger_server.cpp
9896 views
/**************************************************************************/1/* editor_debugger_server.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 "editor_debugger_server.h"3132#include "core/io/tcp_server.h"33#include "core/os/thread.h"34#include "editor/editor_log.h"35#include "editor/editor_node.h"36#include "editor/settings/editor_settings.h"3738class EditorDebuggerServerTCP : public EditorDebuggerServer {39private:40Ref<TCPServer> server;41String endpoint;4243public:44static EditorDebuggerServer *create(const String &p_protocol);4546virtual void poll() override {}47virtual String get_uri() const override;48virtual Error start(const String &p_uri) override;49virtual void stop() override;50virtual bool is_active() const override;51virtual bool is_connection_available() const override;52virtual Ref<RemoteDebuggerPeer> take_connection() override;5354EditorDebuggerServerTCP();55};5657EditorDebuggerServer *EditorDebuggerServerTCP::create(const String &p_protocol) {58ERR_FAIL_COND_V(p_protocol != "tcp://", nullptr);59return memnew(EditorDebuggerServerTCP);60}6162EditorDebuggerServerTCP::EditorDebuggerServerTCP() {63server.instantiate();64}6566String EditorDebuggerServerTCP::get_uri() const {67return endpoint;68}6970Error EditorDebuggerServerTCP::start(const String &p_uri) {71// Default host and port72String bind_host = (String)EDITOR_GET("network/debug/remote_host");73int bind_port = (int)EDITOR_GET("network/debug/remote_port");7475// Optionally override76if (!p_uri.is_empty() && p_uri != "tcp://") {77String scheme, path, fragment;78Error err = p_uri.parse_url(scheme, bind_host, bind_port, path, fragment);79ERR_FAIL_COND_V(err != OK, ERR_INVALID_PARAMETER);80ERR_FAIL_COND_V(!bind_host.is_valid_ip_address() && bind_host != "*", ERR_INVALID_PARAMETER);81}8283// Try listening on ports84const int max_attempts = 5;85for (int attempt = 1;; ++attempt) {86const Error err = server->listen(bind_port, bind_host);87if (err == OK) {88break;89}90if (attempt >= max_attempts) {91EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, remote debugging unavailable.", bind_port), EditorLog::MSG_TYPE_ERROR);92return err;93}94int last_port = bind_port++;95EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, trying %d instead.", last_port, bind_port), EditorLog::MSG_TYPE_WARNING);96}9798// Endpoint that the client should connect to99endpoint = vformat("tcp://%s:%d", bind_host, bind_port);100101return OK;102}103104void EditorDebuggerServerTCP::stop() {105server->stop();106}107108bool EditorDebuggerServerTCP::is_active() const {109return server->is_listening();110}111112bool EditorDebuggerServerTCP::is_connection_available() const {113return server->is_listening() && server->is_connection_available();114}115116Ref<RemoteDebuggerPeer> EditorDebuggerServerTCP::take_connection() {117ERR_FAIL_COND_V(!is_connection_available(), Ref<RemoteDebuggerPeer>());118return memnew(RemoteDebuggerPeerTCP(server->take_connection()));119}120121/// EditorDebuggerServer122HashMap<StringName, EditorDebuggerServer::CreateServerFunc> EditorDebuggerServer::protocols;123124EditorDebuggerServer *EditorDebuggerServer::create(const String &p_protocol) {125ERR_FAIL_COND_V(!protocols.has(p_protocol), nullptr);126return protocols[p_protocol](p_protocol);127}128129void EditorDebuggerServer::register_protocol_handler(const String &p_protocol, CreateServerFunc p_func) {130ERR_FAIL_COND(protocols.has(p_protocol));131protocols[p_protocol] = p_func;132}133134void EditorDebuggerServer::initialize() {135register_protocol_handler("tcp://", EditorDebuggerServerTCP::create);136}137138void EditorDebuggerServer::deinitialize() {139protocols.clear();140}141142143